use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsResourceConnector method upscale.
@Override
public List<CloudResourceStatus> upscale(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
resumeAutoScaling(ac, stack);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
List<Group> scaledGroups = getScaledGroups(stack);
for (Group group : scaledGroups) {
String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, cloudFormationClient, group.getName());
amazonASClient.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest().withAutoScalingGroupName(asGroupName).withMaxSize(group.getInstancesSize()).withDesiredCapacity(group.getInstancesSize()));
LOGGER.info("Updated Auto Scaling group's desiredCapacity: [stack: '{}', to: '{}']", ac.getCloudContext().getId(), resources.size());
}
scheduleStatusChecks(stack, ac, cloudFormationClient);
suspendAutoScaling(ac, stack);
boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(new AwsNetworkView(stack.getNetwork()), amazonEC2Client);
List<Group> gateways = getGatewayGroups(scaledGroups);
if (mapPublicIpOnLaunch && !gateways.isEmpty()) {
String cFStackName = getCloudFormationStackResource(resources).getName();
Map<String, String> eipAllocationIds = getElasticIpAllocationIds(cFStackName, cloudFormationClient);
for (Group gateway : gateways) {
List<String> eips = getEipsForGatewayGroup(eipAllocationIds, gateway);
List<String> freeEips = getFreeIps(eips, amazonEC2Client);
List<String> instanceIds = getInstancesForGroup(ac, amazonASClient, cloudFormationClient, gateway);
List<String> newInstances = instanceIds.stream().filter(iid -> gateway.getInstances().stream().noneMatch(inst -> iid.equals(inst.getInstanceId()))).collect(Collectors.toList());
associateElasticIpsToInstances(amazonEC2Client, freeEips, newInstances);
}
}
return singletonList(new CloudResourceStatus(getCloudFormationStackResource(resources), ResourceStatus.UPDATED));
}
use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsStackValidator method validate.
@Override
public void validate(AuthenticatedContext ac, CloudStack cloudStack) {
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
String cFStackName = cfStackUtil.getCfStackName(ac);
try {
cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName));
throw new CloudConnectorException(String.format("Stack is already exists with the given name: %s", cFStackName));
} catch (AmazonServiceException ignored) {
}
}
use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsDeleteVpcTest method deleteNetwork.
@AfterSuite
@Parameters({ "regionName", "vpcStackName" })
public void deleteNetwork(String regionName, @Optional("it-vpc-stack") String vpcStackName) {
AmazonCloudFormationClient client = new AmazonCloudFormationClient();
client.setRegion(RegionUtils.getRegion(regionName));
client.deleteStack(new DeleteStackRequest().withStackName(vpcStackName));
}
use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsResourceConnector method launch.
@Override
public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentType adjustmentType, Long threshold) throws Exception {
createKeyPair(ac, stack);
String cFStackName = cfStackUtil.getCfStackName(ac);
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
boolean existingVPC = awsNetworkView.isExistingVPC();
boolean existingSubnet = awsNetworkView.isExistingSubnet();
boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
try {
cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName));
LOGGER.info("Stack already exists: {}", cFStackName);
} catch (AmazonServiceException ignored) {
CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).build();
resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
String cidr = stack.getNetwork().getSubnet().getCidr();
String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? findNonOverLappingCIDR(ac, stack) : cidr;
AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
ModelContext modelContext = new ModelContext().withAuthenticatedContext(ac).withStack(stack).withExistingVpc(existingVPC).withSnapshotId(getEbsSnapshotIdIfNeeded(ac, stack)).withExistingIGW(awsNetworkView.isExistingIGW()).withExistingSubnetCidr(existingSubnet ? getExistingSubnetCidr(ac, stack) : null).withExistingSubnetIds(existingSubnet ? awsNetworkView.getSubnetList() : null).mapPublicIpOnLaunch(mapPublicIpOnLaunch).withEnableInstanceProfile(awsInstanceProfileView.isEnableInstanceProfileStrategy()).withInstanceProfileAvailable(awsInstanceProfileView.isInstanceProfileAvailable()).withTemplate(stack.getTemplate()).withDefaultSubnet(subnet);
String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
cfClient.createStack(createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate));
}
LOGGER.info("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
AmazonAutoScalingClient asClient = awsClient.createAutoScalingClient(credentialView, regionName);
PollTask<Boolean> task = awsPollTaskFactory.newAwsCreateStackStatusCheckerTask(ac, cfClient, asClient, CREATE_COMPLETE, CREATE_FAILED, ERROR_STATUSES, cFStackName);
try {
Boolean statePollerResult = task.call();
if (!task.completed(statePollerResult)) {
syncPollingScheduler.schedule(task);
}
} catch (RuntimeException e) {
throw new CloudConnectorException(e.getMessage(), e);
}
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
saveS3AccessRoleArn(ac, stack, cFStackName, cfClient, resourceNotifier);
saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier);
List<CloudResource> cloudResources = getCloudResources(ac, stack, cFStackName, cfClient, amazonEC2Client, amazonASClient, mapPublicIpOnLaunch);
return check(ac, cloudResources);
}
use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsResourceConnector method getCloudResources.
private List<CloudResource> getCloudResources(AuthenticatedContext ac, CloudStack stack, String cFStackName, AmazonCloudFormation client, AmazonEC2 amazonEC2Client, AmazonAutoScaling amazonASClient, boolean mapPublicIpOnLaunch) {
List<CloudResource> cloudResources = new ArrayList<>();
AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
scheduleStatusChecks(stack, ac, cloudFormationClient);
suspendAutoScaling(ac, stack);
if (mapPublicIpOnLaunch) {
Map<String, String> eipAllocationIds = getElasticIpAllocationIds(cFStackName, client);
List<Group> gateways = getGatewayGroups(stack.getGroups());
for (Group gateway : gateways) {
List<String> eips = getEipsForGatewayGroup(eipAllocationIds, gateway);
List<String> instanceIds = getInstancesForGroup(ac, amazonASClient, client, gateway);
associateElasticIpsToInstances(amazonEC2Client, eips, instanceIds);
}
}
return cloudResources;
}
Aggregations