use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack 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);
}
use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.
the class AzureTemplateBuilderDbTest method buildTestWhenDataEncryptionParametersPresentAndKeyVersionError.
@Test
void buildTestWhenDataEncryptionParametersPresentAndKeyVersionError() {
Template template = Optional.ofNullable(factoryBean.getObject()).map(config -> {
try {
return config.getTemplate("templates/arm-dbstack.ftl", "UTF-8");
} catch (IOException e) {
throw new IllegalStateException(e);
}
}).orElseThrow();
Subnet subnet = new Subnet(SUBNET_CIDR);
Network network = new Network(subnet, List.of(NETWORK_CIDR), OutboundInternetTraffic.ENABLED);
network.putParameter("subnets", FULL_SUBNET_ID);
Map<String, Object> params = new HashMap<String, Object>();
params.put("dbVersion", "10");
params.put(KEY_URL, "https://dummyVault.vault.azure.net/keys/dummyKey");
params.put(KEY_VAULT_RESOURCE_GROUP_NAME, "dummyResourceGroup");
DatabaseServer databaseServer = DatabaseServer.builder().serverId(SERVER_ID).rootUserName(ROOT_USER_NAME).rootPassword(ROOT_PASSWORD).location(REGION).params(params).build();
DatabaseStack databaseStack = new DatabaseStack(network, databaseServer, Collections.emptyMap(), template.toString());
assertThrows(IllegalArgumentException.class, () -> underTest.build(cloudContext, databaseStack));
}
use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.
the class AzureTemplateBuilderDbTest method createDatabaseStack.
private DatabaseStack createDatabaseStack(boolean useSslEnforcement, String template) {
Subnet subnet = new Subnet(SUBNET_CIDR);
Network network = new Network(subnet, List.of(NETWORK_CIDR), OutboundInternetTraffic.ENABLED);
network.putParameter("subnets", FULL_SUBNET_ID);
DatabaseServer databaseServer = DatabaseServer.builder().useSslEnforcement(useSslEnforcement).serverId(SERVER_ID).rootUserName(ROOT_USER_NAME).rootPassword(ROOT_PASSWORD).location(REGION).params(Map.of("dbVersion", "10")).build();
return new DatabaseStack(network, databaseServer, Collections.emptyMap(), template);
}
use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.
the class AzureTemplateBuilderDbTest method buildTestWhenDataEncryptionParametersPresent.
@Test
void buildTestWhenDataEncryptionParametersPresent() {
Template template = Optional.ofNullable(factoryBean.getObject()).map(config -> {
try {
return config.getTemplate("templates/arm-dbstack.ftl", "UTF-8");
} catch (IOException e) {
throw new IllegalStateException(e);
}
}).orElseThrow();
Subnet subnet = new Subnet(SUBNET_CIDR);
Network network = new Network(subnet, List.of(NETWORK_CIDR), OutboundInternetTraffic.ENABLED);
network.putParameter("subnets", FULL_SUBNET_ID);
Map<String, Object> params = new HashMap<String, Object>();
params.put("dbVersion", "10");
params.put(KEY_URL, "https://dummyVault.vault.azure.net/keys/dummyKey/dummyVersion");
params.put(KEY_VAULT_RESOURCE_GROUP_NAME, "dummyResourceGroup");
DatabaseServer databaseServer = DatabaseServer.builder().serverId(SERVER_ID).rootUserName(ROOT_USER_NAME).rootPassword(ROOT_PASSWORD).location(REGION).params(params).build();
DatabaseStack databaseStack = new DatabaseStack(network, databaseServer, Collections.emptyMap(), template.toString());
Mockito.when(azureDatabaseTemplateProvider.getTemplate(databaseStack)).thenReturn(template);
Mockito.when(azureUtils.encodeString(SUBNET_ID)).thenReturn("hash");
String result = underTest.build(cloudContext, databaseStack);
assertThat(JsonUtil.isValid(result)).overridingErrorMessage("Invalid JSON: " + result).isTrue();
assertThat(result).contains("\"keyVaultName\": {\n" + " \"type\": \"string\",\n" + " \"defaultValue\" : \"dummyVault\",\n" + " \"metadata\": {\n" + " \"description\": \"Key vault name where the key to use is stored\"\n" + " }\n" + " },\n" + " \"keyVaultResourceGroupName\": {\n" + " \"type\": \"string\",\n" + " \"defaultValue\" : \"dummyResourceGroup\",\n" + " \"metadata\": {\n" + " \"description\": \"Key vault resource group name where it is stored\"\n" + " }\n" + " },\n" + " \"keyName\": {\n" + " \"type\": \"string\",\n" + " \"defaultValue\" : \"dummyKey\",\n" + " \"metadata\": {\n" + " \"description\": \"Key name in the key vault to use as encryption protector\"\n" + " }\n" + " },\n" + " \"keyVersion\": {\n" + " \"type\": \"string\",\n" + " \"defaultValue\" : \"dummyVersion\",\n" + " \"metadata\": {\n" + " \"description\": \"Version of the key in the key vault to use as encryption protector\"\n" + " }\n" + " }");
}
use of com.sequenceiq.cloudbreak.cloud.model.DatabaseStack in project cloudbreak by hortonworks.
the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceIsRunnableNOTAlwaysShouldReturnStarted.
@Test
public void testCheckWhenDbInstanceIsRunnableNOTAlwaysShouldReturnStarted() throws IOException {
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
CloudCredential cloudCredential = mock(CloudCredential.class);
DatabaseStack databaseStack = mock(DatabaseStack.class);
DatabaseServer databaseServer = mock(DatabaseServer.class);
SQLAdmin sqlAdmin = mock(SQLAdmin.class);
SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
when(cloudCredential.getName()).thenReturn("credential");
when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
when(databaseServer.getServerId()).thenReturn("test");
when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
when(sqlAdminInstancesList.execute()).thenReturn(instancesListResponse);
when(instancesListResponse.isEmpty()).thenReturn(false);
DatabaseInstance databaseInstance = new DatabaseInstance();
databaseInstance.setName("test");
databaseInstance.setState("RUNNABLE");
Settings settings = new Settings();
settings.setActivationPolicy("NOT_ALWAYS");
databaseInstance.setSettings(settings);
when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
ExternalDatabaseStatus check = underTest.check(authenticatedContext, databaseStack);
Assert.assertEquals(ExternalDatabaseStatus.STOPPED, check);
}
Aggregations