public void TestImportExport(bool preexistingDatabase, string testName, bool useNetworkIsolation)
        {
            using (SqlManagementTestContext context = new SqlManagementTestContext(this, testName))
            {
                ResourceGroup       resourceGroup = context.CreateResourceGroup();
                Server              server        = context.CreateServer(resourceGroup);
                SqlManagementClient sqlClient     = context.GetClient <SqlManagementClient>();

                string storageAccountName = SqlManagementTestUtilities.GenerateName(prefix: "sqlcrudtest");

                // Begin creating storage account and container
                Task <StorageContainerInfo> storageContainerTask = CreateStorageContainer(context, resourceGroup, storageAccountName);

                string login    = SqlManagementTestUtilities.DefaultLogin;
                string password = SqlManagementTestUtilities.DefaultPassword;
                string dbName   = SqlManagementTestUtilities.GenerateName();
                string dbName2  = SqlManagementTestUtilities.GenerateName();

                Dictionary <string, string> tags = new Dictionary <string, string>()
                {
                    { "tagKey1", "TagValue1" }
                };

                // set server firewall rule
                sqlClient.FirewallRules.CreateOrUpdate(resourceGroup.Name, server.Name, SqlManagementTestUtilities.GenerateName(), new FirewallRule()
                {
                    StartIpAddress = "0.0.0.0",
                    EndIpAddress   = "255.255.255.255"
                });

                // Create 1 or 2 databases
                sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName, new Database()
                {
                    Location = server.Location
                });

                if (preexistingDatabase)
                {
                    sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, dbName2, new Database()
                    {
                        Location = server.Location
                    });

                    // Verify existence of new database
                    Assert.NotNull(sqlClient.Databases.Get(resourceGroup.Name, server.Name, dbName2));
                }

                // Get Storage container credentials
                StorageContainerInfo storageContainerInfo = storageContainerTask.Result;
                string exportBacpacLink = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}/{1}.bacpac",
                    storageContainerInfo.StorageContainerUri, dbName);

                string sqlServerResourceId      = $"/subscriptions/{sqlClient.SubscriptionId}/resourceGroups/{resourceGroup.Name}/providers/Microsoft.Sql/servers/{server.Name}";
                string storageAccountResourceId = $"/subscriptions/{sqlClient.SubscriptionId}/resourcegroups/{resourceGroup.Name}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}";

                var exportDatabaseDefinition = new ExportDatabaseDefinition()
                {
                    AdministratorLogin         = login,
                    AdministratorLoginPassword = password,
                    AuthenticationType         = "SQL",
                    StorageKey     = storageContainerInfo.StorageAccountKey,
                    StorageKeyType = StorageKeyType.StorageAccessKey.ToString(),
                    StorageUri     = exportBacpacLink
                };

                if (useNetworkIsolation)
                {
                    exportDatabaseDefinition.NetworkIsolation = new NetworkIsolationSettings()
                    {
                        SqlServerResourceId      = sqlServerResourceId,
                        StorageAccountResourceId = storageAccountResourceId
                    };
                }

                // Export database to bacpac
                sqlClient.Databases.Export(resourceGroup.Name, server.Name, dbName, exportDatabaseDefinition);

                // Import bacpac to new/existing database
                if (preexistingDatabase)
                {
                    var importDatabaseDefinition = new ImportExistingDatabaseDefinition()
                    {
                        AdministratorLogin         = login,
                        AdministratorLoginPassword = password,
                        AuthenticationType         = "SQL",
                        StorageKey     = storageContainerInfo.StorageAccountKey,
                        StorageKeyType = StorageKeyType.StorageAccessKey.ToString(),
                        StorageUri     = exportBacpacLink
                    };

                    if (useNetworkIsolation)
                    {
                        importDatabaseDefinition.NetworkIsolation = new NetworkIsolationSettings()
                        {
                            SqlServerResourceId      = sqlServerResourceId,
                            StorageAccountResourceId = storageAccountResourceId
                        };
                    }

                    // Import bacpac to existing database
                    sqlClient.Databases.ImportWithHttpMessagesAsync(resourceGroup.Name, server.Name, dbName2, importDatabaseDefinition).Wait();
                }
                else
                {
                    var importDatabaseDefinition = new ImportNewDatabaseDefinition()
                    {
                        AdministratorLogin         = login,
                        AdministratorLoginPassword = password,
                        AuthenticationType         = "SQL",
                        StorageKey           = storageContainerInfo.StorageAccountKey,
                        StorageKeyType       = StorageKeyType.StorageAccessKey.ToString(),
                        StorageUri           = exportBacpacLink,
                        DatabaseName         = dbName2,
                        Edition              = SqlTestConstants.DefaultDatabaseEdition,
                        ServiceObjectiveName = ServiceObjectiveName.Basic,
                        MaxSizeBytes         = (2 * 1024L * 1024L * 1024L).ToString(),
                    };

                    if (useNetworkIsolation)
                    {
                        importDatabaseDefinition.NetworkIsolation = new NetworkIsolationSettings()
                        {
                            SqlServerResourceId      = sqlServerResourceId,
                            StorageAccountResourceId = storageAccountResourceId
                        };
                    }

                    sqlClient.Servers.ImportDatabase(resourceGroup.Name, server.Name, importDatabaseDefinition);
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Imports a bacpac into a new database.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group that contains the resource. You can obtain
 /// this value from the Azure Resource Manager API or the portal.
 /// </param>
 /// <param name='serverName'>
 /// The name of the server.
 /// </param>
 /// <param name='parameters'>
 /// The database import request parameters.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <ImportExportOperationResult> BeginImportDatabaseAsync(this IServersOperations operations, string resourceGroupName, string serverName, ImportNewDatabaseDefinition parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.BeginImportDatabaseWithHttpMessagesAsync(resourceGroupName, serverName, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
        /// <summary>
        /// Creates new import request
        /// </summary>
        public ImportExportOperationResult BeginImportNewDatabase(string resourceGroupName, string serverName, ImportNewDatabaseDefinition parameters, out Uri operationStatusLink)
        {
            LastLocationHeader = null;
            LocationHeaderResetEvent.Reset();
            ImportExportOperationResult result = GetCurrentSqlClient().Servers.BeginImportDatabase(resourceGroupName, serverName, parameters);

            LocationHeaderResetEvent.WaitOne(3000);
            operationStatusLink = LastLocationHeader;
            return(result);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Imports a bacpac into a new database.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group that contains the resource. You can obtain
 /// this value from the Azure Resource Manager API or the portal.
 /// </param>
 /// <param name='serverName'>
 /// The name of the server.
 /// </param>
 /// <param name='parameters'>
 /// The database import request parameters.
 /// </param>
 public static ImportExportOperationResult BeginImportDatabase(this IServersOperations operations, string resourceGroupName, string serverName, ImportNewDatabaseDefinition parameters)
 {
     return(operations.BeginImportDatabaseAsync(resourceGroupName, serverName, parameters).GetAwaiter().GetResult());
 }