/// <summary>
        ///     Ensure that a Deployment resource exists for the specified database server.
        /// </summary>
        /// <returns>
        ///     The Deployment resource, as a <see cref="DeploymentV1Beta1"/>.
        /// </returns>
        public async Task <DeploymentV1Beta1> EnsureDeploymentPresent()
        {
            RequireCurrentState();

            DeploymentV1Beta1 existingDeployment = await FindDeployment();

            if (existingDeployment != null)
            {
                Log.LogInformation("Found existing deployment {DeploymentName} for server {ServerId}.",
                                   existingDeployment.Metadata.Name,
                                   State.Id
                                   );

                return(existingDeployment);
            }

            Log.LogInformation("Creating deployment for server {ServerId}...",
                               State.Id
                               );

            DeploymentV1Beta1 createdDeployment = await KubeClient.DeploymentsV1Beta1().Create(
                KubeResources.Deployment(State,
                                         kubeNamespace: KubeOptions.KubeNamespace
                                         )
                );

            Log.LogInformation("Successfully created deployment {DeploymentName} for server {ServerId}.",
                               createdDeployment.Metadata.Name,
                               State.Id
                               );

            return(createdDeployment);
        }
        /// <summary>
        ///     Find the server's associated Deployment (if it exists).
        /// </summary>
        /// <returns>
        ///     The Deployment, or <c>null</c> if it was not found.
        /// </returns>
        public async Task <DeploymentV1Beta1> FindDeployment()
        {
            RequireCurrentState();

            List <DeploymentV1Beta1> matchingDeployments = await KubeClient.DeploymentsV1Beta1().List(
                labelSelector: $"cloud.dimensiondata.daas.server-id = {State.Id}",
                kubeNamespace: KubeOptions.KubeNamespace
                );

            if (matchingDeployments.Count == 0)
            {
                return(null);
            }

            return(matchingDeployments[matchingDeployments.Count - 1]);
        }
        /// <summary>
        ///     Ensure that a Deployment resource does not exist for the specified database server.
        /// </summary>
        /// <returns>
        ///     <c>true</c>, if the controller is now absent; otherwise, <c>false</c>.
        /// </returns>
        public async Task <bool> EnsureDeploymentAbsent()
        {
            RequireCurrentState();

            DeploymentV1Beta1 controller = await FindDeployment();

            if (controller == null)
            {
                return(true);
            }

            Log.LogInformation("Deleting deployment {DeploymentName} for server {ServerId}...",
                               controller.Metadata.Name,
                               State.Id
                               );

            try
            {
                await KubeClient.DeploymentsV1Beta1().Delete(
                    name: controller.Metadata.Name,
                    kubeNamespace: KubeOptions.KubeNamespace,
                    propagationPolicy: DeletePropagationPolicy.Background
                    );
            }
            catch (HttpRequestException <StatusV1> deleteFailed)
            {
                Log.LogError("Failed to delete deployment {DeploymentName} for server {ServerId} (Message:{FailureMessage}, Reason:{FailureReason}).",
                             controller.Metadata.Name,
                             State.Id,
                             deleteFailed.Response.Message,
                             deleteFailed.Response.Reason
                             );

                return(false);
            }

            Log.LogInformation("Deleted deployment {DeploymentName} for server {ServerId}.",
                               controller.Metadata.Name,
                               State.Id
                               );

            return(true);
        }