Example #1
0
        public void TestManagedInstanceLongTermRetentionResourceGroupBasedCrud()
        {
            // MANUAL TEST INSTRUCTIONS
            // PlayBack Mode:
            //     Make sure information below matches what is gets recorded in Session Records
            // Record Mode:
            //     Create a server and database and fill in the appropriate information below
            //     Set the weekly retention on the database so that the first backup gets picked up
            //     Wait about 18 hours until it gets properly copied and you see the backup when run get backups
            //     OR
            //     Use existing instance/database that already has LTR backups
            //
            string locationName        = "southeastasia";
            string resourceGroupName   = "sdk-test-rg";
            string managedInstanceName = "sdk-test-mi";
            string databaseName        = "test";

            using (SqlManagementTestContext context = new SqlManagementTestContext(this))
            {
                SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>();
                //ManagedDatabase database = sqlClient.ManagedDatabases.Get(resourceGroupName, managedInstanceName, databaseName);

                // Get the backups under the location, server, and database. Assert there is at least one backup for each call.
                //
                IPage <ManagedInstanceLongTermRetentionBackup> backups = sqlClient.LongTermRetentionManagedInstanceBackups.ListByResourceGroupLocation(resourceGroupName, locationName);
                Assert.True(backups.Count() >= 1);
                backups = sqlClient.LongTermRetentionManagedInstanceBackups.ListByResourceGroupInstance(resourceGroupName, locationName, managedInstanceName);
                Assert.True(backups.Count() >= 1);
                backups = sqlClient.LongTermRetentionManagedInstanceBackups.ListByResourceGroupDatabase(resourceGroupName, locationName, managedInstanceName, databaseName);
                Assert.True(backups.Count() >= 1);

                // Get a specific backup using the previous call
                //
                ManagedInstanceLongTermRetentionBackup backup = sqlClient.LongTermRetentionManagedInstanceBackups.GetByResourceGroup(resourceGroupName, locationName, managedInstanceName, databaseName, backups.First().Name);
                Assert.NotNull(backup);

                // Restore the backup
                //
                ManagedDatabase restoredDatabase = sqlClient.ManagedDatabases.CreateOrUpdate(
                    resourceGroupName, managedInstanceName, databaseName: SqlManagementTestUtilities.GenerateName(),
                    parameters: new ManagedDatabase
                {
                    Location   = locationName,
                    CreateMode = CreateMode.RestoreLongTermRetentionBackup,
                    LongTermRetentionBackupResourceId = backup.Id
                });

                // Delete the backup.
                //
                sqlClient.LongTermRetentionManagedInstanceBackups.DeleteByResourceGroupWithHttpMessagesAsync(resourceGroupName, locationName, managedInstanceName, databaseName, backup.Name);
            }
        }
 private AzureSqlManagedDatabaseLongTermRetentionBackupModel GetBackupModel(ManagedInstanceLongTermRetentionBackup backup, string locationName)
 {
     return(new AzureSqlManagedDatabaseLongTermRetentionBackupModel()
     {
         BackupExpirationTime = backup.BackupExpirationTime,
         BackupName = backup.Name,
         BackupTime = backup.BackupTime,
         DatabaseDeletionTime = backup.DatabaseDeletionTime,
         DatabaseName = backup.DatabaseName,
         Location = locationName,
         ResourceId = backup.Id,
         InstanceCreateTime = backup.ManagedInstanceCreateTime,
         ManagedInstanceName = backup.ManagedInstanceName,
         ResourceGroupName = GetResourceGroupNameFromResourceId(backup.Id)
     });
 }