use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsNetworkConnectorTest method testDeleteNetworkWithSubNetsWhenCfStackDoesNotExist.
@Test
public void testDeleteNetworkWithSubNetsWhenCfStackDoesNotExist() {
NetworkDeletionRequest networkDeletionRequest = createNetworkDeletionRequest();
AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(networkDeletionRequest.getRegion()))).thenReturn(cfClient);
when(retryService.testWith2SecDelayMax15Times(any())).thenReturn(false);
underTest.deleteNetworkWithSubnets(networkDeletionRequest);
verify(cfClient, never()).deleteStack(any(DeleteStackRequest.class));
}
use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsNetworkConnectorTest method testCreateNewNetworkWithSubnetsShouldCreateTheNetworkAndSubnets.
@Test
public void testCreateNewNetworkWithSubnetsShouldCreateTheNetworkAndSubnets() {
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));
AmazonServiceException amazonServiceException = new AmazonServiceException("does not exist");
amazonServiceException.setStatusCode(400);
AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
when(cfClient.describeStacks(any(DescribeStacksRequest.class))).thenThrow(amazonServiceException);
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(awsNetworkCfTemplateProvider).provide(networkCreationRequest, subnetRequestList);
verify(creationWaiter, times(1)).run(any());
verify(awsTaggingService).prepareCloudformationTags(any(), any());
verify(cfClient).createStack(any(CreateStackRequest.class));
verify(cfStackUtil).getOutputs(NETWORK_ID, cfClient);
assertEquals(VPC_ID, actual.getNetworkId());
assertEquals(NUMBER_OF_SUBNETS, actual.getSubnets().size());
}
use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsLaunchTest method launchStack.
@Test
public void launchStack() throws Exception {
setup();
setupRetryService();
setupFreemarkerTemplateProcessing();
setupDescribeStacksResponses();
setupDescribeImagesResponse();
setupDescribeStackResourceResponse();
setupAutoscalingResponses();
setupDescribeInstancesResponse();
setupCreateVolumeResponse();
setupDescribeVolumeResponse();
setupDescribeSubnetResponse();
setupDescribePrefixListsResponse();
InMemoryStateStore.putStack(1L, PollGroup.POLLABLE);
AuthenticatedContext authenticatedContext = componentTestUtil.getAuthenticatedContext();
authenticatedContext.putParameter(AmazonEc2Client.class, amazonEc2Client);
awsResourceConnector.launch(authenticatedContext, componentTestUtil.getStackForLaunch(InstanceStatus.CREATE_REQUESTED, InstanceStatus.CREATE_REQUESTED), persistenceNotifier, new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, Long.MAX_VALUE));
// assert
verify(persistenceNotifier).notifyAllocation(argThat(cloudResource -> ResourceType.AWS_VPC.equals(cloudResource.getType())), any());
verify(persistenceNotifier, times(2)).notifyAllocation(argThat(cloudResource -> ResourceType.AWS_VOLUMESET.equals(cloudResource.getType())), any());
verify(persistenceNotifier).notifyAllocation(argThat(cloudResource -> ResourceType.AWS_SUBNET.equals(cloudResource.getType())), any());
verify(persistenceNotifier).notifyAllocation(argThat(cloudResource -> ResourceType.CLOUDFORMATION_STACK.equals(cloudResource.getType())), any());
InOrder inOrder = inOrder(amazonEc2Client, amazonCloudFormationClient, amazonEc2Client);
inOrder.verify(amazonEc2Client).describeImages(any());
inOrder.verify(amazonCloudFormationClient).createStack(any());
inOrder.verify(amazonEc2Client, times(2)).createVolume(any());
inOrder.verify(amazonEc2Client, times(2)).attachVolume(any());
inOrder.verify(amazonEc2Client, never()).describePrefixLists();
}
use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsCloudFormationErrorMessageProvider method getStackResourceStatusReasons.
private String getStackResourceStatusReasons(AwsCredentialView credentialView, String region, String stackName, Set<String> resourceErrorStatuses, AmazonCloudFormationClient cfClient) {
DescribeStackResourcesRequest describeStackResourcesRequest = new DescribeStackResourcesRequest().withStackName(stackName);
DescribeStackResourcesResult describeStackResourcesResult = cfClient.describeStackResources(describeStackResourcesRequest);
String stackResourceStatusReasons = describeStackResourcesResult.getStackResources().stream().filter(stackResource -> resourceErrorStatuses.contains(stackResource.getResourceStatus())).map(stackResource -> getStackResourceMessage(credentialView, region, stackResource)).collect(Collectors.joining(", "));
return stackResourceStatusReasons;
}
use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsCloudFormationSetup method scalingPrerequisites.
@Override
public void scalingPrerequisites(AuthenticatedContext ac, CloudStack stack, boolean upscale) {
if (!upscale) {
return;
}
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AwsCredentialView awsCredential = new AwsCredentialView(ac.getCloudCredential());
AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(awsCredential, regionName);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(awsCredential, regionName);
List<Group> groups = stack.getGroups().stream().filter(g -> g.getInstances().stream().anyMatch(inst -> InstanceStatus.CREATE_REQUESTED == inst.getTemplate().getStatus())).collect(Collectors.toList());
Map<String, Group> groupMap = groups.stream().collect(Collectors.toMap(g -> cfStackUtil.getAutoscalingGroupName(ac, cloudFormationClient, g.getName()), g -> g));
DescribeAutoScalingGroupsResult result = amazonASClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(groupMap.keySet()));
for (AutoScalingGroup asg : result.getAutoScalingGroups()) {
Group group = groupMap.get(asg.getAutoScalingGroupName());
List<CloudInstance> groupInstances = group.getInstances().stream().filter(inst -> InstanceStatus.CREATED.equals(inst.getTemplate().getStatus())).collect(Collectors.toList());
List<CloudVmInstanceStatus> instanceStatuses = instanceConnector.check(ac, groupInstances);
if (checkInstanceStatuses(instanceStatuses, ac.getCloudCredential().getAccountId())) {
String errorMessage = "Not all the existing instances are in allowed state, upscale is not possible!";
LOGGER.info(errorMessage);
throw new CloudConnectorException(errorMessage);
}
List<Instance> asgOnlyInstances = asg.getInstances().stream().filter(inst -> groupInstances.stream().noneMatch(gi -> gi.getInstanceId().equals(inst.getInstanceId()))).collect(Collectors.toList());
List<CloudInstance> cbOnlyInstances = groupInstances.stream().filter(gi -> asg.getInstances().stream().noneMatch(inst -> inst.getInstanceId().equals(gi.getInstanceId()))).collect(Collectors.toList());
if (!asgOnlyInstances.isEmpty() || !cbOnlyInstances.isEmpty()) {
String errorMessage = "The instances in the autoscaling group are not in sync with the instances in cloudbreak! Cloudbreak only instances: [" + cbOnlyInstances.stream().map(CloudInstance::getInstanceId).collect(Collectors.joining(",")) + "], AWS only instances: [" + asgOnlyInstances.stream().map(Instance::getInstanceId).collect(Collectors.joining(",")) + "]. Upscale is not possible!";
LOGGER.info(errorMessage);
throw new CloudConnectorException(errorMessage);
}
if (groupInstances.size() != asg.getDesiredCapacity()) {
String errorMessage = String.format("The autoscale group's desired instance count is not match with the instance count in the cloudbreak." + " Desired count: %d <> cb instance count: %d. Upscale is not possible!", asg.getDesiredCapacity(), groupInstances.size());
LOGGER.info(errorMessage);
throw new CloudConnectorException(errorMessage);
}
}
}
Aggregations