com.amazonaws.services.ec2.AmazonEC2Client Java Examples

The following examples show how to use com.amazonaws.services.ec2.AmazonEC2Client. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SubnetPlugin.java    From fullstop with Apache License 2.0 6 votes vote down vote up
private List<Reservation> fetchReservations(final AmazonEC2Client amazonEC2Client, final CloudTrailEvent event, final List<String> instanceIds){
    final DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();


    DescribeInstancesResult describeInstancesResult = null;
    try {
        describeInstancesResult = amazonEC2Client
                .describeInstances(describeInstancesRequest.withInstanceIds(instanceIds));
    }
    catch (final AmazonServiceException e) {

        LOG.warn("Subnet plugin: {}", e.getErrorMessage());
        return null;
    }

    return describeInstancesResult.getReservations();

}
 
Example #2
Source File: EC2FleetCloudTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void descriptorImpl_doFillFleetItems_returnAllFleetsIfShowAllIsEnabled() {
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    when(ec2Api.connect(anyString(), anyString(), anyString())).thenReturn(amazonEC2Client);

    DescribeSpotFleetRequestsResult describeSpotFleetRequestsResult = mock(DescribeSpotFleetRequestsResult.class);
    when(amazonEC2Client.describeSpotFleetRequests(any(DescribeSpotFleetRequestsRequest.class)))
            .thenReturn(describeSpotFleetRequestsResult);

    when(describeSpotFleetRequestsResult.getSpotFleetRequestConfigs())
            .thenReturn(Arrays.asList(spotFleetRequestConfig1, spotFleetRequestConfig2,
                    spotFleetRequestConfig3, spotFleetRequestConfig4, spotFleetRequestConfig5,
                    spotFleetRequestConfig6, spotFleetRequestConfig7, spotFleetRequestConfig8));

    ListBoxModel r = new EC2FleetCloud.DescriptorImpl().doFillFleetItems(
            true, "", "", "", "");

    assertEquals(8, r.size());
}
 
Example #3
Source File: EC2FleetCloudTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void descriptorImpl_doFillFleetItems_returnSelectedFleetInAnyState() {
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    when(ec2Api.connect(anyString(), anyString(), anyString())).thenReturn(amazonEC2Client);

    DescribeSpotFleetRequestsResult describeSpotFleetRequestsResult = mock(DescribeSpotFleetRequestsResult.class);
    when(amazonEC2Client.describeSpotFleetRequests(any(DescribeSpotFleetRequestsRequest.class)))
            .thenReturn(describeSpotFleetRequestsResult);

    spotFleetRequestConfig1.setSpotFleetRequestId("a");
    spotFleetRequestConfig2.setSpotFleetRequestId("failed_selected");
    spotFleetRequestConfig2.setSpotFleetRequestState(BatchState.Failed);

    when(describeSpotFleetRequestsResult.getSpotFleetRequestConfigs())
            .thenReturn(Arrays.asList(spotFleetRequestConfig1, spotFleetRequestConfig2));

    ListBoxModel r = new EC2FleetCloud.DescriptorImpl().doFillFleetItems(
            false, "", "", "", "failed_selected");

    assertEquals("a", r.get(0).value);
    assertEquals("failed_selected", r.get(1).value);
}
 
Example #4
Source File: EC2FleetCloudTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void descriptorImpl_doFillFleetItems_returnFleetIdAndStatusType() {
    AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
    when(ec2Api.connect(anyString(), anyString(), anyString())).thenReturn(amazonEC2Client);

    DescribeSpotFleetRequestsResult describeSpotFleetRequestsResult = mock(DescribeSpotFleetRequestsResult.class);
    when(amazonEC2Client.describeSpotFleetRequests(any(DescribeSpotFleetRequestsRequest.class)))
            .thenReturn(describeSpotFleetRequestsResult);

    spotFleetRequestConfig1.setSpotFleetRequestId("fleet-id");

    when(describeSpotFleetRequestsResult.getSpotFleetRequestConfigs())
            .thenReturn(Arrays.asList(spotFleetRequestConfig1));

    ListBoxModel r = new EC2FleetCloud.DescriptorImpl().doFillFleetItems(
            false, "", "", "", "");

    assertEquals("fleet-id (active) (maintain)", r.get(0).name);
    assertEquals("fleet-id", r.get(0).value);
}
 
Example #5
Source File: StopInstancesExample.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Stop specified instances (power-on the instances).
 *
 * @param instanceIDs
 *            IDs of the instances to stop
 * @return a list of state changes for the instances
 */
public static List<InstanceStateChange> stopInstances(final List<String> instanceIDs) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    // send the stop request with args as instance IDs to stop running instances
    StopInstancesRequest request = new StopInstancesRequest();
    request.withInstanceIds(instanceIDs);
    StopInstancesResult result = amazonEC2Client.stopInstances(request);

    return result.getStoppingInstances();
}
 
Example #6
Source File: AwsNetworkService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    String region = ac.getCloudContext().getLocation().getRegion().value();
    AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region);

    DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc());
    Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0);
    String vpcCidr = vpc.getCidrBlock();
    LOGGER.debug("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr);

    DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVpc())));
    List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets();
    List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList());
    LOGGER.debug("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), String.join(",", subnetCidrs));

    return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs);
}
 
Example #7
Source File: AwsInstanceConnector.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Retryable(
        value = SdkClientException.class,
        maxAttempts = 15,
        backoff = @Backoff(delay = 1000, multiplier = 2, maxDelay = 10000)
)
@Override
public List<CloudVmInstanceStatus> reboot(AuthenticatedContext ac, List<CloudInstance> vms) {
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()),
            ac.getCloudContext().getLocation().getRegion().value());
    List<CloudInstance> affectedVms = new ArrayList<>();
    try {
        if (!vms.isEmpty()) {
            List<CloudVmInstanceStatus> statuses = check(ac, vms);
            doReboot(affectedVms, amazonEC2Client, getStarted(statuses));
            doStart(affectedVms, ac, getStopped(statuses));
            logInvalidStatuses(getNotStoppedOrStarted(statuses));
        }
    } catch (SdkClientException e) {
        LOGGER.warn("Failed to send reboot request to AWS: ", e);
        throw e;
    }
    return pollerUtil.waitFor(ac, affectedVms, Sets.newHashSet(InstanceStatus.STARTED, InstanceStatus.FAILED));
}
 
Example #8
Source File: TaupageExpirationTimeProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void getExpirationTime() {
    final DescribeTagsResult response = new DescribeTagsResult()
            .withTags(new TagDescription()
                    .withResourceType("image")
                    .withResourceId(IMAGE_ID)
                    .withKey(TaupageExpirationTimeProviderImpl.TAG_KEY)
                    .withValue("2018-06-20T03:00:00+02:00"));

    when(mockEC2Client.describeTags(any(DescribeTagsRequest.class))).thenReturn(response);

    final ZonedDateTime result = expirationTimeProvider.getExpirationTime(REGION_NAME, IMAGE_OWNER, IMAGE_ID);
    assertThat(result).isEqualTo(ZonedDateTime.of(2018, 6, 20, 3, 0, 0, 0, ZoneOffset.ofHours(2)));

    verify(mockClientProvider).getClient(eq(AmazonEC2Client.class), eq(IMAGE_OWNER), eq(getRegion(fromName(REGION_NAME))));
    verify(mockEC2Client).describeTags(
            eq(new DescribeTagsRequest().withFilters(
                    new Filter("resource-id").withValues(IMAGE_ID),
                    new Filter("resource-type").withValues("image"),
                    new Filter("key").withValues(TaupageExpirationTimeProviderImpl.TAG_KEY))));
}
 
Example #9
Source File: AwsNetworkCfTemplateProviderTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testProvideWhenOnlyPublicSubnetsAndInterfaceServicesWithDifferentAzs() throws IOException, TemplateException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedJson = objectMapper.readTree(new File("src/test/resources/json/aws-cf-network-publicsubnet-vpcendpoints-differentazs.json"));

    when(freeMarkerTemplateUtils.processTemplateIntoString(any(), any())).thenCallRealMethod();
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    when(awsClient.createAccess(any(), anyString())).thenReturn(ec2Client);
    when(ec2Client.describeVpcEndpointServices()).thenReturn(createDescribeVpcEndpointServicesResultWithDifferentAzs());
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(true, true);
    List<SubnetRequest> subnetRequestList = createPublicSubnetRequestList();

    String actual = underTest.provide(networkCreationRequest, subnetRequestList);

    JsonNode json = objectMapper.readTree(actual);
    assertEquals(expectedJson, json);
    verify(freeMarkerTemplateUtils).processTemplateIntoString(any(Template.class), anyMap());
}
 
Example #10
Source File: CachingClientProviderTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testCachingClientProvider() {
    final AmazonEC2Client client = provider.getClient(AmazonEC2Client.class, ACCOUNT_ID1, REGION1);
    assertThat(client).isNotNull();

    assertThat(provider.getClient(AmazonEC2Client.class, ACCOUNT_ID1, REGION1))
            .isNotNull()
            .isSameAs(client);
    assertThat(provider.getClient(AmazonEC2Client.class, ACCOUNT_ID2, REGION1))
            .isNotNull()
            .isNotSameAs(client);
    assertThat(provider.getClient(AmazonEC2Client.class, ACCOUNT_ID1, REGION2))
            .isNotNull()
            .isNotSameAs(client);
    assertThat(provider.getClient(AmazonCloudWatchClient.class, ACCOUNT_ID1, REGION1))
            .isNotNull()
            .isNotSameAs(client);
}
 
Example #11
Source File: Ec2InstanceStore.java    From soundwave with Apache License 2.0 6 votes vote down vote up
@Override
public Map<AvailabilityZone, List<Instance>> getInstancesMapForZone(
    AvailabilityZone zone, AmazonEC2Client client) throws Exception {

  OperationStats op = new OperationStats("ec2InstanceStore", "getInstancesMapForZone");

  try {
    Map<AvailabilityZone, List<Instance>> ret = new HashMap<>();
    ret.put(zone, getInstancesForZone(zone, client));

    op.succeed();
    return ret;

  } catch (Exception e) {

    op.failed();
    logger.error(ExceptionUtils.getRootCauseMessage(e));
    throw e;
  }
}
 
Example #12
Source File: Ec2DaoImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * This implementation uses DescribeSpotPriceHistory API which returns the latest spot price history for the specified AZ and instance types. This method
 * then filters the returned list to only contain the latest spot price for each instance type.
 */
@Override
public List<SpotPrice> getLatestSpotPrices(String availabilityZone, Collection<String> instanceTypes, Collection<String> productDescriptions,
    AwsParamsDto awsParamsDto)
{
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest = new DescribeSpotPriceHistoryRequest();
    describeSpotPriceHistoryRequest.setAvailabilityZone(availabilityZone);
    describeSpotPriceHistoryRequest.setInstanceTypes(instanceTypes);
    describeSpotPriceHistoryRequest.setProductDescriptions(productDescriptions);
    DescribeSpotPriceHistoryResult describeSpotPriceHistoryResult = ec2Operations.describeSpotPriceHistory(ec2Client, describeSpotPriceHistoryRequest);
    List<SpotPrice> spotPrices = new ArrayList<>();
    Set<String> instanceTypesFound = new HashSet<>();
    for (SpotPrice spotPriceHistoryEntry : describeSpotPriceHistoryResult.getSpotPriceHistory())
    {
        if (instanceTypesFound.add(spotPriceHistoryEntry.getInstanceType()))
        {
            spotPrices.add(spotPriceHistoryEntry);
        }
    }
    return spotPrices;
}
 
Example #13
Source File: EncryptedSnapshotService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private Optional<String> prepareSnapshotForEncryptionBecauseThatDoesNotExist(AuthenticatedContext ac, CloudStack cloudStack, AwsInstanceView instanceView,
        AmazonEC2Client client, PersistenceNotifier resourceNotifier) {
    LOGGER.debug("Create an encrypted EBS volume for group: '{}'", instanceView.getGroupName());
    CreateVolumeResult volumeResult = client.createVolume(prepareCreateVolumeRequest(ac, instanceView, client, cloudStack));
    String volumeId = volumeResult.getVolume().getVolumeId();
    checkEbsVolumeStatus(ac, client, volumeId);
    saveEncryptedResource(ac, resourceNotifier, ResourceType.AWS_ENCRYPTED_VOLUME, volumeId, instanceView.getGroupName());
    LOGGER.debug("Encrypted EBS volume has been created with id: '{}', for group: '{}'", volumeId, instanceView.getGroupName());

    LOGGER.debug("Create an encrypted snapshot of EBS volume for group: '{}'", instanceView.getGroupName());
    CreateSnapshotResult snapshotResult = client.createSnapshot(prepareCreateSnapshotRequest(volumeResult));
    checkSnapshotReadiness(ac, client, snapshotResult);
    LOGGER.debug("Encrypted snapshot of EBS volume has been created with id: '{}', for group: '{}'", snapshotResult.getSnapshot().getSnapshotId(),
            instanceView.getGroupName());
    client.createTags(prepareCreateTagsRequest(ac, cloudStack, instanceView, snapshotResult));
    return Optional.of(snapshotResult.getSnapshot().getSnapshotId());
}
 
Example #14
Source File: AwsNetworkCfTemplateProviderTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@MethodSource("privateSubnetArguments")
public void testProvideWhenPrivateSubnetCreationEnabled(String expectedTemplate, List<String> gatewayServices, List<String> interfaceServices,
        DescribeVpcEndpointServicesResult describeVpcEndpointServicesResult) throws IOException, TemplateException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedJson = objectMapper.readTree(new File(expectedTemplate));

    when(freeMarkerTemplateUtils.processTemplateIntoString(any(), any())).thenCallRealMethod();
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    when(awsClient.createAccess(any(), anyString())).thenReturn(ec2Client);
    when(ec2Client.describeVpcEndpointServices()).thenReturn(describeVpcEndpointServicesResult);
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(true, true);
    List<SubnetRequest> subnetRequestList = createPrivateAndPublicSubnetRequestList();

    ReflectionTestUtils.setField(underTest, "gatewayServices", gatewayServices);
    ReflectionTestUtils.setField(underTest, "interfaceServices", interfaceServices);

    String actual = underTest.provide(networkCreationRequest, subnetRequestList);

    JsonNode json = objectMapper.readTree(actual);
    assertEquals(expectedJson, json);
    verify(freeMarkerTemplateUtils).processTemplateIntoString(any(Template.class), anyMap());
}
 
Example #15
Source File: AwsSubnetRequestProviderTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testProvideWhenFourAzAvailable() {
    AmazonEC2Client ec2Client = createEc2Client(List.of(createAZ(AZ_1), createAZ(AZ_2), createAZ(AZ_3), createAZ(AZ_4)));
    List<NetworkSubnetRequest> publicSubnets = List.of(createSubnetRequest(CIDR_4), createSubnetRequest(CIDR_5), createSubnetRequest(CIDR_6));
    List<NetworkSubnetRequest> privateSubnets = List.of(createSubnetRequest(CIDR_1), createSubnetRequest(CIDR_2), createSubnetRequest(CIDR_3));

    List<SubnetRequest> actual = underTest.provide(ec2Client, publicSubnets, privateSubnets);

    assertEquals(CIDR_4, actual.get(0).getPublicSubnetCidr());
    assertEquals(AZ_1, actual.get(0).getAvailabilityZone());
    assertEquals(CIDR_5, actual.get(1).getPublicSubnetCidr());
    assertEquals(AZ_2, actual.get(1).getAvailabilityZone());
    assertEquals(CIDR_6, actual.get(2).getPublicSubnetCidr());
    assertEquals(AZ_3, actual.get(2).getAvailabilityZone());

    assertEquals(CIDR_1, actual.get(3).getPrivateSubnetCidr());
    assertEquals(AZ_1, actual.get(3).getAvailabilityZone());
    assertEquals(CIDR_2, actual.get(4).getPrivateSubnetCidr());
    assertEquals(AZ_2, actual.get(4).getAvailabilityZone());
    assertEquals(CIDR_3, actual.get(5).getPrivateSubnetCidr());
    assertEquals(AZ_3, actual.get(5).getAvailabilityZone());
}
 
Example #16
Source File: SGLookupService.java    From Gatekeeper with Apache License 2.0 6 votes vote down vote up
private List<String> loadSgsForAccountRegion(AWSEnvironment environment) {
    logger.info("Grabbing SGs for environment " + environment);
    DescribeSecurityGroupsRequest describeSecurityGroupsRequest = new DescribeSecurityGroupsRequest();

    Filter groupNameFilter = new Filter();
    groupNameFilter.setName("group-name");
    groupNameFilter.setValues(Arrays.asList(securityGroupNames.split(",")));

    AmazonEC2Client amazonEC2Client = awsSessionService.getEC2Session(environment);
    DescribeSecurityGroupsResult result = amazonEC2Client.describeSecurityGroups(describeSecurityGroupsRequest.withFilters(groupNameFilter));

    logger.info("found " + result.getSecurityGroups().size() + " Security Groups with name(s) '" + securityGroupNames + "'");
    return result.getSecurityGroups().stream()
            .map(SecurityGroup::getGroupId)
            .collect(Collectors.toList());

}
 
Example #17
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithVersionSimilarToNumber() throws Exception {
    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString("application_id: fdsa\napplication_version: 6478e18".getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isPresent();

    assertThat(result.get().getApplicationId()).isEqualTo("fdsa");
    assertThat(result.get().getApplicationVersion()).isEqualTo("6478e18");

    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #18
Source File: AwsSetup.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void validateExistingKeyPair(InstanceAuthentication instanceAuthentication, AwsCredentialView credentialView, String region,
        AuthenticatedContext ac) {
    String keyPairName = awsClient.getExistingKeyPairName(instanceAuthentication);
    if (StringUtils.isNotEmpty(keyPairName)) {
        boolean keyPairIsPresentOnEC2 = false;
        try {
            AmazonEC2Client client = new AuthenticatedContextView(ac).getAmazonEC2Client();
            DescribeKeyPairsResult describeKeyPairsResult = client.describeKeyPairs(new DescribeKeyPairsRequest().withKeyNames(keyPairName));
            keyPairIsPresentOnEC2 = describeKeyPairsResult.getKeyPairs().stream().findFirst().isPresent();
        } catch (RuntimeException e) {
            String errorMessage = String.format("Failed to get the key pair [name: '%s'] from EC2 [roleArn:'%s'], detailed message: %s.",
                    keyPairName, credentialView.getRoleArn(), e.getMessage());
            LOGGER.info(errorMessage, e);
        }
        if (!keyPairIsPresentOnEC2) {
            throw new CloudConnectorException(String.format("The key pair '%s' could not be found in the '%s' region of EC2.", keyPairName, region));
        }
    }
}
 
Example #19
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithVersionSimilarToNumber1() throws Exception {
    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString("application_id: fdsa\napplication_version: '6478e18'".getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isPresent();

    assertThat(result.get().getApplicationId()).isEqualTo("fdsa");
    assertThat(result.get().getApplicationVersion()).isEqualTo("6478e18");

    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #20
Source File: AmiProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyAmiNotFound() throws Exception {

    when(ec2InstanceContextMock.getAmiId()).thenReturn(Optional.of(AMI_ID));
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);

    final DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(AMI_ID);
    when(amazonEC2ClientMock.describeImages(eq(describeImagesRequest)))
            .thenReturn(null);

    final Optional<Image> result = amiProvider.apply(ec2InstanceContextMock);

    assertThat(result).isEmpty();

    verify(ec2InstanceContextMock).getAmiId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeImages(eq(describeImagesRequest));
}
 
Example #21
Source File: AwsNetworkCfTemplateProviderTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testProvideWhenPrivateSubnetsAreDisabledAndInterfaceServicesWithDifferentAzs() throws IOException, TemplateException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedJson = objectMapper.readTree(new File("src/test/resources/json/aws-cf-network-publicsubnet-vpcendpoints-differentazs.json"));

    when(freeMarkerTemplateUtils.processTemplateIntoString(any(), any())).thenCallRealMethod();
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    when(awsClient.createAccess(any(), anyString())).thenReturn(ec2Client);
    when(ec2Client.describeVpcEndpointServices()).thenReturn(createDescribeVpcEndpointServicesResultWithDifferentAzs());
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(false, true);
    List<SubnetRequest> subnetRequestList = createPublicSubnetRequestList();

    String actual = underTest.provide(networkCreationRequest, subnetRequestList);

    JsonNode json = objectMapper.readTree(actual);
    assertEquals(expectedJson, json);
    verify(freeMarkerTemplateUtils).processTemplateIntoString(any(Template.class), anyMap());
}
 
Example #22
Source File: AmiProviderImpl.java    From fullstop with Apache License 2.0 5 votes vote down vote up
private Optional<Image> getAmi(@Nonnull final EC2InstanceContext context) {
    final Optional<String> amiId = context.getAmiId();
    try {
        return amiId
                .map(id -> context
                        .getClient(AmazonEC2Client.class)
                        .describeImages(new DescribeImagesRequest().withImageIds(id)))
                .map(DescribeImagesResult::getImages)
                .flatMap(images -> images.stream().findFirst());
    } catch (final AmazonClientException e) {
        log.warn("Could not get AMI of: " + amiId.get(), e);
        return empty();
    }
}
 
Example #23
Source File: EC2Utils.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
public static AmazonEC2Client createEC2Client() {
    BasicCredentialsProvider credentials = BasicCredentialsProvider.standard();
    AmazonEC2Client client = !credentials.isValid() ? null : (AmazonEC2Client)
            AmazonEC2ClientBuilder.standard()
            .withCredentials(credentials)
            .withRegion("eu-west-1")
            .build();
    return client;
}
 
Example #24
Source File: EncryptedImageCopyService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void deleteImage(AmazonEC2Client client, CloudResource encryptedImage, Image image, String regionName) {
    LOGGER.debug("Deregister encrypted AMI: '{}', in region: '{}'", encryptedImage.getName(), regionName);
    DeregisterImageRequest deregisterImageRequest = new DeregisterImageRequest().withImageId(encryptedImage.getName());
    client.deregisterImage(deregisterImageRequest);

    image.getBlockDeviceMappings()
            .stream()
            .filter(deviceMapping -> deviceMapping.getEbs() != null && isNotEmpty(deviceMapping.getEbs().getSnapshotId()))
            .forEach(deviceMapping -> deleteSnapshot(client, deviceMapping, encryptedImage, regionName));
}
 
Example #25
Source File: TaupageYamlProviderImpl.java    From fullstop with Apache License 2.0 5 votes vote down vote up
private Optional<TaupageYaml> getTaupageYaml(@Nonnull final EC2InstanceContext context) {

        if (context.isTaupageAmi().orElse(false)) {

            final String instanceId = context.getInstanceId();

            try {
                return Optional.of(context.getClient(AmazonEC2Client.class))
                        .map(client -> client.describeInstanceAttribute(new DescribeInstanceAttributeRequest()
                                .withInstanceId(instanceId)
                                .withAttribute(USER_DATA)))
                        .map(DescribeInstanceAttributeResult::getInstanceAttribute)
                        .map(InstanceAttribute::getUserData)
                        .map(Base64::decode)
                        .map(String::new)
                        .map(TaupageYamlUtil::parseTaupageYaml);

            } catch (final AmazonClientException e) {
                log.warn("Could not get Taupage YAML for instance: " + instanceId, e);
                return empty();
            } catch (YAMLException | IllegalArgumentException s)   {
                log.warn("Taupage YAML is not valid for instance: " + instanceId, s);
                return empty();
            }

        } else {
            return empty();
        }

    }
 
Example #26
Source File: AwsNetworkServiceTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindNonOverLappingCIDRWit24VpcEmptySubnet() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");

    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity);
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null,
            instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);

    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/24");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Collections.emptyList());

    thrown.expect(CloudConnectorException.class);
    thrown.expectMessage("The selected VPC has to be in a bigger CIDR range than /24");

    underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
}
 
Example #27
Source File: DescribeInstancesExample.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe specified instances within aws-mock.
 *
 * @param instanceIDs
 *            a list of instance IDs to describe
 * @return a list of specified instances
 */
public static List<Instance> describeInstances(final List<String> instanceIDs) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.withInstanceIds(instanceIDs);

    DescribeInstancesResult response = amazonEC2Client.describeInstances(request);
    List<Reservation> reservations = response.getReservations();

    List<Instance> ret = new ArrayList<Instance>();

    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();

        if (null != instances) {

            for (Instance i : instances) {
                ret.add(i);
            }
        }
    }

    return ret;
}
 
Example #28
Source File: AwsPublicKeyConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public void unregister(PublicKeyUnregisterRequest request) {
    LOGGER.debug("Deleting public key {} in {} region on AWS", request.getPublicKeyId(), request.getRegion());
    AwsCredentialView awsCredential = new AwsCredentialView(request.getCredential());
    try {
        AmazonEC2Client client = awsClient.createAccess(awsCredential, request.getRegion());
        DeleteKeyPairRequest deleteKeyPairRequest = new DeleteKeyPairRequest(request.getPublicKeyId());
        client.deleteKeyPair(deleteKeyPairRequest);
    } catch (Exception e) {
        String errorMessage = String.format("Failed to delete public key [%s: '%s', region: '%s'], detailed message: %s",
                getType(awsCredential), getAwsId(awsCredential), request.getRegion(), e.getMessage());
        LOGGER.error(errorMessage, e);
    }
}
 
Example #29
Source File: AwsLaunchService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void setElasticIps(String cFStackName, AmazonCloudFormationRetryClient cfRetryClient, AmazonEC2Client amazonEC2Client,
        List<Group> gateways, Map<String, List<String>> gatewayGroupInstanceMapping) {
    Map<String, String> eipAllocationIds = awsElasticIpService.getElasticIpAllocationIds(cfStackUtil.getOutputs(cFStackName, cfRetryClient), cFStackName);
    for (Group gateway : gateways) {
        List<String> eips = awsElasticIpService.getEipsForGatewayGroup(eipAllocationIds, gateway);
        List<String> instanceIds = gatewayGroupInstanceMapping.get(gateway.getName());
        awsElasticIpService.associateElasticIpsToInstances(amazonEC2Client, eips, instanceIds);
    }
}
 
Example #30
Source File: EC2.java    From h2o-2 with Apache License 2.0 5 votes vote down vote up
private List<Instance> wait(AmazonEC2Client ec2, List<String> ids) {
  System.out.println("Establishing ssh connections, make sure security group '" //
      + securityGroup + "' allows incoming TCP 22.");
  boolean tagsDone = false;
  for( ;; ) {
    try {
      if( !tagsDone ) {
        CreateTagsRequest createTagsRequest = new CreateTagsRequest();
        createTagsRequest.withResources(ids).withTags(new Tag("Name", NAME));
        ec2.createTags(createTagsRequest);
        tagsDone = true;
      }
      DescribeInstancesRequest request = new DescribeInstancesRequest();
      request.withInstanceIds(ids);
      DescribeInstancesResult result = ec2.describeInstances(request);
      List<Reservation> reservations = result.getReservations();
      List<Instance> instances = new ArrayList<Instance>();
      for( Reservation reservation : reservations )
        for( Instance instance : reservation.getInstances() )
          if( ip(instance) != null )
            instances.add(instance);
      if( instances.size() == ids.size() ) {
        // Try to connect to SSH port on each box
        if( canConnect(instances) )
          return instances;
      }
    } catch( AmazonServiceException xe ) {
      // Ignore and retry
    }
    try {
      Thread.sleep(500);
    } catch( InterruptedException e ) {
      throw Log.errRTExcept(e);
    }
  }
}