Beispiel #1
0
        /// <summary>
        /// Process the record with the provided server name
        /// </summary>
        /// <param name="databaseName">The name of the database to retrieve</param>
        private void ProcessWithServerName(string databaseName)
        {
            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;

                if (databaseName != null)
                {
                    // Retrieve the database with the specified name
                    this.WriteObject(context.GetDatabase(databaseName));
                }
                else
                {
                    // No name specified, retrieve all databases in the server
                    this.WriteObject(context.GetDatabases());
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    GetClientRequestId(),
                    ex);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Process the request using the server name
        /// </summary>
        /// <param name="maxSizeGb">the maximum size of the database</param>
        private void ProcessWithServerName(int?maxSizeGb)
        {
            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;

                // Retrieve the database with the specified name
                this.WriteObject(context.CreateNewDatabase(
                                     this.DatabaseName,
                                     maxSizeGb,
                                     this.Collation,
                                     this.Edition,
                                     this.ServiceObjective));
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    GetClientRequestId(),
                    ex);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Process the request using a temporary connection context.
        /// </summary>
        /// <param name="databaseName">The name of the database to remove</param>
        private void ProcessWithServerName(string databaseName)
        {
            string clientRequestId = string.Empty;

            try
            {
                // Get the current subscription data.
                SubscriptionData subscriptionData = this.GetCurrentSubscription();

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

                clientRequestId = context.ClientRequestId;

                // Remove the database with the specified name
                context.RemoveDatabase(databaseName);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    clientRequestId,
                    ex);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Process the record with the provided server name
        /// </summary>
        /// <param name="databaseName">The name of the database to retrieve</param>
        private void ProcessWithServerName(string databaseName)
        {
            string clientRequestId = string.Empty;

            try
            {
                // Get the current subscription data.
                SubscriptionData subscriptionData = this.GetCurrentSubscription();

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

                clientRequestId = context.ClientRequestId;

                if (databaseName != null)
                {
                    // Retrieve the database with the specified name
                    this.WriteObject(context.GetDatabase(databaseName));
                }
                else
                {
                    // No name specified, retrieve all databases in the server
                    this.WriteObject(context.GetDatabases());
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    clientRequestId,
                    ex);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Process the request using a temporary connection context.
        /// </summary>
        /// <param name="databaseName">The name of the database to remove</param>
        private void ProcessWithServerName(string databaseName)
        {
            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;

                // Remove the database with the specified name
                context.RemoveDatabase(databaseName);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    GetClientRequestId(),
                    ex);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Process the request using the server name
        /// </summary>
        /// <param name="maxSizeGb">the maximum size of the database</param>
        private void ProcessWithServerName(int?maxSizeGb)
        {
            string clientRequestId = string.Empty;

            try
            {
                // Get the current subscription data.
                SubscriptionData subscriptionData = this.GetCurrentSubscription();

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

                clientRequestId = context.ClientRequestId;

                // Retrieve the database with the specified name
                this.WriteObject(context.CreateNewDatabase(
                                     this.DatabaseName,
                                     maxSizeGb,
                                     this.Collation,
                                     this.Edition));
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    clientRequestId,
                    ex);
            }
        }
Beispiel #7
0
        public void NewAzureSqlDatabaseWithCertAuth()
        {
            SimpleSqlDatabaseManagement channel = new SimpleSqlDatabaseManagement();

            channel.NewDatabaseThunk = ar =>
            {
                Assert.AreEqual(
                    ((SqlDatabaseInput)ar.Values["input"]).Name,
                    "UnitTestNewDatabase",
                    "The database Name input parameter does not match");
                Assert.AreEqual(
                    ((SqlDatabaseInput)ar.Values["input"]).MaxSizeGB,
                    "1",
                    "The database MaxSizeGB input parameter does not match");
                Assert.AreEqual(
                    ((SqlDatabaseInput)ar.Values["input"]).CollationName,
                    "Japanese_CI_AS",
                    "The database CollationName input parameter does not match");
                Assert.AreEqual(
                    ((SqlDatabaseInput)ar.Values["input"]).Edition,
                    "Web",
                    "The database Edition input parameter does not match");

                SqlDatabaseResponse operationResult = new SqlDatabaseResponse();
                operationResult.CollationName    = "Japanese_CI_AS";
                operationResult.Edition          = "Web";
                operationResult.Id               = "1";
                operationResult.MaxSizeGB        = "1";
                operationResult.Name             = "TestDatabaseName";
                operationResult.CreationDate     = DateTime.Now.ToString();
                operationResult.IsFederationRoot = true.ToString();
                operationResult.IsSystemObject   = true.ToString();
                operationResult.MaxSizeBytes     = "1073741824";

                return(operationResult);
            };

            SubscriptionData subscriptionData = UnitTestHelper.CreateUnitTestSubscription();

            subscriptionData.ServiceEndpoint = MockHttpServer.DefaultHttpsServerPrefixUri.AbsoluteUri;

            NewAzureSqlDatabaseServerContext contextCmdlet = new NewAzureSqlDatabaseServerContext();

            ServerDataServiceCertAuth service =
                contextCmdlet.GetServerDataServiceByCertAuth("TestServer", subscriptionData);

            service.Channel = channel;

            Database result =
                service.CreateNewDatabase("UnitTestNewDatabase", 1, "Japanese_CI_AS", DatabaseEdition.Web);

            // Verify that the result matches the stuff in the thunk.
            Assert.AreEqual(result.CollationName, "Japanese_CI_AS", "The collation does not match");
            Assert.AreEqual(result.Edition, DatabaseEdition.Web.ToString(), "The edition does not match");
            Assert.AreEqual(result.MaxSizeGB, 1, "The max db size does not match");
            Assert.AreEqual(result.Name, "TestDatabaseName", "The name does not match");
        }
        public void NewAzureSqlDatabaseServerContextWithCertAuth()
        {
            AzureSubscription subscription = UnitTestHelper.CreateUnitTestSubscription();

            NewAzureSqlDatabaseServerContext serverContext = new NewAzureSqlDatabaseServerContext();
            ServerDataServiceCertAuth        service       = serverContext.GetServerDataServiceByCertAuth(
                "testServer",
                subscription);

            Assert.IsNotNull(service, "The ServerDataServiceCertAuth object returned from "
                             + "NewAzureSqlDatabaseServerContext.GetServerDataServiceByCertAuth is null");
        }
        /// <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);
            }
        }
Beispiel #10
0
        public void NewAzureSqlDatabaseServerContextWithCertAuth()
        {
            WindowsAzureSubscription subscription = UnitTestHelper.CreateUnitTestSubscription();

            subscription.ServiceEndpoint = new Uri(MockHttpServer.DefaultHttpsServerPrefixUri.AbsoluteUri);

            NewAzureSqlDatabaseServerContext serverContext = new NewAzureSqlDatabaseServerContext();
            ServerDataServiceCertAuth        service       = serverContext.GetServerDataServiceByCertAuth(
                "testServer",
                subscription);

            Assert.IsNotNull(service, "The ServerDataServiceCertAuth object returned from "
                             + "NewAzureSqlDatabaseServerContext.GetServerDataServiceByCertAuth is null");
        }
Beispiel #11
0
        public void GetAzureSqlDatabaseWithCertAuth()
        {
            SimpleSqlDatabaseManagement channel = new SimpleSqlDatabaseManagement();

            channel.GetDatabaseThunk = ar =>
            {
                Assert.AreEqual(
                    ar.Values["databaseName"],
                    "testdb1",
                    "The input databaseName did not match the expected");

                SqlDatabaseResponse db1 = new SqlDatabaseResponse();
                db1.CollationName    = "Japanese_CI_AS";
                db1.Edition          = "Web";
                db1.Id               = "1";
                db1.MaxSizeGB        = "1";
                db1.Name             = "testdb1";
                db1.CreationDate     = DateTime.Now.ToString();
                db1.IsFederationRoot = true.ToString();
                db1.IsSystemObject   = true.ToString();
                db1.MaxSizeBytes     = "1073741824";

                return(db1);
            };

            SubscriptionData subscriptionData = UnitTestHelper.CreateUnitTestSubscription();

            subscriptionData.ServiceEndpoint =
                MockHttpServer.DefaultHttpsServerPrefixUri.AbsoluteUri;

            NewAzureSqlDatabaseServerContext contextCmdlet =
                new NewAzureSqlDatabaseServerContext();

            ServerDataServiceCertAuth service =
                contextCmdlet.GetServerDataServiceByCertAuth("TestServer", subscriptionData);

            service.Channel = channel;

            Database database = service.GetDatabase("testdb1");

            Assert.AreEqual("testdb1", database.Name, "Expected db name to be testdb1");

            Assert.AreEqual(
                "Japanese_CI_AS",
                database.CollationName,
                "Expected collation to be Japanese_CI_AS");
            Assert.AreEqual("Web", database.Edition, "Expected edition to be Web");
            Assert.AreEqual(1, database.MaxSizeGB, "Expected max size to be 1 GB");
        }
Beispiel #12
0
        /// <summary>
        /// Process the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            // Obtain the source server and database name from the given parameters.
            var sourceServerName =
                this.SourceDatabase != null ? this.SourceDatabase.ServerName :
                this.SourceServerName ??
                this.TargetServerName;

            var sourceDatabaseName =
                this.SourceDatabase != null ? this.SourceDatabase.Name :
                this.SourceDatabaseName;

            IServerDataServiceContext connectionContext = null;

            // If a database object was piped in, use its connection context...
            if (this.SourceDatabase != null)
            {
                connectionContext = this.SourceDatabase.Context;
            }
            else
            {
                // ... else create a temporary context
                connectionContext = ServerDataServiceCertAuth.Create(this.TargetServerName, WindowsAzureProfile.Instance.CurrentSubscription);
            }

            string clientRequestId = connectionContext.ClientRequestId;

            try
            {
                if (sourceDatabaseName != null)
                {
                    // Retrieve the database with the specified name and deletion date
                    this.WriteObject(connectionContext.GetRecoverableDatabase(sourceServerName, sourceDatabaseName));
                }
                else
                {
                    // No name specified, retrieve all restorable dropped databases in the server
                    this.WriteObject(connectionContext.GetRecoverableDatabases(sourceServerName), true);
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    clientRequestId,
                    ex);
            }
        }
Beispiel #13
0
        /// <summary>
        /// Execute the command.
        /// </summary>
        protected override void ProcessRecord()
        {
            IServerDataServiceContext context = null;

            switch (this.ParameterSetName)
            {
            case ByConnectionContext:
                context = this.Context;
                break;

            case ByServerName:
                context = ServerDataServiceCertAuth.Create(this.ServerName, WindowsAzureProfile.Instance.CurrentSubscription);
                break;
            }
            ProcessWithContext(context);
        }
Beispiel #14
0
        /// <summary>
        /// Execute the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            this.SourceDatabaseName =
                this.SourceDatabase != null ? this.SourceDatabase.Name :
                this.SourceDatabaseName;

            this.SourceServerName =
                this.SourceDatabase != null ? this.SourceDatabase.ServerName :
                this.SourceServerName;

            this.TargetDatabaseName = this.TargetDatabaseName ?? this.SourceDatabaseName;

            // Get the current subscription data
            var subscription = WindowsAzureProfile.Instance.CurrentSubscription;

            IServerDataServiceContext connectionContext = null;

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

            string clientRequestId = connectionContext.ClientRequestId;

            try
            {
                RecoverDatabaseOperation operation = connectionContext.RecoverDatabase(
                    this.SourceServerName,
                    this.SourceDatabaseName,
                    this.TargetDatabaseName);

                this.WriteObject(operation);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    clientRequestId,
                    ex);
            }
        }
        /// <summary>
        /// Process the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            ParameterValidation();
            IServerDataServiceContext context = null;

            switch (this.ParameterSetName)
            {
            case ByConnectionContext:
                context = this.ConnectionContext;
                break;

            case ByServerName:
                context = ServerDataServiceCertAuth.Create(this.ServerName, Profile, Profile.Context.Subscription);
                break;
            }
            ProcessWithContext(context);
        }
        /// <summary>
        /// Execute the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            // Obtain the database name from the given parameters.
            string databaseName = null;

            if (this.MyInvocation.BoundParameters.ContainsKey("Database"))
            {
                databaseName = this.Database.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey("DatabaseName"))
            {
                databaseName = this.DatabaseName;
            }

            // Use the provided ServerDataServiceContext or create one from the
            // provided ServerName and the active subscription.
            IServerDataServiceContext context = ServerDataServiceCertAuth.Create(this.ServerName,
                                                                                 Profile,
                                                                                 Profile.Context.Subscription);

            try
            {
                if (this.MyInvocation.BoundParameters.ContainsKey("DatabaseCopy"))
                {
                    // Refresh the specified database copy object
                    this.WriteObject(context.GetDatabaseCopy(this.DatabaseCopy), true);
                }
                else
                {
                    // Retrieve all database copy object with matching parameters
                    DatabaseCopyModel[] copies = context.GetDatabaseCopy(
                        databaseName,
                        this.PartnerServer,
                        this.PartnerDatabase);
                    this.WriteObject(copies, true);
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    context.ClientRequestId,
                    ex);
            }
        }
Beispiel #17
0
        /// <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="edition">the new edition for the database or null</param>
        private void ProcessWithServerName(string databaseName, int?maxSizeGb, DatabaseEdition?edition)
        {
            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;

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

                // 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);
            }
        }
Beispiel #18
0
        /// <summary>
        /// Connect to Azure SQL Server using certificate authentication.
        /// </summary>
        /// <param name="serverName">The name of the server to connect to</param>
        /// <param name="subscriptionData">The subscription data to use for authentication</param>
        /// <returns>A new <see cref="ServerDataServiceCertAuth"/> context,
        /// or <c>null</c> if an error occurred.</returns>
        internal ServerDataServiceCertAuth GetServerDataServiceByCertAuth(
            string serverName,
            SubscriptionData subscriptionData)
        {
            ServerDataServiceCertAuth context = null;

            try
            {
                context = ServerDataServiceCertAuth.Create(serverName, subscriptionData);
            }
            catch (ArgumentException e)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(this, string.Empty, e);

                context = null;
            }

            return(context);
        }
        /// <summary>
        /// Execute the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            IServerDataServiceContext context = null;

            switch (this.ParameterSetName)
            {
            case ByConnectionContext:
                context = this.Context;
                break;

            case ByServerName:
                context = ServerDataServiceCertAuth.Create(this.ServerName, WindowsAzureProfile.Instance.CurrentSubscription);
                break;

            default:
                throw new InvalidPowerShellStateException("Unrecognized parameter set name used.");
            }
            ProcessWithContext(context);
        }
Beispiel #20
0
        /// <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="edition">the new edition for the database or null</param>
        private void ProcessWithServerName(string databaseName, int?maxSizeGb, DatabaseEdition?edition)
        {
            string clientRequestId = null;

            try
            {
                // Get the current subscription data.
                SubscriptionData subscriptionData = this.GetCurrentSubscription();

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

                clientRequestId = context.ClientRequestId;

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

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

                if (this.MyInvocation.BoundParameters.ContainsKey("Database"))
                {
                    this.Database.CopyFields(database);
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    clientRequestId,
                    ex);
            }
        }
        /// <summary>
        /// Process the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            IServerDataServiceContext context = null;

            switch (this.ParameterSetName)
            {
            case ByConnectionContext:
                context = this.ConnectionContext;
                break;

            case ByServerName:
                context = ServerDataServiceCertAuth.Create(this.ServerName, AzureSession.CurrentContext.Subscription);
                break;

            default:
                throw new NotSupportedException("ParameterSet");
            }

            ProcessWithContext(context);
        }
Beispiel #22
0
        /// <summary>
        /// Connect to Azure SQL Server using certificate authentication.
        /// </summary>
        /// <param name="serverName">The name of the server to connect to</param>
        /// <param name="subscription">The subscription data to use for authentication</param>
        /// <returns>A new <see cref="ServerDataServiceCertAuth"/> context,
        /// or <c>null</c> if an error occurred.</returns>
        internal ServerDataServiceCertAuth GetServerDataServiceByCertAuth(
            string serverName,
            IAzureSubscription subscription)
        {
            ServerDataServiceCertAuth context = null;

            SqlDatabaseCmdletBase.ValidateSubscription(subscription);

            try
            {
                context = ServerDataServiceCertAuth.Create(serverName, Profile, subscription);
            }
            catch (ArgumentException e)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(this, string.Empty, e);

                context = null;
            }

            return(context);
        }
Beispiel #23
0
        /// <summary>
        /// Process the request using the server name
        /// </summary>
        /// <param name="maxSizeGb">the maximum size of the database</param>
        /// <param name="maxSizeBytes"></param>
        private void ProcessWithServerName(int?maxSizeGb, long?maxSizeBytes)
        {
            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;

                Services.Server.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);
            }
        }
Beispiel #24
0
        public void GetAzureSqlDatabasesWithCertAuth()
        {
            SimpleSqlDatabaseManagement channel = new SimpleSqlDatabaseManagement();

            channel.GetDatabasesThunk = ar =>
            {
                List <SqlDatabaseResponse> databases = new List <SqlDatabaseResponse>();

                SqlDatabaseResponse db1 = new SqlDatabaseResponse();
                db1.CollationName    = "Japanese_CI_AS";
                db1.Edition          = "Web";
                db1.Id               = "1";
                db1.MaxSizeGB        = "1";
                db1.Name             = "testdb1";
                db1.CreationDate     = DateTime.Now.ToString();
                db1.IsFederationRoot = true.ToString();
                db1.IsSystemObject   = true.ToString();
                db1.MaxSizeBytes     = "1073741824";
                databases.Add(db1);

                SqlDatabaseResponse db2 = new SqlDatabaseResponse();
                db2.CollationName    = "Japanese_CI_AS";
                db2.Edition          = "Business";
                db2.Id               = "2";
                db2.MaxSizeGB        = "10";
                db2.Name             = "testdb2";
                db2.CreationDate     = DateTime.Now.ToString();
                db2.IsFederationRoot = true.ToString();
                db2.IsSystemObject   = true.ToString();
                db2.MaxSizeBytes     = "10737418240";
                databases.Add(db2);

                SqlDatabaseList operationResult = new SqlDatabaseList(databases);

                return(operationResult);
            };

            SubscriptionData subscriptionData = UnitTestHelper.CreateUnitTestSubscription();

            subscriptionData.ServiceEndpoint =
                MockHttpServer.DefaultHttpsServerPrefixUri.AbsoluteUri;

            NewAzureSqlDatabaseServerContext contextCmdlet =
                new NewAzureSqlDatabaseServerContext();

            ServerDataServiceCertAuth service =
                contextCmdlet.GetServerDataServiceByCertAuth("TestServer", subscriptionData);

            service.Channel = channel;

            Database[] results = service.GetDatabases();

            // Expecting master, testdb1, testdb2
            Assert.AreEqual(2, results.Length, "Expecting two Database objects");

            Database database1Obj = results[0];

            Assert.AreEqual("testdb1", database1Obj.Name, "Expected db name to be testdb1");

            Database database2Obj = results[1];

            Assert.AreEqual("testdb2", database2Obj.Name, "Expected db name to be testdb2");
            Assert.AreEqual(
                "Japanese_CI_AS",
                database2Obj.CollationName,
                "Expected collation to be Japanese_CI_AS");
            Assert.AreEqual("Business", database2Obj.Edition, "Expected edition to be Business");
            Assert.AreEqual(10, database2Obj.MaxSizeGB, "Expected max size to be 10 GB");
        }
Beispiel #25
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);
            }
        }
Beispiel #26
0
 /// <summary>
 /// Process the request using the provided connection context
 /// </summary>
 /// <param name="context">The connection context</param>
 private void ProcessWithContext(ServerDataServiceCertAuth context)
 {
     this.WriteObject(context.GetDatabaseUsages(this.DatabaseName), true);
 }
Beispiel #27
0
        /// <summary>
        /// Process the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            var context = ServerDataServiceCertAuth.Create(this.ServerName, Profile, Profile.Context.Subscription);

            ProcessWithContext(context);
        }
        /// <summary>
        /// Execute the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            // Obtain the database name from the given parameters.
            string databaseName = null;

            if (this.MyInvocation.BoundParameters.ContainsKey("Database"))
            {
                databaseName = this.Database.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey("DatabaseName"))
            {
                databaseName = this.DatabaseName;
            }

            string partnerServerName   = this.PartnerServer;
            string partnerDatabaseName = this.PartnerDatabase;

            if (this.ContinuousCopy.IsPresent)
            {
                // Default partnerDatabaseName to the only allowed value for continuous copies.
                partnerDatabaseName = partnerDatabaseName ?? databaseName;
            }
            else
            {
                // Default partnerServerName to the only allowed value for normal copies.
                partnerServerName = partnerServerName ?? this.ServerName;
            }

            // Do nothing if force is not specified and user cancelled the operation
            string actionDescription = string.Format(
                CultureInfo.InvariantCulture,
                Resources.StartAzureSqlDatabaseCopyDescription,
                this.ServerName,
                databaseName,
                partnerServerName,
                partnerDatabaseName);
            string actionWarning = string.Format(
                CultureInfo.InvariantCulture,
                Resources.StartAzureSqlDatabaseCopyWarning,
                this.ServerName,
                databaseName,
                partnerServerName,
                partnerDatabaseName);

            this.WriteVerbose(actionDescription);
            if (!this.Force.IsPresent &&
                !this.ShouldProcess(
                    actionDescription,
                    actionWarning,
                    Resources.ShouldProcessCaption))
            {
                return;
            }

            // Use the provided ServerDataServiceContext or create one from the
            // provided ServerName and the active subscription.
            IServerDataServiceContext context = ServerDataServiceCertAuth.Create(this.ServerName,
                                                                                 WindowsAzureProfile.Instance.CurrentSubscription);

            try
            {
                // Update the database with the specified name
                DatabaseCopyModel databaseCopy = context.StartDatabaseCopy(
                    databaseName,
                    partnerServerName,
                    partnerDatabaseName,
                    this.ContinuousCopy.IsPresent,
                    this.OfflineSecondary.IsPresent);

                this.WriteObject(databaseCopy, true);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    context.ClientRequestId,
                    ex);
            }
        }
Beispiel #29
0
        public void SetAzureSqlDatabaseNameWithCertAuth()
        {
            SimpleSqlDatabaseManagement channel = new SimpleSqlDatabaseManagement();

            // This is needed because UpdateDatabases calls GetDatabases in order to
            // get the necessary database information for the delete.
            channel.GetDatabaseThunk = ar =>
            {
                Assert.AreEqual(
                    ar.Values["databaseName"],
                    "testdb1",
                    "The input databaseName (for get) did not match the expected");

                SqlDatabaseResponse db1 = new SqlDatabaseResponse();
                db1.CollationName    = "Japanese_CI_AS";
                db1.Edition          = "Web";
                db1.Id               = "1";
                db1.MaxSizeGB        = "1";
                db1.Name             = "testdb1";
                db1.CreationDate     = DateTime.Now.ToString();
                db1.IsFederationRoot = true.ToString();
                db1.IsSystemObject   = true.ToString();
                db1.MaxSizeBytes     = "1073741824";

                return(db1);
            };

            channel.UpdateDatabaseThunk = ar =>
            {
                Assert.AreEqual(
                    "testdb1",
                    ar.Values["databaseName"],
                    "The input databaseName (for update) did not match the expected");

                Assert.AreEqual(
                    "newTestDb1",
                    ((SqlDatabaseInput)ar.Values["input"]).Name,
                    "The database Name input parameter does not match");
                Assert.AreEqual(
                    "1",
                    ((SqlDatabaseInput)ar.Values["input"]).MaxSizeGB,
                    "The database MaxSizeGB input parameter does not match");
                Assert.AreEqual(
                    "Japanese_CI_AS",
                    ((SqlDatabaseInput)ar.Values["input"]).CollationName,
                    "The database CollationName input parameter does not match");
                Assert.AreEqual(
                    "Web",
                    ((SqlDatabaseInput)ar.Values["input"]).Edition,
                    "The database Edition input parameter does not match");

                SqlDatabaseResponse response = new SqlDatabaseResponse();
                response.CollationName    = ((SqlDatabaseInput)ar.Values["input"]).CollationName;
                response.CreationDate     = DateTime.Now.ToString();
                response.MaxSizeBytes     = "1073741824";
                response.Edition          = ((SqlDatabaseInput)ar.Values["input"]).Edition.ToString();
                response.Id               = ((SqlDatabaseInput)ar.Values["input"]).Id;
                response.IsFederationRoot = true.ToString();
                response.IsSystemObject   = true.ToString();
                response.MaxSizeGB        = ((SqlDatabaseInput)ar.Values["input"]).MaxSizeGB.ToString();
                response.Name             = ((SqlDatabaseInput)ar.Values["input"]).Name;

                return(response);
            };

            SubscriptionData subscriptionData = UnitTestHelper.CreateUnitTestSubscription();

            subscriptionData.ServiceEndpoint = MockHttpServer.DefaultHttpsServerPrefixUri.AbsoluteUri;

            NewAzureSqlDatabaseServerContext contextCmdlet = new NewAzureSqlDatabaseServerContext();

            ServerDataServiceCertAuth service =
                contextCmdlet.GetServerDataServiceByCertAuth("TestServer", subscriptionData);

            service.Channel = channel;

            Database database = service.UpdateDatabase("testdb1", "newTestDb1", null, null, null);

            Assert.AreEqual(
                database.CollationName,
                "Japanese_CI_AS",
                "The updated database collation name is wrong");
            Assert.AreEqual(
                database.Edition,
                "Web",
                "The updated database Edition is wrong");
            Assert.AreEqual(
                database.MaxSizeGB,
                1,
                "The updated database Edition is wrong");
            Assert.AreEqual(
                database.Name,
                "newTestDb1",
                "The updated database Edition is wrong");
        }
        /// <summary>
        /// Execute the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            // Obtain the database name from the given parameters.
            string databaseName = null;

            if (this.MyInvocation.BoundParameters.ContainsKey("Database"))
            {
                databaseName = this.Database.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey("DatabaseName"))
            {
                databaseName = this.DatabaseName;
            }

            // Use the provided ServerDataServiceContext or create one from the
            // provided ServerName and the active subscription.
            IServerDataServiceContext context = ServerDataServiceCertAuth.Create(this.ServerName,
                                                                                 WindowsAzureProfile.Instance.CurrentSubscription);

            DatabaseCopyModel databaseCopy;

            if (this.MyInvocation.BoundParameters.ContainsKey("DatabaseCopy"))
            {
                // Refreshes the given copy object
                databaseCopy = context.GetDatabaseCopy(this.DatabaseCopy);
            }
            else
            {
                // Retrieve all database copy object with matching parameters
                DatabaseCopyModel[] copies = context.GetDatabaseCopy(
                    databaseName,
                    this.PartnerServer,
                    this.PartnerDatabase);
                if (copies.Length == 0)
                {
                    throw new ApplicationException(Resources.DatabaseCopyNotFoundGeneric);
                }
                else if (copies.Length > 1)
                {
                    throw new ApplicationException(Resources.MoreThanOneDatabaseCopyFound);
                }

                databaseCopy = copies.Single();
            }

            // Do nothing if force is not specified and user cancelled the operation
            string actionDescription = string.Format(
                CultureInfo.InvariantCulture,
                Resources.StopAzureSqlDatabaseCopyDescription,
                databaseCopy.SourceServerName,
                databaseCopy.SourceDatabaseName,
                databaseCopy.DestinationServerName,
                databaseCopy.DestinationDatabaseName);
            string actionWarning = string.Format(
                CultureInfo.InvariantCulture,
                Resources.StopAzureSqlDatabaseCopyWarning,
                databaseCopy.SourceServerName,
                databaseCopy.SourceDatabaseName,
                databaseCopy.DestinationServerName,
                databaseCopy.DestinationDatabaseName);

            this.WriteVerbose(actionDescription);
            if (!this.Force.IsPresent &&
                !this.ShouldProcess(
                    actionDescription,
                    actionWarning,
                    Resources.ShouldProcessCaption))
            {
                return;
            }

            try
            {
                // Stop the specified database copy
                context.StopDatabaseCopy(
                    databaseCopy,
                    this.ForcedTermination.IsPresent);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    context.ClientRequestId,
                    ex);
            }
        }