/// <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="serverName">The name of the server that we want to use to create the database.</param> /// <param name="databaseName">The name of the database we are creating.</param> /// <param name="databaseEdition">The edition of the database we are creating.</param> /// <param name="collationName">The database collation name.</param> /// <param name="sizeInGb">The maximum database size in GB.</param> /// <param name="createAppUser"></param> /// <returns>The async <see cref="Task"/> wrapper.</returns> public static async Task CreateDatabaseIfNotExistsAsync( this SqlManagementClient client, string serverName, string databaseName, string databaseEdition, string collationName, int sizeInGb, bool createAppUser) { Contract.Requires(client != null); Contract.Requires(!string.IsNullOrWhiteSpace(serverName)); Contract.Requires(!string.IsNullOrWhiteSpace(databaseName)); Contract.Requires(!string.IsNullOrWhiteSpace(databaseEdition)); Contract.Requires(!string.IsNullOrWhiteSpace(collationName)); Contract.Requires(sizeInGb > 0 && sizeInGb <= 250); DatabaseGetResponse ns = null; FlexStreams.BuildEventsObserver.OnNext(new CheckIfExistsEvent(AzureResource.SqlDatabase, databaseName)); try { ns = await client.Databases.GetAsync(serverName, databaseName); } catch (CloudException cex) { if (!cex.Error.Message.Contains($"Resource with the name '{databaseName}' does not exist")) { throw; } } if (ns != null) { FlexStreams.BuildEventsObserver.OnNext(new FoundExistingEvent(AzureResource.SqlDatabase, databaseName)); return; } await client.Databases.CreateAsync( serverName, new DatabaseCreateParameters { Name = databaseName, Edition = databaseEdition, CollationName = collationName, MaximumDatabaseSizeInGB = sizeInGb, }); FlexStreams.BuildEventsObserver.OnNext(new ProvisionEvent(AzureResource.SqlDatabase, databaseName)); if (!createAppUser) { return; } using (var adb = new DevOpsAzureDatabase(serverName, databaseName, FlexConfiguration.FlexSaUser, FlexConfiguration.FlexSaPwd)) { await adb.CreateDatabaseUserAsync(FlexConfiguration.FlexAppUser, FlexConfiguration.FlexAppUser, "dbo"); } }
/// <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="serverName">The name of the server that we want to use to create the database.</param> /// <param name="databaseName">The name of the database we are creating.</param> /// <param name="databaseEdition">The edition of the database we are creating.</param> /// <param name="collationName">The database collation name.</param> /// <param name="sizeInGb">The maximum database size in GB.</param> /// <param name="createAppUser"></param> /// <returns>The async <see cref="Task"/> wrapper.</returns> public static async Task CreateDatabaseIfNotExistsAsync( this SqlManagementClient client, string serverName, string databaseName, string databaseEdition, string collationName, int sizeInGb, bool createAppUser) { Contract.Requires(client != null); Contract.Requires(!string.IsNullOrWhiteSpace(serverName)); Contract.Requires(!string.IsNullOrWhiteSpace(databaseName)); Contract.Requires(!string.IsNullOrWhiteSpace(databaseEdition)); Contract.Requires(!string.IsNullOrWhiteSpace(collationName)); Contract.Requires(sizeInGb > 0 && sizeInGb <= 250); DatabaseGetResponse ns = null; FlexStreams.BuildEventsObserver.OnNext(new CheckIfExistsEvent(AzureResource.SqlDatabase, databaseName)); try { ns = await client.Databases.GetAsync(serverName, databaseName); } catch (CloudException cex) { if (!cex.Error.Message.Contains($"Resource with the name '{databaseName}' does not exist")) throw; } if (ns != null) { FlexStreams.BuildEventsObserver.OnNext(new FoundExistingEvent(AzureResource.SqlDatabase, databaseName)); return; } await client.Databases.CreateAsync( serverName, new DatabaseCreateParameters { Name = databaseName, Edition = databaseEdition, CollationName = collationName, MaximumDatabaseSizeInGB = sizeInGb, }); FlexStreams.BuildEventsObserver.OnNext(new ProvisionEvent(AzureResource.SqlDatabase, databaseName)); if (!createAppUser) return; using (var adb = new DevOpsAzureDatabase(serverName, databaseName, FlexConfiguration.FlexSaUser, FlexConfiguration.FlexSaPwd)) { await adb.CreateDatabaseUserAsync(FlexConfiguration.FlexAppUser, FlexConfiguration.FlexAppUser, "dbo"); } }