public string GetToken(string tenantId)
        {
            AuthenticationContext context = new AuthenticationContext(ServiceUrls.GetLoginUrl(app.Default.AzureEnvironment) + tenantId);

            AuthenticationResult result = null;

            result = context.AcquireToken(ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment), app.Default.ClientId, new Uri(app.Default.ReturnURL), PromptBehavior.Auto);
            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the token");
            }


            return(result.AccessToken);
        }
Exemple #2
0
        public virtual XmlDocument GetAzureASMResources(string resourceType, string subscriptionId, Hashtable info, string token)
        {
            _logProvider.WriteLog("GetAzureASMResources", "Start");

            string url = null;

            switch (resourceType)
            {
            case "Subscriptions":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions";
                _statusProvider.UpdateStatus("BUSY: Getting Subscriptions...");
                break;

            case "VirtualNetworks":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/virtualnetwork";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Networks for Subscription ID : " + subscriptionId + "...");
                break;

            case "ClientRootCertificates":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway/clientrootcertificates";
                _statusProvider.UpdateStatus("BUSY: Getting Client Root Certificates for Virtual Network : " + info["virtualnetworkname"] + "...");
                break;

            case "ClientRootCertificate":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway/clientrootcertificates/" + info["thumbprint"];
                _statusProvider.UpdateStatus("BUSY: Getting certificate data for certificate : " + info["thumbprint"] + "...");
                break;

            case "NetworkSecurityGroup":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/networksecuritygroups/" + info["name"] + "?detaillevel=Full";
                _statusProvider.UpdateStatus("BUSY: Getting Network Security Group : " + info["name"] + "...");
                break;

            case "RouteTable":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/routetables/" + info["name"] + "?detailLevel=full";
                _statusProvider.UpdateStatus("BUSY: Getting Route Table : " + info["routetablename"] + "...");
                break;

            case "NSGSubnet":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/virtualnetwork/" + info["virtualnetworkname"] + "/subnets/" + info["subnetname"] + "/networksecuritygroups";
                _statusProvider.UpdateStatus("BUSY: Getting NSG for subnet " + info["subnetname"] + "...");
                break;

            case "VirtualNetworkGateway":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Network Gateway : " + info["virtualnetworkname"] + "...");
                break;

            case "VirtualNetworkGatewaySharedKey":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway/connection/" + info["localnetworksitename"] + "/sharedkey";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Network Gateway Shared Key: " + info["localnetworksitename"] + "...");
                break;

            case "StorageAccounts":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/storageservices";
                _statusProvider.UpdateStatus("BUSY: Getting Storage Accounts for Subscription ID : " + subscriptionId + "...");
                break;

            case "StorageAccount":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/storageservices/" + info["name"];
                _statusProvider.UpdateStatus("BUSY: Getting Storage Accounts for Subscription ID : " + subscriptionId + "...");
                break;

            case "StorageAccountKeys":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/storageservices/" + info["name"] + "/keys";
                _statusProvider.UpdateStatus("BUSY: Getting Storage Accounts for Subscription ID : " + subscriptionId + "...");
                break;

            case "CloudServices":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/hostedservices";
                _statusProvider.UpdateStatus("BUSY: Getting Cloud Services for Subscription ID : " + subscriptionId + "...");
                break;

            case "CloudService":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/hostedservices/" + info["name"] + "?embed-detail=true";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Machines for Cloud Service : " + info["name"] + "...");
                break;

            case "VirtualMachine":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/hostedservices/" + info["cloudservicename"] + "/deployments/" + info["deploymentname"] + "/roles/" + info["virtualmachinename"];
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Machines for Cloud Service : " + info["virtualmachinename"] + "...");
                break;

            case "VMImages":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/images";
                break;

            case "ReservedIPs":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/reservedips";
                break;

            case "AffinityGroup":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/affinitygroups/" + info["affinitygroupname"];
                break;
            }

            Application.DoEvents();

            _logProvider.WriteLog("GetAzureASMResources", "GET " + url);

            if (_documentCache.ContainsKey(url))
            {
                _logProvider.WriteLog("GetAzureASMResources", "FROM XML CACHE");
                _logProvider.WriteLog("GetAzureASMResources", "End");
                writeXMLtoFile(url, "Cached");
                return(_documentCache[url]);
            }

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
            request.Headers.Add("x-ms-version", "2015-04-01");
            request.Method = "GET";


            string xml = "";

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                xml = new StreamReader(response.GetResponseStream()).ReadToEnd();
                _logProvider.WriteLog("GetAzureASMResources", "RESPONSE " + response.StatusCode);
            }
            catch (Exception exception)
            {
                _logProvider.WriteLog("GetAzureASMResources", "EXCEPTION " + exception.Message);
                DialogResult dialogresult = MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                xml = "";
                //Application.ExitThread();
            }

            if (xml != "")
            {
                XmlDocument xmlDoc = RemoveXmlns(xml);

                _logProvider.WriteLog("GetAzureASMResources", "End");
                writeXMLtoFile(url, xml);

                _documentCache.Add(url, xmlDoc);
                return(xmlDoc);
            }
            else
            {
                //XmlNodeList xmlnode = null;
                //return xmlnode;
                XmlDocument xmlDoc = new XmlDocument();

                _logProvider.WriteLog("GetAzureASMResources", "End");
                writeXMLtoFile(url, "");
                return(null);
            }
        }
Exemple #3
0
        public virtual string GetAzureARMResources(string resourceType, string subscriptionId, Hashtable info, string token, string RGName)
        {
            _logProvider.WriteLog("GetAzureARMResources", "Start");

            string url = null;

            switch (resourceType)
            {
            case "Subscriptions":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions?api-version=2015-01-01";
                _statusProvider.UpdateStatus("BUSY: Getting Subscriptions...");
                break;

            case "Tenants":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "tenants?api-version=2015-01-01";
                _statusProvider.UpdateStatus("BUSY: Getting Tenants...");
                break;

            case "RG":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions/" + subscriptionId + "/resourcegroups?api-version=2015-01-01";
                _statusProvider.UpdateStatus("BUSY: Getting Resource Groups...");
                break;

            case "VirtualNetworks":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions/" + subscriptionId + "/resourceGroups/" + RGName + "/providers/Microsoft.Network/virtualnetworks?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Networks for Subscription ID : " + subscriptionId + " in the Resource Group " + RGName + "...");
                break;

            case "VirtualNetwork":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["VirtualNWId"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Network details...");
                break;

            case "VirtualMachines":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions/" + subscriptionId + "/resourceGroups/" + RGName + "/providers/Microsoft.Compute/virtualMachines?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Machines for Subscription ID : " + subscriptionId + " in the Resource Group " + RGName + "...");
                break;

            case "VirtualMachine":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions/" + subscriptionId + "/resourceGroups/" + RGName + "/providers/Microsoft.Compute/virtualMachines/" + info["virtualmachineName"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Machine for Subscription ID : " + subscriptionId + " in the Resource Group " + RGName + "...");
                break;

            case "NetworkInterface":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["networkinterfaceId"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Network Interface details...");
                break;

            case "Loadbalancer":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["LBId"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting LoadBalancer details...");
                break;

            case "NetworkSecurityGroup":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["NetworkSecurityGroup"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Network Security Group : " + info["name"] + "...");
                break;

            case "RouteTable":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["RouteTable"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Route Table : " + info["routetablename"] + "...");
                break;

            case "Subnet":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["SubnetId"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Subnet details : " + info["SubnetId"] + "...");
                break;

            case "AvailabilitySet":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["AvailId"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Availability Set details : " + info["AvailId"] + "...");
                break;

            case "VirtualNetworkGateway":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions/" + subscriptionId + "/resourceGroups/" + info["RGName"] + "/providers/Microsoft.Network/virtualNetworkGateways/" + info["vnetGWName"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Network Gateway : " + info["vnetGWName"] + "...");
                break;

            case "PublicIP":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["publicipId"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting PublicIP details...");
                break;

            case "Connections":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions/" + subscriptionId + "/resourceGroups/" + RGName + "/providers/Microsoft.Network/connections?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting Connections from the Resource Group : " + RGName + "...");
                break;

            case "Domains":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.GraphAuth) + "myorganization/domains?api-version=1.6";
                _statusProvider.UpdateStatus("BUSY: Getting Tenant Domain details from Graph...");
                break;

            case "sharedkey":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["connectionid"] + "/sharedkey?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting SharedKey details...");
                break;

            case "localNetworkGateway":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + info["localnwgwid"] + "?api-version=2016-03-30";
                _statusProvider.UpdateStatus("BUSY: Getting LocalNWGateway details...");
                break;

            case "ClientRootCertificates":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway/clientrootcertificates";
                _statusProvider.UpdateStatus("BUSY: Getting Client Root Certificates for Virtual Network : " + info["virtualnetworkname"] + "...");
                break;

            case "ClientRootCertificate":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway/clientrootcertificates/" + info["thumbprint"];
                _statusProvider.UpdateStatus("BUSY: Getting certificate data for certificate : " + info["thumbprint"] + "...");
                break;

            case "NSGSubnet":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/virtualnetwork/" + info["virtualnetworkname"] + "/subnets/" + info["subnetname"] + "/networksecuritygroups";
                _statusProvider.UpdateStatus("BUSY: Getting NSG for subnet " + info["subnetname"] + "...");
                break;

            case "VirtualNetworkGatewaySharedKey":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway/connection/" + info["localnetworksitename"] + "/sharedkey";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Network Gateway Shared Key: " + info["localnetworksitename"] + "...");
                break;

            case "StorageAccounts":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions/" + subscriptionId + "/resourceGroups/" + RGName + "/providers/Microsoft.Storage/storageAccounts?api-version=2015-05-01-preview";
                _statusProvider.UpdateStatus("BUSY: Getting Storage Accounts for Subscription ID : " + subscriptionId + " in the Resource Group " + RGName + "...");
                break;

            case "StorageAccount":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions/" + subscriptionId + "/resourceGroups/" + RGName + "/providers/Microsoft.Storage/storageAccounts/" + info["name"] + "?api-version=2015-05-01-preview";
                _statusProvider.UpdateStatus("BUSY: Getting Storage Accounts for Subscription ID : " + subscriptionId + "...");
                break;

            case "StorageAccountKeys":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + "subscriptions/" + subscriptionId + "/resourceGroups/" + RGName + "/providers/Microsoft.Storage/storageAccounts/" + info["name"] + "/listKeys?api-version=2016-01-01";
                _statusProvider.UpdateStatus("BUSY: Getting Storage Accounts for Subscription ID : " + subscriptionId + "...");
                break;

            case "CloudServices":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/hostedservices";
                _statusProvider.UpdateStatus("BUSY: Getting Cloud Services for Subscription ID : " + subscriptionId + "...");
                break;

            case "CloudService":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/hostedservices/" + info["name"] + "?embed-detail=true";
                _statusProvider.UpdateStatus("BUSY: Getting Virtual Machines for Cloud Service : " + info["name"] + "...");
                break;

            case "VMImages":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/images";
                break;

            case "ReservedIPs":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/services/networking/reservedips";
                break;

            case "AffinityGroup":
                url = ServiceUrls.GetServiceManagementUrl(app.Default.AzureEnvironment) + subscriptionId + "/affinitygroups/" + info["affinitygroupname"];
                break;
            }

            Application.DoEvents();

            _logProvider.WriteLog("GetAzureARMResources", "GET " + url);


            if (_documentCache.ContainsKey(url))
            {
                _logProvider.WriteLog("GetAzureARMResources", "FROM XML CACHE");
                _logProvider.WriteLog("GetAzureARMResources", "End");
                writeXMLtoFile(url, "Cached");
                return(_documentCache[url]);
            }


            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
            request.ContentType = "application/json";
            // request.Headers.Add("x-ms-version", "2015-04-01");
            if (resourceType == "StorageAccountKeys")
            {
                request.Method        = "POST";
                request.ContentLength = 0;
            }
            else
            {
                request.Method = "GET";
            }
            string json = "";

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                json = new StreamReader(response.GetResponseStream()).ReadToEnd();
                _logProvider.WriteLog("GetAzureARMResources", "RESPONSE " + response.StatusCode);

                /*
                 * using (var streamReader = new StreamReader(response.GetResponseStream()))
                 * {
                 *   result = streamReader.ReadToEnd();
                 * }
                 */
            }
            catch (Exception exception)
            {
                _logProvider.WriteLog("GetAzureARMResources", "EXCEPTION " + exception.Message);
                DialogResult dialogresult = MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                json = "";
                //Application.ExitThread();
            }

            if (json != "")
            {
                _logProvider.WriteLog("GetAzureARMResources", "End");
                writeXMLtoFile(url, json);

                if ((resourceType != "Subscriptions") && (resourceType != "Domains"))
                {
                    _documentCache.Add(url, json);
                }

                return(json);
            }
            else
            {
                _logProvider.WriteLog("GetAzureARMResources", "End");
                writeXMLtoFile(url, "");
                return(null);
            }
        }