Search in sources :

Example 11 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method buildTestWithInstanceProfileWithoutVPCAndIGWAndPublicIpOnLaunchAndRole.

@Test
public void buildTestWithInstanceProfileWithoutVPCAndIGWAndPublicIpOnLaunchAndRole() throws Exception {
    // GIVEN
    boolean existingVPC = false;
    boolean existingIGW = false;
    boolean mapPublicIpOnLaunch = false;
    boolean enableInstanceProfile = true;
    boolean instanceProfileAvailable = false;
    // WHEN
    modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(existingVPC).withExistingIGW(existingIGW).withExistingSubnetCidr(Lists.newArrayList(existingSubnetCidr)).mapPublicIpOnLaunch(mapPublicIpOnLaunch).withEnableInstanceProfile(enableInstanceProfile).withInstanceProfileAvailable(instanceProfileAvailable).withTemplate(awsCloudFormationTemplate);
    when(defaultCostTaggingService.prepareAllTagsForTemplate()).thenReturn(defaultTags);
    String templateString = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    assertThat(templateString, containsString("InstanceProfile"));
    assertThat(templateString, not(containsString("VPCId")));
    assertThat(templateString, not(containsString("SubnetCIDR")));
    assertThat(templateString, containsString("SubnetId"));
    assertThat(templateString, containsString("SubnetConfig"));
    assertThat(templateString, containsString("\"AttachGateway\""));
    assertThat(templateString, containsString("\"InternetGateway\""));
    assertThat(templateString, containsString("AvailabilitySet"));
    assertThat(templateString, not(containsString("EIP")));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 12 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext 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);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)

Example 13 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method buildTestWithVPCAndRoleWithoutIGWAndPublicIpOnLaunchAndInstanceProfile.

@Test
public void buildTestWithVPCAndRoleWithoutIGWAndPublicIpOnLaunchAndInstanceProfile() throws Exception {
    // GIVEN
    boolean existingVPC = true;
    boolean existingIGW = false;
    boolean mapPublicIpOnLaunch = false;
    boolean enableInstanceProfile = false;
    boolean instanceProfileAvailable = true;
    // WHEN
    modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(existingVPC).withExistingIGW(existingIGW).withExistingSubnetCidr(Lists.newArrayList(existingSubnetCidr)).mapPublicIpOnLaunch(mapPublicIpOnLaunch).withEnableInstanceProfile(enableInstanceProfile).withInstanceProfileAvailable(instanceProfileAvailable).withTemplate(awsCloudFormationTemplate);
    when(defaultCostTaggingService.prepareAllTagsForTemplate()).thenReturn(defaultTags);
    String templateString = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    assertThat(templateString, containsString("InstanceProfile"));
    assertThat(templateString, containsString("VPCId"));
    assertThat(templateString, not(containsString("SubnetCIDR")));
    assertThat(templateString, containsString("SubnetId"));
    assertThat(templateString, not(containsString("SubnetConfig")));
    assertThat(templateString, not(containsString("\"AttachGateway\"")));
    assertThat(templateString, not(containsString("\"InternetGateway\"")));
    assertThat(templateString, containsString("AvailabilitySet"));
    assertThat(templateString, not(containsString("EIP")));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 14 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method buildTestWithVPCAndIGWWithoutPublicIpOnLaunchAndInstanceProfileAndRole.

@Test
public void buildTestWithVPCAndIGWWithoutPublicIpOnLaunchAndInstanceProfileAndRole() throws Exception {
    // GIVEN
    boolean existingVPC = true;
    boolean existingIGW = true;
    boolean mapPublicIpOnLaunch = false;
    boolean enableInstanceProfile = false;
    boolean instanceProfileAvailable = false;
    // WHEN
    modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(existingVPC).withExistingIGW(existingIGW).withExistingSubnetCidr(Lists.newArrayList(existingSubnetCidr)).mapPublicIpOnLaunch(mapPublicIpOnLaunch).withEnableInstanceProfile(enableInstanceProfile).withInstanceProfileAvailable(instanceProfileAvailable).withTemplate(awsCloudFormationTemplate);
    when(defaultCostTaggingService.prepareAllTagsForTemplate()).thenReturn(defaultTags);
    String templateString = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    assertThat(templateString, not(containsString("InstanceProfile")));
    assertThat(templateString, containsString("VPCId"));
    assertThat(templateString, not(containsString("SubnetCIDR")));
    assertThat(templateString, containsString("SubnetId"));
    assertThat(templateString, not(containsString("SubnetConfig")));
    assertThat(templateString, not(containsString("\"AttachGateway\"")));
    assertThat(templateString, not(containsString("\"InternetGateway\"")));
    assertThat(templateString, containsString("AvailabilitySet"));
    assertThat(templateString, not(containsString("EIP")));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 15 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method buildTestWithInstanceProfileAndRoleWithoutVPCAndIGWAndPublicIpOnLaunch.

@Test
public void buildTestWithInstanceProfileAndRoleWithoutVPCAndIGWAndPublicIpOnLaunch() throws Exception {
    // GIVEN
    boolean existingVPC = false;
    boolean existingIGW = false;
    boolean mapPublicIpOnLaunch = false;
    boolean enableInstanceProfile = true;
    boolean instanceProfileAvailable = true;
    // WHEN
    modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(existingVPC).withExistingIGW(existingIGW).withExistingSubnetCidr(Lists.newArrayList(existingSubnetCidr)).mapPublicIpOnLaunch(mapPublicIpOnLaunch).withEnableInstanceProfile(enableInstanceProfile).withInstanceProfileAvailable(instanceProfileAvailable).withTemplate(awsCloudFormationTemplate);
    when(defaultCostTaggingService.prepareAllTagsForTemplate()).thenReturn(defaultTags);
    String templateString = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    assertThat(templateString, containsString("InstanceProfile"));
    assertThat(templateString, not(containsString("VPCId")));
    assertThat(templateString, not(containsString("SubnetCIDR")));
    assertThat(templateString, containsString("SubnetId"));
    assertThat(templateString, containsString("SubnetConfig"));
    assertThat(templateString, containsString("\"AttachGateway\""));
    assertThat(templateString, containsString("\"InternetGateway\""));
    assertThat(templateString, containsString("AvailabilitySet"));
    assertThat(templateString, not(containsString("EIP")));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

ModelContext (com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 Test (org.junit.Test)17 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)1 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)1 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)1 AwsInstanceProfileView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)1 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)1