Пример #1
0
        public IActionResult CreateSqlServer([FromBody] NewSqlServer newDatabaseServer)
        {
            if (newDatabaseServer == null)
            {
                return(BadRequest(new
                {
                    EntityType = "DatabaseServer",
                    Reason = "InvalidRequest",
                    Message = "Must supply database details in the request body."
                }));
            }

            Tenant tenant = DocumentSession.Load <Tenant>(newDatabaseServer.TenantId);

            if (tenant == null)
            {
                return(NotFound(new
                {
                    Id = newDatabaseServer.TenantId,
                    EntityType = "Tenant",
                    Message = $"No tenant found with Id {newDatabaseServer.TenantId}."
                }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var databaseServer = new DatabaseServer
            {
                Name     = newDatabaseServer.Name,
                Kind     = DatabaseServerKind.SqlServer,
                Settings = new SqlServerSettings
                {
                    AdminPassword = newDatabaseServer.AdminPassword,
                    Storage       =
                    {
                        SizeMB = newDatabaseServer.SizeMB
                    }
                },
                TenantId = tenant.Id,
                Action   = ProvisioningAction.Provision,
                Status   = ProvisioningStatus.Pending,
            };

            DocumentSession.Store(databaseServer);

            databaseServer.AddProvisioningEvent($"Provisioning requested for '${databaseServer.Id}'.");

            DocumentSession.SaveChanges();

            return(StatusCode(StatusCodes.Status202Accepted, new
            {
                Id = databaseServer.Id,
                Name = databaseServer.Name,
                Message = $"SQL Server instance {databaseServer.Id} queued for creation."
            }));
        }
Пример #2
0
        public IActionResult DestroyServer(string serverId)
        {
            DatabaseServer targetServer = DocumentSession.GetDatabaseServerById(serverId);

            if (targetServer == null)
            {
                return(NotFound(new
                {
                    Id = serverId,
                    EntityType = "DatabaseServer",
                    Message = $"No database server found with Id '{serverId}'."
                }));
            }

            if (DocumentSession.DoesServerHaveDatabases(targetServer.Id))
            {
                return(StatusCode(StatusCodes.Status400BadRequest, new
                {
                    Id = targetServer.Id,
                    TenantId = serverId,
                    EntityType = "DatabaseServer",
                    RequestedAction = ProvisioningAction.Deprovision,
                    Action = targetServer.Action,
                    Status = targetServer.Status,
                    Message = $"Cannot de-provision database server {targetServer.Id} because it still hosts one or more databases. First de-provision these databases and then retry the operation."
                }));
            }

            if (targetServer.Action != ProvisioningAction.None)
            {
                return(StatusCode(StatusCodes.Status409Conflict, new
                {
                    Id = targetServer.Id,
                    TenantId = serverId,
                    EntityType = "DatabaseServer",
                    RequestedAction = ProvisioningAction.Deprovision,
                    Action = targetServer.Action,
                    Status = targetServer.Status,
                    Message = $"Cannot de-provision database server {targetServer.Id} because an action ({targetServer.Action}) is already in progress for this server."
                }));
            }

            targetServer.Action = ProvisioningAction.Deprovision;
            targetServer.Status = ProvisioningStatus.Pending;
            targetServer.AddProvisioningEvent($"De-provisioning requested for '${targetServer.Id}'.");
            DocumentSession.SaveChanges();

            return(StatusCode(StatusCodes.Status202Accepted, new
            {
                Id = serverId,
                Message = $"Database server {targetServer.Id} queued for deletion.",
                EntityType = "DatabaseServer"
            }));
        }
Пример #3
0
        public IActionResult ReconfigureServer(string serverId)
        {
            DatabaseServer targetServer = DocumentSession.GetDatabaseServerById(serverId);

            if (targetServer == null)
            {
                return(NotFound(new
                {
                    Id = serverId,
                    EntityType = "DatabaseServer",
                    Message = $"No database server found with Id '{serverId}'."
                }));
            }

            if (targetServer.Action != ProvisioningAction.None)
            {
                return(StatusCode(StatusCodes.Status409Conflict, new
                {
                    Id = targetServer.Id,
                    TenantId = serverId,
                    EntityType = "DatabaseServer",
                    RequestedAction = ProvisioningAction.Reconfigure,
                    Action = targetServer.Action,
                    Status = targetServer.Status,
                    Message = $"Cannot reconfigure database server {targetServer.Id} because another server-level action is already in progress."
                }));
            }

            targetServer.Action = ProvisioningAction.Reconfigure;
            targetServer.Status = ProvisioningStatus.Pending;
            targetServer.AddProvisioningEvent($"Reconfiguration requested for '${targetServer.Id}'.");

            DocumentSession.SaveChanges();

            return(StatusCode(StatusCodes.Status202Accepted, new
            {
                Id = serverId,
                Message = $"Database server {targetServer.Id} queued for reconfiguration.",
                EntityType = "DatabaseServer"
            }));
        }
Пример #4
0
        /// <summary>
        ///     Update server provisioning status.
        /// </summary>
        /// <param name="serverIngressChanged">
        ///     A <see cref="ServerStatusChanged"/> message describing the change.
        /// </param>
        /// <returns>
        ///     A <see cref="Task"/> representing the operation.
        /// </returns>
        async Task UpdateServerProvisioningStatus(ServerStatusChanged serverProvisioningNotification)
        {
            if (serverProvisioningNotification == null)
            {
                throw new ArgumentNullException(nameof(serverProvisioningNotification));
            }

            Log.Debug("Updating provisioning status for server {ServerId} due to {NotificationType} notification...",
                      serverProvisioningNotification.ServerId,
                      serverProvisioningNotification.GetType().Name
                      );

            using (IAsyncDocumentSession session = DocumentStore.OpenAsyncSession())
            {
                DatabaseServer server = await session.LoadAsync <DatabaseServer>(serverProvisioningNotification.ServerId);

                if (server == null)
                {
                    Log.Warning("Received ServerStatusChanged notification for non-existent server (Id:{ServerId}).",
                                serverProvisioningNotification.ServerId
                                );

                    return;
                }

                Log.Debug("Existing provisioning status for server {ServerId} is {Action}:{Status}:{Phase}.",
                          server.Id,
                          server.Action,
                          server.Status,
                          server.Phase
                          );

                ProvisioningAction action = server.Action;

                if (serverProvisioningNotification.Status.HasValue)
                {
                    server.Status = serverProvisioningNotification.Status.Value;

                    switch (server.Status)
                    {
                    case ProvisioningStatus.Ready:
                    case ProvisioningStatus.Error:
                    {
                        server.Action = ProvisioningAction.None;
                        server.Phase  = ServerProvisioningPhase.None;

                        break;
                    }

                    case ProvisioningStatus.Deprovisioned:
                    {
                        session.Delete(server);

                        break;
                    }
                    }
                }

                if (serverProvisioningNotification.Phase.HasValue)
                {
                    server.Phase = serverProvisioningNotification.Phase.Value;
                }

                Log.Debug("New provisioning status for server {ServerId} is {Action}:{Status}:{Phase}.",
                          server.Id,
                          server.Action,
                          server.Status,
                          server.Phase
                          );

                server.AddProvisioningEvent(serverProvisioningNotification.Messages);

                await session.SaveChangesAsync();
            }

            Log.Debug("Updated provisioning status for server {ServerId}.", serverProvisioningNotification.ServerId);
        }