Search in sources :

Example 11 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsTerminateService method terminate.

public List<CloudResourceStatus> terminate(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
    LOGGER.debug("Deleting stack: {}", ac.getCloudContext().getId());
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    AuthenticatedContextView authenticatedContextView = new AuthenticatedContextView(ac);
    String regionName = authenticatedContextView.getRegion();
    AmazonEc2Client amazonEC2Client = authenticatedContextView.getAmazonEC2Client();
    AmazonCloudFormationClient amazonCloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName);
    LOGGER.debug("Calling deleteCloudWatchAlarmsForSystemFailures from AwsTerminateService");
    awsCloudWatchService.deleteAllCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView);
    waitAndDeleteCloudformationStack(ac, stack, resources, amazonCloudFormationClient);
    awsComputeResourceService.deleteComputeResources(ac, stack, resources);
    deleteKeyPair(ac, stack, amazonEC2Client, credentialView, regionName);
    deleteLaunchConfiguration(resources, ac);
    LOGGER.debug("Deleting stack finished");
    return awsResourceConnector.check(ac, resources);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 12 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AutoScalingGroupHandler method getAutoScalingGroups.

public Map<AutoScalingGroup, String> getAutoScalingGroups(AmazonCloudFormationClient cloudFormationClient, AmazonAutoScalingClient autoScalingClient, String stackName) {
    DescribeStackResourcesRequest resourcesRequest = new DescribeStackResourcesRequest();
    resourcesRequest.setStackName(stackName);
    DescribeStackResourcesResult resourcesResult = cloudFormationClient.describeStackResources(resourcesRequest);
    Map<String, String> autoScalingGroups = resourcesResult.getStackResources().stream().filter(stackResource -> "AWS::AutoScaling::AutoScalingGroup".equalsIgnoreCase(stackResource.getResourceType())).collect(Collectors.toMap(StackResource::getPhysicalResourceId, StackResource::getLogicalResourceId));
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
    request.setAutoScalingGroupNames(autoScalingGroups.keySet());
    List<AutoScalingGroup> scalingGroups = autoScalingClient.describeAutoScalingGroups(request).getAutoScalingGroups();
    return scalingGroups.stream().collect(Collectors.toMap(scalingGroup -> scalingGroup, scalingGroup -> autoScalingGroups.get(scalingGroup.getAutoScalingGroupName())));
}
Also used : Logger(org.slf4j.Logger) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) StackResource(com.amazonaws.services.cloudformation.model.StackResource) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) List(java.util.List) Component(org.springframework.stereotype.Component) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) Map(java.util.Map) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest) LaunchConfiguration(com.amazonaws.services.autoscaling.model.LaunchConfiguration) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest)

Example 13 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsNetworkConnectorTest method testDeleteNetworkWithSubNetsShouldDeleteTheStackAndTheResourceGroup.

@Test
public void testDeleteNetworkWithSubNetsShouldDeleteTheStackAndTheResourceGroup() {
    NetworkDeletionRequest networkDeletionRequest = createNetworkDeletionRequest();
    AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(networkDeletionRequest.getRegion()))).thenReturn(cfClient);
    when(cfClient.waiters()).thenReturn(cfWaiters);
    when(retryService.testWith2SecDelayMax15Times(any())).thenReturn(true);
    when(cfWaiters.stackDeleteComplete()).thenReturn(deletionWaiter);
    underTest.deleteNetworkWithSubnets(networkDeletionRequest);
    verify(cfClient).deleteStack(any(DeleteStackRequest.class));
    verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(deletionWaiter, times(1)).run(any());
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) NetworkDeletionRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkDeletionRequest) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) Test(org.junit.Test)

Example 14 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsNetworkConnectorTest method testCreateNetworkWithSubnetsShouldReturnTheNetworkAndSubnets.

@Test
public void testCreateNetworkWithSubnetsShouldReturnTheNetworkAndSubnets() {
    String networkCidr = "0.0.0.0/16";
    Set<NetworkSubnetRequest> subnets = Set.of(new NetworkSubnetRequest("1.1.1.1/8", PUBLIC), new NetworkSubnetRequest("1.1.1.2/8", PUBLIC));
    AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
    AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
    Map<String, String> output = createOutput();
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(networkCidr, subnets);
    List<SubnetRequest> subnetRequestList = createSubnetRequestList();
    Set<CreatedSubnet> createdSubnets = Set.of(new CreatedSubnet(), new CreatedSubnet(), new CreatedSubnet());
    when(awsClient.createEc2Client(any(), any())).thenReturn(ec2Client);
    when(awsSubnetRequestProvider.provide(ec2Client, new ArrayList<>(subnets), new ArrayList<>(subnets))).thenReturn(subnetRequestList);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient);
    when(cfClient.waiters()).thenReturn(cfWaiters);
    when(cfWaiters.stackCreateComplete()).thenReturn(creationWaiter);
    when(cfStackUtil.getOutputs(NETWORK_ID, cfClient)).thenReturn(output);
    when(awsCreatedSubnetProvider.provide(output, subnetRequestList, true)).thenReturn(createdSubnets);
    CreatedCloudNetwork actual = underTest.createNetworkWithSubnets(networkCreationRequest);
    verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(creationWaiter, times(1)).run(any());
    verify(cfStackUtil).getOutputs(NETWORK_ID, cfClient);
    verify(awsTaggingService, never()).prepareCloudformationTags(any(), any());
    verify(cfClient, never()).createStack(any(CreateStackRequest.class));
    assertEquals(VPC_ID, actual.getNetworkId());
    assertEquals(NUMBER_OF_SUBNETS, actual.getSubnets().size());
}
Also used : NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) CreateStackRequest(com.amazonaws.services.cloudformation.model.CreateStackRequest) Test(org.junit.Test)

Example 15 with AmazonCloudFormationClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.

the class AwsNetworkConnectorTest method testDeleteNetworkWithSubNetsShouldThrowAnExceptionWhenTheStackDeletionFailed.

@Test(expected = CloudConnectorException.class)
public void testDeleteNetworkWithSubNetsShouldThrowAnExceptionWhenTheStackDeletionFailed() {
    NetworkDeletionRequest networkDeletionRequest = createNetworkDeletionRequest();
    AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(networkDeletionRequest.getRegion()))).thenReturn(cfClient);
    when(cfClient.waiters()).thenReturn(cfWaiters);
    when(retryService.testWith2SecDelayMax15Times(any())).thenReturn(true);
    when(cfWaiters.stackDeleteComplete()).thenReturn(deletionWaiter);
    doThrow(new WaiterTimedOutException("fail")).when(deletionWaiter).run(any());
    underTest.deleteNetworkWithSubnets(networkDeletionRequest);
    verify(cfClient).deleteStack(any(DeleteStackRequest.class));
    verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) NetworkDeletionRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkDeletionRequest) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) Test(org.junit.Test)

Aggregations

AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)32 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)27 AmazonAutoScalingClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient)15 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)12 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)11 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)11 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)11 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)10 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)9 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)9 AmazonServiceException (com.amazonaws.AmazonServiceException)8 List (java.util.List)8 ArrayList (java.util.ArrayList)7 Inject (javax.inject.Inject)7 DescribeAutoScalingGroupsResult (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult)6 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)6 Group (com.sequenceiq.cloudbreak.cloud.model.Group)6 AdjustmentTypeWithThreshold (com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6