Beispiel #1
0
        protected override bool Unprovision(string name, ServiceCredentials[] bindings)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(false);
            }

            Logger.Debug(Strings.SqlNodeUnprovisionDatabaseDebugMessage, name, JsonConvertibleObject.SerializeToJson(bindings.Select(binding => binding.ToJsonIntermediateObject()).ToArray()));

            ProvisionedService provisioned_service = ProvisionedService.GetService(name);

            if (provisioned_service == null)
            {
                throw new MSSqlErrorException(MSSqlErrorException.MSSqlConfigNotFound, name);
            }

            // TODO: validate that database files are not lingering
            // Delete all bindings, ignore not_found error since we are unprovision
            try
            {
                if (bindings != null)
                {
                    foreach (ServiceCredentials credential in bindings)
                    {
                        this.Unbind(credential);
                    }
                }
            }
            catch (Exception)
            {
                // ignore
            }

            this.DeleteDatabase(provisioned_service);
            long storage = this.StorageForService(provisioned_service);

            this.availableStorageBytes += storage;
            this.availableCapacity     += this.CapacityUnit();

            if (!provisioned_service.Destroy())
            {
                Logger.Error(Strings.SqlNodeDeleteServiceErrorMessage, provisioned_service.Name);
                throw new MSSqlErrorException(MSSqlErrorException.MSSqlLocalDBError);
            }

            Logger.Debug(Strings.SqlNodeUnprovisionSuccessDebugMessage, name);
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Binds a SQL Server database to an app.
        /// </summary>
        /// <param name="name">The name of the service.</param>
        /// <param name="bindOptions">Binding options.</param>
        /// <param name="credentials">Already existing credentials.</param>
        /// <returns>
        /// A new set of credentials used for binding.
        /// </returns>
        protected override ServiceCredentials Bind(string name, Dictionary <string, object> bindOptions, ServiceCredentials credentials)
        {
            Logger.Debug(Strings.SqlNodeBindServiceDebugMessage, name, JsonConvertibleObject.SerializeToJson(bindOptions));
            Dictionary <string, object> binding = null;

            try
            {
                ProvisionedService service = ProvisionedService.GetService(name);
                if (service == null)
                {
                    throw new MSSqlErrorException(MSSqlErrorException.MSSqlConfigNotFound, name);
                }

                // create new credential for binding
                binding = new Dictionary <string, object>();

                if (credentials != null)
                {
                    binding["user"]     = credentials.User;
                    binding["password"] = credentials.Password;
                }
                else
                {
                    binding["user"]     = "******" + Credentials.GenerateCredential();
                    binding["password"] = "******" + Credentials.GenerateCredential();
                }

                binding["bind_opts"] = bindOptions;

                this.CreateDatabaseUser(name, binding["user"] as string, binding["password"] as string);
                ServiceCredentials response = this.GenerateCredential(name, binding["user"] as string, binding["password"] as string);

                Logger.Debug(Strings.SqlNodeBindResponseDebugMessage, response.SerializeToJson());
                this.bindingServed += 1;
                return(response);
            }
            catch (Exception)
            {
                if (binding != null)
                {
                    this.DeleteDatabaseUser(binding["user"] as string);
                }

                throw;
            }
        }