Java 类com.amazonaws.services.ec2.AmazonEC2 实例源码

项目:oscm    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupNoProxy() throws Exception {
    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);

    assertEquals(-1, clientConfig.getProxyPort());
    assertNull(clientConfig.getProxyHost());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

    // Test again for validating cached value
    AmazonEC2 newClient = ec2comm.getEC2();
    assertTrue(client == newClient);

}
项目:oscm    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupProxy() throws Exception {
    // Define proxy
    System.setProperty("https.proxyHost", "proxy");
    System.setProperty("https.proxyPort", "8080");

    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);

    assertEquals(8080, clientConfig.getProxyPort());
    assertEquals("proxy", clientConfig.getProxyHost());

    assertTrue(clientConfig.getProxyUsername() == null
            || clientConfig.getProxyUsername().isEmpty());
    assertTrue(clientConfig.getProxyPassword() == null
            || clientConfig.getProxyPassword().isEmpty());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

}
项目:oscm    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupProxyWithCredentials() throws Exception {
    // Define proxy
    System.setProperty("https.proxyHost", "proxy");
    System.setProperty("https.proxyPort", "8080");
    System.setProperty("https.proxyUser", PROXYUSER);
    System.setProperty("https.proxyPassword", PROXYPWD);

    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);
    clientConfig.getProxyUsername();
    assertEquals(8080, clientConfig.getProxyPort());
    assertEquals("proxy", clientConfig.getProxyHost());
    assertEquals(PROXYUSER, clientConfig.getProxyUsername());
    assertEquals(PROXYPWD, clientConfig.getProxyPassword());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

}
项目:oscm    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupProxyWithEmptyCredentials() throws Exception {
    // Define proxy
    System.setProperty("https.proxyHost", "proxy");
    System.setProperty("https.proxyPort", "8080");
    System.setProperty("https.proxyUser", "");
    System.setProperty("https.proxyPassword", "");

    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);

    assertEquals(8080, clientConfig.getProxyPort());
    assertEquals("proxy", clientConfig.getProxyHost());

    assertTrue(clientConfig.getProxyUsername() == null
            || clientConfig.getProxyUsername().isEmpty());
    assertTrue(clientConfig.getProxyPassword() == null
            || clientConfig.getProxyPassword().isEmpty());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

}
项目:oscm    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupNonProxy() throws Exception {
    // Define proxy
    System.setProperty("https.proxyHost", "proxy");
    System.setProperty("https.proxyPort", "8080");
    // But set endpoint as non proxy
    System.setProperty("http.nonProxyHosts", "*.amazonaws.com");

    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);

    assertEquals(8080, clientConfig.getProxyPort());
    assertTrue(clientConfig.getProxyUsername() == null
            || clientConfig.getProxyUsername().isEmpty());
    assertTrue(clientConfig.getProxyPassword() == null
            || clientConfig.getProxyPassword().isEmpty());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

}
项目:oscm    文件:EC2CommunicationTest.java   
@Test
public void testAWSCredentials() throws Exception {
    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);

    AWSCredentials credentials = credProvider.getCredentials();
    assertEquals("access_key", credentials.getAWSAccessKeyId());
    assertEquals("secret_key", credentials.getAWSSecretKey());

    credProvider.refresh();
    credentials = credProvider.getCredentials();
    assertEquals("access_key", credentials.getAWSAccessKeyId());
    assertEquals("secret_key", credentials.getAWSSecretKey());

}
项目:ec2-spot-jenkins-plugin    文件:EC2FleetCloud.java   
public ListBoxModel doFillRegionItems(@QueryParameter final String credentialsId,
                                      @QueryParameter final String region)
        throws IOException, ServletException {
    final List<Region> regionList;

    try {
        final AmazonEC2 client = connect(credentialsId, null);
        final DescribeRegionsResult regions=client.describeRegions();
        regionList=regions.getRegions();
    } catch(final Exception ex) {
        //Ignore bad exceptions
        return new ListBoxModel();
    }

    final ListBoxModel model = new ListBoxModel();
    for(final Region reg : regionList) {
        model.add(new ListBoxModel.Option(reg.getRegionName(), reg.getRegionName()));
    }
    return model;
}
项目:photon-model    文件:TestUtils.java   
public static AmazonEC2 getEC2SynchronousClient(AuthCredentialsServiceState credentials,
        String region) {
    ClientConfiguration configuration = new ClientConfiguration();
    configuration.withRetryPolicy(new RetryPolicy(new CustomRetryCondition(),
            DEFAULT_BACKOFF_STRATEGY,
            DEFAULT_MAX_ERROR_RETRY,
            true));

    AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(
            new BasicAWSCredentials(credentials.privateKeyId,
                    EncryptionUtils.decrypt(credentials.privateKey)));

    AmazonEC2ClientBuilder ec2ClientBuilder = AmazonEC2ClientBuilder.standard()
            .withCredentials(awsStaticCredentialsProvider)
            .withRegion(region)
            .withClientConfiguration(configuration);

    return ec2ClientBuilder.build();
}
项目:aws-doc-sdk-examples    文件:RebootInstance.java   
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply an instance id\n" +
        "Ex: RebootInstnace <instance_id>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String instance_id = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    RebootInstancesRequest request = new RebootInstancesRequest()
        .withInstanceIds(instance_id);

    RebootInstancesResult response = ec2.rebootInstances(request);

    System.out.printf(
        "Successfully rebooted instance %s", instance_id);
}
项目:aws-doc-sdk-examples    文件:DeleteSecurityGroup.java   
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply a security group id\n" +
        "Ex: DeleteSecurityGroup <security-group-id>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String group_id = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DeleteSecurityGroupRequest request = new DeleteSecurityGroupRequest()
        .withGroupId(group_id);

    DeleteSecurityGroupResult response = ec2.deleteSecurityGroup(request);

    System.out.printf(
        "Successfully deleted security group with id %s", group_id);
}
项目:aws-doc-sdk-examples    文件:DescribeAddresses.java   
public static void main(String[] args)
{
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DescribeAddressesResult response = ec2.describeAddresses();

    for(Address address : response.getAddresses()) {
        System.out.printf(
                "Found address with public IP %s, " +
                "domain %s, " +
                "allocation id %s " +
                "and NIC id %s",
                address.getPublicIp(),
                address.getDomain(),
                address.getAllocationId(),
                address.getNetworkInterfaceId());
    }
}
项目:aws-doc-sdk-examples    文件:CreateKeyPair.java   
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply a key pair name\n" +
        "Ex: CreateKeyPair <key-pair-name>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String key_name = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    CreateKeyPairRequest request = new CreateKeyPairRequest()
        .withKeyName(key_name);

    CreateKeyPairResult response = ec2.createKeyPair(request);

    System.out.printf(
        "Successfulyl created key pair named %s",
        key_name);
}
项目:aws-doc-sdk-examples    文件:ReleaseAddress.java   
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply an allocation ID.\n" +
        "Ex: ReleaseAddress <allocation_id>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String alloc_id = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    ReleaseAddressRequest request = new ReleaseAddressRequest()
        .withAllocationId(alloc_id);

    ReleaseAddressResult response = ec2.releaseAddress(request);

    System.out.printf(
        "Successfully released elastic IP address %s", alloc_id);
}
项目:aws-doc-sdk-examples    文件:DeleteKeyPair.java   
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply a key pair name\n" +
        "Ex: DeleteKeyPair <key-pair-name>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String key_name = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DeleteKeyPairRequest request = new DeleteKeyPairRequest()
        .withKeyName(key_name);

    DeleteKeyPairResult response = ec2.deleteKeyPair(request);

    System.out.printf(
        "Successfully deleted key pair named %s", key_name);
}
项目:cerberus-lifecycle-cli    文件:AmiTagCheckServiceTest.java   
@Test
public void isAmiWithTagExistTrue() {
    AmazonEC2 ec2Client = mock(AmazonEC2.class);
    AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client);

    String amiId = "ami-1234abcd";
    String tagName = "sometag";
    String tagValue = "someval";

    when(ec2Client.describeImages(
            new DescribeImagesRequest()
                    .withFilters(new Filter().withName(tagName).withValues(tagValue))
                    .withFilters(new Filter().withName("image-id").withValues(amiId))
            )
    ).thenReturn(
            new DescribeImagesResult().withImages(new Image())
            );

    // invoke method under test
    assertTrue(amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue));
}
项目:cerberus-lifecycle-cli    文件:AmiTagCheckServiceTest.java   
@Test
public void isAmiWithTagExistFalse() {
    AmazonEC2 ec2Client = mock(AmazonEC2.class);
    AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client);

    String amiId = "ami-1234abcd";
    String tagName = "sometag";
    String tagValue = "someval";

    when(ec2Client.describeImages(
            new DescribeImagesRequest()
                    .withFilters(new Filter().withName(tagName).withValues(tagValue))
                    .withFilters(new Filter().withName("image-id").withValues(amiId))
            )
    ).thenReturn(
            new DescribeImagesResult()
            );

    // invoke method under test
    assertFalse(amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue));
}
项目:cerberus-lifecycle-cli    文件:AmiTagCheckServiceTest.java   
@Test
public void isAmiWithTagExistNotFound() {
    AmazonEC2 ec2Client = mock(AmazonEC2.class);
    AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client);

    String amiId = "ami-1234abcd";
    String tagName = "sometag";
    String tagValue = "someval";

    AmazonServiceException ex = new AmazonServiceException("fake-exception");
    ex.setErrorCode("InvalidAMIID.NotFound");

    when(ec2Client.describeImages(
            new DescribeImagesRequest()
                    .withFilters(new Filter().withName(tagName).withValues(tagValue))
                    .withFilters(new Filter().withName("image-id").withValues(amiId))
            )
    ).thenThrow(ex);

    // invoke method under test
    assertFalse(amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue));
}
项目:cerberus-lifecycle-cli    文件:AmiTagCheckServiceTest.java   
@Test
public void isAmiWithTagExistThrowException() {
    AmazonEC2 ec2Client = mock(AmazonEC2.class);
    AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client);

    String amiId = "ami-1234abcd";
    String tagName = "sometag";
    String tagValue = "someval";
    String unknownAwsExMessage = "Unknown AWS exception message";

    when(ec2Client.describeImages(
            new DescribeImagesRequest()
                    .withFilters(new Filter().withName(tagName).withValues(tagValue))
                    .withFilters(new Filter().withName("image-id").withValues(amiId))
            )
    ).thenThrow(new AmazonServiceException(unknownAwsExMessage));

    try {
        // invoke method under test
        amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue);
        fail("Expected exception message '" + unknownAwsExMessage + "'not received");
    } catch (AmazonServiceException ex) {
        // pass
        assertEquals(unknownAwsExMessage, ex.getErrorMessage());
    }
}
项目:development    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupNoProxy() throws Exception {
    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);

    assertEquals(-1, clientConfig.getProxyPort());
    assertNull(clientConfig.getProxyHost());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

    // Test again for validating cached value
    AmazonEC2 newClient = ec2comm.getEC2();
    assertTrue(client == newClient);

}
项目:development    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupProxy() throws Exception {
    // Define proxy
    System.setProperty("https.proxyHost", "proxy");
    System.setProperty("https.proxyPort", "8080");

    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);

    assertEquals(8080, clientConfig.getProxyPort());
    assertEquals("proxy", clientConfig.getProxyHost());

    assertTrue(clientConfig.getProxyUsername() == null
            || clientConfig.getProxyUsername().isEmpty());
    assertTrue(clientConfig.getProxyPassword() == null
            || clientConfig.getProxyPassword().isEmpty());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

}
项目:development    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupProxyWithCredentials() throws Exception {
    // Define proxy
    System.setProperty("https.proxyHost", "proxy");
    System.setProperty("https.proxyPort", "8080");
    System.setProperty("https.proxyUser", PROXYUSER);
    System.setProperty("https.proxyPassword", PROXYPWD);

    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);
    clientConfig.getProxyUsername();
    assertEquals(8080, clientConfig.getProxyPort());
    assertEquals("proxy", clientConfig.getProxyHost());
    assertEquals(PROXYUSER, clientConfig.getProxyUsername());
    assertEquals(PROXYPWD, clientConfig.getProxyPassword());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

}
项目:development    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupProxyWithEmptyCredentials() throws Exception {
    // Define proxy
    System.setProperty("https.proxyHost", "proxy");
    System.setProperty("https.proxyPort", "8080");
    System.setProperty("https.proxyUser", "");
    System.setProperty("https.proxyPassword", "");

    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);

    assertEquals(8080, clientConfig.getProxyPort());
    assertEquals("proxy", clientConfig.getProxyHost());

    assertTrue(clientConfig.getProxyUsername() == null
            || clientConfig.getProxyUsername().isEmpty());
    assertTrue(clientConfig.getProxyPassword() == null
            || clientConfig.getProxyPassword().isEmpty());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

}
项目:development    文件:EC2CommunicationTest.java   
@Test
public void testEC2SetupNonProxy() throws Exception {
    // Define proxy
    System.setProperty("https.proxyHost", "proxy");
    System.setProperty("https.proxyPort", "8080");
    // But set endpoint as non proxy
    System.setProperty("http.nonProxyHosts", "*.amazonaws.com");

    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);
    assertNotNull(clientConfig);

    assertEquals(8080, clientConfig.getProxyPort());
    assertTrue(clientConfig.getProxyUsername() == null
            || clientConfig.getProxyUsername().isEmpty());
    assertTrue(clientConfig.getProxyPassword() == null
            || clientConfig.getProxyPassword().isEmpty());
    verify(ec2).setEndpoint("ec2.test.amazonaws.com");

}
项目:development    文件:EC2CommunicationTest.java   
@Test
public void testAWSCredentials() throws Exception {
    // Ask for connection
    AmazonEC2 client = ec2comm.getEC2();
    assertNotNull(client);
    assertNotNull(credProvider);

    AWSCredentials credentials = credProvider.getCredentials();
    assertEquals("access_key", credentials.getAWSAccessKeyId());
    assertEquals("secret_key", credentials.getAWSSecretKey());

    credProvider.refresh();
    credentials = credProvider.getCredentials();
    assertEquals("access_key", credentials.getAWSAccessKeyId());
    assertEquals("secret_key", credentials.getAWSSecretKey());

}
项目:ec2-util    文件:AwsEc2Client.java   
/**
 * Search Ec2 Instance by Name tag.
 * 
 * @param ec2
 * @param targetName
 *            Search Keyword for Name tag
 * @return Instance with Name tag equals targetName. If it does't found,
 *         then return null.
 */
public static Instance findInstanceByName(AmazonEC2 ec2, String targetName) {
    DescribeInstancesResult instanceResult = ec2.describeInstances();
    List<Reservation> reservations = instanceResult.getReservations();
    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();
        for (Instance instance : instances) {
            List<Tag> tagList = instance.getTags();
            String name = "";
            for (Tag tag : tagList) {
                String tagKey = tag.getKey();
                String tagValue = tag.getValue();
                if (tagKey.contains("Name")) {
                    name = tagValue;
                    if (targetName.equals(name)) {
                        return instance;
                    }
                    break;
                }
            }
        }
    }
    return null;
}
项目:enhanced-snapshots    文件:VolumeServiceImpl.java   
private Set<VolumeDto> getVolumes(AmazonEC2 amazonEC2) {
    if (cache != null) {
        return cache;
    } else {
        try {
            Set<VolumeDto> result = new TreeSet<>(volumeDtoComparator);
            result.addAll(VolumeDtoConverter.convert(amazonEC2.describeVolumes().getVolumes()));
            result.addAll(getHistoryVolumes());
            result = setSchedules(result);
            LOG.debug("Volume list: [{}]", result);
            cache = result;
            return result;
        } catch (RuntimeException e) {
            LOG.error("Failed to get volume list.", e);
            throw new DataAccessException("Failed to get volume list.", e);
        }
    }
}
项目:ec2-plugin    文件:SlaveTemplate.java   
/**
 * Update the tags stored in EC2 with the specified information.
 * Re-try 5 times if instances isn't up by catchErrorCode - e.g. InvalidSpotInstanceRequestID.NotFound or InvalidInstanceRequestID.NotFound
 * @param ec2
 * @param inst_tags
 * @param catchErrorCode
 * @param params
 * @throws InterruptedException
 */
private void updateRemoteTags(AmazonEC2 ec2, Collection<Tag> inst_tags, String catchErrorCode, String... params) throws InterruptedException {
    for (int i = 0; i < 5; i++) {
        try {
            CreateTagsRequest tag_request = new CreateTagsRequest();
            tag_request.withResources(params).setTags(inst_tags);
            ec2.createTags(tag_request);
            break;
        } catch (AmazonServiceException e) {
            if (e.getErrorCode().equals(catchErrorCode)) {
                Thread.sleep(5000);
                continue;
            }
            LOGGER.log(Level.SEVERE, e.getErrorMessage(), e);
        }
    }
}
项目:ec2-plugin    文件:SlaveTemplate.java   
/**
 * Provisions a new EC2 slave based on the currently running instance on EC2,
 * instead of starting a new one.
 */
public EC2AbstractSlave attach(String instanceId, TaskListener listener) throws AmazonClientException, IOException {
    PrintStream logger = listener.getLogger();
    AmazonEC2 ec2 = getParent().connect();

    try {
        logger.println("Attaching to "+instanceId);
        LOGGER.info("Attaching to "+instanceId);
        DescribeInstancesRequest request = new DescribeInstancesRequest();
        request.setInstanceIds(Collections.singletonList(instanceId));
        Instance inst = ec2.describeInstances(request).getReservations().get(0).getInstances().get(0);
        return newOndemandSlave(inst);
    } catch (FormException e) {
        throw new AssertionError(); // we should have discovered all configuration issues upfront
    }
}
项目:ec2-plugin    文件:EC2AbstractSlave.java   
public static Instance getInstance(String instanceId, EC2Cloud cloud) {
    if (instanceId == null || instanceId == "" || cloud == null) {
        return null;
    }

    Instance i = null;
    try {
        DescribeInstancesRequest request = new DescribeInstancesRequest();
        request.setInstanceIds(Collections.<String>singletonList(instanceId));
        AmazonEC2 ec2 = cloud.connect();
        List<Reservation> reservations = ec2.describeInstances(request).getReservations();
        if (reservations.size() > 0) {
            List<Instance> instances = reservations.get(0).getInstances();
            if (instances.size() > 0) {
                i = instances.get(0);
            }
        }
    } catch (AmazonClientException e) {
        LOGGER.log(Level.WARNING, "Failed to fetch EC2 instance: " + instanceId, e);
    }
    return i;
}
项目:ec2-plugin    文件:EC2AbstractSlave.java   
public static ListBoxModel fillZoneItems(AWSCredentialsProvider credentialsProvider, String region) {
    ListBoxModel model = new ListBoxModel();
    if (AmazonEC2Cloud.testMode) {
        model.add(TEST_ZONE);
        return model;
    }

    if (!StringUtils.isEmpty(region)) {
        AmazonEC2 client = EC2Cloud.connect(credentialsProvider, AmazonEC2Cloud.getEc2EndpointUrl(region));
        DescribeAvailabilityZonesResult zones = client.describeAvailabilityZones();
        List<AvailabilityZone> zoneList = zones.getAvailabilityZones();
        model.add("<not specified>", "");
        for (AvailabilityZone z : zoneList) {
            model.add(z.getZoneName(), z.getZoneName());
        }
    }
    return model;
}
项目:ec2-plugin    文件:EC2Cloud.java   
protected FormValidation doTestConnection(URL ec2endpoint,
        boolean useInstanceProfileForCredentials, String accessId, String secretKey, String privateKey) throws IOException, ServletException {
    try {
        AWSCredentialsProvider credentialsProvider = createCredentialsProvider(useInstanceProfileForCredentials, accessId, secretKey);
        AmazonEC2 ec2 = connect(credentialsProvider, ec2endpoint);
        ec2.describeInstances();

        if (privateKey == null) {
            return FormValidation.error("Private key is not specified. Click 'Generate Key' to generate one.");
        }

        if (privateKey.trim().length() > 0) {
            // check if this key exists
            EC2PrivateKey pk = new EC2PrivateKey(privateKey);
            if (pk.find(ec2) == null) {
                return FormValidation.error("The EC2 key pair private key isn't registered to this EC2 region (fingerprint is " + pk.getFingerprint() + ")");
            }
        }

        return FormValidation.ok(Messages.EC2Cloud_Success());
    } catch (AmazonClientException e) {
        LOGGER.log(Level.WARNING, "Failed to check EC2 credential", e);
        return FormValidation.error(e.getMessage());
    }
}
项目:ec2-plugin    文件:AmazonEC2Cloud.java   
public ListBoxModel doFillRegionItems(@QueryParameter boolean useInstanceProfileForCredentials,
        @QueryParameter String accessId, @QueryParameter String secretKey,
        @QueryParameter String region) throws IOException, ServletException {
    ListBoxModel model = new ListBoxModel();
    if (testMode) {
        model.add(DEFAULT_EC2_HOST);
        return model;
    }

    if (useInstanceProfileForCredentials || (!StringUtils.isEmpty(accessId) && !StringUtils.isEmpty(secretKey))) {
        AWSCredentialsProvider credentialsProvider = createCredentialsProvider(useInstanceProfileForCredentials, accessId, secretKey);
        AmazonEC2 client = connect(credentialsProvider, new URL("http://ec2.amazonaws.com"));
        DescribeRegionsResult regions = client.describeRegions();
        List<Region> regionList = regions.getRegions();
        for (Region r : regionList) {
            String name = r.getRegionName();
            model.add(name, name);
        }
    }
    return model;
}
项目:cloudwatch-tomcat-valve    文件:ElapsedTimeAggregatorTest.java   
/**
 * Default constructor
 */
public ElapsedTimeAggregatorTest() {
    AmazonEC2 ec2Client = mock(AmazonEC2.class);
    AmazonCloudWatch cloudWatchClient = mock(AmazonCloudWatch.class);
    Region region = Region.getRegion(Regions.US_WEST_1);

    when(ec2Client.describeTags(any(DescribeTagsRequest.class))).
            thenReturn(new DescribeTagsResult());

    instanceOnlyAggregator = new ElapsedTimeAggregator("TEST", region, "i-500f6ca6", null, ec2Client, cloudWatchClient);

    when(ec2Client.describeTags(any(DescribeTagsRequest.class))).
            thenReturn(new DescribeTagsResult().withTags(
                    new TagDescription().
                            withKey("aws:autoscaling:groupName").
                            withValue("TEST")
            ));
    asgAggregator = new ElapsedTimeAggregator("TEST", region, "i-500f6ca6", null, ec2Client, cloudWatchClient);
}
项目:jenkins-docker-build-plugin    文件:EC2DockerHostProvider.java   
@Override
public Collection<HostAndPort> listHosts() throws Exception {
    List<HostAndPort> hosts = newArrayList();

    for (AmazonEC2 client : _amazonEC2) {
        try {
            hosts.addAll(listHosts(client));
        } catch (Throwable ex) {
            LOG.log(WARNING, "Error listing EC2 hosts", ex);
        }
    }

    LOG.log(FINE, "Discovered {0} instances: filters={1}", new Object[]{hosts.size(), _filters});

    return hosts;
}
项目:primecloud-controller    文件:AbstractAwsClientWrapper.java   
/**
 * TODO: メソッドコメント
 * 
 * @param client
 * @return
 */
public AmazonEC2 wrap(final AmazonEC2 client) {
    InvocationHandler handler = new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (!AmazonWebServiceResult.class.isAssignableFrom(method.getReturnType())) {
                return method.invoke(client, args);
            }

            return doInvoke(client, proxy, method, args);
        }
    };

    return (AmazonEC2) Proxy.newProxyInstance(LoggingAwsClientWrapper.class.getClassLoader(),
            new Class[] { AmazonEC2.class }, handler);
}
项目:primecloud-controller    文件:AmazonAwsClientFactory.java   
/**
 * {@inheritDoc}
 */
@Override
public AmazonEC2 createEc2Client(String awsAccessId, String awsSecretKey) {
    AWSCredentials credentials = new BasicAWSCredentials(awsAccessId, awsSecretKey);
    ClientConfiguration configuration = createConfiguration();

    AmazonEC2 client = new AmazonEC2Client(credentials, configuration);

    if (host != null) {
        client.setEndpoint(AmazonEC2.ENDPOINT_PREFIX + "." + host);
    }

    client = new ExceptionHandleAwsClientWrapper().wrap(client);

    return client;
}
项目:incubator-gobblin    文件:AWSSdkClient.java   
/***
 * Create an Amazon AWS security group
 *
 * @param groupName Security group name
 * @param description Security group description
 */
public void createSecurityGroup(String groupName,
    String description) {

  AmazonEC2 amazonEC2 = getEc2Client();
  try {
    final CreateSecurityGroupRequest securityGroupRequest = new CreateSecurityGroupRequest()
        .withGroupName(groupName)
        .withDescription(description);
    amazonEC2.createSecurityGroup(securityGroupRequest);

    LOGGER.info("Created Security Group: " + groupName);
  } catch (AmazonServiceException ase) {
    // This might mean that security group is already created, hence ignore
    LOGGER.warn("Issue in creating security group", ase);
  }
}
项目:incubator-gobblin    文件:AWSSdkClient.java   
/***
 * Open firewall for a security group
 *
 * @param groupName Open firewall for this security group
 * @param ipRanges Open firewall for this IP range
 * @param ipProtocol Open firewall for this protocol type (eg. tcp, udp)
 * @param fromPort Open firewall for port range starting at this port
 * @param toPort Open firewall for port range ending at this port
 */
public void addPermissionsToSecurityGroup(String groupName,
    String ipRanges,
    String ipProtocol,
    Integer fromPort,
    Integer toPort) {

  final AmazonEC2 amazonEC2 = getEc2Client();

  final IpPermission ipPermission = new IpPermission()
      .withIpRanges(ipRanges)
      .withIpProtocol(ipProtocol)
      .withFromPort(fromPort)
      .withToPort(toPort);
  final AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest =
      new AuthorizeSecurityGroupIngressRequest()
          .withGroupName(groupName)
          .withIpPermissions(ipPermission);
  amazonEC2.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);

  LOGGER.info("Added permissions: " + ipPermission + " to security group: " + groupName);
}
项目:incubator-gobblin    文件:AWSSdkClient.java   
/***
 * Get list of EC2 {@link Instance}s for a auto scaling group
 *
 * @param groupName Auto scaling group name
 * @param status Instance status (eg. running)
 * @return List of EC2 instances found for the input auto scaling group
 */
public List<Instance> getInstancesForGroup(String groupName,
    String status) {

  final AmazonEC2 amazonEC2 = getEc2Client();

  final DescribeInstancesResult instancesResult = amazonEC2.describeInstances(new DescribeInstancesRequest()
      .withFilters(new Filter().withName("tag:aws:autoscaling:groupName").withValues(groupName)));

  final List<Instance> instances = new ArrayList<>();
  for (Reservation reservation : instancesResult.getReservations()) {
    for (Instance instance : reservation.getInstances()) {
      if (null == status|| null == instance.getState()
          || status.equals(instance.getState().getName())) {
        instances.add(instance);
        LOGGER.info("Found instance: " + instance + " which qualified filter: " + status);
      } else {
        LOGGER.info("Found instance: " + instance + " but did not qualify for filter: " + status);
      }
    }
  }

  return instances;
}
项目:Raigad    文件:RaigadConfiguration.java   
/**
 * Get the fist 3 available zones in the region
 */
public void setDefaultRACList(String region) {
    AmazonEC2 client = new AmazonEC2Client(provider.getAwsCredentialProvider());
    client.setEndpoint("ec2." + region + ".amazonaws.com");
    DescribeAvailabilityZonesResult res = client.describeAvailabilityZones();
    List<String> zone = Lists.newArrayList();

    for (AvailabilityZone reg : res.getAvailabilityZones()) {
        if (reg.getState().equals("available")) {
            zone.add(reg.getZoneName());
        }
        if (zone.size() == 3) {
            break;
        }
    }
    DEFAULT_AVAILABILITY_ZONES = ImmutableList.copyOf(zone);
}