Search in sources :

Example 1 with GetTemplateResult

use of com.amazonaws.services.cloudformation.model.GetTemplateResult in project cloudbreak by hortonworks.

the class AwsLaunchConfigurationImageUpdateServiceTest method shouldUpdateImage.

@Test
public void shouldUpdateImage() {
    String lcName = "lcName";
    CloudResource cfResource = CloudResource.builder().type(ResourceType.CLOUDFORMATION_STACK).name("cf").build();
    String autoScalingGroupName = "autoScalingGroupName";
    Map<AutoScalingGroup, String> scalingGroupStringMap = Collections.singletonMap(new AutoScalingGroup().withLaunchConfigurationName(lcName).withAutoScalingGroupName(autoScalingGroupName), autoScalingGroupName);
    when(cloudFormationClient.getTemplate(any())).thenReturn(new GetTemplateResult().withTemplateBody("AWS::AutoScaling::LaunchConfiguration"));
    when(autoScalingGroupHandler.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource)).thenReturn(scalingGroupStringMap);
    List<LaunchConfiguration> oldLaunchConfigs = Collections.singletonList(new LaunchConfiguration().withLaunchConfigurationName(lcName));
    when(launchConfigurationHandler.getLaunchConfigurations(autoScalingClient, scalingGroupStringMap.keySet())).thenReturn(oldLaunchConfigs);
    String newLCName = "newLCName";
    when(launchConfigurationHandler.createNewLaunchConfiguration(eq("imageName"), eq(autoScalingClient), eq(oldLaunchConfigs.get(0)), eq(ac.getCloudContext()))).thenReturn(newLCName);
    underTest.updateImage(ac, stack, cfResource);
    verify(autoScalingGroupHandler, times(1)).getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource);
    verify(launchConfigurationHandler, times(1)).getLaunchConfigurations(autoScalingClient, scalingGroupStringMap.keySet());
    verify(launchConfigurationHandler, times(1)).createNewLaunchConfiguration(anyString(), eq(autoScalingClient), eq(oldLaunchConfigs.get(0)), eq(ac.getCloudContext()));
    verify(autoScalingGroupHandler, times(1)).updateAutoScalingGroupWithLaunchConfiguration(autoScalingClient, autoScalingGroupName, oldLaunchConfigs.get(0), newLCName);
    verify(launchConfigurationHandler, times(1)).removeOldLaunchConfiguration(oldLaunchConfigs.get(0), autoScalingClient, ac.getCloudContext());
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) LaunchConfiguration(com.amazonaws.services.autoscaling.model.LaunchConfiguration) GetTemplateResult(com.amazonaws.services.cloudformation.model.GetTemplateResult) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.Test)

Example 2 with GetTemplateResult

use of com.amazonaws.services.cloudformation.model.GetTemplateResult 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 3 with GetTemplateResult

use of com.amazonaws.services.cloudformation.model.GetTemplateResult 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)

Example 4 with GetTemplateResult

use of com.amazonaws.services.cloudformation.model.GetTemplateResult in project cloudbreak by hortonworks.

the class AwsLaunchTemplateUpdateServiceTest method testUpdateImageWithWrongTemplateParams.

@Test
public void testUpdateImageWithWrongTemplateParams() 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().withWarning(new ValidationWarning().withErrors(new ValidationError().withCode("1").withMessage("error"))));
    // WHEN and THEN exception
    Assert.assertThrows(CloudConnectorException.class, () -> underTest.updateFields(ac, cfResource.getName(), Map.of(LaunchTemplateField.IMAGE_ID, stack.getImage().getImageName())));
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) GetTemplateResult(com.amazonaws.services.cloudformation.model.GetTemplateResult) CreateLaunchTemplateVersionResult(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult) CreateLaunchTemplateVersionRequest(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ValidationWarning(com.amazonaws.services.ec2.model.ValidationWarning) ValidationError(com.amazonaws.services.ec2.model.ValidationError) Test(org.junit.Test)

Example 5 with GetTemplateResult

use of com.amazonaws.services.cloudformation.model.GetTemplateResult in project cloudbreak by hortonworks.

the class AwsLaunchTemplateUpdateServiceTest method testUpdateFieldsUpdatesWithMissingParams.

@Test
public void testUpdateFieldsUpdatesWithMissingParams() 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);
    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(DESCRIPTION, request.getVersionDescription());
    Assertions.assertNull(request.getLaunchTemplateData().getUserData());
}
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

AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)5 GetTemplateResult (com.amazonaws.services.cloudformation.model.GetTemplateResult)5 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)5 Test (org.junit.Test)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 CreateLaunchTemplateVersionRequest (com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionRequest)4 CreateLaunchTemplateVersionResult (com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult)4 LaunchTemplateVersion (com.amazonaws.services.ec2.model.LaunchTemplateVersion)3 LaunchConfiguration (com.amazonaws.services.autoscaling.model.LaunchConfiguration)1 UpdateAutoScalingGroupRequest (com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest)1 UpdateAutoScalingGroupResult (com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupResult)1 ModifyLaunchTemplateRequest (com.amazonaws.services.ec2.model.ModifyLaunchTemplateRequest)1 ModifyLaunchTemplateResult (com.amazonaws.services.ec2.model.ModifyLaunchTemplateResult)1 ValidationError (com.amazonaws.services.ec2.model.ValidationError)1 ValidationWarning (com.amazonaws.services.ec2.model.ValidationWarning)1