Search in sources :

Example 1 with CreateLaunchTemplateVersionResult

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;
}
Also used : ModifyLaunchTemplateRequest(com.amazonaws.services.ec2.model.ModifyLaunchTemplateRequest) ModifyLaunchTemplateResult(com.amazonaws.services.ec2.model.ModifyLaunchTemplateResult)

Example 2 with CreateLaunchTemplateVersionResult

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;
}
Also used : CreateLaunchTemplateVersionResult(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult) RequestLaunchTemplateData(com.amazonaws.services.ec2.model.RequestLaunchTemplateData) CreateLaunchTemplateVersionRequest(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionRequest)

Example 3 with 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);
        }
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) LaunchTemplateSpecification(com.amazonaws.services.autoscaling.model.LaunchTemplateSpecification) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) CreateLaunchTemplateVersionResult(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) Map(java.util.Map)

Example 4 with 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());
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) UpdateAutoScalingGroupResult(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupResult) GetTemplateResult(com.amazonaws.services.cloudformation.model.GetTemplateResult) CreateLaunchTemplateVersionResult(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult) LaunchTemplateVersion(com.amazonaws.services.ec2.model.LaunchTemplateVersion) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) CreateLaunchTemplateVersionRequest(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ModifyLaunchTemplateRequest(com.amazonaws.services.ec2.model.ModifyLaunchTemplateRequest) ModifyLaunchTemplateResult(com.amazonaws.services.ec2.model.ModifyLaunchTemplateResult) Test(org.junit.Test)

Example 5 with CreateLaunchTemplateVersionResult

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());
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) GetTemplateResult(com.amazonaws.services.cloudformation.model.GetTemplateResult) CreateLaunchTemplateVersionResult(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult) LaunchTemplateVersion(com.amazonaws.services.ec2.model.LaunchTemplateVersion) CreateLaunchTemplateVersionRequest(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.Test)

Aggregations

CreateLaunchTemplateVersionResult (com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult)6 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)5 CreateLaunchTemplateVersionRequest (com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionRequest)5 GetTemplateResult (com.amazonaws.services.cloudformation.model.GetTemplateResult)4 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)4 Test (org.junit.Test)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 LaunchTemplateVersion (com.amazonaws.services.ec2.model.LaunchTemplateVersion)3 ModifyLaunchTemplateRequest (com.amazonaws.services.ec2.model.ModifyLaunchTemplateRequest)2 ModifyLaunchTemplateResult (com.amazonaws.services.ec2.model.ModifyLaunchTemplateResult)2 LaunchTemplateSpecification (com.amazonaws.services.autoscaling.model.LaunchTemplateSpecification)1 UpdateAutoScalingGroupRequest (com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest)1 UpdateAutoScalingGroupResult (com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupResult)1 RequestLaunchTemplateData (com.amazonaws.services.ec2.model.RequestLaunchTemplateData)1 ValidationError (com.amazonaws.services.ec2.model.ValidationError)1 ValidationWarning (com.amazonaws.services.ec2.model.ValidationWarning)1 AmazonAutoScalingClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient)1 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)1 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)1