use of com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult in project cloudbreak by hortonworks.
the class AwsLaunchTemplateUpdateService method modifyLaunchTemplate.
private ModifyLaunchTemplateResult modifyLaunchTemplate(AmazonEc2Client ec2Client, LaunchTemplateSpecification launchTemplateSpecification, CreateLaunchTemplateVersionResult createLaunchTemplateVersionResult, boolean setLaunchtemplateVersionToDefault) {
ModifyLaunchTemplateRequest modifyLaunchTemplateRequest = new ModifyLaunchTemplateRequest().withLaunchTemplateId(launchTemplateSpecification.getLaunchTemplateId());
if (setLaunchtemplateVersionToDefault) {
modifyLaunchTemplateRequest.withDefaultVersion(createLaunchTemplateVersionResult.getLaunchTemplateVersion().getVersionNumber().toString());
} else {
LOGGER.debug("In case of dryrun the default version of the used launch template remain the same: {}", launchTemplateSpecification.getVersion());
modifyLaunchTemplateRequest.withDefaultVersion(launchTemplateSpecification.getVersion());
}
ModifyLaunchTemplateResult modifyLaunchTemplateResult = ec2Client.modifyLaunchTemplate(modifyLaunchTemplateRequest);
LOGGER.debug("Modified launch template: {}", modifyLaunchTemplateResult);
return modifyLaunchTemplateResult;
}
use of com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult in project cloudbreak by hortonworks.
the class AwsLaunchTemplateUpdateService method getCreateLaunchTemplateVersionRequest.
private CreateLaunchTemplateVersionResult getCreateLaunchTemplateVersionRequest(AmazonEc2Client ec2Client, Map<LaunchTemplateField, String> updatableFields, LaunchTemplateSpecification launchTemplateSpecification) {
CreateLaunchTemplateVersionRequest createLaunchTemplateVersionRequest = new CreateLaunchTemplateVersionRequest().withLaunchTemplateId(launchTemplateSpecification.getLaunchTemplateId()).withSourceVersion(launchTemplateSpecification.getVersion()).withVersionDescription(updatableFields.getOrDefault(LaunchTemplateField.DESCRIPTION, null)).withLaunchTemplateData(new RequestLaunchTemplateData().withImageId(updatableFields.getOrDefault(LaunchTemplateField.IMAGE_ID, null)).withUserData(updatableFields.getOrDefault(LaunchTemplateField.USER_DATA, null)));
CreateLaunchTemplateVersionResult createLaunchTemplateVersionResult = ec2Client.createLaunchTemplateVersion(createLaunchTemplateVersionRequest);
validateCreatedLaunchTemplateVersionResult(createLaunchTemplateVersionResult);
LOGGER.debug("Updated field in new launch template version: {}", updatableFields);
return createLaunchTemplateVersionResult;
}
use of com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult in project cloudbreak by hortonworks.
the class AwsLaunchTemplateUpdateService method updateFields.
public void updateFields(AuthenticatedContext authenticatedContext, String stackName, Map<LaunchTemplateField, String> updatableFields, boolean dryRun) {
AwsCredentialView credentialView = new AwsCredentialView(authenticatedContext.getCloudCredential());
String regionName = authenticatedContext.getCloudContext().getLocation().getRegion().getRegionName();
AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName);
AmazonAutoScalingClient autoScalingClient = awsClient.createAutoScalingClient(credentialView, regionName);
AmazonEc2Client ec2Client = awsClient.createEc2Client(credentialView, regionName);
Map<AutoScalingGroup, String> autoScalingGroups = autoScalingGroupHandler.getAutoScalingGroups(cloudFormationClient, autoScalingClient, stackName);
autoScalingGroups = filterGroupsForDryRun(autoScalingGroups, dryRun);
LOGGER.debug("Modifying the {} fields for the [{}] autoscaling groups' launchtemplates [dryrun: {}]", updatableFields, autoScalingGroups.values(), dryRun);
for (Map.Entry<AutoScalingGroup, String> asgEntry : autoScalingGroups.entrySet()) {
AutoScalingGroup autoScalingGroup = asgEntry.getKey();
LOGGER.debug("Creating new launchtemplate version for [{}] autoscale group...", autoScalingGroup.getAutoScalingGroupName());
LaunchTemplateSpecification launchTemplateSpecification = getLaunchTemplateSpecification(autoScalingGroup);
CreateLaunchTemplateVersionResult createLaunchTemplateVersionResult = getCreateLaunchTemplateVersionRequest(ec2Client, updatableFields, launchTemplateSpecification);
modifyLaunchTemplate(ec2Client, launchTemplateSpecification, createLaunchTemplateVersionResult, !dryRun);
if (dryRun) {
LOGGER.debug("Autoscale group update will be skipped because of dryrun, which just test the permissions for modifiying the launch template.");
} else {
updateAutoScalingGroup(updatableFields, autoScalingClient, autoScalingGroup, launchTemplateSpecification, createLaunchTemplateVersionResult);
}
}
}
use of com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult in project cloudbreak by hortonworks.
the class AwsLaunchTemplateUpdateServiceTest method shouldUpdateImage.
@Test
public void shouldUpdateImage() throws IOException {
// GIVEN
String cfStackName = "cf";
CloudResource cfResource = CloudResource.builder().type(ResourceType.CLOUDFORMATION_STACK).name(cfStackName).build();
String template = FileReaderUtils.readFileFromClasspath("json/aws-cf-template.json");
String cfTemplateBody = JsonUtil.minify(String.format(template, "{\"Ref\":\"AMI\"}"));
when(cloudFormationClient.getTemplate(any())).thenReturn(new GetTemplateResult().withTemplateBody(cfTemplateBody));
Map<AutoScalingGroup, String> autoScalingGroupsResult = createAutoScalingGroupHandler();
when(autoScalingGroupHandler.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource.getName())).thenReturn(autoScalingGroupsResult);
when(ec2Client.createLaunchTemplateVersion(any(CreateLaunchTemplateVersionRequest.class))).thenReturn(new CreateLaunchTemplateVersionResult().withLaunchTemplateVersion(new LaunchTemplateVersion().withVersionNumber(1L)));
when(ec2Client.modifyLaunchTemplate(any(ModifyLaunchTemplateRequest.class))).thenReturn(new ModifyLaunchTemplateResult());
when(autoScalingClient.updateAutoScalingGroup(any(UpdateAutoScalingGroupRequest.class))).thenReturn(new UpdateAutoScalingGroupResult());
// WHEN
underTest.updateFields(ac, cfResource.getName(), Map.of(LaunchTemplateField.IMAGE_ID, stack.getImage().getImageName()));
// THEN
Mockito.verify(ec2Client).createLaunchTemplateVersion(argumentCaptor.capture());
CreateLaunchTemplateVersionRequest request = argumentCaptor.getValue();
Assertions.assertEquals(stack.getImage().getImageName(), request.getLaunchTemplateData().getImageId());
}
use of com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult in project cloudbreak by hortonworks.
the class AwsLaunchTemplateUpdateServiceTest method testUpdateFieldsUpdatesTheAppropriateParams.
@Test
public void testUpdateFieldsUpdatesTheAppropriateParams() throws IOException {
// GIVEN
String cfStackName = "cf";
CloudResource cfResource = CloudResource.builder().type(ResourceType.CLOUDFORMATION_STACK).name(cfStackName).build();
String template = FileReaderUtils.readFileFromClasspath("json/aws-cf-template.json");
String cfTemplateBody = JsonUtil.minify(String.format(template, "{\"Ref\":\"AMI\"}"));
when(cloudFormationClient.getTemplate(any())).thenReturn(new GetTemplateResult().withTemplateBody(cfTemplateBody));
Map<AutoScalingGroup, String> autoScalingGroupsResult = createAutoScalingGroupHandler();
when(autoScalingGroupHandler.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource.getName())).thenReturn(autoScalingGroupsResult);
Map<LaunchTemplateField, String> updatableFieldMap = Map.of(LaunchTemplateField.IMAGE_ID, stack.getImage().getImageName(), LaunchTemplateField.DESCRIPTION, DESCRIPTION, LaunchTemplateField.USER_DATA, USER_DATA);
when(ec2Client.createLaunchTemplateVersion(any(CreateLaunchTemplateVersionRequest.class))).thenReturn(new CreateLaunchTemplateVersionResult().withLaunchTemplateVersion(new LaunchTemplateVersion().withVersionNumber(1L)));
// WHEN
underTest.updateFields(ac, cfResource.getName(), updatableFieldMap);
// THEN
Mockito.verify(ec2Client).createLaunchTemplateVersion(argumentCaptor.capture());
CreateLaunchTemplateVersionRequest request = argumentCaptor.getValue();
Assertions.assertEquals(stack.getImage().getImageName(), request.getLaunchTemplateData().getImageId());
Assertions.assertEquals(USER_DATA, request.getLaunchTemplateData().getUserData());
Assertions.assertEquals(DESCRIPTION, request.getVersionDescription());
}
Aggregations