public void TestDatabaseTransparentDataEncryptionConfiguration() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); // Create database only required parameters // string dbName = SqlManagementTestUtilities.GenerateName(); var db1 = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, new Database() { Location = server.Location, }); Assert.NotNull(db1); // Get TDE config // Recently changed to be enabled by default var config = sqlClient.TransparentDataEncryptions.Get(resourceGroup.Name, server.Name, dbName); Assert.Equal(TransparentDataEncryptionStatus.Enabled, config.Status); // Update TDE config config.Status = TransparentDataEncryptionStatus.Disabled; // Sometimes the config is still being updated from the previous PUT, so execute with retry SqlManagementTestUtilities.ExecuteWithRetry(() => { config = sqlClient.TransparentDataEncryptions.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, config); }, TimeSpan.FromMinutes(2), TimeSpan.FromSeconds(5), (CloudException e) => { return(e.Response.StatusCode == HttpStatusCode.Conflict); }); Assert.Equal(TransparentDataEncryptionStatus.Disabled, config.Status); } }
public void TestCrudInstanceFailoverGroup() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); // Names of pre-existing ManagedInstances string sourceManagedInstanceName = "mi-primary-wcus"; string targetManagedInstanceName = "mi-tooling-cus"; string sourceResourceGroup = ManagedInstanceTestUtilities.ResourceGroupName; string targetResourceGroup = ManagedInstanceTestUtilities.ResourceGroupName; // Create server var sourceManagedInstance = sqlClient.ManagedInstances.Get(sourceResourceGroup, sourceManagedInstanceName); // Create second server var targetManagedInstance = sqlClient.ManagedInstances.Get(targetResourceGroup, targetManagedInstanceName); // Create database only required parameters string mdbName = "database1"; var mdb1 = sqlClient.ManagedDatabases.CreateOrUpdate(sourceResourceGroup, sourceManagedInstance.Name, mdbName, new ManagedDatabase() { Location = sourceManagedInstance.Location, }); Assert.NotNull(mdb1); // Create a failover group string instanceFailoverGroupName = SqlManagementTestUtilities.GenerateName(); var fgInput = new InstanceFailoverGroup() { ReadOnlyEndpoint = new InstanceFailoverGroupReadOnlyEndpoint() { FailoverPolicy = ReadOnlyEndpointFailoverPolicy.Disabled, }, ReadWriteEndpoint = new InstanceFailoverGroupReadWriteEndpoint() { FailoverPolicy = ReadWriteEndpointFailoverPolicy.Manual, }, PartnerRegions = new List <PartnerRegionInfo>() { new PartnerRegionInfo() { Location = targetManagedInstance.Location }, }, ManagedInstancePairs = new List <ManagedInstancePairInfo>() { new ManagedInstancePairInfo() { PrimaryManagedInstanceId = sourceManagedInstance.Id, PartnerManagedInstanceId = targetManagedInstance.Id }, }, }; var instanceFailoverGroup = sqlClient.InstanceFailoverGroups.CreateOrUpdate(sourceResourceGroup, sourceManagedInstance.Location, instanceFailoverGroupName, fgInput); SqlManagementTestUtilities.ValidateInstanceFailoverGroup(fgInput, instanceFailoverGroup, instanceFailoverGroupName); var pePrimaryDatabase = sqlClient.ManagedDatabases.Get(sourceResourceGroup, sourceManagedInstance.Name, mdbName); // A brief wait may be needed until the secondary for the pre-existing database is created ManagedDatabase peSecondaryDatabase = new ManagedDatabase(); SqlManagementTestUtilities.ExecuteWithRetry(() => { peSecondaryDatabase = sqlClient.ManagedDatabases.Get(targetResourceGroup, targetManagedInstance.Name, mdbName); }, TimeSpan.FromMinutes(2), TimeSpan.FromSeconds(5), (CloudException e) => { return(e.Response.StatusCode == HttpStatusCode.NotFound); }); // Update a few settings var fgSetInput = new InstanceFailoverGroup() { ReadOnlyEndpoint = new InstanceFailoverGroupReadOnlyEndpoint { FailoverPolicy = ReadOnlyEndpointFailoverPolicy.Enabled }, ReadWriteEndpoint = new InstanceFailoverGroupReadWriteEndpoint { FailoverPolicy = ReadWriteEndpointFailoverPolicy.Automatic, FailoverWithDataLossGracePeriodMinutes = 120 }, PartnerRegions = new List <PartnerRegionInfo>() { new PartnerRegionInfo() { Location = instanceFailoverGroup.PartnerRegions.FirstOrDefault().Location }, }, ManagedInstancePairs = new List <ManagedInstancePairInfo>() { new ManagedInstancePairInfo() { PrimaryManagedInstanceId = instanceFailoverGroup.ManagedInstancePairs.FirstOrDefault().PrimaryManagedInstanceId, PartnerManagedInstanceId = instanceFailoverGroup.ManagedInstancePairs.FirstOrDefault().PartnerManagedInstanceId }, }, }; var instanceFailoverGroupUpdated2 = sqlClient.InstanceFailoverGroups.CreateOrUpdate(sourceResourceGroup, sourceManagedInstance.Location, instanceFailoverGroupName, fgSetInput); // Set expectations and verify update fgInput.ReadWriteEndpoint = fgSetInput.ReadWriteEndpoint; fgInput.ReadOnlyEndpoint = fgSetInput.ReadOnlyEndpoint; SqlManagementTestUtilities.ValidateInstanceFailoverGroup(fgInput, instanceFailoverGroupUpdated2, instanceFailoverGroupName); // Failover failover group sqlClient.InstanceFailoverGroups.Failover(targetResourceGroup, targetManagedInstance.Location, instanceFailoverGroupName); instanceFailoverGroup = sqlClient.InstanceFailoverGroups.Get(targetResourceGroup, targetManagedInstance.Location, instanceFailoverGroupName); // Get failover group on the new secondary server and verify its replication role Assert.Equal(sourceManagedInstance.Id, instanceFailoverGroup.ManagedInstancePairs.FirstOrDefault().PartnerManagedInstanceId); Assert.Equal(targetManagedInstance.Id, instanceFailoverGroup.ManagedInstancePairs.FirstOrDefault().PrimaryManagedInstanceId); // Delete failover group sqlClient.InstanceFailoverGroups.Delete(targetResourceGroup, targetManagedInstance.Location, instanceFailoverGroupName); Assert.Throws <Microsoft.Rest.Azure.CloudException>(() => sqlClient.InstanceFailoverGroups.Get(sourceResourceGroup, sourceManagedInstance.Location, instanceFailoverGroupName)); //Delete the managed database sqlClient.ManagedDatabases.Delete(sourceResourceGroup, sourceManagedInstance.Name, mdbName); sqlClient.ManagedDatabases.Delete(targetResourceGroup, targetManagedInstance.Name, mdbName); } }
public void TestCrudFailoverGroup() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); // Create primary and partner servers // var sourceServer = context.CreateServer(resourceGroup); var targetServer = context.CreateServer(resourceGroup, location: TestEnvironmentUtilities.DefaultSecondaryLocationId); // Create a failover group // string failoverGroupName = SqlManagementTestUtilities.GenerateName(); var fgInput = new FailoverGroup() { ReadOnlyEndpoint = new FailoverGroupReadOnlyEndpoint() { FailoverPolicy = ReadOnlyEndpointFailoverPolicy.Disabled, }, ReadWriteEndpoint = new FailoverGroupReadWriteEndpoint() { FailoverPolicy = ReadWriteEndpointFailoverPolicy.Manual, }, PartnerServers = new List <PartnerInfo>() { new PartnerInfo() { Id = targetServer.Id }, }, Databases = new List <string>(), }; var failoverGroup = sqlClient.FailoverGroups.CreateOrUpdate(resourceGroup.Name, sourceServer.Name, failoverGroupName, fgInput); SqlManagementTestUtilities.ValidateFailoverGroup(fgInput, failoverGroup, failoverGroupName); var failoverGroupOnPartner = sqlClient.FailoverGroups.Get(resourceGroup.Name, targetServer.Name, failoverGroupName); Assert.NotNull(failoverGroupOnPartner); // Update a few settings // var fgPatchInput = new FailoverGroupUpdate { ReadOnlyEndpoint = new FailoverGroupReadOnlyEndpoint { FailoverPolicy = ReadOnlyEndpointFailoverPolicy.Enabled }, ReadWriteEndpoint = new FailoverGroupReadWriteEndpoint { FailoverPolicy = ReadWriteEndpointFailoverPolicy.Automatic, FailoverWithDataLossGracePeriodMinutes = 120 }, Tags = new Dictionary <string, string> { { "tag1", "value1" } } }; var failoverGroupUpdated2 = sqlClient.FailoverGroups.Update(resourceGroup.Name, sourceServer.Name, failoverGroupName, fgPatchInput); // Set expectations and verify update // fgInput.ReadWriteEndpoint = fgPatchInput.ReadWriteEndpoint; fgInput.ReadOnlyEndpoint = fgPatchInput.ReadOnlyEndpoint; fgInput.Tags = fgPatchInput.Tags; SqlManagementTestUtilities.ValidateFailoverGroup(fgInput, failoverGroupUpdated2, failoverGroupName); // Create a database in the primary server // string databaseName = "testdb"; var dbInput = new Database() { Location = sourceServer.Location }; Database database = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, sourceServer.Name, databaseName, dbInput); Assert.NotNull(database); // Update failover group to add database and change read-write endpoint's failover policy. // var fgUpdateInput = new FailoverGroup() { ReadOnlyEndpoint = new FailoverGroupReadOnlyEndpoint() { FailoverPolicy = ReadOnlyEndpointFailoverPolicy.Disabled, }, ReadWriteEndpoint = new FailoverGroupReadWriteEndpoint() { FailoverPolicy = ReadWriteEndpointFailoverPolicy.Manual, }, PartnerServers = new List <PartnerInfo>() { new PartnerInfo() { Id = targetServer.Id }, }, Databases = new List <string>() { database.Id, }, }; failoverGroup = sqlClient.FailoverGroups.CreateOrUpdate(resourceGroup.Name, sourceServer.Name, failoverGroupName, fgUpdateInput); SqlManagementTestUtilities.ValidateFailoverGroup(fgUpdateInput, failoverGroup, failoverGroupName); // List failover groups on the secondary server and verify // var failoverGroupsOnSecondary = sqlClient.FailoverGroups.ListByServer(resourceGroup.Name, targetServer.Name); Assert.NotNull(failoverGroupsOnSecondary); Assert.Equal(1, failoverGroupsOnSecondary.Count()); var primaryDatabase = sqlClient.Databases.Get(resourceGroup.Name, sourceServer.Name, databaseName); // A brief wait may be needed until the secondary database is fully created Database secondaryDatabase = new Database(); SqlManagementTestUtilities.ExecuteWithRetry(() => { secondaryDatabase = sqlClient.Databases.Get(resourceGroup.Name, targetServer.Name, databaseName); }, TimeSpan.FromMinutes(2), TimeSpan.FromSeconds(5), (CloudException e) => { return(e.Response.StatusCode == HttpStatusCode.NotFound); }); Assert.NotNull(primaryDatabase.FailoverGroupId); Assert.NotNull(secondaryDatabase.FailoverGroupId); // Failover failover group // failoverGroup = sqlClient.FailoverGroups.Failover(resourceGroup.Name, targetServer.Name, failoverGroupName); // Get failover group on the new secondary server and verify its replication role // var failoverGroupOnSecondary = sqlClient.FailoverGroups.Get(resourceGroup.Name, sourceServer.Name, failoverGroupName); Assert.Equal(FailoverGroupReplicationRole.Secondary, failoverGroupOnSecondary.ReplicationRole); Assert.Equal(FailoverGroupReplicationRole.Primary, failoverGroupOnSecondary.PartnerServers.First().ReplicationRole); // Delete failover group and verify that databases are removed from the failover group // sqlClient.FailoverGroups.Delete(resourceGroup.Name, targetServer.Name, failoverGroupName); primaryDatabase = sqlClient.Databases.Get(resourceGroup.Name, targetServer.Name, databaseName); secondaryDatabase = sqlClient.Databases.Get(resourceGroup.Name, sourceServer.Name, databaseName); Assert.Null(primaryDatabase.FailoverGroupId); Assert.Null(secondaryDatabase.FailoverGroupId); Assert.Throws <Microsoft.Rest.Azure.CloudException>(() => sqlClient.FailoverGroups.Get(resourceGroup.Name, sourceServer.Name, failoverGroupName)); Assert.Throws <Microsoft.Rest.Azure.CloudException>(() => sqlClient.FailoverGroups.Get(resourceGroup.Name, targetServer.Name, failoverGroupName)); } }