Wednesday 22 August 2018

How to Create a Database on Azure Elastic Pool.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Sql;
using Microsoft.Azure.Management.Sql.Models;
using Microsoft.Azure;
using Microsoft.Azure.Management.Resources.Models;
using System.Web.Mvc;
using saytya.Models;

namespace Satya.Utils
{
    public class DatabaseElaticPoolHelper
    {
        private static string azureActiveDirectoryInstance = "https://login.microsoftonline.com/";
        // private static string tenant = "satya@satyabrata.onmicrosoft.com";
        private static string tenant = "satyabrata.onmicrosoft.com";
        private static string clientId = "dsdsdsdsds-weew-4d46-rewrw-353455";
       
        private static string appKey = "fdsfg4545366546a/657jyj65534fgfn54=";
        private static string resource = "https://management.core.windows.net/";
        private static string resourceGroup = "satyabrata";
        private static string subscriptionId = "42424324-7777-466-hg65-hgh75765756";

        static string authority = azureActiveDirectoryInstance + tenant;
        public  string  CreateDtabase(string dbname)
        {
            // "satya@satyabrata.onmicrosoft.com";
            var client = GetSqlManagementClient();

            // The Db we want to use as template
            var originalDb = client.Databases.Get(resourceGroup, "satyaDB", "SatyaMeta");

            // Create a new database within the same elastic pool
            var parameters = new DatabaseCreateOrUpdateParameters()
            {
                Location = "Central India",
                Properties = new DatabaseCreateOrUpdateProperties()
                {
                    ElasticPoolName = "satyaPOOL",
                    SourceDatabaseId = originalDb.Database.Id
                }
            };
            var response = client.Databases.CreateOrUpdate(
              resourceGroup,
              "Satyabratabege",
              dbname,
              parameters
            );
          var ss= response.Status;
            if(ss.ToString()== "Succeeded")
            {
                return "Yes";
            }
            else
            {
                return "No";
            }
        }

        private static string GetToken()
        {
            var httpClient = new HttpClient();
            var authContext = new AuthenticationContext(authority);
            var clientCredential = new ClientCredential(clientId, appKey);
            var result = authContext.AcquireToken(resource, clientCredential);
            return result.AccessToken;
        }

        private static SqlManagementClient GetSqlManagementClient()
        {
            var token = GetToken();
            var credentials = new Microsoft.Azure.TokenCloudCredentials(subscriptionId, token);
            var client = new SqlManagementClient(credentials);
            return client;
        }
    }
}

No comments:

Post a Comment