Exemplo n.º 1
0
        public void ValidateListUsageResponse(ListUsagesResponse luResponse)
        {
            Assert.True(luResponse.StatusCode == HttpStatusCode.OK);
            Assert.True(luResponse.Usages != null);
            Assert.True(luResponse.Usages.Count > 0);

            // Can't do any validation on primitive fields, but will make sure strings are populated and non-null as expected.
            foreach(var usage in luResponse.Usages)
            {
                Assert.True(usage.Name.LocalizedValue != null);
                Assert.True(usage.Name.Value != null);
            }
        }
Exemplo n.º 2
0
        public void ValidateListUsageResponse(ListUsagesResponse luResponse)
        {
            Assert.True(luResponse.StatusCode == HttpStatusCode.OK);
            Assert.NotNull(luResponse.Usages);
            Assert.True(luResponse.Usages.Count > 0);

            // Can't do any validation on primitive fields, but will make sure strings are populated and non-null as expected.
            foreach (var usage in luResponse.Usages)
            {
                Assert.True(usage.Name.LocalizedValue != null);
                Assert.True(usage.Name.Value != null);
            }
        }
Exemplo n.º 3
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            ListUsagesResponse result = this.UsageClient.List(this.Location.Canonicalize());

            List <PSUsage> psResultList = new List <PSUsage>();

            foreach (var item in result.Usages)
            {
                var psItem = Mapper.Map <PSUsage>(item);
                psResultList.Add(psItem);
            }

            WriteObject(psResultList, true);
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            ExecuteClientAction(() =>
            {
                ListUsagesResponse result = this.UsageClient.List(this.Location.Canonicalize());

                List <PSUsage> psResultList = new List <PSUsage>();
                foreach (var item in result.Usages)
                {
                    var psItem = Mapper.Map <PSUsage>(item);
                    psItem     = Mapper.Map <AzureOperationResponse, PSUsage>(result, psItem);
                    psResultList.Add(psItem);
                }

                WriteObject(psResultList, true);
            });
        }
Exemplo n.º 5
0
        /// <summary>
        /// Lists compute usages for a subscription.
        /// </summary>
        /// <param name='location'>
        /// Required. The location upon which resource usage is queried.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The List Usages operation response.
        /// </returns>
        public async Task <ListUsagesResponse> ListAsync(string location, CancellationToken cancellationToken)
        {
            // Validate
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }
            if (location != null && location.Length > 1000)
            {
                throw new ArgumentOutOfRangeException("location");
            }
            if (Regex.IsMatch(location, "^[-\\w\\._]+$") == false)
            {
                throw new ArgumentOutOfRangeException("location");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("location", location);
                TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/providers/";
            url = url + "Microsoft.Compute";
            url = url + "/locations/";
            url = url + Uri.EscapeDataString(location);
            url = url + "/usages";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2015-06-15");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    ListUsagesResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new ListUsagesResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    Usage usageInstance = new Usage();
                                    result.Usages.Add(usageInstance);

                                    JToken unitValue = valueValue["unit"];
                                    if (unitValue != null && unitValue.Type != JTokenType.Null)
                                    {
                                        UsageUnit unitInstance = ((UsageUnit)Enum.Parse(typeof(UsageUnit), ((string)unitValue), true));
                                        usageInstance.Unit = unitInstance;
                                    }

                                    JToken currentValueValue = valueValue["currentValue"];
                                    if (currentValueValue != null && currentValueValue.Type != JTokenType.Null)
                                    {
                                        int currentValueInstance = ((int)currentValueValue);
                                        usageInstance.CurrentValue = currentValueInstance;
                                    }

                                    JToken limitValue = valueValue["limit"];
                                    if (limitValue != null && limitValue.Type != JTokenType.Null)
                                    {
                                        uint limitInstance = ((uint)limitValue);
                                        usageInstance.Limit = limitInstance;
                                    }

                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        UsageName nameInstance = new UsageName();
                                        usageInstance.Name = nameInstance;

                                        JToken valueValue2 = nameValue["value"];
                                        if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                        {
                                            string valueInstance = ((string)valueValue2);
                                            nameInstance.Value = valueInstance;
                                        }

                                        JToken localizedValueValue = nameValue["localizedValue"];
                                        if (localizedValueValue != null && localizedValueValue.Type != JTokenType.Null)
                                        {
                                            string localizedValueInstance = ((string)localizedValueValue);
                                            nameInstance.LocalizedValue = localizedValueInstance;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }