Ejemplo n.º 1
0
        /// <summary>
        /// Execute the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            try
            {
                // Get the version from the command line
                float?version = null;
                if (this.MyInvocation.BoundParameters.ContainsKey("Version"))
                {
                    version = this.Version;
                }

                SqlDatabaseServerContext context = this.NewAzureSqlDatabaseServerProcess(
                    this.AdministratorLogin,
                    this.AdministratorLoginPassword,
                    this.Location,
                    version);

                if (context != null)
                {
                    WriteObject(context, true);
                }
            }
            catch (Exception ex)
            {
                this.WriteErrorDetails(ex);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Verifys the server object to make sure the fields match what is provided
 /// </summary>
 /// <param name="server">The server object to validate</param>
 /// <param name="adminLogin">The expected administration login</param>
 /// <param name="location">The expected server location</param>
 /// <param name="version">The expected server verions</param>
 /// <param name="state">The expected state of the server</param>
 private static void VerifyServer(SqlDatabaseServerContext server, string adminLogin, string location, string version, string state)
 {
     Assert.AreEqual(adminLogin, server.AdministratorLogin, "Expecting server login to match.");
     Assert.AreEqual(location, server.Location, "Expecting matching location.");
     Assert.AreEqual(10, server.ServerName.Length, "Expecting a valid server name.");
     Assert.AreEqual(version, server.Version, "Server version doesn't match");
     Assert.AreEqual(state, server.State, "Server state does not match");
 }
        /// <summary>
        /// Creates a new server in the current subscription.
        /// </summary>
        /// <param name="adminLogin">
        /// The administrator login name for the new server.
        /// </param>
        /// <param name="adminLoginPassword">
        /// The administrator login password for the new server.
        /// </param>
        /// <param name="location">
        /// The location in which to create the new server.
        /// </param>
        /// <returns>The context to the newly created server.</returns>
        internal SqlDatabaseServerContext NewAzureSqlDatabaseServerProcess(
            string adminLogin,
            string adminLoginPassword,
            string location,
            float?version)
        {
            // Do nothing if force is not specified and user cancelled the operation
            if (!Force.IsPresent &&
                !ShouldProcess(
                    Resources.NewAzureSqlDatabaseServerDescription,
                    Resources.NewAzureSqlDatabaseServerWarning,
                    Resources.ShouldProcessCaption))
            {
                return(null);
            }

            // Get the SQL management client for the current subscription
            SqlManagementClient sqlManagementClient = GetCurrentSqlClient();

            // Set the retry policty to not retry attempts.
            sqlManagementClient.SetRetryPolicy(new RetryPolicy(new DefaultHttpErrorDetectionStrategy(), 0));

            // Issue the create server request
            ServerCreateResponse response = sqlManagementClient.Servers.Create(
                new ServerCreateParameters()
            {
                Location = location,
                AdministratorUserName = adminLogin,
                AdministratorPassword = adminLoginPassword,
                Version = version.HasValue ? version.Value.ToString("F1", CultureInfo.InvariantCulture) : null
            });

            var newServer = sqlManagementClient.Servers.List().Servers.Where(s => s.Name == response.ServerName).FirstOrDefault();

            if (newServer == null)
            {
                throw new ItemNotFoundException(string.Format(
                                                    CultureInfo.InvariantCulture,
                                                    Resources.CreateServerServerNotFound,
                                                    response.ServerName));
            }

            SqlDatabaseServerContext operationContext = new SqlDatabaseServerContext()
            {
                OperationStatus      = Services.Constants.OperationSuccess,
                OperationDescription = CommandRuntime.ToString(),
                OperationId          = response.RequestId,
                ServerName           = newServer.Name,
                Location             = location,
                AdministratorLogin   = adminLogin,
                State   = newServer.State,
                Version = newServer.Version
            };

            return(operationContext);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new server in the current subscription.
        /// </summary>
        /// <param name="adminLogin">
        /// The administrator login name for the new server.
        /// </param>
        /// <param name="adminLoginPassword">
        /// The administrator login password for the new server.
        /// </param>
        /// <param name="location">
        /// The location in which to create the new server.
        /// </param>
        /// <returns>The context to the newly created server.</returns>
        internal SqlDatabaseServerContext NewAzureSqlDatabaseServerProcess(
            string adminLogin,
            string adminLoginPassword,
            string location,
            float?version)
        {
            // Do nothing if force is not specified and user cancelled the operation
            if (!Force.IsPresent &&
                !ShouldProcess(
                    Resources.NewAzureSqlDatabaseServerDescription,
                    Resources.NewAzureSqlDatabaseServerWarning,
                    Resources.ShouldProcessCaption))
            {
                return(null);
            }

            // Get the SQL management client for the current subscription
            SqlManagementClient sqlManagementClient = GetCurrentSqlClient();

            // Set the retry policty to not retry attempts.
            CloudExtensions.SetRetryPolicy <SqlManagementClient>(
                sqlManagementClient,
                new WindowsAzure.Common.TransientFaultHandling.RetryPolicy(new WindowsAzure.Common.TransientFaultHandling.DefaultHttpErrorDetectionStrategy(), 0));

            // Issue the create server request
            ServerCreateResponse response = sqlManagementClient.Servers.Create(
                new ServerCreateParameters()
            {
                Location = location,
                AdministratorUserName = adminLogin,
                AdministratorPassword = adminLoginPassword,
                Version = version.HasValue ? version.Value.ToString("F1") : null
            });

            SqlDatabaseServerContext operationContext = new SqlDatabaseServerContext()
            {
                OperationStatus      = Services.Constants.OperationSuccess,
                OperationDescription = CommandRuntime.ToString(),
                OperationId          = response.RequestId,
                ServerName           = response.ServerName,
                Location             = location,
                AdministratorLogin   = adminLogin,
            };

            return(operationContext);
        }
        /// <summary>
        /// Execute the command.
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                base.ProcessRecord();
                SqlDatabaseServerContext context = this.NewAzureSqlDatabaseServerProcess(this.AdministratorLogin, this.AdministratorLoginPassword, this.Location);

                if (context != null)
                {
                    WriteObject(context, true);
                }
            }
            catch (Exception ex)
            {
                SafeWriteError(new ErrorRecord(ex, string.Empty, ErrorCategory.CloseError, null));
            }
        }
        /// <summary>
        /// Execute the command.
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                base.ProcessRecord();
                SqlDatabaseServerContext context = this.NewAzureSqlDatabaseServerProcess(
                    this.AdministratorLogin,
                    this.AdministratorLoginPassword,
                    this.Location);

                if (context != null)
                {
                    WriteObject(context, true);
                }
            }
            catch (Exception ex)
            {
                this.WriteErrorDetails(ex);
            }
        }
        /// <summary>
        /// Creates a new server in the current subscription.
        /// </summary>
        /// <param name="adminLogin">
        /// The administrator login name for the new server.
        /// </param>
        /// <param name="adminLoginPassword">
        /// The administrator login password for the new server.
        /// </param>
        /// <param name="location">
        /// The location in which to create the new server.
        /// </param>
        /// <returns>The context to the newly created server.</returns>
        internal SqlDatabaseServerContext NewAzureSqlDatabaseServerProcess(string adminLogin, string adminLoginPassword, string location)
        {
            // Do nothing if force is not specified and user cancelled the operation
            if (!Force.IsPresent &&
                !ShouldProcess(
                    Resources.NewAzureSqlDatabaseServerDescription,
                    Resources.NewAzureSqlDatabaseServerWarning,
                    Resources.ShouldProcessCaption))
            {
                return(null);
            }

            SqlDatabaseServerContext operationContext = null;

            try
            {
                InvokeInOperationContext(() =>
                {
                    XmlElement serverName = RetryCall(subscription =>
                                                      Channel.NewServer(subscription, adminLogin, adminLoginPassword, location));
                    WAPPSCmdlet.Operation operation = WaitForSqlDatabaseOperation();

                    operationContext = new SqlDatabaseServerContext()
                    {
                        ServerName           = serverName.InnerText,
                        Location             = location,
                        AdministratorLogin   = adminLogin,
                        OperationStatus      = operation.Status,
                        OperationDescription = CommandRuntime.ToString(),
                        OperationId          = operation.OperationTrackingId
                    };
                });
            }
            catch (CommunicationException ex)
            {
                this.WriteErrorDetails(ex);
            }

            return(operationContext);
        }
        /// <summary>
        /// Creates a new server in the current subscription.
        /// </summary>
        /// <param name="adminLogin">
        /// The administrator login name for the new server.
        /// </param>
        /// <param name="adminLoginPassword">
        /// The administrator login password for the new server.
        /// </param>
        /// <param name="location">
        /// The location in which to create the new server.
        /// </param>
        /// <returns>The context to the newly created server.</returns>
        internal SqlDatabaseServerContext NewAzureSqlDatabaseServerProcess(
            string adminLogin,
            string adminLoginPassword,
            string location)
        {
            // Do nothing if force is not specified and user cancelled the operation
            if (!Force.IsPresent &&
                !ShouldProcess(
                    Resources.NewAzureSqlDatabaseServerDescription,
                    Resources.NewAzureSqlDatabaseServerWarning,
                    Resources.ShouldProcessCaption))
            {
                return(null);
            }

            // Get the SQL management client for the current subscription
            SqlManagementClient sqlManagementClient = SqlDatabaseCmdletBase.GetCurrentSqlClient();

            // Issue the create server request
            ServerCreateResponse response = sqlManagementClient.Servers.Create(
                new ServerCreateParameters()
            {
                Location = location,
                AdministratorUserName = adminLogin,
                AdministratorPassword = adminLoginPassword
            });

            SqlDatabaseServerContext operationContext = new SqlDatabaseServerContext()
            {
                OperationStatus      = Services.Constants.OperationSuccess,
                OperationDescription = CommandRuntime.ToString(),
                OperationId          = response.RequestId,
                ServerName           = response.ServerName,
                Location             = location,
                AdministratorLogin   = adminLogin,
            };

            return(operationContext);
        }
Ejemplo n.º 9
0
        public void AzureSqlDatabaseServerTests()
        {
            // This test uses the https endpoint, setup the certificates.
            MockHttpServer.SetupCertificates();

            SqlTestPsHost host = new SqlTestPsHost();
            SqlCustomPsHostUserInterface ui = host.UI as SqlCustomPsHostUserInterface;

            using (Runspace space = RunspaceFactory.CreateRunspace(host))
            {
                space.Open();

                using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
                {
                    powershell.Runspace = space;

                    // Setup the subscription used for the test
                    AzureSubscription subscription =
                        UnitTestHelper.SetupUnitTestSubscription(powershell);

                    // Create a new server
                    HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                        "UnitTest.AzureSqlDatabaseServerTests");
                    ServerTestHelper.SetDefaultTestSessionSettings(testSession);
                    testSession.RequestValidator =
                        new Action <HttpMessage, HttpMessage.Request>(
                            (expected, actual) =>
                    {
                        Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                        Assert.IsTrue(
                            actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                            "Missing proper UserAgent string.");
                    });

                    powershell.Runspace.SessionStateProxy.SetVariable("login", "mylogin");
                    powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd!");
                    powershell.Runspace.SessionStateProxy.SetVariable("location", "East Asia");
                    Collection <PSObject> newServerResult = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        return(powershell.InvokeBatchScript(
                                   @"New-AzureSqlDatabaseServer" +
                                   @" -AdministratorLogin $login" +
                                   @" -AdministratorLoginPassword $password" +
                                   @" -Location $location"));
                    });

                    ui.PromptInputs = new PSObject[] { "mylogin", "Pa$$w0rd", "East Asia" };
                    Collection <PSObject> newServerResult2 = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        return(powershell.InvokeBatchScript(@"New-AzureSqlDatabaseServer"));
                    });
                    ui.PromptInputs = null;

                    Collection <PSObject> getServerResult = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                        return(powershell.InvokeBatchScript(
                                   @"Get-AzureSqlDatabaseServer $server.ServerName"));
                    });

                    Collection <PSObject> setServerResult = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                        powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd2");
                        powershell.InvokeBatchScript(
                            @"$server | Set-AzureSqlDatabaseServer" +
                            @" -AdminPassword $password" +
                            @" -Force");
                        return(powershell.InvokeBatchScript(
                                   @"$server | Get-AzureSqlDatabaseServer"));
                    });

                    ui.PromptInputs = new PSObject[] { "Pa$$w0rd2" };
                    Collection <PSObject> setServerResult2 = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult2);
                        powershell.InvokeBatchScript(@"$server | Set-AzureSqlDatabaseServer");
                        return(powershell.InvokeBatchScript(@"$server | Get-AzureSqlDatabaseServer"));
                    });
                    ui.PromptInputs = null;

                    Collection <PSObject> removeServerResult = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                        powershell.InvokeBatchScript(
                            @"$server | Remove-AzureSqlDatabaseServer" +
                            @" -Force");

                        return(powershell.InvokeBatchScript(
                                   @"Get-AzureSqlDatabaseServer"));
                    });

                    ui.PromptInputs = new PSObject[] { ((SqlDatabaseServerContext)newServerResult2[0].BaseObject).ServerName };
                    ui.PromptForChoiceInputIndex = 0;   //answer yes to delete database prompt
                    Collection <PSObject> removeServerResult2 = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.InvokeBatchScript(@"Remove-AzureSqlDatabaseServer");

                        return(powershell.InvokeBatchScript(
                                   @"Get-AzureSqlDatabaseServer"));
                    });
                    ui.PromptForChoiceInputIndex = -1;
                    ui.PromptInputs = null;

                    Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!");
                    Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!");

                    // Validate New-AzureSqlDatabaseServer results
                    SqlDatabaseServerContext server =
                        newServerResult.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    SqlDatabaseServerContext server2 = newServerResult2.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server2, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    // Validate Get-AzureSqlDatabaseServer results
                    server = getServerResult.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    server = setServerResult.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    server2 = setServerResult2.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server2,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    // Validate Remove-AzureSqlDatabaseServer results
                    Assert.IsFalse(
                        removeServerResult.Any((o) => o.GetVariableValue <string>("ServerName") == server.ServerName),
                        "Server should have been removed.");

                    Assert.IsFalse(
                        removeServerResult2.Any((o) => o.GetVariableValue <string>("ServerName") == server2.ServerName),
                        "Server 2 should have been removed.");

                    powershell.Streams.ClearStreams();
                }

                space.Close();
            }
        }
Ejemplo n.º 10
0
        public void AzureSqlDatabaseServerV2Tests()
        {
            // This test uses the https endpoint, setup the certificates.
            MockHttpServer.SetupCertificates();

            using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
            {
                // Setup the subscription used for the test
                AzureSubscription subscription =
                    UnitTestHelper.SetupUnitTestSubscription(powershell);

                // Create a new V2 server
                HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.AzureSqlDatabaseServerV2Tests");
                ServerTestHelper.SetDefaultTestSessionSettings(testSession);
                testSession.RequestValidator =
                    new Action <HttpMessage, HttpMessage.Request>(
                        (expected, actual) =>
                {
                    Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                    Assert.IsTrue(
                        actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                        "Missing proper UserAgent string.");
                });

                powershell.Runspace.SessionStateProxy.SetVariable("login", "mylogin");
                powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd!");
                powershell.Runspace.SessionStateProxy.SetVariable("location", "East Asia");
                Collection <PSObject> newServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabaseServer" +
                               @" -AdministratorLogin $login" +
                               @" -AdministratorLoginPassword $password" +
                               @" -Location $location" +
                               @" -Version 2"));
                });

                Collection <PSObject> getServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServer $server.ServerName"));
                });

                Collection <PSObject> removeServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    powershell.InvokeBatchScript(
                        @"$server | Remove-AzureSqlDatabaseServer" +
                        @" -Force");

                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServer"));
                });

                Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!");
                Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!");

                // Validate New-AzureSqlDatabaseServer results
                SqlDatabaseServerContext server =
                    newServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                VerifyServer(
                    server,
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    ServerVersion2,
                    "Ready");


                // Validate Get-AzureSqlDatabaseServer results
                server = getServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                VerifyServer(
                    server,
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    ServerVersion2,
                    "Ready");

                powershell.Streams.ClearStreams();
            }
        }
Ejemplo n.º 11
0
 private static void VerifyServer(SqlDatabaseServerContext server, string adminLogin, string location, string version, string state)
 {
     VerifyServer(server, adminLogin, location);
     Assert.AreEqual(version, server.Version, "Server version doesn't match");
     Assert.AreEqual(state, server.State, "Server state does not match");
 }
        /// <summary>
        /// Creates a new server in the current subscription.
        /// </summary>
        /// <param name="adminLogin">
        /// The administrator login name for the new server.
        /// </param>
        /// <param name="adminLoginPassword">
        /// The administrator login password for the new server.
        /// </param>
        /// <param name="location">
        /// The location in which to create the new server.
        /// </param>
        /// <returns>The context to the newly created server.</returns>
        internal SqlDatabaseServerContext NewAzureSqlDatabaseServerProcess(string adminLogin, string adminLoginPassword, string location)
        {
            // Do nothing if force is not specified and user cancelled the operation
            if (!Force.IsPresent &&
                !ShouldProcess(
                    Resources.NewAzureSqlDatabaseServerDescription,
                    Resources.NewAzureSqlDatabaseServerWarning,
                    Resources.ShouldProcessCaption))
            {
                return null;
            }

            SqlDatabaseServerContext operationContext = null;
            try
            {
                InvokeInOperationContext(() =>
                {
                    XmlElement serverName = RetryCall(subscription =>
                        Channel.NewServer(subscription, adminLogin, adminLoginPassword, location));
                    Operation operation = WaitForSqlDatabaseOperation();

                    operationContext = new SqlDatabaseServerContext()
                    {
                        ServerName = serverName.InnerText,
                        Location = location,
                        AdministratorLogin = adminLogin,
                        OperationStatus = operation.Status,
                        OperationDescription = CommandRuntime.ToString(),
                        OperationId = operation.OperationTrackingId
                    };
                });
            }
            catch (CommunicationException ex)
            {
                this.WriteErrorDetails(ex);
            }

            return operationContext;
        }
        /// <summary>
        /// Creates a new server in the current subscription.
        /// </summary>
        /// <param name="adminLogin">
        /// The administrator login name for the new server.
        /// </param>
        /// <param name="adminLoginPassword">
        /// The administrator login password for the new server.
        /// </param>
        /// <param name="location">
        /// The location in which to create the new server.
        /// </param>
        /// <returns>The context to the newly created server.</returns>
        internal SqlDatabaseServerContext NewAzureSqlDatabaseServerProcess(
            string adminLogin,
            string adminLoginPassword,
            string location,
            float? version)
        {
            // Do nothing if force is not specified and user cancelled the operation
            if (!Force.IsPresent &&
                !ShouldProcess(
                    Resources.NewAzureSqlDatabaseServerDescription,
                    Resources.NewAzureSqlDatabaseServerWarning,
                    Resources.ShouldProcessCaption))
            {
                return null;
            }

            // Get the SQL management client for the current subscription
            SqlManagementClient sqlManagementClient = GetCurrentSqlClient();

            // Issue the create server request
            ServerCreateResponse response = sqlManagementClient.Servers.Create(
                new ServerCreateParameters()
                {
                    Location = location,
                    AdministratorUserName = adminLogin,
                    AdministratorPassword = adminLoginPassword,
                    Version = version.HasValue ? version.Value.ToString("F1") : null
                });

            SqlDatabaseServerContext operationContext = new SqlDatabaseServerContext()
            {
                OperationStatus = Services.Constants.OperationSuccess,
                OperationDescription = CommandRuntime.ToString(),
                OperationId = response.RequestId,
                ServerName = response.ServerName,
                Location = location,
                AdministratorLogin = adminLogin,
            };

            return operationContext;
        }
Ejemplo n.º 14
0
        public void AzureSqlDatabaseServerTests()
        {
            // This test uses the https endpoint, setup the certificates.
            MockHttpServer.SetupCertificates();

            using (PowerShell powershell = PowerShell.Create())
            {
                // Setup the subscription used for the test
                WindowsAzureSubscription subscription =
                    UnitTestHelper.SetupUnitTestSubscription(powershell);

                // Create a new server
                HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.AzureSqlDatabaseServerTests");
                ServerTestHelper.SetDefaultTestSessionSettings(testSession);
                testSession.RequestValidator =
                    new Action <HttpMessage, HttpMessage.Request>(
                        (expected, actual) =>
                {
                    Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                    Assert.IsTrue(
                        actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                        "Missing proper UserAgent string.");
                    Assert.IsTrue(
                        UnitTestHelper.GetUnitTestClientCertificate().Equals(actual.Certificate),
                        "Expected correct client certificate");
                });

                powershell.Runspace.SessionStateProxy.SetVariable("login", "mylogin");
                powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd!");
                powershell.Runspace.SessionStateProxy.SetVariable("location", "East Asia");
                Collection <PSObject> newServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabaseServer" +
                               @" -AdministratorLogin $login" +
                               @" -AdministratorLoginPassword $password" +
                               @" -Location $location"));
                });

                Collection <PSObject> getServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServer $server.ServerName"));
                });

                Collection <PSObject> setServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd2");
                    powershell.InvokeBatchScript(
                        @"$server | Set-AzureSqlDatabaseServer" +
                        @" -AdminPassword $password" +
                        @" -Force");
                    return(powershell.InvokeBatchScript(
                               @"$server | Get-AzureSqlDatabaseServer"));
                });

                Collection <PSObject> removeServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    powershell.InvokeBatchScript(
                        @"$server | Remove-AzureSqlDatabaseServer" +
                        @" -Force");

                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServer"));
                });

                Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!");
                Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!");

                // Validate New-AzureSqlDatabaseServer results
                SqlDatabaseServerContext server =
                    newServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");

                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    server.AdministratorLogin,
                    ignoreCase: true,
                    message: "Expecting matching login.");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    server.Location,
                    ignoreCase: true,
                    message: "Expecting matching location.");
                Assert.AreEqual(10, server.ServerName.Length, "Expecting a valid server name.");

                // Validate Get-AzureSqlDatabaseServer results
                server = getServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    server.AdministratorLogin,
                    ignoreCase: true,
                    message: "Expecting matching login.");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    server.Location,
                    ignoreCase: true,
                    message: "Expecting matching location.");
                Assert.AreEqual(10, server.ServerName.Length, "Expecting a valid server name.");

                server = setServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    server.AdministratorLogin,
                    ignoreCase: true,
                    message: "Expecting matching login.");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    server.Location,
                    ignoreCase: true,
                    message: "Expecting matching location.");
                Assert.AreEqual(10, server.ServerName.Length, "Expecting a valid server name.");

                // Validate Remove-AzureSqlDatabaseServer results
                Assert.IsFalse(
                    removeServerResult.Any((o) => o.GetVariableValue <string>("ServerName") == server.ServerName),
                    "Server should have been removed.");

                powershell.Streams.ClearStreams();
            }
        }