/// <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); }
/// <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); }