Search in sources :

Example 1 with AzureDatabaseServerView

use of com.sequenceiq.cloudbreak.cloud.azure.view.AzureDatabaseServerView in project cloudbreak by hortonworks.

the class AzureDatabaseTemplateBuilder method build.

public String build(CloudContext cloudContext, DatabaseStack databaseStack) {
    try {
        String location = cloudContext.getLocation().getRegion().getRegionName();
        AzureNetworkView azureNetworkView = new AzureNetworkView(databaseStack.getNetwork());
        AzureDatabaseServerView azureDatabaseServerView = new AzureDatabaseServerView(databaseStack.getDatabaseServer());
        Map<String, Object> model = new HashMap<>();
        model.put("usePrivateEndpoints", USE_PRIVATE_ENDPOINT.equals(azureNetworkView.getEndpointType()));
        model.put("subnetIdForPrivateEndpoint", azureNetworkView.getSubnetIdForPrivateEndpoint());
        model.put("existingDatabasePrivateDnsZoneId", azureNetworkView.getExistingDatabasePrivateDnsZoneId());
        model.put("adminLoginName", azureDatabaseServerView.getAdminLoginName());
        model.put("adminPassword", azureDatabaseServerView.getAdminPassword());
        model.put("backupRetentionDays", azureDatabaseServerView.getBackupRetentionDays());
        model.put("dbServerName", azureDatabaseServerView.getDbServerName());
        model.put("dbVersion", azureDatabaseServerView.getDbVersion());
        model.put("geoRedundantBackup", azureDatabaseServerView.getGeoRedundantBackup());
        model.put("location", location);
        if (azureDatabaseServerView.getPort() != null) {
            LOGGER.warn("Found port {} in database stack, but Azure ignores it", azureDatabaseServerView.getPort());
        }
        model.put("dataEncryption", false);
        String keyVaultUrl = azureDatabaseServerView.getKeyVaultUrl();
        if (keyVaultUrl != null) {
            String keyVaultName;
            String keyName;
            String keyVersion;
            Matcher matcher = ENCRYPTION_KEY_URL_VAULT_NAME.matcher(keyVaultUrl);
            if (matcher.matches()) {
                keyVaultName = matcher.group(1);
            } else {
                throw new IllegalArgumentException(String.format("keyVaultName cannot be fetched from encryptionKeyUrl %s.", keyVaultUrl));
            }
            matcher = ENCRYPTION_KEY_NAME.matcher(keyVaultUrl);
            if (matcher.matches()) {
                keyName = matcher.group(1);
            } else {
                throw new IllegalArgumentException(String.format("keyName cannot be fetched from encryptionKeyUrl %s.", keyVaultUrl));
            }
            matcher = ENCRYPTION_KEY_VERSION.matcher(keyVaultUrl);
            if (matcher.matches()) {
                keyVersion = matcher.group(1);
            } else {
                throw new IllegalArgumentException(String.format("keyVersion cannot be fetched from encryptionKeyUrl %s.", keyVaultUrl));
            }
            model.put("dataEncryption", true);
            model.put("keyVaultName", keyVaultName);
            model.put("keyVaultResourceGroupName", azureDatabaseServerView.getKeyVaultResourceGroupName());
            model.put("keyName", keyName);
            model.put("keyVersion", keyVersion);
        }
        model.put("serverTags", databaseStack.getTags());
        model.put("skuCapacity", azureDatabaseServerView.getSkuCapacity());
        model.put("skuFamily", azureDatabaseServerView.getSkuFamily());
        model.put("skuName", azureDatabaseServerView.getSkuName());
        model.put("skuSizeMB", azureDatabaseServerView.getAllocatedStorageInMb());
        model.put("skuTier", getSkuTier(azureDatabaseServerView));
        model.put("useSslEnforcement", azureDatabaseServerView.isUseSslEnforcement());
        model.put("storageAutoGrow", azureDatabaseServerView.getStorageAutoGrow());
        model.put("subnets", azureNetworkView.getSubnets());
        String[] subnets = azureNetworkView.getSubnets().split(",");
        model.put("subnetIdList", subnets);
        // if subnet number is 1 then Azure does not create the endpoints if the batchsize is 5
        model.put("batchSize", azureNetworkView.getSubnets().split(",").length >= defaultBatchSize ? defaultBatchSize : 1);
        model.put("location", azureDatabaseServerView.getLocation());
        model.put("privateEndpointName", String.format("pe-%s-to-%s", azureUtils.encodeString(getSubnetName(azureNetworkView.getSubnetList().get(0))), azureDatabaseServerView.getDbServerName()));
        String generatedTemplate = freeMarkerTemplateUtils.processTemplateIntoString(azureDatabaseTemplateProvider.getTemplate(databaseStack), model);
        LOGGER.debug("Generated ARM database template: {}", AnonymizerUtil.anonymize(generatedTemplate));
        return generatedTemplate;
    } catch (IOException | TemplateException e) {
        throw new CloudConnectorException("Failed to process the ARM TemplateBuilder", e);
    }
}
Also used : AzureNetworkView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureNetworkView) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) TemplateException(freemarker.template.TemplateException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) AzureDatabaseServerView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureDatabaseServerView)

Aggregations

AzureDatabaseServerView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureDatabaseServerView)1 AzureNetworkView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureNetworkView)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1