public SqlManagementController(string publishSettingsFilePath)
        {
            // To authenticate against the Microsoft Azure service management API we require management certificate
            // load this from a publish settings file and use it to new up an instance of SqlManagementClient CloudClient
            var credentials = CredentialsHelper.GetSubscriptionCloudCredentials(publishSettingsFilePath);

            _sqlManagementClient = CloudContext.Clients.CreateSqlManagementClient(credentials);
        }
 private SqlManagementClient createSqlManagementClient()
 {
     //To use this, create and upload your own management certificate for Azure.  Then export the pfx file from Certificates MMC with the private key.
     //Save that file in the root directory called azure-mgt-cert.pfx and add the password to the web.config file
     //The file in this solution is empty and just a placeholder
     var cert = new X509Certificate2(Server.MapPath("/azure-mgt-cert.pfx"), ConfigurationManager.AppSettings["CertificatePassword"], X509KeyStorageFlags.MachineKeySet);
     SqlManagementClient sqlManagementClient = new SqlManagementClient(new CertificateCloudCredentials(ConfigurationManager.AppSettings["SubscriptionID"], cert));
     return sqlManagementClient;
 }
 /// <summary>
 /// Add the tracing session and request headers to the client.
 /// </summary>
 /// <param name="sqlManagementClient">The client to add the headers on.</param>
 private void AddTracingHeaders(SqlManagementClient sqlManagementClient)
 {
     sqlManagementClient.HttpClient.DefaultRequestHeaders.Add(
         Constants.ClientSessionIdHeaderName,
         this.ClientSessionId);
     sqlManagementClient.HttpClient.DefaultRequestHeaders.Add(
         Constants.ClientRequestIdHeaderName,
         this.ClientRequestId);
 }
        /// <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>
        /// Provisions all the components in the <see cref="DevOpsFlexDbContext"/>.
        /// </summary>
        /// <param name="context">The database context that we want to provision components from.</param>
        /// <param name="subscriptionId">The subscription Id where we want to provision in.</param>
        /// <param name="settingsPath">The path to the settings file with the management certificate.</param>
        /// <returns>The async <see cref="Task"/> wrapper.</returns>
        public static async Task ProvisionAllAsync(this DevOpsFlexDbContext context, string subscriptionId, string settingsPath)
        {
            Contract.Requires(context != null);
            Contract.Requires(string.IsNullOrEmpty(subscriptionId));
            Contract.Requires(string.IsNullOrEmpty(settingsPath));

            var azureSubscription = new AzureSubscription(settingsPath, subscriptionId);
            var azureCert = new X509Certificate2(Convert.FromBase64String(azureSubscription.ManagementCertificate));

            using (var computeClient = new ComputeManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert)))
            using (var networkClient = new NetworkManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert)))
            using (var sbClient = new ServiceBusManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert)))
            using (var sqlClient = new SqlManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert)))
            using (var storageClient = new StorageManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert)))
            using (var webSiteClient = new WebSiteManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert)))
            {
                var tasks = new[]
                {
                    context.Components.OfType<AzureCloudService>().ToList().ProvisionAllAsync(computeClient),
                    context.Components.OfType<AzureCloudService>().ToList().ReserveAllIpsAsync(networkClient),
                    context.Components.OfType<AzureServiceBusNamespace>().ToList().ProvisionAllAsync(sbClient),
                    context.Components.OfType<SqlAzureDb>().ToList().ProvisionAllAsync(sqlClient),
                    context.Components.OfType<AzureStorageContainer>().ToList().ProvisionAllAsync(storageClient),
                    context.Components.OfType<AzureWebSite>().ToList().ProvisionAllAsync(webSiteClient)
                };

                await Task.WhenAll(tasks);
            }
        }
        /// <summary>
        /// Provisions all the firewall rules in the <see cref="IEnumerable{T}"/> of <see cref="SqlFirewallRule"/>.
        /// </summary>
        /// <param name="rules">The list of <see cref="SqlFirewallRule"/> 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<SqlFirewallRule> rules, SqlManagementClient client)
        {
            Contract.Requires(rules != null);
            Contract.Requires(client != null);

            var tasks = rules.Select(
                async r =>
                {
                    await client.CreateFirewallRuleIfNotExistsAsync(r);
                });

            await Task.WhenAll(tasks);
        }
 /// <summary>
 /// Finds a valid SQL Server in the subscription to create web sites on.
 /// </summary>
 /// <param name="client">The <see cref="SqlManagementClient"/> that is performing the operation.</param>
 /// <param name="location">The Azure location where we want to create the SQL Server. This is a value in <see cref="Microsoft.WindowsAzure.Management.Models.LocationNames"/></param>
 /// <returns>A suitable sql server name if one is found, null otherwise.</returns>
 public async Task<string> Choose(SqlManagementClient client, string location)
 {
     return (await client.Servers.ListAsync())
         .FirstOrDefault(s => s.Location == location)?
         .Name;
 }
        private void CreateSqlManagementClient()
        {
            if (_sqlManagementClient != null)
                _sqlManagementClient.Dispose();

            _sqlManagementClient = new SqlManagementClient(
                new CertificateCloudCredentials(_host.SelectedSubscription.SubscriptionId,
                    new X509Certificate2(
                        Convert.FromBase64String(
                            _host.SelectedSubscription.ManagementCertificate)))
                );
        }