/// <summary>
        /// Terminate an ongoing database copy operation.
        /// </summary>
        /// <param name="copyModel">The database copy to terminate.</param>
        /// <param name="forcedTermination"><c>true</c> to forcefully terminate the copy.</param>
        public void StopDatabaseCopy(
            DatabaseCopyModel copyModel,
            bool forcedTermination)
        {
            DatabaseCopy databaseCopy = GetCopyForCopyModel(copyModel);

            // Create a new request Id for this operation
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            try
            {
                // Mark Forced/Friendly flag on the databaseCopy object first
                databaseCopy.IsForcedTerminate = forcedTermination;
                this.UpdateObject(databaseCopy);
                this.SaveChanges();

                // Mark the copy operation for delete
                this.DeleteObject(databaseCopy);
                this.SaveChanges();
            }
            catch
            {
                this.RevertChanges(databaseCopy);
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieves the list of all operations on the database.
        /// </summary>
        /// <param name="databaseName">The name of database to retrieve operations.</param>
        /// <returns>An array of all operations on the database.</returns>
        public DatabaseOperation[] GetDatabaseOperations(string databaseName)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve all operations on specified database
            DatabaseOperationListResponse response = sqlManagementClient.DatabaseOperations.ListByDatabase(
                this.serverName,
                databaseName);

            // For any database which has ever been created, there should be at least one operation
            if (response.Count() == 0)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              Resources.DatabaseOperationNotFoundOnDatabase,
                              this.ServerName,
                              databaseName));
            }

            // Construct the resulting database operations
            DatabaseOperation[] operations = response.Select(operation => CreateDatabaseOperationsFromResponse(operation)).ToArray();
            return(operations);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new sql database.
        /// </summary>
        /// <param name="databaseName">The name for the new database</param>
        /// <param name="databaseMaxSizeInGB">The maximum size of the new database</param>
        /// <param name="databaseCollation">The collation for the new database</param>
        /// <param name="databaseEdition">The edition for the new database</param>
        /// <returns>The newly created Sql Database</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int?databaseMaxSizeInGB,
            string databaseCollation,
            DatabaseEdition databaseEdition,
            ServiceObjective serviceObjective)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Create the database
            DatabaseCreateResponse response = sqlManagementClient.Databases.Create(
                this.serverName,
                new DatabaseCreateParameters()
            {
                Name    = databaseName,
                Edition = databaseEdition != DatabaseEdition.None ?
                          databaseEdition.ToString() : DatabaseEdition.Web.ToString(),
                CollationName           = databaseCollation ?? string.Empty,
                MaximumDatabaseSizeInGB = databaseMaxSizeInGB ??
                                          (databaseEdition == DatabaseEdition.Business || databaseEdition == DatabaseEdition.Premium ? 10 : 1),
                ServiceObjectiveId = serviceObjective != null ? serviceObjective.Id.ToString() : null,
            });

            // Construct the resulting Database object
            Database database = CreateDatabaseFromResponse(response);

            return(database);
        }
 /// <summary>
 /// Creates an instance of a SQLAuth to TSql class
 /// </summary>
 /// <param name="fullyQualifiedServerName">The full server name</param>
 /// <param name="credentials">The login credentials for the server</param>
 public TSqlConnectionContext(Guid sessionActivityId, string fullyQualifiedServerName, SqlAuthenticationCredentials credentials, string serverName)
 {
     this.serverName        = serverName;
     this.sessionActivityId = sessionActivityId;
     this.clientRequestId   = SqlDatabaseCmdletBase.GenerateClientTracingId();
     this.credentials       = credentials;
     builder = GenerateSqlConnectionBuilder(fullyQualifiedServerName, credentials.UserName, credentials.Password);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Retrieves the metadata for the context as a <see cref="XDocument"/>
        /// </summary>
        /// <returns>The metadata for the context as a <see cref="XDocument"/></returns>
        public XDocument RetrieveMetadata()
        {
            // Create a new request Id for this operation
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            XDocument doc = DataConnectionUtility.GetMetadata(this, EnhanceRequest);

            return(doc);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a new Sql Database.
        /// </summary>
        /// <param name="databaseName">The name for the new database.</param>
        /// <param name="databaseMaxSize">The max size for the database.</param>
        /// <param name="databaseCollation">The collation for the database.</param>
        /// <param name="databaseEdition">The edition for the database.</param>
        /// <returns>The newly created Sql Database.</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int?databaseMaxSize,
            string databaseCollation,
            DatabaseEdition databaseEdition)
        {
            // Create a new request Id for this operation
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Create the new entity and set its properties
            Database database = new Database();

            database.Name = databaseName;

            if (databaseMaxSize != null)
            {
                database.MaxSizeGB = (int)databaseMaxSize;
            }

            if (!string.IsNullOrEmpty(databaseCollation))
            {
                database.CollationName = databaseCollation;
            }

            if (databaseEdition != DatabaseEdition.None)
            {
                database.Edition = databaseEdition.ToString();
            }

            // Save changes
            this.AddToDatabases(database);
            try
            {
                this.SaveChanges(SaveChangesOptions.None);

                // Re-Query the database for server side updated information
                database = this.RefreshEntity(database);
                if (database == null)
                {
                    throw new ApplicationException(Resources.ErrorRefreshingDatabase);
                }
            }
            catch
            {
                this.ClearTrackedEntity(database);
                throw;
            }

            // Load the extra properties for this object.
            this.LoadExtraProperties(database);

            return(database);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Remove a database from a server
        /// </summary>
        /// <param name="databaseName">The name of the database to delete</param>
        public void RemoveDatabase(string databaseName)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the list of databases
            OperationResponse response = sqlManagementClient.Databases.Delete(
                this.serverName,
                databaseName);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets a list of all the databases in the current context.
        /// </summary>
        /// <returns>An array of databases in the current context</returns>
        public Database[] GetDatabases()
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the list of databases
            DatabaseListResponse response = sqlManagementClient.Databases.List(this.serverName);

            // Construct the resulting Database objects
            Database[] databases = response.Databases.Select((db) => CreateDatabaseFromResponse(db)).ToArray();
            return(databases);
        }
        /// <summary>
        /// Start database copy on the database with the name <paramref name="databaseName"/>.
        /// </summary>
        /// <param name="databaseName">The name of the database to copy.</param>
        /// <param name="partnerServer">The name for the partner server.</param>
        /// <param name="partnerDatabaseName">The name of the database on the partner server.</param>
        /// <param name="continuousCopy"><c>true</c> to make this a continuous copy.</param>
        /// <param name="isOfflineSecondary"><c>true</c> to make this an offline secondary copy.</param>
        /// <returns>The new instance of database copy operation.</returns>
        public DatabaseCopyModel StartDatabaseCopy(
            string databaseName,
            string partnerServer,
            string partnerDatabaseName,
            bool continuousCopy,
            bool isOfflineSecondary)
        {
            // Create a new request Id for this operation
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Create a new database copy object with all the required properties
            DatabaseCopy databaseCopy = new DatabaseCopy();

            databaseCopy.SourceServerName        = this.ServerName;
            databaseCopy.SourceDatabaseName      = databaseName;
            databaseCopy.DestinationServerName   = partnerServer;
            databaseCopy.DestinationDatabaseName = partnerDatabaseName;

            // Set the optional continuous copy flag
            databaseCopy.IsContinuous = continuousCopy;

            // Set the optional IsOfflineSecondary flag
            databaseCopy.IsOfflineSecondary = isOfflineSecondary;

            this.AddToDatabaseCopies(databaseCopy);
            DatabaseCopy trackedDatabaseCopy = databaseCopy;

            try
            {
                this.SaveChanges(SaveChangesOptions.None);

                // Requery for the entity to obtain updated linkid and state
                databaseCopy = this.RefreshEntity(databaseCopy);
                if (databaseCopy == null)
                {
                    throw new ApplicationException(Resources.ErrorRefreshingDatabaseCopy);
                }
            }
            catch
            {
                this.RevertChanges(trackedDatabaseCopy);
                throw;
            }

            return(CreateCopyModelFromCopy(databaseCopy));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Retrieves the list of all service objectives on the server.
        /// </summary>
        /// <returns>An array of all service objectives on the server.</returns>
        public ServiceObjective[] GetServiceObjectives()
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the specified database
            ServiceObjectiveListResponse response = sqlManagementClient.ServiceObjectives.List(
                this.serverName);

            // Construct the resulting Database object
            ServiceObjective[] serviceObjectives = response.Select(serviceObjective => CreateServiceObjectiveFromResponse(serviceObjective)).ToArray();
            return(serviceObjectives);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ServerDataServiceSqlAuth"/> class.
        /// </summary>
        /// <param name="managementServiceUri">The server's management service Uri.</param>
        /// <param name="connectionType">The server connection type with the server name</param>
        /// <param name="sessionActivityId">An activity ID provided by the user that should be associated with this session.</param>
        /// <param name="accessToken">The authentication access token to be used for executing web requests.</param>
        /// <param name="credentials">The SQL authentication credentials used for this context</param>
        private ServerDataServiceSqlAuth(
            Uri managementServiceUri,
            DataServiceConnectionType connectionType,
            Guid sessionActivityId,
            AccessTokenResult accessToken,
            SqlAuthenticationCredentials credentials)
            : base(new Uri(managementServiceUri, connectionType.RelativeEntityUri))
        {
            this.sessionActivityId = sessionActivityId;
            this.connectionType    = connectionType;
            this.accessToken       = accessToken;
            this.credentials       = credentials;

            // Generate a requestId and retrieve the server name
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();
            this.serverName      = this.Servers.First().Name;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Retrieves the list of all databases' operations on the server.
        /// </summary>
        /// <returns>An array of all operations on the server.</returns>
        public DatabaseOperation[] GetDatabasesOperations()
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the operations on specified server
            // We do not validate the number of operations returned since it's possible that there is no
            // database operations on a new created server.
            DatabaseOperationListResponse response = sqlManagementClient.DatabaseOperations.ListByServer(
                this.serverName);

            // Construct the resulting database operations array
            DatabaseOperation[] operations = response.Select(operation => CreateDatabaseOperationsFromResponse(operation)).ToArray();
            return(operations);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Retrieve information on operation with the guid
        /// </summary>
        /// <param name="OperationGuid">The Guid of the operation to retrieve.</param>
        /// <returns>An object containing the information about the specific operation.</returns>
        public DatabaseOperation GetDatabaseOperation(Guid OperationGuid)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the specified Operation
            DatabaseOperationGetResponse response = sqlManagementClient.DatabaseOperations.Get(
                this.serverName,
                OperationGuid.ToString());

            // Construct the resulting Operation object
            DatabaseOperation operation = CreateDatabaseOperationFromResponse(response);

            return(operation);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Retrieve information on latest service objective with service objective
        /// </summary>
        /// <param name="serviceObjective">The service objective to refresh.</param>
        /// <returns>
        /// An object containing the information about the specific service objective.
        /// </returns>
        public ServiceObjective GetServiceObjective(ServiceObjective serviceObjectiveToRefresh)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the specified database
            ServiceObjectiveGetResponse response = sqlManagementClient.ServiceObjectives.Get(
                this.serverName,
                serviceObjectiveToRefresh.Id.ToString());

            // Construct the resulting Database object
            ServiceObjective serviceObjective = CreateServiceObjectiveFromResponse(response);

            return(serviceObjective);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Retrieve a specific database from the current context
        /// </summary>
        /// <param name="databaseName">The name of the database to retrieve</param>
        /// <returns>A database object</returns>
        public Database GetDatabase(string databaseName)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the specified database
            DatabaseGetResponse response = sqlManagementClient.Databases.Get(
                this.serverName,
                databaseName);

            // Construct the resulting Database object
            Database database = CreateDatabaseFromResponse(response);

            return(database);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Update a database on the server.
        /// </summary>
        /// <param name="databaseName">The name of the database to modify.</param>
        /// <param name="newDatabaseName">The new name of the database.</param>
        /// <param name="databaseMaxSizeInGB">The new maximum size of the database.</param>
        /// <param name="databaseEdition">The new edition of the database.</param>
        /// <param name="serviceObjective">The new service objective of the database.</param>
        /// <returns>The updated database.</returns>
        public Database UpdateDatabase(
            string databaseName,
            string newDatabaseName,
            int?databaseMaxSizeInGB,
            DatabaseEdition?databaseEdition,
            ServiceObjective serviceObjective)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>();

            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the specified database
            DatabaseGetResponse database = sqlManagementClient.Databases.Get(
                this.serverName,
                databaseName);

            // Update the database with the new properties
            DatabaseUpdateResponse response = sqlManagementClient.Databases.Update(
                this.serverName,
                databaseName,
                new DatabaseUpdateParameters()
            {
                Id   = database.Id,
                Name = !string.IsNullOrEmpty(newDatabaseName) ?
                       newDatabaseName : database.Name,
                Edition = databaseEdition.HasValue && (databaseEdition != DatabaseEdition.None) ?
                          databaseEdition.ToString() : (database.Edition ?? string.Empty),
                CollationName           = database.CollationName ?? string.Empty,
                MaximumDatabaseSizeInGB = databaseMaxSizeInGB.HasValue ?
                                          databaseMaxSizeInGB.Value : database.MaximumDatabaseSizeInGB,
                ServiceObjectiveId = serviceObjective != null ?
                                     serviceObjective.Id.ToString() : null,
            });

            // Construct the resulting Database object
            Database updatedDatabase = CreateDatabaseFromResponse(response);

            return(updatedDatabase);
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Creates an instance of a SQLAuth to TSql class
 /// </summary>
 /// <param name="fullyQualifiedServerName"></param>
 /// <param name="username"></param>
 /// <param name="password"></param>
 public TSqlConnectionContext(Guid sessionActivityId, string fullyQualifiedServerName, string username, string password)
 {
     this.sessionActivityId = sessionActivityId;
     this.clientRequestId   = SqlDatabaseCmdletBase.GenerateClientTracingId();
     builder = GenerateSqlConnectionBuilder(fullyQualifiedServerName, username, password);
 }