Search in sources :

Example 1 with RDSModelContext

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

the class AwsRdsLaunchService method launch.

public List<CloudResourceStatus> launch(AuthenticatedContext ac, DatabaseStack stack, PersistenceNotifier resourceNotifier) {
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
    DatabaseServer databaseServer = stack.getDatabaseServer();
    boolean useSslEnforcement = databaseServer.isUseSslEnforcement();
    try {
        cfClient.describeStacks(describeStacksRequest);
        LOGGER.debug("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException exception) {
        // all subnets desired for DB subnet group are in the stack
        boolean existingSubnet = awsNetworkView.isExistingSubnet();
        LOGGER.warn("API call failed with this error:", exception);
        if (!existingSubnet) {
            throw new CloudConnectorException("Can only create RDS instance with existing subnets", exception);
        }
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).availabilityZone(ac.getCloudContext().getLocation().getAvailabilityZone().value()).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
        RDSModelContext rdsModelContext = new RDSModelContext().withTemplate(stack.getTemplate()).withNetworkCidrs(awsNetworkView.getExistingVpcCidrs()).withHasPort(databaseServer.getPort() != null).withUseSslEnforcement(useSslEnforcement).withSslCertificateIdentifierDefined(new AwsRdsInstanceView(databaseServer).isSslCertificateIdentifierDefined()).withHasSecurityGroup(!databaseServer.getSecurity().getCloudSecurityIds().isEmpty()).withIsKmsCustom(stack.getDatabaseServer().getParameters().containsKey("key")).withGetKmsKey(getKmsKey(stack));
        String cfTemplate = cloudFormationTemplateBuilder.build(rdsModelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, cfTemplate));
    }
    LOGGER.debug("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
    Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
    StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
    run(creationWaiter, describeStacksRequest, stackCancellationCheck, String.format("RDS CloudFormation stack %s creation failed", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, CREATE_FAILED));
    List<CloudResource> databaseResources = getCreatedOutputs(ac, stack, cFStackName, cfClient, resourceNotifier, useSslEnforcement);
    databaseResources.forEach(dbr -> resourceNotifier.notifyAllocation(dbr, ac.getCloudContext()));
    // FIXME: For now, just return everything wrapped in a status object
    return databaseResources.stream().map(resource -> new CloudResourceStatus(resource, ResourceStatus.CREATED)).collect(Collectors.toList());
// FIXME check does nothing?!
// return awsResourceConnector.check(ac, databaseResources);
}
Also used : AwsRdsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsInstanceView) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) AwsCloudFormationErrorMessageProvider(com.sequenceiq.cloudbreak.cloud.aws.util.AwsCloudFormationErrorMessageProvider) CloudFormationTemplateBuilder(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder) LoggerFactory(org.slf4j.LoggerFactory) CREATE_FAILED(com.amazonaws.services.cloudformation.model.ResourceStatus.CREATE_FAILED) ArrayList(java.util.ArrayList) RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) Service(org.springframework.stereotype.Service) Map(java.util.Map) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) Waiter(com.amazonaws.waiters.Waiter) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) AwsStackRequestHelper(com.sequenceiq.cloudbreak.cloud.aws.AwsStackRequestHelper) AmazonServiceException(com.amazonaws.AmazonServiceException) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Collectors(java.util.stream.Collectors) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) List(java.util.List) WaiterRunner.run(com.sequenceiq.cloudbreak.cloud.aws.scheduler.WaiterRunner.run) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) CloudFormationTemplateBuilder(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) AmazonServiceException(com.amazonaws.AmazonServiceException) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsRdsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsInstanceView)

Example 2 with RDSModelContext

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

the class CloudFormationTemplateBuilderDBTest method buildTestWhenKmsKeyIdPresent.

@ParameterizedTest(name = "{0}")
@MethodSource("templatesPathDataProvider")
void buildTestWhenKmsKeyIdPresent(String templatePath) throws IOException {
    // GIVEN
    String awsCloudFormationTemplate = factoryBean.getObject().getTemplate(templatePath, "UTF-8").toString();
    // WHEN
    RDSModelContext modelContext = new RDSModelContext().withIsKmsCustom(true).withGetKmsKey("dummyKeyArn").withTemplate(awsCloudFormationTemplate);
    String result = cloudFormationTemplateBuilder.build(modelContext);
    assertThat(JsonUtil.isValid(result)).overridingErrorMessage("Invalid JSON: " + result).isTrue();
    assertThat(result).contains("\"StorageEncrypted\": true");
    assertThat(result).contains("\"KmsKeyId\" : \"dummyKeyArn\"");
}
Also used : RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with RDSModelContext

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

the class CloudFormationTemplateBuilderDBTest method buildTestWhenNoSecurityGroupAndHavingPortAndMultipleCidr.

@ParameterizedTest(name = "{0}")
@MethodSource("templatesPathDataProvider")
void buildTestWhenNoSecurityGroupAndHavingPortAndMultipleCidr(String templatePath) throws IOException {
    // GIVEN
    String awsCloudFormationTemplate = factoryBean.getObject().getTemplate(templatePath, "UTF-8").toString();
    // WHEN
    RDSModelContext modelContext = new RDSModelContext().withHasPort(true).withNetworkCidrs(List.of(CIDR_1, CIDR_2)).withTemplate(awsCloudFormationTemplate);
    String result = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    assertThat(JsonUtil.isValid(result)).overridingErrorMessage("Invalid JSON: " + result).isTrue();
    assertThat(result).contains("\"PortParameter\": {");
    assertThat(result).doesNotContain("\"DBParameterGroupNameParameter\": {");
    assertThat(result).doesNotContain("\"DBParameterGroupFamilyParameter\": {");
    assertThat(result).doesNotContain("\"VPCSecurityGroupsParameter\": {");
    assertThat(result).contains("\"DBSecurityGroupNameParameter\": {");
    assertThat(result).contains("\"VPCIdParameter\": {");
    assertThat(result).contains("\"VPCSecurityGroup\": {");
    assertThat(result).doesNotContain("\"FromPort\": 5432,");
    assertThat(result).doesNotContain("\"ToPort\" : 5432,");
    assertThat(result).contains("\"FromPort\": { \"Ref\": \"PortParameter\" },");
    assertThat(result).contains("\"ToPort\" : { \"Ref\": \"PortParameter\" },");
    assertThat(result).contains(String.format("\"CidrIp\" : \"%s\"", CIDR_1));
    assertThat(result).contains(String.format("\"CidrIp\" : \"%s\"", CIDR_2));
    assertThat(result).doesNotContain("\"DBParameterGroup\": {");
    assertThat(result).doesNotContain("\"DBParameterGroupName\": { \"Ref\": \"DBParameterGroup\" },");
    assertThat(result).contains("\"Port\": { \"Ref\": \"PortParameter\" },");
    assertThat(result).doesNotContain("\"VPCSecurityGroups\": { \"Ref\": \"VPCSecurityGroupsParameter\" }");
    assertThat(result).contains("\"VPCSecurityGroups\": [{ \"Ref\": \"VPCSecurityGroup\" }]");
    assertThat(result).doesNotContain("\"CreatedDBParameterGroup\": { \"Value\": { \"Ref\": \"DBParameterGroup\" } },");
}
Also used : RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with RDSModelContext

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

the class CloudFormationTemplateBuilderDBTest method buildTestWhenHavingSecurityGroupAndNoPortAndNoSslEnforcementAndWithSslCertificateIdentifier.

@ParameterizedTest(name = "{0}")
@MethodSource("templatesPathDataProvider")
void buildTestWhenHavingSecurityGroupAndNoPortAndNoSslEnforcementAndWithSslCertificateIdentifier(String templatePath) throws IOException {
    // GIVEN
    String awsCloudFormationTemplate = factoryBean.getObject().getTemplate(templatePath, "UTF-8").toString();
    // WHEN
    RDSModelContext modelContext = new RDSModelContext().withHasSecurityGroup(true).withUseSslEnforcement(false).withSslCertificateIdentifierDefined(true).withTemplate(awsCloudFormationTemplate);
    String result = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    assertThat(JsonUtil.isValid(result)).overridingErrorMessage("Invalid JSON: " + result).isTrue();
    assertThat(result).doesNotContain("\"PortParameter\": {");
    assertThat(result).doesNotContain("\"DBParameterGroupNameParameter\": {");
    assertThat(result).doesNotContain("\"DBParameterGroupFamilyParameter\": {");
    assertThat(result).doesNotContain("\"SslCertificateIdentifierParameter\": {");
    assertThat(result).contains("\"VPCSecurityGroupsParameter\": {");
    assertThat(result).doesNotContain("\"DBSecurityGroupNameParameter\": {");
    assertThat(result).doesNotContain("\"VPCIdParameter\": {");
    assertThat(result).doesNotContain("\"VPCSecurityGroup\": {");
    assertThat(result).doesNotContain("\"FromPort\"");
    assertThat(result).doesNotContain("\"ToPort\"");
    assertThat(result).doesNotContain("\"CidrIp\" :");
    assertThat(result).doesNotContain("\"DBParameterGroup\": {");
    assertThat(result).doesNotContain("\"DBParameterGroupName\": { \"Ref\": \"DBParameterGroup\" },");
    assertThat(result).doesNotContain("\"CACertificateIdentifier\": { \"Ref\": \"SslCertificateIdentifierParameter\" },");
    assertThat(result).doesNotContain("\"Port\": { \"Ref\": \"PortParameter\" },");
    assertThat(result).contains("\"VPCSecurityGroups\": { \"Ref\": \"VPCSecurityGroupsParameter\" }");
    assertThat(result).doesNotContain("\"VPCSecurityGroups\": [{ \"Ref\": \"VPCSecurityGroup\" }]");
    assertThat(result).doesNotContain("\"CreatedDBParameterGroup\": { \"Value\": { \"Ref\": \"DBParameterGroup\" } },");
}
Also used : RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with RDSModelContext

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

the class CloudFormationTemplateBuilderDBTest method buildTestWhenNoSecurityGroupAndNoPortAndSingleCidr.

@ParameterizedTest(name = "{0}")
@MethodSource("templatesPathDataProvider")
void buildTestWhenNoSecurityGroupAndNoPortAndSingleCidr(String templatePath) throws IOException {
    // GIVEN
    String awsCloudFormationTemplate = factoryBean.getObject().getTemplate(templatePath, "UTF-8").toString();
    // WHEN
    RDSModelContext modelContext = new RDSModelContext().withNetworkCidrs(List.of(CIDR_1)).withTemplate(awsCloudFormationTemplate);
    String result = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    assertThat(JsonUtil.isValid(result)).overridingErrorMessage("Invalid JSON: " + result).isTrue();
    assertThat(result).doesNotContain("\"PortParameter\": {");
    assertThat(result).doesNotContain("\"DBParameterGroupNameParameter\": {");
    assertThat(result).doesNotContain("\"DBParameterGroupFamilyParameter\": {");
    assertThat(result).doesNotContain("\"VPCSecurityGroupsParameter\": {");
    assertThat(result).contains("\"DBSecurityGroupNameParameter\": {");
    assertThat(result).contains("\"VPCIdParameter\": {");
    assertThat(result).contains("\"VPCSecurityGroup\": {");
    assertThat(result).contains("\"FromPort\": 5432,");
    assertThat(result).contains("\"ToPort\" : 5432,");
    assertThat(result).doesNotContain("\"FromPort\": { \"Ref\": \"PortParameter\" },");
    assertThat(result).doesNotContain("\"ToPort\" : { \"Ref\": \"PortParameter\" },");
    assertThat(result).contains(String.format("\"CidrIp\" : \"%s\"", CIDR_1));
    assertThat(result).doesNotContain("\"DBParameterGroup\": {");
    assertThat(result).doesNotContain("\"DBParameterGroupName\": { \"Ref\": \"DBParameterGroup\" },");
    assertThat(result).doesNotContain("\"Port\": { \"Ref\": \"PortParameter\" },");
    assertThat(result).doesNotContain("\"VPCSecurityGroups\": { \"Ref\": \"VPCSecurityGroupsParameter\" }");
    assertThat(result).contains("\"VPCSecurityGroups\": [{ \"Ref\": \"VPCSecurityGroup\" }]");
    assertThat(result).doesNotContain("\"CreatedDBParameterGroup\": { \"Value\": { \"Ref\": \"DBParameterGroup\" } },");
}
Also used : RDSModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

RDSModelContext (com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.RDSModelContext)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 MethodSource (org.junit.jupiter.params.provider.MethodSource)10 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)1 CREATE_FAILED (com.amazonaws.services.cloudformation.model.ResourceStatus.CREATE_FAILED)1 Waiter (com.amazonaws.waiters.Waiter)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 AwsCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient)1 AwsStackRequestHelper (com.sequenceiq.cloudbreak.cloud.aws.AwsStackRequestHelper)1 CloudFormationStackUtil (com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil)1 CloudFormationTemplateBuilder (com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder)1 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)1 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView)1 StackCancellationCheck (com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck)1 WaiterRunner.run (com.sequenceiq.cloudbreak.cloud.aws.scheduler.WaiterRunner.run)1 AwsCloudFormationErrorMessageProvider (com.sequenceiq.cloudbreak.cloud.aws.util.AwsCloudFormationErrorMessageProvider)1 AwsRdsInstanceView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsInstanceView)1