Exemple #1
0
        public static async Task CreateResourceGroup(string location, string groupName)
        {
            // get access token
            var accessToken = await AadAuthHelper.GetAadAuthToken(SettingsHelper.AzureManagementApiResourceId);

            // create request
            var azQueryPath = string.Format("subscriptions/{0}/resourcegroups/{1}/?api-version=2016-02-01", SettingsHelper.AzureSubscriptionId, groupName);
            var query       = string.Format(SettingsHelper.AzureManagementApiEndpoint, azQueryPath);
            var request     = (HttpWebRequest)HttpWebRequest.Create(query);

            request.Method      = "PUT";
            request.ContentType = "application/json";
            request.Accept      = "application/json";
            request.Headers.Add("Authorization", "Bearer " + accessToken);

            // create payload
            var newAzResourceGroup = new AzureResourceGroup {
                Location = location
            };
            string content = JsonConvert.SerializeObject(newAzResourceGroup,
                                                         Formatting.None,
                                                         new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }
                                                         );

            using (var writer = new StreamWriter(request.GetRequestStream())) {
                writer.Write(content);
            }

            // submit request
            await request.GetResponseAsync();
        }
Exemple #2
0
        public static async Task <List <AzureResourceGroup> > GetAzureResourceGroups()
        {
            // get access token
            var accessToken = await AadAuthHelper.GetAadAuthToken(SettingsHelper.AzureManagementApiResourceId);

            // setup authenticated http client
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

            // create request
            var azQueryPath = string.Format("subscriptions/{0}/resourceGroups/?api-version=2014-04-01", SettingsHelper.AzureSubscriptionId);
            var query       = string.Format(SettingsHelper.AzureManagementApiEndpoint, azQueryPath);
            var request     = new HttpRequestMessage(HttpMethod.Get, query);

            // issue request and convert response to typed object
            var response = await httpClient.SendAsync(request);

            var rawResponseString = await response.Content.ReadAsStringAsync();

            var azrgResponse = JsonConvert.DeserializeObject <AzureResourceGroups>(rawResponseString);

            // convert to normal collection
            List <AzureResourceGroup> resourceGroups = new List <AzureResourceGroup>();

            resourceGroups.AddRange(azrgResponse.Items);

            return(resourceGroups);
        }
        public static async Task <AzureRateCardHttpResponse> GetRates(string subscriptionId,
                                                                      string offerId,
                                                                      AzureRegion region,
                                                                      string locale)
        {
            // get access token
            var accessToken = await AadAuthHelper.GetAadAuthToken(SettingsHelper.AzureManagementApiResourceId);

            // setup authenticated http client
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

            // create request
            var azQueryPath = string.Format("subscriptions/{0}/providers/Microsoft.Commerce/RateCard?api-version=2015-06-01-preview&$filter=OfferDurableId eq '{1}' and Currency eq '{2}' and Locale eq '{3}' and RegionInfo eq '{4}'",
                                            HttpUtility.UrlEncode(subscriptionId),
                                            HttpUtility.UrlEncode(offerId),
                                            HttpUtility.UrlEncode(region.CurrencyCode),
                                            HttpUtility.UrlEncode(locale),
                                            HttpUtility.UrlEncode(region.Id));
            var query   = string.Format(SettingsHelper.AzureManagementApiEndpoint, azQueryPath);
            var request = new HttpRequestMessage(HttpMethod.Get, query);

            // issue request and convert response to typed object
            var response = await httpClient.SendAsync(request);

            var rawResponseString = await response.Content.ReadAsStringAsync();

            var azarmResponse = JsonConvert.DeserializeObject <AzureRateCardHttpResponse>(rawResponseString);

            return(azarmResponse);
        }
Exemple #4
0
        public static async Task <AzureUsageHttpResponse> GetUsage(string subscriptionId,
                                                                   DateTime reportedStart,
                                                                   DateTime reportedEnd,
                                                                   AggregationGranularity?granularity = AggregationGranularity.Daily,
                                                                   bool?showDetails = true)
        {
            // get access token
            var accessToken = await AadAuthHelper.GetAadAuthToken(SettingsHelper.AzureManagementApiResourceId);

            // setup authenticated http client
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

            // create request
            var azQueryPath = string.Format("subscriptions/{0}/providers/Microsoft.Commerce/UsageAggregates?api-version=2015-06-01-preview&reportedStartTime={1}&reportedEndTime={2}&aggregationGranularity={3}&showDetails={4}",
                                            HttpUtility.UrlEncode(subscriptionId),
                                            HttpUtility.UrlEncode(reportedStart.ToString("s")),
                                            HttpUtility.UrlEncode(reportedEnd.ToString("s")),
                                            HttpUtility.UrlEncode(granularity.ToString()),
                                            HttpUtility.UrlEncode(showDetails.ToString()));
            var query   = string.Format(SettingsHelper.AzureManagementApiEndpoint, azQueryPath);
            var request = new HttpRequestMessage(HttpMethod.Get, query);

            // issue request and convert response to typed object
            var response = await httpClient.SendAsync(request);

            var rawResponseString = await response.Content.ReadAsStringAsync();

            var azarmResponse = JsonConvert.DeserializeObject <AzureUsageHttpResponse>(rawResponseString);

            // POSTPROCESS - some fields contain a dynamic JSON object as a string
            //  need to do additional processing on these
            //  if data present in RawInstanceData, handle it
            if (azarmResponse.LineItems != null)
            {
                foreach (var lineItem in azarmResponse.LineItems)
                {
                    if (lineItem.Properties.InstanceDataRaw != null)
                    {
                        lineItem.Properties.InstanceData = PostProcessInstanceData(lineItem.Properties.InstanceDataRaw);
                    }
                }
                ;
            }

            return(azarmResponse);
        }
        public static async Task <List <AzureSubscription> > GetSubscriptions(bool fromServer = true)
        {
            List <AzureSubscription> results = new List <AzureSubscription>();

            // if fromServer = false, try to get previous subscriptions from cache
            if (!fromServer)
            {
                results = HttpContext.Current.Session[KEY_AZURE_SUBSCRIPTIONS] as List <AzureSubscription>;
            }

            // if have sessions from cache | told to get from server, return them...
            if (results.Any())
            {
                return(results);
            }
            // ... else continue to get them from server

            // get access token
            var accessToken = await AadAuthHelper.GetAadAuthToken(SettingsHelper.AzureManagementApiResourceId);

            // setup authenticated http client
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

            // create request
            var azQueryPath = "subscriptions/?api-version=2014-04-01";
            var query       = string.Format(SettingsHelper.AzureManagementApiEndpoint, azQueryPath);
            var request     = new HttpRequestMessage(HttpMethod.Get, query);

            // issue request and convert response to typed object
            var response = await httpClient.SendAsync(request);

            var rawResponseString = await response.Content.ReadAsStringAsync();

            var azarmResponse = JsonConvert.DeserializeObject <AzureSubscriptionsHttpResponse>(rawResponseString);

            // convert to normal collection
            results.AddRange(azarmResponse.AzureSubscriptions);

            return(results);
        }
Exemple #6
0
        private static async Task <IEnumerable <AzureResourceGroupItem> > GetArmVms(string groupName)
        {
            // get access token
            var accessToken = await AadAuthHelper.GetAadAuthToken(SettingsHelper.AzureManagementApiResourceId);

            // setup authenticated http client
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Accept", "appliscation/json");
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

            // create request to get all classic VMs
            var azQueryPath = string.Format("subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/?api-version=2016-03-30", SettingsHelper.AzureSubscriptionId, groupName);
            var query       = string.Format(SettingsHelper.AzureManagementApiEndpoint, azQueryPath);
            var request     = new HttpRequestMessage(HttpMethod.Get, query);

            // issue request and convert response to typed object
            var response = await httpClient.SendAsync(request);

            var rawResponseString = await response.Content.ReadAsStringAsync();

            var azResponse = JsonConvert.DeserializeObject <AzureVirtualMachines>(rawResponseString);

            // convert to normal collection
            List <AzureResourceGroupItem> contents = new List <AzureResourceGroupItem>();

            foreach (var item in azResponse.Items)
            {
                contents.Add(new AzureResourceGroupItem {
                    Id       = item.id,
                    Type     = "Virtual Machine",
                    Name     = item.name,
                    Location = item.location
                });
            }

            return(contents);
        }
Exemple #7
0
        public static async Task CreateTemplateDeployment(string resourceGroup, string vmType, string adminUsername, string adminPassword, string dnsName)
        {
            // create a new template deployment...
            var armTemplateDeploymentRequest = new ArmTemplateDeploymentRequest {
                Properties = new ArmTemplateDeploymentRequestProperties {
                    Mode         = "Incremental",
                    TemplateLink = new Templatelink {
                        ContentVersion = "1.0.0.0"
                    },
                    Parameters = new ArmTemplateDeploymentRequestParameters {
                        AdminUsername = new TemplateParameter {
                            Value = adminUsername
                        },
                        AdminPassword = new TemplateParameter {
                            Value = adminPassword
                        },
                        DnsLabel = new TemplateParameter {
                            Value = dnsName
                        }
                    }
                }
            };

            // add the template & OS version depending on the template
            if (vmType == "windows")
            {
                armTemplateDeploymentRequest.Properties.TemplateLink.Uri            = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json";
                armTemplateDeploymentRequest.Properties.Parameters.WindowsOsVersion = new TemplateParameter {
                    Value = "2008-R2-SP1"
                };
            }
            else
            {
                armTemplateDeploymentRequest.Properties.TemplateLink.Uri           = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-linux/azuredeploy.json";
                armTemplateDeploymentRequest.Properties.Parameters.UbuntuOsVersion = new TemplateParameter {
                    Value = "14.04.2-LTS"
                };
            }

            // get access token
            var accessToken = await AadAuthHelper.GetAadAuthToken(SettingsHelper.AzureManagementApiResourceId);

            // create request
            var azQueryPath = string.Format("subscriptions/{0}/resourcegroups/{1}/providers/microsoft.resources/deployments/{2}/?api-version=2016-02-01",
                                            SettingsHelper.AzureSubscriptionId,
                                            resourceGroup,
                                            Guid.NewGuid().ToString());
            var query   = string.Format(SettingsHelper.AzureManagementApiEndpoint, azQueryPath);
            var request = (HttpWebRequest)HttpWebRequest.Create(query);

            request.Method      = "PUT";
            request.ContentType = "application/json";
            request.Accept      = "application/json";
            request.Headers.Add("Authorization", "Bearer " + accessToken);

            // create payload
            string content = JsonConvert.SerializeObject(armTemplateDeploymentRequest,
                                                         Formatting.None,
                                                         new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }
                                                         );

            using (var writer = new StreamWriter(request.GetRequestStream())) {
                writer.Write(content);
            }

            // submit request
            await request.GetResponseAsync();
        }