/// <summary>
        /// Process the request using the server name
        /// </summary>
        /// <param name="maxSizeGb">the maximum size of the database</param>
        private void ProcessWithServerName(int? maxSizeGb, long? maxSizeBytes)
        {
            Func<string> GetClientRequestId = () => string.Empty;
            try
            {
                // Get the current subscription data.
                WindowsAzureSubscription subscription = WindowsAzureProfile.Instance.CurrentSubscription;

                // Create a temporary context
                ServerDataServiceCertAuth context =
                    ServerDataServiceCertAuth.Create(this.ServerName, subscription);

                GetClientRequestId = () => context.ClientRequestId;
                
                Database response = context.CreateNewDatabase(
                    this.DatabaseName,
                    maxSizeGb,
                    maxSizeBytes,
                    this.Collation,
                    this.Edition,
                    this.ServiceObjective);

                response = CmdletCommon.WaitForDatabaseOperation(this, context, response, this.DatabaseName, true);

                // Retrieve the database with the specified name
                this.WriteObject(response);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    GetClientRequestId(),
                    ex);
            }
        }
        /// <summary>
        /// Process the request using a temporary connection context using certificate authentication
        /// </summary>
        /// <param name="databaseName">The name of the database to update</param>
        /// <param name="maxSizeGb">the new size for the database or null</param>
        /// <param name="maxSizeBytes"></param>
        /// <param name="edition">the new edition for the database or null</param>
        private void ProcessWithServerName(string databaseName, int?maxSizeGb, long?maxSizeBytes, DatabaseEdition?edition)
        {
            Func <string> GetClientRequestId = () => string.Empty;

            try
            {
                // Get the current subscription data.
                AzureSubscription subscription = Profile.Context.Subscription;

                // Create a temporary context
                ServerDataServiceCertAuth context =
                    ServerDataServiceCertAuth.Create(this.ServerName, Profile, subscription);

                GetClientRequestId = () => context.ClientRequestId;

                // Remove the database with the specified name
                Services.Server.Database database = context.UpdateDatabase(
                    databaseName,
                    this.NewDatabaseName,
                    maxSizeGb,
                    maxSizeBytes,
                    edition,
                    this.ServiceObjective);

                if (this.Sync.IsPresent)
                {
                    // Wait for the operation to complete on the server.
                    database = CmdletCommon.WaitForDatabaseOperation(this, context, database, this.DatabaseName, false);
                }

                // Update the passed in database object
                if (this.MyInvocation.BoundParameters.ContainsKey("Database"))
                {
                    this.Database.CopyFields(database);
                    database = this.Database;
                }

                // If PassThru was specified, write back the updated object to the pipeline
                if (this.PassThru.IsPresent)
                {
                    this.WriteObject(database);
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    GetClientRequestId(),
                    ex);
            }
        }
        /// <summary>
        /// process the request using the connection context.
        /// </summary>
        /// <param name="databaseName">the name of the database to alter</param>
        /// <param name="maxSizeGb">the new maximum size for the database</param>
        /// <param name="maxSizeBytes"></param>
        /// <param name="edition">the new edition for the database</param>
        private void ProcessWithConnectionContext(string databaseName, int?maxSizeGb, long?maxSizeBytes, DatabaseEdition?edition)
        {
            try
            {
                // Update the database with the specified name
                Services.Server.Database database = this.ConnectionContext.UpdateDatabase(
                    databaseName,
                    this.NewDatabaseName,
                    maxSizeGb,
                    maxSizeBytes,
                    edition,
                    this.ServiceObjective);

                if (this.Sync.IsPresent)
                {
                    // Wait for the operation to complete on the server.
                    database = CmdletCommon.WaitForDatabaseOperation(this, this.ConnectionContext, database, this.DatabaseName, false);
                }

                // If PassThru was specified, write back the updated object to the pipeline
                if (this.PassThru.IsPresent)
                {
                    this.WriteObject(database);
                }

                if (this.ConnectionContext.GetType() == typeof(ServerDataServiceCertAuth))
                {
                    if (this.MyInvocation.BoundParameters.ContainsKey("Database"))
                    {
                        this.Database.CopyFields(database);
                    }
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    this.ConnectionContext.ClientRequestId,
                    ex);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Process the request using the connection context.
        /// </summary>
        /// <param name="maxSizeGb">the maximum size for the new database</param>
        /// <param name="maxSizeBytes"></param>
        private void ProcessWithConnectionContext(int?maxSizeGb, long?maxSizeBytes)
        {
            try
            {
                Services.Server.Database database = this.ConnectionContext.CreateNewDatabase(
                    this.DatabaseName,
                    maxSizeGb,
                    maxSizeBytes,
                    this.Collation,
                    this.Edition,
                    this.ServiceObjective);

                database = CmdletCommon.WaitForDatabaseOperation(this, this.ConnectionContext, database, this.DatabaseName, true);

                this.WriteObject(database, true);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    this.ConnectionContext.ClientRequestId,
                    ex);
            }
        }
        /// <summary>
        /// Process the request using the provided connection context
        /// </summary>
        /// <param name="context">The connection context</param>
        private void ProcessWithContext(IServerDataServiceContext context)
        {
            // This is to enforce the mutual exclusivity of the parameters: Database
            // and DatabaseName.  This can't be done with parameter sets without changing
            // existing behaviour of the cmdlet.
            if (
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                this.WriteError(new ErrorRecord(
                                    new PSArgumentException(
                                        string.Format(Resources.InvalidParameterCombination, DatabaseParameter, DatabaseNameParameter)),
                                    string.Empty,
                                    ErrorCategory.InvalidArgument,
                                    null));
            }

            // ... and similarly for RestorableDroppedDatabase and DatabaseName / DatabaseDeletionDate
            if (
                this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                this.WriteError(new ErrorRecord(
                                    new PSArgumentException(
                                        string.Format(Resources.InvalidParameterCombination, RestorableDroppedDatabaseParameter, DatabaseNameParameter)),
                                    string.Empty,
                                    ErrorCategory.InvalidArgument,
                                    null));
            }
            if (
                this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                this.WriteError(new ErrorRecord(
                                    new PSArgumentException(
                                        string.Format(Resources.InvalidParameterCombination, RestorableDroppedDatabaseParameter, DatabaseDeletionDateParameter)),
                                    string.Empty,
                                    ErrorCategory.InvalidArgument,
                                    null));
            }

            // The DatabaseDeletionDate parameter can only be used if the RestorableDropped switch is also present
            if (!this.RestorableDropped.IsPresent && this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                throw new PSArgumentException(
                          string.Format(Resources.RestorableDroppedSwitchNotSpecified, DatabaseDeletionDateParameter));
            }

            // The Database parameter can only be used if the RestorableDropped switch is not present
            if (this.RestorableDropped.IsPresent && this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter))
            {
                throw new PSArgumentException(
                          string.Format(Resources.InvalidParameterCombination, RestorableDroppedParameter, DatabaseParameter));
            }

            // If the RestorableDropped switch is present, then either both the DatabaseName and DatabaseDeletionDate parameters must be present, or neither of them should be present.
            if (
                this.RestorableDropped.IsPresent && (
                    (this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter) && !this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter)) ||
                    (!this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter) && this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))))
            {
                throw new PSArgumentException(Resources.BothDatabaseNameAndDeletionDateNeedToBeSpecified);
            }

            // Obtain the database name from the given parameters.
            string databaseName = null;

            if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter))
            {
                databaseName = this.Database.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter))
            {
                databaseName = this.RestorableDroppedDatabase.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                databaseName = this.DatabaseName;
            }

            DateTime databaseDeletionDate = default(DateTime);

            if (this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter))
            {
                databaseDeletionDate = this.RestorableDroppedDatabase.DeletionDate;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                databaseDeletionDate = this.DatabaseDeletionDate;
            }
            databaseDeletionDate = CmdletCommon.NormalizeToUtc(databaseDeletionDate);

            try
            {
                if (!this.RestorableDropped.IsPresent && this.RestorableDroppedDatabase == null)
                {
                    // Live databases

                    if (databaseName != null)
                    {
                        // Retrieve the database with the specified name
                        this.WriteObject(context.GetDatabase(databaseName), true);
                    }
                    else
                    {
                        // No name specified, retrieve all databases in the server
                        this.WriteObject(context.GetDatabases(), true);
                    }
                }

                else
                {
                    // Dropped databases

                    if (databaseName != null)
                    {
                        // Retrieve the database with the specified name
                        this.WriteObject(context.GetRestorableDroppedDatabase(databaseName, databaseDeletionDate), true);
                    }
                    else
                    {
                        // No name specified, retrieve all databases in the server
                        this.WriteObject(context.GetRestorableDroppedDatabases(), true);
                    }
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    context.ClientRequestId,
                    ex);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Execute the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            // Obtain the name of the source database / source restorable dropped database from the parameters
            this.SourceDatabaseName =
                this.SourceDatabase != null ? this.SourceDatabase.Name :
                this.SourceRestorableDroppedDatabase != null ? this.SourceRestorableDroppedDatabase.Name :
                this.SourceDatabaseName;

            // Obtain the deletion date of the source restorable dropped database from the parameters
            DateTime?sourceDatabaseDeletionDate = null;

            if (this.SourceRestorableDroppedDatabase != null)
            {
                sourceDatabaseDeletionDate = this.SourceRestorableDroppedDatabase.DeletionDate;
            }
            else if (this.RestorableDropped.IsPresent)
            {
                sourceDatabaseDeletionDate = this.SourceDatabaseDeletionDate;
            }

            // Normalize the deletion date and point in time to UTC, if given
            if (sourceDatabaseDeletionDate != null)
            {
                sourceDatabaseDeletionDate = CmdletCommon.NormalizeToUtc(sourceDatabaseDeletionDate.Value);
            }

            if (this.PointInTime != null)
            {
                this.PointInTime = CmdletCommon.NormalizeToUtc(this.PointInTime.Value);
            }

            IServerDataServiceContext connectionContext = null;

            // If a database object was piped in, use its connection context...
            if (this.SourceDatabase != null)
            {
                connectionContext = this.SourceDatabase.Context;
            }
            else if (this.SourceRestorableDroppedDatabase != null)
            {
                connectionContext = this.SourceRestorableDroppedDatabase.Context;
            }

            // ... but only if it's a cert auth context. Otherwise, create a cert auth context using this.ServerName or the server name of the SQL auth context.
            if (!(connectionContext is ServerDataServiceCertAuth))
            {
                string serverName = this.SourceServerName ?? connectionContext.ServerName;

                connectionContext = ServerDataServiceCertAuth.Create(serverName, Profile, Profile.Context.Subscription);
            }

            string clientRequestId = connectionContext.ClientRequestId;

            try
            {
                RestoreDatabaseOperation operation = connectionContext.RestoreDatabase(
                    this.SourceDatabaseName,
                    sourceDatabaseDeletionDate,
                    this.TargetServerName,
                    this.TargetDatabaseName,
                    this.PointInTime);

                this.WriteObject(operation);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    clientRequestId,
                    ex);
            }
        }