Ejemplo n.º 1
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");
        }
Ejemplo n.º 2
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");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Given a <see cref="SqlDatabaseResponse"/> this will create and return a <see cref="Database"/>
        /// object with the fields filled in.
        /// </summary>
        /// <param name="response">The response to turn into a <see cref="Database"/></param>
        /// <returns>a <see cref="Database"/> object.</returns>
        private Database CreateDatabaseFromResponse(SqlDatabaseResponse response)
        {
            Database result = new Database()
            {
                CollationName    = response.CollationName,
                CreationDate     = DateTime.Parse(response.CreationDate, CultureInfo.InvariantCulture),
                Edition          = response.Edition,
                Id               = int.Parse(response.Id),
                IsFederationRoot = bool.Parse(response.IsFederationRoot),
                IsSystemObject   = bool.Parse(response.IsSystemObject),
                MaxSizeGB        = int.Parse(response.MaxSizeGB),
                MaxSizeBytes     = long.Parse(response.MaxSizeBytes),
                Name             = response.Name,
            };

            // Parse the service objective information
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentErrorCode))
            {
                result.ServiceObjectiveAssignmentErrorCode = int.Parse(response.ServiceObjectiveAssignmentErrorCode);
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentErrorDescription))
            {
                result.ServiceObjectiveAssignmentErrorDescription = response.ServiceObjectiveAssignmentErrorDescription;
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentState))
            {
                result.ServiceObjectiveAssignmentState = byte.Parse(response.ServiceObjectiveAssignmentState);
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentStateDescription))
            {
                result.ServiceObjectiveAssignmentStateDescription = response.ServiceObjectiveAssignmentStateDescription;
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentSuccessDate))
            {
                result.ServiceObjectiveAssignmentSuccessDate = DateTime.Parse(response.ServiceObjectiveAssignmentSuccessDate, CultureInfo.InvariantCulture);
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveId))
            {
                result.ServiceObjectiveId = Guid.Parse(response.ServiceObjectiveId);
            }

            result.LoadExtraProperties(this);

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

            //create a channel to the server for communication
            ISqlDatabaseManagement channel = GetManagementChannel();

            //query the server for the database
            SqlDatabaseResponse database =
                channel.EndGetDatabase(
                    channel.BeginGetDatabase(this.subscriptionId, this.ServerName, databaseName, null, null));

            //Create the database from the response
            Database result = CreateDatabaseFromResponse(database);

            //return the database
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new sql database.
        /// </summary>
        /// <param name="databaseName">The name for the new database</param>
        /// <param name="databaseMaxSize">The maximum size of the new database</param>
        /// <param name="databaseCollation">The collation for the new database</param>
        /// <param name="databaseEdition">The edition for the new database</param>
        /// <returns>The newly created Sql Database</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int?databaseMaxSize,
            string databaseCollation,
            DatabaseEdition databaseEdition)
        {
            this.clientRequestId = SqlDatabaseManagementHelper.GenerateClientTracingId();

            ISqlDatabaseManagement channel = GetManagementChannel();

            SqlDatabaseInput input = new SqlDatabaseInput();

            input.Name          = databaseName;
            input.CollationName = databaseCollation ?? string.Empty;

            //determine the edition
            if (databaseEdition != DatabaseEdition.None)
            {
                input.Edition = databaseEdition.ToString();
            }
            else
            {
                input.Edition = string.Empty;
            }

            //determine the maximum size
            if (databaseMaxSize.HasValue)
            {
                input.MaxSizeGB = databaseMaxSize.ToString();
            }

            //create a new database on the server
            SqlDatabaseResponse response =
                channel.EndNewDatabase(
                    channel.BeginNewDatabase(this.subscriptionId, this.serverName, input, null, null));

            Database database = CreateDatabaseFromResponse(response);

            return(database);
        }
Ejemplo n.º 6
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>
        /// Given a <see cref="SqlDatabaseResponse"/> this will create and return a <see cref="Database"/> 
        /// object with the fields filled in.
        /// </summary>
        /// <param name="response">The response to turn into a <see cref="Database"/></param>
        /// <returns>a <see cref="Database"/> object.</returns>
        private Database CreateDatabaseFromResponse(SqlDatabaseResponse response)
        {
            Database result = new Database()
            {
                CollationName = response.CollationName,
                CreationDate = DateTime.Parse(response.CreationDate, CultureInfo.InvariantCulture),
                Edition = response.Edition,
                Id = int.Parse(response.Id),
                IsFederationRoot = bool.Parse(response.IsFederationRoot),
                IsSystemObject = bool.Parse(response.IsSystemObject),
                MaxSizeGB = int.Parse(response.MaxSizeGB),
                MaxSizeBytes = long.Parse(response.MaxSizeBytes),
                Name = response.Name,
            };

            // Parse the service objective information
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentErrorCode))
            {
                result.ServiceObjectiveAssignmentErrorCode = int.Parse(response.ServiceObjectiveAssignmentErrorCode);
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentErrorDescription))
            {
                result.ServiceObjectiveAssignmentErrorDescription = response.ServiceObjectiveAssignmentErrorDescription;
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentState))
            {
                result.ServiceObjectiveAssignmentState = byte.Parse(response.ServiceObjectiveAssignmentState);
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentStateDescription))
            {
                result.ServiceObjectiveAssignmentStateDescription = response.ServiceObjectiveAssignmentStateDescription;
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentSuccessDate))
            {
                result.ServiceObjectiveAssignmentSuccessDate = DateTime.Parse(response.ServiceObjectiveAssignmentSuccessDate, CultureInfo.InvariantCulture);
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveId))
            {
                result.ServiceObjectiveId = Guid.Parse(response.ServiceObjectiveId);
            }

            result.LoadExtraProperties(this);

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

            ISqlDatabaseManagement channel = GetManagementChannel();

            Database database = this.GetDatabase(databaseName);

            //make sure the database exists.
            if (database == null)
            {
                throw new Exception(
                          "Error: Result of GetDatabase() in ServerDataServiceCertAuth.UpdateDatabase() is null");
            }

            SqlDatabaseInput input = new SqlDatabaseInput();

            //Set the database ID and collation
            input.Id            = database.Id.ToString();
            input.CollationName = database.CollationName;

            if (serviceObjective != null)
            {
                input.ServiceObjectiveId = serviceObjective.Id.ToString();
            }

            //Determine what the new name for the database should be
            if (!string.IsNullOrEmpty(newDatabaseName))
            {
                input.Name = newDatabaseName;
            }
            else
            {
                input.Name = database.Name;
            }

            //Determine what the new edition for the database should be
            if (databaseEdition.HasValue && (databaseEdition != DatabaseEdition.None))
            {
                input.Edition = databaseEdition.ToString();
            }
            else
            {
                input.Edition = database.Edition;
            }

            //Determine what the new maximum size for the database should be.
            if (databaseMaxSize.HasValue)
            {
                input.MaxSizeGB = databaseMaxSize.ToString();
            }
            else
            {
                input.MaxSizeGB = database.MaxSizeGB.ToString();
            }

            //Send the update request and wait for the response.
            SqlDatabaseResponse response = channel.EndUpdateDatabase(
                channel.BeginUpdateDatabase(
                    this.subscriptionId,
                    this.serverName,
                    databaseName,
                    input,
                    null,
                    null));

            //Transform the response into a database object.
            Database updatedDatabase = CreateDatabaseFromResponse(response);

            return(updatedDatabase);
        }
Ejemplo n.º 9
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");
        }