public void TestCreateAndUpdateIPv6FirewallRule() { string testPrefix = "ipv6firewallrulecrudtest-"; using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); // Create IPv6 Firewall Rule and Validate string ipv6FirewallRuleName = SqlManagementTestUtilities.GenerateName(testPrefix); IPv6FirewallRule toCreate = new IPv6FirewallRule() { StartIPv6Address = "0000:0000:0000:0000:0000:ffff:0000:0000", EndIPv6Address = "0000:0000:0000:0000:0000:ffff:0000:0000" }; var fr1 = sqlClient.IPv6FirewallRules.CreateOrUpdate(resourceGroup.Name, server.Name, ipv6FirewallRuleName, toCreate); SqlManagementTestUtilities.ValidateIPv6FirewallRule(toCreate, fr1, ipv6FirewallRuleName); // Update Firewall Rule and Validate toCreate = new IPv6FirewallRule() { StartIPv6Address = "0000:0000:0000:0000:0000:ffff:0101:0101", EndIPv6Address = "0000:0000:0000:0000:0000:ffff:ffff:ffff" }; fr1 = sqlClient.IPv6FirewallRules.CreateOrUpdate(resourceGroup.Name, server.Name, ipv6FirewallRuleName, toCreate); SqlManagementTestUtilities.ValidateIPv6FirewallRule(toCreate, fr1, ipv6FirewallRuleName); } }
public void FailoverElasticPool() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Server server = context.CreateServer(resourceGroup); // Create elastic pool string epName = SqlManagementTestUtilities.GenerateName(); var ep = sqlClient.ElasticPools.CreateOrUpdate(resourceGroup.Name, server.Name, epName, new ElasticPool() { Location = server.Location }); Assert.NotNull(ep); // Create database in elastic pool string dbName = SqlManagementTestUtilities.GenerateName(); var dbInput = new Database() { Location = server.Location, ElasticPoolId = ep.Id }; var db = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, dbInput); Assert.NotNull(db); // Failover elastic pool sqlClient.ElasticPools.Failover( resourceGroup.Name, server.Name, epName); } }
public void TestCreateLedgerDatabase() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); // Create database with IsLedgerOn string databaseName = SqlManagementTestUtilities.GenerateName(); Database ledgerDb = sqlClient.Databases.CreateOrUpdate( resourceGroup.Name, server.Name, databaseName, new Database() { Location = server.Location, IsLedgerOn = true }); // Get the created database and validate IsLedgerOn parameter is set Database databaseResponse = sqlClient.Databases.Get(resourceGroup.Name, server.Name, databaseName); Assert.True(databaseResponse.IsLedgerOn); } }
public virtual void TestDatabaseSensitivityLabels() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); string resourceGroupName = resourceGroup.Name; Server server = context.CreateServer(resourceGroup); string serverName = server.Name; ISqlManagementClient client = context.GetClient <SqlManagementClient>(); CreateFirewallRule(resourceGroupName, serverName, client); string databaseName = GetDatabaseName(); Database database = client.Databases.CreateOrUpdate( resourceGroupName, serverName, databaseName, new Database() { Location = server.Location, }); Assert.NotNull(database); CreateTableIfNeeded(serverName, server.FullyQualifiedDomainName, databaseName); RunTest(client, resourceGroupName, serverName, databaseName); client.Databases.Delete(resourceGroupName, serverName, databaseName); client.Servers.Delete(resourceGroupName, serverName); } }
public void FailoverReadableSecondaryDatabase() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Server server = context.CreateServer(resourceGroup); // Create database string dbName = SqlManagementTestUtilities.GenerateName(); var dbInput = new Database() { Location = server.Location, Sku = new Microsoft.Azure.Management.Sql.Models.Sku(ServiceObjectiveName.P1) }; var db = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, dbInput); Assert.NotNull(db); // Failover database sqlClient.Databases.Failover( resourceGroup.Name, server.Name, dbName, "ReadableSecondary"); } }
public async void TestCreateUpdateGetManagedInstanceVulnerabilityAssessments() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); var resourceGroup = context.CreateResourceGroup(); var managedInstance = context.CreateManagedInstance(resourceGroup); // Verify Policy is empty to begin with ManagedInstanceVulnerabilityAssessment policyThatWasReceived = sqlClient.ManagedInstanceVulnerabilityAssessments.Get(resourceGroup.Name, managedInstance.Name); Assert.Null(policyThatWasReceived.StorageContainerPath); Assert.Null(policyThatWasReceived.StorageAccountAccessKey); Assert.False(policyThatWasReceived.RecurringScans.IsEnabled); // Set policy and then get policy and verify correctness ManagedInstanceVulnerabilityAssessment policyThatWasSet = await SetPolicy(context, sqlClient, resourceGroup, managedInstance); policyThatWasReceived = sqlClient.ManagedInstanceVulnerabilityAssessments.Get(resourceGroup.Name, managedInstance.Name); Assert.Equal(policyThatWasSet.StorageContainerPath, policyThatWasReceived.StorageContainerPath); Assert.Null(policyThatWasSet.StorageAccountAccessKey); Assert.Equal(policyThatWasSet.RecurringScans.IsEnabled, policyThatWasReceived.RecurringScans.IsEnabled); SqlManagementTestUtilities.AssertCollection(policyThatWasSet.RecurringScans.Emails, policyThatWasReceived.RecurringScans.Emails); Assert.Equal(policyThatWasSet.RecurringScans.EmailSubscriptionAdmins, policyThatWasReceived.RecurringScans.EmailSubscriptionAdmins); // Delete policy and then get policy and verify correctness sqlClient.ManagedInstanceVulnerabilityAssessments.Delete(resourceGroup.Name, managedInstance.Name); // Get policy after deletion policyThatWasReceived = sqlClient.ManagedInstanceVulnerabilityAssessments.Get(resourceGroup.Name, managedInstance.Name); Assert.Null(policyThatWasReceived.StorageContainerPath); Assert.Null(policyThatWasReceived.StorageAccountAccessKey); Assert.False(policyThatWasReceived.RecurringScans.IsEnabled); } }
public void TestShortTermRetentionPolicyOnPremium() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { // Valid Retention Days for Basic DB is 1 to 35 days. int defaultRetentionDays = 7; // Valid Differential Backup Interval Hours is 12 or 24. int defaultDiffBackupIntervalHours = 12; // Create a DTU - Premium DB. ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Database database = sqlClient.Databases.CreateOrUpdate( resourceGroup.Name, server.Name, SqlManagementTestUtilities.GenerateName(), new Database { Location = server.Location, Sku = new Microsoft.Azure.Management.Sql.Models.Sku(ServiceObjectiveName.P1) }); // Test GET operation can get default retention days and diffbackupinterval value. BackupShortTermRetentionPolicy policyDefault = sqlClient.BackupShortTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(defaultRetentionDays, policyDefault.RetentionDays); Assert.Equal(defaultDiffBackupIntervalHours, policyDefault.DiffBackupIntervalInHours); // Attempt to set retention period to 36 days (invalid); Attemp to set the differential backup interval to 24 hours (valid); Verify the operation fails on updating the policy. BackupShortTermRetentionPolicy parameters1 = new BackupShortTermRetentionPolicy(retentionDays: 36, diffBackupIntervalInHours: 24); sqlClient.BackupShortTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroup.Name, server.Name, database.Name, parameters1); Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestUtilities.Wait(TimeSpan.FromSeconds(3)); BackupShortTermRetentionPolicy policy = sqlClient.BackupShortTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(defaultRetentionDays, policy.RetentionDays); Assert.Equal(defaultDiffBackupIntervalHours, policy.DiffBackupIntervalInHours); // Attempt to set retention period to 35 days (valid); Attemp to set the differential backup interval to 20 hours (invalid); Verify the operation fails on updating the policy. BackupShortTermRetentionPolicy parameters2 = new BackupShortTermRetentionPolicy(retentionDays: 35, diffBackupIntervalInHours: 20); sqlClient.BackupShortTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroup.Name, server.Name, database.Name, parameters2); Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestUtilities.Wait(TimeSpan.FromSeconds(3)); BackupShortTermRetentionPolicy policy2 = sqlClient.BackupShortTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(defaultRetentionDays, policy2.RetentionDays); Assert.Equal(defaultDiffBackupIntervalHours, policy2.DiffBackupIntervalInHours); // Increase retention period to 35 days (valid); Increase differential backup interval to 24 hours (valid); Verify the operation success. BackupShortTermRetentionPolicy parameters3 = new BackupShortTermRetentionPolicy(retentionDays: 35, diffBackupIntervalInHours: 24); sqlClient.BackupShortTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroup.Name, server.Name, database.Name, parameters3); Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestUtilities.Wait(TimeSpan.FromSeconds(3)); BackupShortTermRetentionPolicy policy3 = sqlClient.BackupShortTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(parameters3.RetentionDays, policy3.RetentionDays); Assert.Equal(parameters3.DiffBackupIntervalInHours, policy3.DiffBackupIntervalInHours); // Decrease retention period to 7 days again; Decrease differential backup interval to 12 hours again (valid); Verify the operation success. BackupShortTermRetentionPolicy parameters4 = new BackupShortTermRetentionPolicy(retentionDays: 7, diffBackupIntervalInHours: 12); sqlClient.BackupShortTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroup.Name, server.Name, database.Name, parameters4); Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestUtilities.Wait(TimeSpan.FromSeconds(3)); BackupShortTermRetentionPolicy policy4 = sqlClient.BackupShortTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(parameters4.RetentionDays, policy4.RetentionDays); Assert.Equal(parameters4.DiffBackupIntervalInHours, policy4.DiffBackupIntervalInHours); } }
public void TestDatabasePointInTimeRestore() { // Warning: This test takes around 20 minutes to run in record mode. using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Database db1 = CreateDatabaseAndWaitUntilBackupCreated( sqlClient, resourceGroup, server, dbName: SqlManagementTestUtilities.GenerateName()); // Create a new database that is the first database restored to an earlier point in time string db2Name = SqlManagementTestUtilities.GenerateName(); Database db2Input = new Database { Location = server.Location, CreateMode = CreateMode.PointInTimeRestore, RestorePointInTime = db1.EarliestRestoreDate.Value, SourceDatabaseId = db1.Id }; Database db2 = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, db2Name, db2Input); Assert.NotNull(db2); SqlManagementTestUtilities.ValidateDatabase(db2Input, db2, db2Name); } }
public void TestAllScenariosPrivateEndpointConnection() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { sqlClient = context.GetClient <SqlManagementClient>(); networkClient = context.GetClient <NetworkManagementClient>(); var location = TestEnvironmentUtilities.DefaultEuapPrimaryLocationId; ResourceGroup resourceGroup = context.CreateResourceGroup(location); Server server = context.CreateServer(resourceGroup, location); VirtualNetwork vnet = CreateVirtualNetwork(resourceGroup, location); IList <PrivateEndpointConnection> pecs = CreatePrivateEndpoints(resourceGroup, location, server, vnet, n: 2); PrivateEndpointConnection pec1 = pecs[0]; pec1.PrivateLinkServiceConnectionState.Status = "Approved"; sqlClient.PrivateEndpointConnections.CreateOrUpdate(resourceGroup.Name, server.Name, pec1.Name, pec1); PrivateEndpointConnection pec1r = sqlClient.PrivateEndpointConnections.Get(resourceGroup.Name, server.Name, pec1.Name); SqlManagementTestUtilities.ValidatePrivateEndpointConnection(pec1, pec1r); PrivateEndpointConnection pec2 = pecs[1]; pec2.PrivateLinkServiceConnectionState.Status = "Rejected"; sqlClient.PrivateEndpointConnections.CreateOrUpdate(resourceGroup.Name, server.Name, pec2.Name, pec2); PrivateEndpointConnection pec2r = sqlClient.PrivateEndpointConnections.Get(resourceGroup.Name, server.Name, pec2.Name); SqlManagementTestUtilities.ValidatePrivateEndpointConnection(pec2, pec2r); sqlClient.PrivateEndpointConnections.Delete(resourceGroup.Name, server.Name, pec1.Name); Assert.Throws <Microsoft.Rest.Azure.CloudException>(() => sqlClient.PrivateEndpointConnections.Get(resourceGroup.Name, server.Name, pec1.Name)); sqlClient.PrivateEndpointConnections.Delete(resourceGroup.Name, server.Name, pec2.Name); Assert.Throws <Microsoft.Rest.Azure.CloudException>(() => sqlClient.PrivateEndpointConnections.Get(resourceGroup.Name, server.Name, pec2.Name)); } }
public void TestCreateAndUpdateFirewallRule() { string testPrefix = "firewallrulecrudtest-"; using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); // Create Firewall Rule and Validate // string firewallRuleName = SqlManagementTestUtilities.GenerateName(testPrefix); FirewallRule toCreate = new FirewallRule() { StartIpAddress = "0.0.0.0", EndIpAddress = "0.0.0.0" }; var fr1 = sqlClient.FirewallRules.CreateOrUpdate(resourceGroup.Name, server.Name, firewallRuleName, toCreate); SqlManagementTestUtilities.ValidateFirewallRule(toCreate, fr1, firewallRuleName); // Update Firewall Rule and Validate toCreate = new FirewallRule() { StartIpAddress = "1.1.1.1", EndIpAddress = "255.255.255.255" }; fr1 = sqlClient.FirewallRules.CreateOrUpdate(resourceGroup.Name, server.Name, firewallRuleName, toCreate); SqlManagementTestUtilities.ValidateFirewallRule(toCreate, fr1, firewallRuleName); } }
public async void TestExportDatabaseVulnerabilityAssessmentScans() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { string testPrefix = "sqlvulnerabilityassessmentexportscantest-"; SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); var resourceGroup = context.CreateResourceGroup(); var managedInstance = context.CreateManagedInstance(resourceGroup); // Create managed database // string dbName = SqlManagementTestUtilities.GenerateName(testPrefix); var db1 = sqlClient.ManagedDatabases.CreateOrUpdate(resourceGroup.Name, managedInstance.Name, dbName, new ManagedDatabase() { Location = managedInstance.Location, }); Assert.NotNull(db1); // Set policy await SetPolicy(context, sqlClient, resourceGroup, managedInstance, dbName); // Run some scans string scanId = string.Format("scan1_{0}", testPrefix); sqlClient.ManagedDatabaseVulnerabilityAssessmentScans.InitiateScan(resourceGroup.Name, managedInstance.Name, dbName, scanId); sqlClient.ManagedDatabaseVulnerabilityAssessmentScans.Export(resourceGroup.Name, managedInstance.Name, dbName, scanId); } }
public async void TestCreateUpdateGetDeleteDatabaseVulnerabilityAssessmentBaselines() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { string testPrefix = "sqlvulnerabilityassessmentbaselinetest-"; SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); var resourceGroup = context.CreateResourceGroup(); var managedInstance = context.CreateManagedInstance(resourceGroup); // Create managed database // string dbName = SqlManagementTestUtilities.GenerateName(testPrefix); var db1 = sqlClient.ManagedDatabases.CreateOrUpdate(resourceGroup.Name, managedInstance.Name, dbName, new ManagedDatabase() { Location = managedInstance.Location, }); Assert.NotNull(db1); // Set policy await SetPolicy(context, sqlClient, resourceGroup, managedInstance, dbName); // Test baseline for database level rule ValidateBaselineRule(sqlClient, resourceGroup, managedInstance, dbName, isServerLevelRule: true); // Test baseline for server level rule ValidateBaselineRule(sqlClient, resourceGroup, managedInstance, dbName, isServerLevelRule: false); } }
public void TestShortTermRetentionPolicyOnBasic() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { int basicRetention = 7; // Create a Basic DB so it defaults to 7 days retention. ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Database database = sqlClient.Databases.CreateOrUpdate( resourceGroup.Name, server.Name, SqlManagementTestUtilities.GenerateName(), new Database { Location = server.Location, Sku = new Microsoft.Azure.Management.Sql.Models.Sku(ServiceObjectiveName.Basic) }); // Attempt to increase retention period to 8 days and verfiy that the operation fails. BackupShortTermRetentionPolicy parameters = new BackupShortTermRetentionPolicy(retentionDays: 8); sqlClient.BackupShortTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroup.Name, server.Name, database.Name, parameters); Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestUtilities.Wait(TimeSpan.FromSeconds(3)); BackupShortTermRetentionPolicy policy = sqlClient.BackupShortTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(basicRetention, policy.RetentionDays); // Attempt to dncrease retention period to 3 days and verify operation failure. parameters = new BackupShortTermRetentionPolicy(retentionDays: 3); sqlClient.BackupShortTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroup.Name, server.Name, database.Name, parameters); Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestUtilities.Wait(TimeSpan.FromSeconds(3)); policy = sqlClient.BackupShortTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(basicRetention, policy.RetentionDays); } }
public void TestShortTermRetentionPolicyOnPremium() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { // Create a Premium DB so it defaults to 35 days retention. ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Database database = sqlClient.Databases.CreateOrUpdate( resourceGroup.Name, server.Name, SqlManagementTestUtilities.GenerateName(), new Database { Location = server.Location, Sku = new Microsoft.Azure.Management.Sql.Models.Sku(ServiceObjectiveName.P1) }); // Decrease retention period to 8 days and verfiy that it was updated. BackupShortTermRetentionPolicy parameters = new BackupShortTermRetentionPolicy(retentionDays: 8); sqlClient.BackupShortTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroup.Name, server.Name, database.Name, parameters); Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestUtilities.Wait(TimeSpan.FromSeconds(3)); BackupShortTermRetentionPolicy policy = sqlClient.BackupShortTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(parameters.RetentionDays, policy.RetentionDays); // Increase retention period to 35 days again and verfiy that it was updated. parameters = new BackupShortTermRetentionPolicy(retentionDays: 35); sqlClient.BackupShortTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroup.Name, server.Name, database.Name, parameters); Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestUtilities.Wait(TimeSpan.FromSeconds(3)); policy = sqlClient.BackupShortTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(parameters.RetentionDays, policy.RetentionDays); } }
public void FailoverReadableSecondaryInstance() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); var rg = context.CreateResourceGroup(ManagedInstanceTestUtilities.Region); ManagedInstance managedInstance = context.CreateManagedInstance(rg); Assert.NotNull(managedInstance); var resourceGroupName = rg.Name; // Wait for first full backup to finish if (HttpMockServer.Mode == HttpRecorderMode.Record) { Thread.Sleep(TimeSpan.FromMinutes(6)); } try { sqlClient.ManagedInstances.Failover(resourceGroupName, managedInstance.Name, ReplicaType.ReadableSecondary); } catch (Exception ex) { Assert.Contains("failover is not supported", ex.Message); } } }
public void TestPauseResumeDatabase() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); // Create data warehouse string dbName = SqlManagementTestUtilities.GenerateName(); var db1 = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, new Database() { Location = server.Location, Sku = SqlTestConstants.DefaultDataWarehouseSku() }); Assert.NotNull(db1); // Pause sqlClient.Databases.Pause(resourceGroup.Name, server.Name, dbName); // TODO: Get result and verify that status is now resumed - blocked by https://github.com/Azure/autorest/issues/2295 // Resume sqlClient.Databases.Resume(resourceGroup.Name, server.Name, dbName); // TODO: Get result and verify that status is now resumed - blocked by https://github.com/Azure/autorest/issues/2295 } }
public void TestListElasticPoolActivity() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Dictionary <string, string> tags = new Dictionary <string, string>() { { "tagKey1", "TagValue1" } }; // Create a elastic pool // string epName = SqlManagementTestUtilities.GenerateName(); var epInput = new ElasticPool() { Location = server.Location, Tags = tags, DatabaseDtuMax = 5, DatabaseDtuMin = 0 }; var returnedEp = sqlClient.ElasticPools.CreateOrUpdate(resourceGroup.Name, server.Name, epName, epInput); SqlManagementTestUtilities.ValidateElasticPool(epInput, returnedEp, epName); // Get the Elastic Pool Activity List var activity = sqlClient.ElasticPoolActivities.ListByElasticPool(resourceGroup.Name, server.Name, epName); Assert.Equal(1, activity.Where(a => a.ElasticPoolName == epName).Count()); Assert.Equal(1, activity.Where(a => a.Operation == "CREATE").Count()); } }
public void TestLongTermRetentionV2Policies() { string defaultPolicy = "PT0S"; using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Database database = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, SqlManagementTestUtilities.GenerateName(), new Database { Location = server.Location }); // Get the policy and verify it is the default policy // LongTermRetentionPolicy policy = sqlClient.LongTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(defaultPolicy, policy.WeeklyRetention); Assert.Equal(defaultPolicy, policy.MonthlyRetention); Assert.Equal(defaultPolicy, policy.YearlyRetention); Assert.Equal(0, policy.WeekOfYear); // Set the retention policy to two weeks for the weekly retention policy // LongTermRetentionPolicy parameters = new LongTermRetentionPolicy(weeklyRetention: "P2W"); sqlClient.LongTermRetentionPolicies.CreateOrUpdate(resourceGroup.Name, server.Name, database.Name, parameters); // Get the policy and verify the weekly policy is two weeks but all the rest stayed the same // policy = sqlClient.LongTermRetentionPolicies.Get(resourceGroup.Name, server.Name, database.Name); Assert.Equal(parameters.WeeklyRetention, policy.WeeklyRetention); Assert.Equal(defaultPolicy, policy.MonthlyRetention); Assert.Equal(defaultPolicy, policy.YearlyRetention); Assert.Equal(0, policy.WeekOfYear); } }
public ManagedInstanceTestFixture() { Context = new SqlManagementTestContext(this); try { SqlManagementClient sqlClient = Context.GetClient <SqlManagementClient>(); ResourceGroup = Context.CreateResourceGroup(); // Create vnet and get the subnet id VirtualNetwork vnet = CreateVirtualNetwork(Context, ResourceGroup, TestEnvironmentUtilities.DefaultLocationId); Sku sku = new Sku(); sku.Name = "MIGP8G4"; sku.Tier = "GeneralPurpose"; ManagedInstance = sqlClient.ManagedInstances.CreateOrUpdate(ResourceGroup.Name, "crud-tests-" + SqlManagementTestUtilities.GenerateName(), new ManagedInstance() { AdministratorLogin = SqlManagementTestUtilities.DefaultLogin, AdministratorLoginPassword = SqlManagementTestUtilities.DefaultPassword, Sku = sku, SubnetId = vnet.Subnets[0].Id, Tags = new Dictionary <string, string>(), Location = TestEnvironmentUtilities.DefaultLocationId, }); } catch (Exception ex) { Context.Dispose(); } }
public void TestCreateAndDropVirtualNetworkRule() { string testPrefix = "virtualnetworkrulecrudtest-"; using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { // Basic setup var location = TestEnvironmentUtilities.DefaultEuapPrimaryLocationId; ResourceGroup resourceGroup = context.CreateResourceGroup(location); VirtualNetwork getVnetResponse = CreateVirtualNetwork(context, resourceGroup, location: location, subnetCount: 1); Server server = context.CreateServer(resourceGroup, location); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); // create virtual network rules string vnetfirewallRuleName = SqlManagementTestUtilities.GenerateName(testPrefix); VirtualNetworkRule rule = new VirtualNetworkRule() { VirtualNetworkSubnetId = getVnetResponse.Subnets[0].Id.ToString(), IgnoreMissingVnetServiceEndpoint = false }; VirtualNetworkRule vfr = sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName, rule); SqlManagementTestUtilities.ValidateVirtualNetworkRule(rule, vfr, vnetfirewallRuleName); // delete virtual network rules sqlClient.VirtualNetworkRules.Delete(resourceGroup.Name, server.Name, vfr.Name); } }
public async void TestShortTermRetentionPolicyOnGeneralPurposeScriptUse() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { // Valid Retention Days for GeneralPurpose DB is 1 to 35 days. int defaultRetentionDays = 7; // Create a vCore - GeneralPurpose DB. ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Database database = sqlClient.Databases.CreateOrUpdate( resourceGroup.Name, server.Name, SqlManagementTestUtilities.GenerateName(), new Database { Location = server.Location, Sku = new Microsoft.Azure.Management.Sql.Models.Sku(ServiceObjectiveName.P1) }); // Test Update operation through GET operation. var strPolicy = await sqlClient.BackupShortTermRetentionPolicies.GetAsync(resourceGroup.Name.ToString(), server.Name.ToString(), database.Name.ToString()); strPolicy.RetentionDays = 28; await sqlClient.BackupShortTermRetentionPolicies.BeginCreateOrUpdateAsync(resourceGroup.Name.ToString(), server.Name.ToString(), database.Name.ToString(), strPolicy); Assert.Equal(28, strPolicy.RetentionDays); } }
public async void TestDatabaseBlobAuditingPolicy() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); SqlManagementClient client = context.GetClient <SqlManagementClient>(); Server server = context.CreateServer(resourceGroup); Database database = client.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, SqlManagementTestUtilities.GenerateName(DatabaseNamePrefix), new Database() { Location = server.Location, }); Assert.NotNull(database); StorageAccountInformation storageAccountInformation = await CreateStorageAccountAsync(context, resourceGroup); DatabaseBlobAuditingPolicy databasePolicy = new DatabaseBlobAuditingPolicy { State = BlobAuditingPolicyState.Enabled, StorageEndpoint = storageAccountInformation.Endpoint, StorageAccountAccessKey = storageAccountInformation.PrimaryKey, RetentionDays = RetentionDays, IsStorageSecondaryKeyInUse = IsStorageSecondaryKeyInUse, IsAzureMonitorTargetEnabled = true, QueueDelayMs = 1000 }; ExtendedDatabaseBlobAuditingPolicy extendedDatabasePolicy = new ExtendedDatabaseBlobAuditingPolicy { State = BlobAuditingPolicyState.Enabled, StorageEndpoint = storageAccountInformation.Endpoint, StorageAccountAccessKey = storageAccountInformation.PrimaryKey, RetentionDays = RetentionDays, IsStorageSecondaryKeyInUse = IsStorageSecondaryKeyInUse, PredicateExpression = PredicateExpression, IsAzureMonitorTargetEnabled = true, QueueDelayMs = 1000 }; DatabaseBlobAuditingPolicy databaseResultPolicy = await client.DatabaseBlobAuditingPolicies.CreateOrUpdateAsync(resourceGroup.Name, server.Name, database.Name, databasePolicy); VerifyDatabaseBlobAuditingPolicy(databasePolicy, databaseResultPolicy); databaseResultPolicy = await client.DatabaseBlobAuditingPolicies.GetAsync(resourceGroup.Name, server.Name, database.Name); VerifyDatabaseBlobAuditingPolicy(databasePolicy, databaseResultPolicy); ExtendedDatabaseBlobAuditingPolicy extendedDatabaseResultPolicy = await client.ExtendedDatabaseBlobAuditingPolicies.CreateOrUpdateAsync(resourceGroup.Name, server.Name, database.Name, extendedDatabasePolicy); VerifyExtendedDatabaseBlobAuditingPolicy(extendedDatabasePolicy, extendedDatabaseResultPolicy); extendedDatabaseResultPolicy = await client.ExtendedDatabaseBlobAuditingPolicies.GetAsync(resourceGroup.Name, server.Name, database.Name); VerifyExtendedDatabaseBlobAuditingPolicy(extendedDatabasePolicy, extendedDatabaseResultPolicy); await client.Databases.DeleteAsync(resourceGroup.Name, server.Name, database.Name); await client.Servers.DeleteAsync(resourceGroup.Name, server.Name); await DeleteStorageAccountAsync(context, resourceGroup.Name, storageAccountInformation.Name); } }
public void TestGetAndListVirtualNetworkRule() { string testPrefix = "virtualnetworkrulescrudtest-"; using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { // Basic setup var location = TestEnvironmentUtilities.DefaultEuapPrimaryLocationId; ResourceGroup resourceGroup = context.CreateResourceGroup(location); VirtualNetwork getVnetResponse = CreateVirtualNetwork(context, resourceGroup, location: location, subnetCount: 2); Server server = context.CreateServer(resourceGroup, location); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); // create virtual network rules Dictionary <string, VirtualNetworkRule> rules = new Dictionary <string, VirtualNetworkRule>(); //rule 1 string vnetfirewallRuleName1 = SqlManagementTestUtilities.GenerateName(testPrefix); VirtualNetworkRule rule1 = new VirtualNetworkRule() { VirtualNetworkSubnetId = getVnetResponse.Subnets[0].Id.ToString(), IgnoreMissingVnetServiceEndpoint = false }; sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName1, rule1); rules.Add(vnetfirewallRuleName1, rule1); //rule 2 string vnetfirewallRuleName2 = SqlManagementTestUtilities.GenerateName(testPrefix); VirtualNetworkRule rule2 = new VirtualNetworkRule() { VirtualNetworkSubnetId = getVnetResponse.Subnets[1].Id.ToString(), IgnoreMissingVnetServiceEndpoint = false }; sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName2, rule2); rules.Add(vnetfirewallRuleName2, rule2); foreach (var rul in rules) { VirtualNetworkRule response = sqlClient.VirtualNetworkRules.Get(resourceGroup.Name, server.Name, rul.Key); SqlManagementTestUtilities.ValidateVirtualNetworkRule(rul.Value, response, rul.Key); } var listResponse = sqlClient.VirtualNetworkRules.ListByServer(resourceGroup.Name, server.Name); Assert.Equal(rules.Count(), listResponse.Count()); foreach (var rul in listResponse) { SqlManagementTestUtilities.ValidateVirtualNetworkRule(rules[rul.Name], rul, rul.Name); } foreach (var rul in rules) { SqlManagementTestUtilities.ValidateVirtualNetworkRule(rul.Value, listResponse.Single(r => r.Name == rul.Key), rul.Key); } } }
public async void TestExecuteGetListDatabaseVulnerabilityAssessmentScans() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { string testPrefix = "sqlvulnerabilityassessmentscantest-"; ResourceGroup resourceGroup = context.CreateResourceGroup(); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Server server = context.CreateServer(resourceGroup); // Create database // string dbName = SqlManagementTestUtilities.GenerateName(testPrefix); var db1 = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, new Database() { Location = server.Location, }); Assert.NotNull(db1); // Turn ON database threat detection as a prerequisite to use VA DatabaseSecurityAlertPolicy updatedDatabasePolicy = new DatabaseSecurityAlertPolicy { State = SecurityAlertPolicyState.Enabled, EmailAccountAdmins = SecurityAlertPolicyEmailAccountAdmins.Enabled }; sqlClient.DatabaseThreatDetectionPolicies.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, updatedDatabasePolicy); // Set policy await SetPolicy(context, sqlClient, resourceGroup, server, dbName); // Run some scans string scanId = string.Format("scantest1_{0}", testPrefix); sqlClient.DatabaseVulnerabilityAssessmentScans.InitiateScan(resourceGroup.Name, server.Name, dbName, scanId); string scanId1 = string.Format("scantest2_{0}", testPrefix); sqlClient.DatabaseVulnerabilityAssessmentScans.InitiateScan(resourceGroup.Name, server.Name, dbName, scanId1); // Verify get scan and list scans VulnerabilityAssessmentScanRecord scanRecord = sqlClient.DatabaseVulnerabilityAssessmentScans.Get(resourceGroup.Name, server.Name, dbName, scanId); Assert.Equal(scanId, scanRecord.ScanId); IPage <VulnerabilityAssessmentScanRecord> scanRecords = sqlClient.DatabaseVulnerabilityAssessmentScans.ListByDatabase(resourceGroup.Name, server.Name, dbName); Assert.Equal(2, scanRecords.ToList().Count); Assert.Contains(scanRecords.ToList(), item => item.ScanId == scanId); Assert.Contains(scanRecords.ToList(), item => item.ScanId == scanId1); VulnerabilityAssessmentScanRecord scanId1Record = sqlClient.DatabaseVulnerabilityAssessmentScans.Get(resourceGroup.Name, server.Name, dbName, scanId1); VulnerabilityAssessmentScanRecord scanId1RecordFromList = scanRecords.FirstOrDefault(item => item.ScanId == scanId1); Assert.Equal(scanId1Record.ScanId, scanId1RecordFromList.ScanId); Assert.Equal(scanId1Record.TriggerType, scanId1RecordFromList.TriggerType); Assert.Equal(scanId1Record.State, scanId1RecordFromList.State); Assert.Equal(scanId1Record.StartTime, scanId1RecordFromList.StartTime); Assert.Equal(scanId1Record.EndTime, scanId1RecordFromList.EndTime); Assert.Equal(scanId1Record.Errors, scanId1RecordFromList.Errors); Assert.Equal(scanId1Record.StorageContainerPath, scanId1RecordFromList.StorageContainerPath); Assert.Equal(scanId1Record.NumberOfFailedSecurityChecks, scanId1RecordFromList.NumberOfFailedSecurityChecks); } }
public void TestGetListFailoverReplicationLink() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); string databaseName = "testdb"; Dictionary <string, string> tags = new Dictionary <string, string>(); //Create a server and a database var v12Server = context.CreateServer(resourceGroup); var dbInput = new Database() { Location = v12Server.Location }; var database = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, v12Server.Name, databaseName, dbInput); // Create another server var v12Server2 = context.CreateServer(resourceGroup); // Create another database as an online secondary of the first database var dbInput2 = new Database() { Location = v12Server2.Location, CreateMode = CreateMode.OnlineSecondary, SourceDatabaseId = database.Id }; var database2 = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, v12Server2.Name, databaseName, dbInput2); // Verify there is one Link, get replication link id var replicationLinks = sqlClient.ReplicationLinks.ListByDatabase(resourceGroup.Name, v12Server2.Name, databaseName); Assert.True(replicationLinks.Count() == 1); string replicationLinkId = replicationLinks.First().Name; // Verify Get replication link var replicationLink = sqlClient.ReplicationLinks.Get(resourceGroup.Name, v12Server2.Name, databaseName, replicationLinkId); // Verify that the second database has a replicationLink to the first, with the first being the primary and the second being the secondary string primaryRole = "Primary"; string secondaryRole = "Secondary"; Assert.True(replicationLink.PartnerServer == v12Server.Name); Assert.True(replicationLink.PartnerDatabase == databaseName); Assert.True(replicationLink.PartnerRole == primaryRole); Assert.True(replicationLink.Role == secondaryRole); // Failover Replication Link sqlClient.ReplicationLinks.Failover(resourceGroup.Name, v12Server2.Name, databaseName, replicationLinkId); // Verify Replication Link after Failover replicationLink = sqlClient.ReplicationLinks.Get(resourceGroup.Name, v12Server2.Name, databaseName, replicationLinkId); // Verify that Primary and Secondary have switched Assert.True(replicationLink.PartnerRole == secondaryRole); Assert.True(replicationLink.Role == primaryRole); } }
public void TestUpdateEncryptionProtector() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = sqlClient.Servers.CreateOrUpdate( resourceGroup.Name, serverName: SqlManagementTestUtilities.GenerateName(), parameters: new Server { AdministratorLogin = SqlManagementTestUtilities.DefaultLogin, AdministratorLoginPassword = SqlManagementTestUtilities.DefaultPassword, Location = resourceGroup.Location, Identity = new ResourceIdentity() { Type = IdentityType.SystemAssigned } }); var keyBundle = SqlManagementTestUtilities.CreateKeyVaultKeyWithServerAccess(context, resourceGroup, server); // Create server key string serverKeyName = SqlManagementTestUtilities.GetServerKeyNameFromKeyBundle(keyBundle); string serverKeyUri = keyBundle.Key.Kid; var serverKey = sqlClient.ServerKeys.CreateOrUpdate(resourceGroup.Name, server.Name, serverKeyName, new ServerKey() { ServerKeyType = "AzureKeyVault", Uri = serverKeyUri }); SqlManagementTestUtilities.ValidateServerKey(serverKey, serverKeyName, "AzureKeyVault", serverKeyUri); // Update to Key Vault sqlClient.EncryptionProtectors.CreateOrUpdate(resourceGroup.Name, server.Name, new EncryptionProtector() { ServerKeyName = serverKeyName, ServerKeyType = "AzureKeyVault" }); EncryptionProtector encProtector1 = sqlClient.EncryptionProtectors.Get(resourceGroup.Name, server.Name); Assert.Equal("AzureKeyVault", encProtector1.ServerKeyType); Assert.Equal(serverKeyName, encProtector1.ServerKeyName); // Update to Service Managed sqlClient.EncryptionProtectors.CreateOrUpdate(resourceGroup.Name, server.Name, new EncryptionProtector() { ServerKeyName = "ServiceManaged", ServerKeyType = "ServiceManaged" }); EncryptionProtector encProtector2 = sqlClient.EncryptionProtectors.Get(resourceGroup.Name, server.Name); Assert.Equal("ServiceManaged", encProtector2.ServerKeyType); Assert.Equal("ServiceManaged", encProtector2.ServerKeyName); } }
public async Task TestCancelDatabaseOperation() { string testPrefix = "sqldblistcanceloperation-"; using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup("West Europe"); Server server = context.CreateServer(resourceGroup, "westeurope"); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Dictionary <string, string> tags = new Dictionary <string, string>() { { "tagKey1", "TagValue1" } }; // Create database only required parameters // string dbName = SqlManagementTestUtilities.GenerateName(testPrefix); var db1 = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, new Database() { Sku = new Microsoft.Azure.Management.Sql.Models.Sku(ServiceObjectiveName.S0), Location = server.Location, }); Assert.NotNull(db1); // Start updateslo operation // var dbUpdateResponse = sqlClient.Databases.BeginCreateOrUpdateWithHttpMessagesAsync(resourceGroup.Name, server.Name, dbName, new Database() { Sku = new Microsoft.Azure.Management.Sql.Models.Sku(ServiceObjectiveName.P2), Location = server.Location, }); TestUtilities.Wait(TimeSpan.FromSeconds(3)); // Get the updateslo operation // AzureOperationResponse <IPage <DatabaseOperation> > response = sqlClient.DatabaseOperations.ListByDatabaseWithHttpMessagesAsync( resourceGroup.Name, server.Name, dbName).Result; Assert.Equal(response.Response.StatusCode, HttpStatusCode.OK); IList <DatabaseOperation> responseObject = response.Body.ToList(); Assert.Equal(responseObject.Count(), 1); // Cancel the database updateslo operation // string requestId = responseObject[0].Name; sqlClient.DatabaseOperations.Cancel(resourceGroup.Name, server.Name, dbName, Guid.Parse(requestId)); CloudException ex = await Assert.ThrowsAsync <CloudException>(() => sqlClient.GetPutOrPatchOperationResultAsync(dbUpdateResponse.Result, new Dictionary <string, List <string> >(), CancellationToken.None)); Assert.Contains("Long running operation failed with status 'Canceled'", ex.Message); // Make sure the database is not updated due to cancel operation // var dbGetResponse = sqlClient.Databases.Get(resourceGroup.Name, server.Name, dbName); Assert.Equal(dbGetResponse.ServiceLevelObjective, ServiceObjectiveName.S0); } }
public void TestCreateDropDatabase() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); Server server = context.CreateServer(resourceGroup); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Dictionary <string, string> tags = new Dictionary <string, string>() { { "tagKey1", "TagValue1" } }; // 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); // Create a database with all parameters specified // dbName = SqlManagementTestUtilities.GenerateName(); var db2Input = new Database() { Location = server.Location, Collation = SqlTestConstants.DefaultCollation, Sku = SqlTestConstants.DefaultDatabaseSku(), MaxSizeBytes = 2 * 1024L * 1024L * 1024L, Tags = tags, CreateMode = "Default", SampleName = SampleName.AdventureWorksLT }; var db2 = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, db2Input); Assert.NotNull(db2); SqlManagementTestUtilities.ValidateDatabase(db2Input, db2, dbName); // Service Objective Name // dbName = SqlManagementTestUtilities.GenerateName(); var db4Input = new Database() { Location = server.Location, Sku = new Microsoft.Azure.Management.Sql.Models.Sku(ServiceObjectiveName.S0), Tags = tags, }; var db4 = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, db4Input); Assert.NotNull(db4); SqlManagementTestUtilities.ValidateDatabase(db4Input, db4, dbName); sqlClient.Databases.Delete(resourceGroup.Name, server.Name, db1.Name); sqlClient.Databases.Delete(resourceGroup.Name, server.Name, db2.Name); sqlClient.Databases.Delete(resourceGroup.Name, server.Name, db4.Name); } }
public void TestUpdateEncryptionProtector() { using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); ResourceManagementClient resourceClient = context.GetClient <ResourceManagementClient>(); ResourceGroup resourceGroup = context.CreateResourceGroup(); string resourceGroupName = resourceGroup.Name; ManagedInstance managedInstance = context.CreateManagedInstance(resourceGroup, new ManagedInstance() { Identity = new ResourceIdentity() { Type = IdentityType.SystemAssignedUserAssigned, UserAssignedIdentities = ManagedInstanceTestUtilities.UserIdentity }, PrimaryUserAssignedIdentityId = ManagedInstanceTestUtilities.UAMI }); managedInstance = sqlClient.ManagedInstances.Get(resourceGroupName, managedInstance.Name); var keyBundle = SqlManagementTestUtilities.CreateKeyVaultKeyWithManagedInstanceAccess(context, resourceGroup, managedInstance); // Create server key string serverKeyName = SqlManagementTestUtilities.GetServerKeyNameFromKeyBundle(keyBundle); string serverKeyUri = keyBundle.Key.Kid; var managedInstanceKey = sqlClient.ManagedInstanceKeys.CreateOrUpdate(resourceGroup.Name, managedInstance.Name, serverKeyName, new ManagedInstanceKey() { ServerKeyType = "AzureKeyVault", Uri = serverKeyUri }); SqlManagementTestUtilities.ValidateManagedInstanceKey(managedInstanceKey, serverKeyName, "AzureKeyVault", serverKeyUri); // Update to Key Vault sqlClient.ManagedInstanceEncryptionProtectors.CreateOrUpdate(resourceGroup.Name, managedInstance.Name, new ManagedInstanceEncryptionProtector() { ServerKeyName = serverKeyName, ServerKeyType = "AzureKeyVault" }); ManagedInstanceEncryptionProtector encProtector1 = sqlClient.ManagedInstanceEncryptionProtectors.Get(resourceGroup.Name, managedInstance.Name); Assert.Equal("AzureKeyVault", encProtector1.ServerKeyType); Assert.Equal(serverKeyName, encProtector1.ServerKeyName); // Update to Service Managed sqlClient.ManagedInstanceEncryptionProtectors.CreateOrUpdate(resourceGroup.Name, managedInstance.Name, new ManagedInstanceEncryptionProtector() { ServerKeyName = "ServiceManaged", ServerKeyType = "ServiceManaged" }); ManagedInstanceEncryptionProtector encProtector2 = sqlClient.ManagedInstanceEncryptionProtectors.Get(resourceGroup.Name, managedInstance.Name); Assert.Equal("ServiceManaged", encProtector2.ServerKeyType); Assert.Equal("ServiceManaged", encProtector2.ServerKeyName); } }
public void SyncAgentCRUDTest() { string testPrefix = "syncagentcrudtest-"; using (SqlManagementTestContext context = new SqlManagementTestContext(this)) { ResourceGroup resourceGroup = context.CreateResourceGroup(); SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>(); Server server = context.CreateServer(resourceGroup); // Create database string dbName = SqlManagementTestUtilities.GenerateName(testPrefix); Database db = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, new Database() { Location = server.Location, }); Assert.NotNull(db); Assert.NotNull(db.Id); // Create sync agent string agentName = SqlManagementTestUtilities.GenerateName("syncagentcrudtest"); SyncAgent createAgent = sqlClient.SyncAgents.CreateOrUpdate(resourceGroup.Name, server.Name, agentName, new SyncAgent { SyncDatabaseId = db.Id }); Assert.NotNull(createAgent); // Get sync agent SyncAgent getAgent = sqlClient.SyncAgents.Get(resourceGroup.Name, server.Name, agentName); Assert.NotNull(getAgent); // List sync agent IPage <SyncAgent> listAgents = sqlClient.SyncAgents.ListByServer(resourceGroup.Name, server.Name); Assert.Equal(1, listAgents.Count()); Assert.Equal(agentName, listAgents.Single().Name); // Generate key SyncAgentKeyProperties agentKey = sqlClient.SyncAgents.GenerateKey(resourceGroup.Name, server.Name, agentName); Assert.NotNull(agentKey); Assert.NotNull(agentKey.SyncAgentKey); // Regenerate key SyncAgentKeyProperties agentKey2 = sqlClient.SyncAgents.GenerateKey(resourceGroup.Name, server.Name, agentName); Assert.NotNull(agentKey2); Assert.NotNull(agentKey2.SyncAgentKey); // Get linked databases IPage <SyncAgentLinkedDatabase> linkedDatabases = sqlClient.SyncAgents.ListLinkedDatabases(resourceGroup.Name, server.Name, agentName); Assert.NotNull(linkedDatabases); Assert.Equal(0, linkedDatabases.Count()); // Delete the sync agent sqlClient.SyncAgents.Delete(resourceGroup.Name, server.Name, agentName); } }