private void AreEqual(StorageMetricAggregation exp, StorageMetricAggregation act)
 {
     if (exp != null)
     {
         Assert.Equal(exp.Level, act.Level);
         Assert.Equal(exp.Retention, act.Retention);
         Assert.Equal(exp.ScheduledTransferPeriod, act.ScheduledTransferPeriod);
     }
 }
Exemple #2
0
        /// <summary>
        /// Gets the diagnostic settings for the specified storage service.
        /// </summary>
        /// <param name='resourceUri'>
        /// Required. The resource identifier of the storage service.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// A standard service response including an HTTP status code and
        /// request ID.
        /// </returns>
        public async Task <StorageDiagnosticSettingsGetResponse> GetAsync(string resourceUri, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceUri == null)
            {
                throw new ArgumentNullException("resourceUri");
            }

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

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

            // Construct URL
            string url = "/" + Uri.EscapeDataString(resourceUri) + "/diagnosticSettings/storage?";

            url = url + "api-version=2014-04-01";
            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
                httpRequest.Headers.Add("Accept", "application/json");

                // 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
                    StorageDiagnosticSettingsGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                result.Name = nameInstance;
                            }

                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                result.Location = locationInstance;
                            }

                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                StorageDiagnosticSettings propertiesInstance = new StorageDiagnosticSettings();
                                result.Properties = propertiesInstance;

                                JToken loggingValue = propertiesValue["logging"];
                                if (loggingValue != null && loggingValue.Type != JTokenType.Null)
                                {
                                    StorageLoggingDiagnosticSettings loggingInstance = new StorageLoggingDiagnosticSettings();
                                    propertiesInstance.LoggingDiagnosticSettings = loggingInstance;

                                    JToken deleteValue = loggingValue["delete"];
                                    if (deleteValue != null && deleteValue.Type != JTokenType.Null)
                                    {
                                        bool deleteInstance = ((bool)deleteValue);
                                        loggingInstance.Delete = deleteInstance;
                                    }

                                    JToken readValue = loggingValue["read"];
                                    if (readValue != null && readValue.Type != JTokenType.Null)
                                    {
                                        bool readInstance = ((bool)readValue);
                                        loggingInstance.Read = readInstance;
                                    }

                                    JToken writeValue = loggingValue["write"];
                                    if (writeValue != null && writeValue.Type != JTokenType.Null)
                                    {
                                        bool writeInstance = ((bool)writeValue);
                                        loggingInstance.Write = writeInstance;
                                    }

                                    JToken retentionValue = loggingValue["retention"];
                                    if (retentionValue != null && retentionValue.Type != JTokenType.Null)
                                    {
                                        TimeSpan retentionInstance = XmlConvert.ToTimeSpan(((string)retentionValue));
                                        loggingInstance.Retention = retentionInstance;
                                    }
                                }

                                JToken metricsValue = propertiesValue["metrics"];
                                if (metricsValue != null && metricsValue.Type != JTokenType.Null)
                                {
                                    StorageMetricDiagnosticSettings metricsInstance = new StorageMetricDiagnosticSettings();
                                    propertiesInstance.MetricDiagnosticSettings = metricsInstance;

                                    JToken aggregationsArray = metricsValue["aggregations"];
                                    if (aggregationsArray != null && aggregationsArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken aggregationsValue in ((JArray)aggregationsArray))
                                        {
                                            StorageMetricAggregation storageMetricAggregationInstance = new StorageMetricAggregation();
                                            metricsInstance.MetricAggregations.Add(storageMetricAggregationInstance);

                                            JToken scheduledTransferPeriodValue = aggregationsValue["scheduledTransferPeriod"];
                                            if (scheduledTransferPeriodValue != null && scheduledTransferPeriodValue.Type != JTokenType.Null)
                                            {
                                                TimeSpan scheduledTransferPeriodInstance = XmlConvert.ToTimeSpan(((string)scheduledTransferPeriodValue));
                                                storageMetricAggregationInstance.ScheduledTransferPeriod = scheduledTransferPeriodInstance;
                                            }

                                            JToken retentionValue2 = aggregationsValue["retention"];
                                            if (retentionValue2 != null && retentionValue2.Type != JTokenType.Null)
                                            {
                                                TimeSpan retentionInstance2 = XmlConvert.ToTimeSpan(((string)retentionValue2));
                                                storageMetricAggregationInstance.Retention = retentionInstance2;
                                            }

                                            JToken levelValue = aggregationsValue["level"];
                                            if (levelValue != null && levelValue.Type != JTokenType.Null)
                                            {
                                                StorageMetricLevel levelInstance = ((StorageMetricLevel)Enum.Parse(typeof(StorageMetricLevel), ((string)levelValue), true));
                                                storageMetricAggregationInstance.Level = levelInstance;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    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();
                }
            }
        }