use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsResourceConnector method terminate.
@Override
public List<CloudResourceStatus> terminate(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
LOGGER.info("Deleting stack: {}", ac.getCloudContext().getId());
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
if (resources != null && !resources.isEmpty()) {
AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
CloudResource stackResource = getCloudFormationStackResource(resources);
if (stackResource == null) {
return Collections.emptyList();
}
String cFStackName = stackResource.getName();
LOGGER.info("Deleting CloudFormation stack for stack: {} [cf stack id: {}]", cFStackName, ac.getCloudContext().getId());
DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
try {
retryService.testWith2SecDelayMax5Times(() -> {
try {
cfClient.describeStacks(describeStacksRequest);
} catch (AmazonServiceException e) {
if (!e.getErrorMessage().contains(cFStackName + " does not exist")) {
throw e;
}
throw new ActionWentFailException("Stack not exists");
}
return Boolean.TRUE;
});
} catch (ActionWentFailException ignored) {
LOGGER.info(String.format("Stack not found with name: %s", cFStackName));
AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
releaseReservedIp(amazonEC2Client, resources);
return Collections.emptyList();
}
resumeAutoScalingPolicies(ac, stack);
DeleteStackRequest deleteStackRequest = new DeleteStackRequest().withStackName(cFStackName);
cfClient.deleteStack(deleteStackRequest);
PollTask<Boolean> task = awsPollTaskFactory.newAwsTerminateStackStatusCheckerTask(ac, cfClient, DELETE_COMPLETE, DELETE_FAILED, ERROR_STATUSES, cFStackName);
try {
Boolean statePollerResult = task.call();
if (!task.completed(statePollerResult)) {
syncPollingScheduler.schedule(task);
}
} catch (Exception e) {
throw new CloudConnectorException(e.getMessage(), e);
}
AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
releaseReservedIp(amazonEC2Client, resources);
deleteKeyPair(ac, stack);
} else if (resources != null) {
AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
releaseReservedIp(amazonEC2Client, resources);
LOGGER.info("No CloudFormation stack saved for stack.");
} else {
LOGGER.info("No resources to release.");
}
return check(ac, resources);
}
use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsClient method createCloudFormationClient.
public AmazonCloudFormationClient createCloudFormationClient(AwsCredentialView awsCredential, String regionName) {
AmazonCloudFormationClient client = isRoleAssumeRequired(awsCredential) ? new AmazonCloudFormationClient(credentialClient.retrieveCachedSessionCredentials(awsCredential)) : new AmazonCloudFormationClient(createAwsCredentials(awsCredential));
client.setRegion(RegionUtils.getRegion(regionName));
return client;
}
use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsMetadataCollector method collect.
@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
List<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>();
try {
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonCloudFormationClient amazonCFClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), region);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), region);
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region);
// contains all instances
ListMultimap<String, CloudInstance> groupByInstanceGroup = groupByInstanceGroup(vms);
for (String key : groupByInstanceGroup.keySet()) {
List<CloudInstance> cloudInstances = groupByInstanceGroup.get(key);
cloudVmMetaDataStatuses.addAll(collectGroupMetaData(ac, amazonASClient, amazonEC2Client, amazonCFClient, key, cloudInstances));
}
return cloudVmMetaDataStatuses;
} catch (RuntimeException e) {
throw new CloudConnectorException(e.getMessage(), e);
}
}
use of com.amazonaws.services.cloudformation.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsCreateVpcNetworkTest method createNetwork.
@Test
@Parameters({ "networkName", "description", "publicInAccount", "regionName", "vpcStackName", "vpcName", "existingSubnet" })
public void createNetwork(String networkName, @Optional("") String description, @Optional("false") boolean publicInAccount, String regionName, @Optional("it-vpc-stack") String vpcStackName, @Optional("it-vpc") String vpcName, boolean existingSubnet) {
AmazonCloudFormationClient client = new AmazonCloudFormationClient();
client.setRegion(RegionUtils.getRegion(regionName));
Map<String, Object> networkMap = new HashMap<>();
String vpcCreationJson = existingSubnet ? "public_vpc_with_subnet.json" : "public_vpc_wihout_subnet.json";
try (InputStream vpcJsonInputStream = getClass().getResourceAsStream("/cloudformation/" + vpcCreationJson)) {
String vpcCFTemplateString = IOUtils.toString(vpcJsonInputStream);
CreateStackRequest stackRequest = createStackRequest(vpcStackName, vpcName, vpcCFTemplateString);
client.createStack(stackRequest);
List<Output> outputForRequest = getOutputForRequest(vpcStackName, client);
if (existingSubnet) {
networkMap.put("vpcId", outputForRequest.get(0).getOutputValue());
networkMap.put("subnetId", outputForRequest.get(1).getOutputValue());
} else {
networkMap.put("vpcId", outputForRequest.get(1).getOutputValue());
networkMap.put("internetGatewayId", outputForRequest.get(0).getOutputValue());
}
} catch (IOException e) {
LOGGER.error("can't read vpc cloudformation template file");
throw new RuntimeException(e);
}
NetworkRequest networkRequest = new NetworkRequest();
networkRequest.setName(networkName);
networkRequest.setDescription(description);
networkRequest.setParameters(networkMap);
if (!existingSubnet) {
networkRequest.setSubnetCIDR("10.0.0.0/24");
}
networkRequest.setCloudPlatform("AWS");
String id = getCloudbreakClient().networkEndpoint().postPrivate(networkRequest).getId().toString();
getItContext().putContextParam(CloudbreakITContextConstants.NETWORK_ID, id, true);
}
Aggregations