Exemple #1
0
 private static void LogRequest(ILogger log, ServiceInstanceProvisionRequest request)
 {
     log.LogInformation(
         $"Provision - request: {{ organization_guid = {request.OrganizationGuid}, " +
         $"space_guid = {request.SpaceGuid}, " +
         $"service_id = {request.ServiceId}, " +
         $"plan_id = {request.PlanId}, " +
         $"parameters = {request.Parameters}, " +
         $"context = {request.Context} }}");
 }
Exemple #2
0
        public async Task <ServiceInstanceProvision> ProvisionAsync(ServiceInstanceContext context, ServiceInstanceProvisionRequest request)
        {
            _logger.LogInformation("Provisioning instance {0} as service {1}.", context.InstanceId, request.ServiceId);

            if (GetService(request.ServiceId).Plans.All(x => x.Id != request.PlanId))
            {
                throw new BadRequestException($"Unknown plan ID '{request.PlanId}'.");
            }

            var entity = await _context.ServiceInstances.FindAsync(context.InstanceId);

            if (entity != null)
            {
                if (entity.ServiceId == request.ServiceId &&
                    entity.PlanId == request.PlanId &&
                    JsonConvert.SerializeObject(request.Parameters) == (entity.Parameters ?? "null"))
                {
                    return new ServiceInstanceProvision {
                               Unchanged = true
                    }
                }
                ;
                else
                {
                    throw new ConflictException($"There is already an instance {context.InstanceId} with different settings.");
                }
            }

            await _context.ServiceInstances.AddAsync(new ServiceInstanceEntity
            {
                Id         = context.InstanceId,
                ServiceId  = request.ServiceId,
                PlanId     = request.PlanId,
                Parameters = JsonConvert.SerializeObject(request.Parameters)
            });

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw new ConflictException(ex.InnerException?.Message ?? ex.Message);
            }

            _metrics.Provisioned(request.ServiceId);
            return(new ServiceInstanceProvision());
        }
Exemple #3
0
        public async Task <ServiceInstanceProvision> ProvisionAsync(ServiceInstanceContext context, ServiceInstanceProvisionRequest request)
        {
            LogContext(_log, "Provision", context);
            LogRequest(_log, request);

            var orgId             = request.OrganizationGuid;
            var spaceId           = request.SpaceGuid;
            var resourceGroupName = $"{orgId}_{spaceId}";

            // Create resource group if it does not yet exist.
            var exists = await _azureResourceGroupClient.ResourceGroupExists(resourceGroupName);

            if (exists)
            {
                _log.LogInformation($"Resource group {resourceGroupName} exists");
            }
            else
            {
                _log.LogInformation($"Resource group {resourceGroupName} does not exist: creating");

                var resourceGroup = await _azureResourceGroupClient.CreateResourceGroup(new ResourceGroup
                {
                    Name     = resourceGroupName,
                    Location = "westeurope",
                    Tags     = new Dictionary <string, string>
                    {
                        { "cf_org_id", orgId },
                        { "cf_space_id", spaceId }
                    }
                });

                _log.LogInformation($"Resource group {resourceGroupName} created: {resourceGroup.Id}");
            }

            // Create storage account.
            var storageAccountName = context.InstanceId.Replace("-", "").Substring(0, 24);
            await _azureStorageClient.CreateStorageAccount(
                resourceGroupName,
                new StorageAccount
            {
                Name       = storageAccountName,
                Kind       = StorageKind.StorageV2,
                Location   = "westeurope",
                Properties = new StorageAccountProperties
                {
                    AccessTier = StorageAccessTier.Hot,
                    Encryption = new StorageEncryption
                    {
                        KeySource = StorageEncryptionKeySource.Storage,
                        Services  = new StorageEncryptionServices
                        {
                            Blob = new StorageEncryptionService {
                                Enabled = true
                            },
                            File = new StorageEncryptionService {
                                Enabled = true
                            },
                            Table = new StorageEncryptionService {
                                Enabled = true
                            },
                            Queue = new StorageEncryptionService {
                                Enabled = true
                            }
                        }
                    },
                    SupportsHttpsTrafficOnly = true
                },
                Sku = new StorageSku
                {
                    Name = StorageSkuName.Standard_LRS,
                    Tier = StorageSkuTier.Standard
                },
                Tags = new Dictionary <string, string>
                {
                    { "cf_org_id", orgId },
                    { "cf_space_id", spaceId },
                    { "cf_service_id", request.ServiceId },
                    { "cf_plan_id", request.PlanId },
                    { "cf_service_instance_id", context.InstanceId }
                }
            });

            return(new ServiceInstanceProvision());
        }
Exemple #4
0
        public async Task <ServiceInstanceProvision> ProvisionAsync(ServiceInstanceContext context, ServiceInstanceProvisionRequest request)
        {
            LogContext(_log, "Provision", context);
            LogRequest(_log, request);

            _log.LogInformation($"The CF api is: {_appConfig.Value.CF_Api}");

            var orgId             = request.OrganizationGuid;
            var spaceId           = request.SpaceGuid;
            var resourceGroupName = $"{orgId}_{spaceId}";

            return(await Task.FromResult(new ServiceInstanceProvision()));
        }