Esempio n. 1
0
 private static void LogContext(ILogger log, string operation, ServiceInstanceContext context)
 {
     log.LogInformation(
         $"{operation} - context: {{ instance_id = {context.InstanceId}, " +
         $"originating_identity = {{ platform = {context.OriginatingIdentity?.Platform}, " +
         $"value = {context.OriginatingIdentity?.Value} }} }}");
 }
Esempio n. 2
0
            public InstancePipe(ServiceInstanceContext context)
            {
                _context = context;

                _pipe            = default;
                _sendContextPipe = default;
            }
Esempio n. 3
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()));
        }
Esempio n. 4
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());
        }
Esempio n. 5
0
    public async Task DeprovisionAsync(ServiceInstanceContext context, string serviceId, string planId)
    {
        _logger.LogInformation("Deprovisioning instance {InstanceId}", context.InstanceId);

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

        if (entity == null)
        {
            throw new GoneException($"Instance '{context.InstanceId}' not found.");
        }
        if (entity.ServiceId != serviceId || entity.PlanId != planId)
        {
            throw new BadRequestException($"Service and/or plan ID for instance '{context.InstanceId}' do not match.");
        }

        _context.ServiceInstances.Remove(entity);
        await _context.SaveChangesAsync();
    }
Esempio n. 6
0
        public async Task UpdateAsync(ServiceInstanceContext context, ServiceInstanceUpdateRequest request)
        {
            _logger.LogInformation("Updating instance {0} as service {1}.", context.InstanceId, request.ServiceId);

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

            if (entity == null)
            {
                throw new NotFoundException($"Instance '{context.InstanceId}' not found.");
            }

            if (request.ServiceId != entity.ServiceId)
            {
                throw new BadRequestException($"Cannot change service ID of instance '{context.InstanceId}' from '{entity.ServiceId}' to '{request.ServiceId}'.");
            }
            if (request.PlanId != null && request.PlanId != entity.PlanId)
            {
                var service = GetService(request.ServiceId);
                if (!service.PlanUpdateable)
                {
                    throw new BadRequestException($"Service ID '{request.ServiceId}' does not allow changing the Plan ID.");
                }
                if (service.Plans.All(x => x.Id != request.PlanId))
                {
                    throw new BadRequestException($"Unknown plan ID '{request.PlanId}'.");
                }
                entity.PlanId = request.PlanId;
            }
            if (request.Parameters != null)
            {
                entity.Parameters = JsonConvert.SerializeObject(request.Parameters);
            }

            _context.Update(entity);
            await _context.SaveChangesAsync();
        }
Esempio n. 7
0
        public async Task DeprovisionAsync(ServiceInstanceContext context, string serviceId, string planId)
        {
            LogContext(_log, "Deprovision", context);
            _log.LogInformation($"Deprovision: {{ service_id = {serviceId}, planId = {planId} }}");

            // First retrieve all storage accounts in the subscription because we do not have information here
            // about the resource group of the storage account we wish to delete.
            var storageAccounts = await _azureStorageProviderClient.ListStorageAccounts();

            // Find storage account with the tag containing the service instance id.
            var storageAccount = storageAccounts
                                 .SingleOrDefault(account => account.Tags
                                                  .Any(tag => tag.Key == "cf_service_instance_id" && tag.Value == context.InstanceId));

            if (storageAccount != null)
            {
                // Delete storage account based on storage account resource id.
                await _azureStorageClient.DeleteStorageAccount(storageAccount.Id);

                // Parse resource group name from resource id and delete resource group if empty.
                var resourceGroupName = storageAccount.Id.Split('/')[4];
                await _azureResourceGroupClient.DeleteResourceGroupIfEmpty(resourceGroupName);
            }
        }
Esempio n. 8
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());
        }
Esempio n. 9
0
 public Task UpdateAsync(ServiceInstanceContext context, ServiceInstanceUpdateRequest request)
 {
     throw new System.NotImplementedException();
 }
Esempio n. 10
0
 public void ValueRemoved(INode <ServiceInstanceContext> node, ServiceInstanceContext value)
 {
     _distribution.Remove(value);
 }
Esempio n. 11
0
 public void ValueAdded(INode <ServiceInstanceContext> node, ServiceInstanceContext value)
 {
 }
Esempio n. 12
0
 static byte[] GetHashKey(ServiceInstanceContext context)
 {
     return(context.InstanceId.ToByteArray());
 }
Esempio n. 13
0
 public async Task DeprovisionAsync(ServiceInstanceContext context, string serviceId, string planId)
 {
     LogContext(_log, "Deprovision", context);
     _log.LogInformation($"Deprovision: {{ service_id = {serviceId}, planId = {planId} }}");
 }
Esempio n. 14
0
 public ServiceClientSendEndpoint(ISendEndpoint endpoint, ServiceInstanceContext context)
 {
     _endpoint = endpoint;
     _context  = context;
 }
Esempio n. 15
0
 public InstancePipe(ServiceInstanceContext context, IPipe <SendContext <TMessage> > pipe)
 {
     _context         = context;
     _pipe            = pipe;
     _sendContextPipe = pipe as ISendContextPipe;
 }