private SqlAzureServer FindServerToReuse(string user, string password) { SqlAzureServer server = null; try { var servers = GetSqlServersDetails(); foreach (var s in servers) { if (string.Compare( s.AdministratorUserName, user, StringComparison.InvariantCultureIgnoreCase) == 0) { var tempServer = new SqlAzureServer(s.Name, user, password); if (!tempServer.CanHaveMoreDatabases) { continue; } server = tempServer; break; } } } catch (Exception e) { TestEasyLog.Instance.Warning(string.Format("Could not reuse SqlServers, there was an exception: '{0}'", e.Message)); } return(server); }
/// <summary> /// Create a database on sql server /// </summary> /// <param name="databaseName"></param> /// <param name="server"></param> /// <param name="maxSize"></param> /// <returns></returns> public SqlAzureDatabase CreateSqlDatabase( string databaseName, SqlAzureServer server, SqlAzureDatabaseMaxSize maxSize = SqlAzureDatabaseMaxSize.Gb1) { var existingDatabasesOnServer = SqlManagementClient.Databases.ListAsync(server.Name, new CancellationToken()).Result.Databases; if (existingDatabasesOnServer.Any(d => d.Name == databaseName)) { SqlManagementClient.Databases.DeleteAsync(server.Name, databaseName, new CancellationToken()).Wait(); } return(server.CreateDatabase(databaseName, maxSize)); }
/// <summary> /// Create sql server /// </summary> /// <param name="user"></param> /// <param name="password"></param> /// <param name="location"></param> /// <param name="reuseExistingIfPossible"></param> /// <returns></returns> public SqlAzureServer CreateSqlServer(string user = AzureServiceConstants.SqlServerAdminLogin, string password = AzureServiceConstants.SqlServerAdminPassword, string location = null, bool reuseExistingIfPossible = true) { if (string.IsNullOrEmpty(location)) { location = Dependencies.Subscription.DefaultLocation; } SqlAzureServer server = null; var ip = GetInterNetworkIp(); var pip = GetPublicIP(); if (reuseExistingIfPossible) { server = FindServerToReuse(user, password); } if (server == null) { TestEasyLog.Instance.Info(string.Format("Creating azure sql server with login '{0}'", user)); var newServerName = SqlManagementClient.Servers.CreateAsync(new ServerCreateParameters { AdministratorPassword = password, AdministratorUserName = user, Location = location, }, new CancellationToken()).Result.ServerName; server = new SqlAzureServer(newServerName, user, password); } Dependencies.TestResourcesCollector.Remember(AzureResourceType.SqlServer, server.Name, server); server.CreateFirewallRule("azureapps", "0.0.0.0", "0.0.0.0"); server.CreateFirewallRule("workaround", "0.0.0.0", "255.255.255.255"); server.CreateFirewallRule(Environment.MachineName + "_ip", ip, ip); server.CreateFirewallRule(Environment.MachineName + "_pip", pip, pip); return(server); }