/// <summary> /// Provisions all the databases in the <see cref="IEnumerable{T}"/> of <see cref="SqlAzureDb"/>. /// </summary> /// <param name="databases">The list of <see cref="SqlAzureDb"/> to provision.</param> /// <param name="client">The <see cref="SqlManagementClient"/> that is performing the operation.</param> /// <returns>The async <see cref="Task"/> wrapper.</returns> public static IEnumerable <Task> ProvisionAllAsync(this IEnumerable <SqlAzureDb> databases, SqlManagementClient client) { Contract.Requires(databases != null); Contract.Requires(client != null); return(databases.Select(d => Task.Factory.StartNew(() => client.CreateDatabaseIfNotExistsAsync(d).Wait()))); }
/// <summary> /// Provisions all the databases in the <see cref="IEnumerable{T}"/> of <see cref="SqlAzureDb"/>. /// </summary> /// <param name="databases">The list of <see cref="SqlAzureDb"/> to provision.</param> /// <param name="client">The <see cref="SqlManagementClient"/> that is performing the operation.</param> /// <returns>The async <see cref="Task"/> wrapper.</returns> public static async Task ProvisionAllAsync(this IEnumerable <SqlAzureDb> databases, SqlManagementClient client) { Contract.Requires(databases != null); Contract.Requires(client != null); var tasks = databases.Select( async d => { await client.CreateDatabaseIfNotExistsAsync(d); }); await Task.WhenAll(tasks); }
/// <summary> /// Checks for the existence of a specific Azure Sql Database, if it doesn't exist it will create it. /// </summary> /// <param name="client">The <see cref="SqlManagementClient"/> that is performing the operation.</param> /// <param name="model">The DevOpsFlex rich model object that contains everything there is to know about this database spec.</param> /// <returns>The async <see cref="Task"/> wrapper.</returns> public static async Task CreateDatabaseIfNotExistsAsync(this SqlManagementClient client, SqlAzureDb model) { Contract.Requires(client != null); Contract.Requires(model != null); string serverName; lock (SqlServerGate) { FlexStreams.BuildEventsObserver.OnNext(new CheckForParentResourceEvent(AzureResource.SqlServer, AzureResource.SqlDatabase, model.Name)); serverName = FlexConfiguration.SqlServerChooser.Choose(client, model.System.Location.GetEnumDescription()).Result; if (serverName == null) { string serverMaxVersion = null; try { client.Servers.Create( new ServerCreateParameters { AdministratorUserName = FlexConfiguration.FlexSaUser, AdministratorPassword = FlexConfiguration.FlexSaPwd, Location = model.System.Location.GetEnumDescription(), Version = "100.0" // This needs to be an invalid version number }); } catch (CloudException ex) { if (ex.Error.Code == "40856") // SQL Version doesn't exist in the target location { var serverVersions = Regex.Match(ex.Error.Message, "server versions: '([^']*)'.").Groups[1].Value.Split(','); var maxVersion = serverVersions.Max((s => decimal.Parse(s))); serverMaxVersion = serverVersions.First(s => s.Contains(maxVersion.ToString("F1", CultureInfo.InvariantCulture))); } else { throw; } } serverName = client.Servers.Create( new ServerCreateParameters { AdministratorUserName = FlexConfiguration.FlexSaUser, AdministratorPassword = FlexConfiguration.FlexSaPwd, Location = model.System.Location.GetEnumDescription(), Version = serverMaxVersion }).ServerName; FlexStreams.BuildEventsObserver.OnNext(new ProvisionEvent(AzureResource.SqlServer, serverName)); } else { FlexStreams.BuildEventsObserver.OnNext(new FoundExistingEvent(AzureResource.SqlServer, serverName)); } } var dbName = FlexConfiguration.GetNaming <SqlAzureDb>() .GetSlotName( model, FlexDataConfiguration.Branch, FlexDataConfiguration.Configuration); await client.CreateDatabaseIfNotExistsAsync(serverName, dbName, model.Edition.GetEnumDescription(), model.CollationName, model.MaximumDatabaseSizeInGB, model.CreateAppUser); }