Advertisement
Guest User

aws test

a guest
Feb 26th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import com.google.common.collect.ImmutableSet;
  2. import com.google.common.collect.Iterables;
  3. import com.google.inject.Module;
  4. import org.jclouds.ContextBuilder;
  5. import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions;
  6. import org.jclouds.compute.ComputeServiceContext;
  7. import org.jclouds.compute.domain.Image;
  8. import org.jclouds.compute.domain.NodeMetadata;
  9. import org.jclouds.compute.domain.OsFamily;
  10. import org.jclouds.compute.domain.Template;
  11. import org.jclouds.domain.Location;
  12. import org.jclouds.location.reference.LocationConstants;
  13. import org.jclouds.logging.log4j.config.Log4JLoggingModule;
  14. import org.jclouds.sshj.config.SshjSshClientModule;
  15.  
  16. import java.util.Properties;
  17. import java.util.Set;
  18.  
  19. public class MainApp {
  20.  
  21.  
  22. public static void main(String[] args){
  23.  
  24. String accesskeyid="xxxx";
  25. String secretkey="xxxxxx";
  26.  
  27. Properties props = new Properties();
  28.  
  29. props.setProperty(LocationConstants.PROPERTY_REGIONS, "us-east-1");
  30.  
  31. // get a context with ec2 that offers the portable ComputeService API
  32. ComputeServiceContext context = ContextBuilder.newBuilder("aws-ec2")
  33. .credentials(accesskeyid, secretkey).overrides(props)
  34. .modules(ImmutableSet.<Module> of(new Log4JLoggingModule(),
  35. new SshjSshClientModule()))
  36. .buildView(ComputeServiceContext.class);
  37.  
  38. // here's an example of the portable api
  39. Set<? extends Location> locations =
  40. context.getComputeService().listAssignableLocations();
  41.  
  42. Set<? extends Image> images = context.getComputeService().listImages();
  43.  
  44. System.out.println(images.size());
  45.  
  46. // pick the highest version of the RightScale CentOS template
  47. // Template template = context.getComputeService().templateBuilder().osFamily(OsFamily.CENTOS).build();
  48. //
  49. //// specify your own groups which already have the correct rules applied
  50. // template.getOptions().as(AWSEC2TemplateOptions.class).securityGroups(group1);
  51. //
  52. //// specify your own keypair for use in creating nodes
  53. // template.getOptions().as(AWSEC2TemplateOptions.class).keyPair(keyPair);
  54. //
  55. //// run a couple nodes accessible via group
  56. // Set<? extends NodeMetadata> nodes = context.getComputeService().createNodesInGroup("webserver", 2, template);
  57. //
  58. //// when you need access to very ec2-specific features, use the provider-specific context
  59. // AWSEC2Client ec2Client = AWSEC2Client.class.cast(context.getProviderSpecificContext().getApi());
  60. //
  61. //// ex. to get an ip and associate it with a node
  62. // NodeMetadata node = Iterables.get(nodes, 0);
  63. // String ip = ec2Client.getElasticIPAddressServices().allocateAddressInRegion(node.getLocation().getId());
  64. // ec2Client.getElasticIPAddressServices().associateAddressInRegion(node.getLocation().getId(),ip, node.getProviderId());
  65.  
  66. context.close();
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement