/// <summary>
 /// Creates or update the diagnostic settings for the specified storage
 /// service.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Insights.IStorageDiagnosticSettingsOperations.
 /// </param>
 /// <param name='resourceUri'>
 /// Required. The resource identifier of the storage service.
 /// </param>
 /// <param name='parameters'>
 /// Required. The storage diagnostic settings parameters.
 /// </param>
 /// <returns>
 /// Generic empty response. We only pass it to ensure json error
 /// handling
 /// </returns>
 public static EmptyResponse Put(this IStorageDiagnosticSettingsOperations operations, string resourceUri, StorageDiagnosticSettingsPutParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IStorageDiagnosticSettingsOperations)s).PutAsync(resourceUri, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
コード例 #2
0
        /// <summary>
        /// Creates or update the diagnostic settings for the specified storage
        /// service.
        /// </summary>
        /// <param name='resourceUri'>
        /// Required. The resource identifier of the storage service.
        /// </param>
        /// <param name='parameters'>
        /// Required. The storage diagnostic settings parameters.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Generic empty response. We only pass it to ensure json error
        /// handling
        /// </returns>
        public async Task <EmptyResponse> PutAsync(string resourceUri, StorageDiagnosticSettingsPutParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceUri == null)
            {
                throw new ArgumentNullException("resourceUri");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            // 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);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "PutAsync", 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.Put;
                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);

                // Serialize Request
                string requestContent = null;
                JToken requestDoc     = null;

                JObject storageDiagnosticSettingsPutParametersValue = new JObject();
                requestDoc = storageDiagnosticSettingsPutParametersValue;

                if (parameters.Properties != null)
                {
                    JObject propertiesValue = new JObject();
                    storageDiagnosticSettingsPutParametersValue["properties"] = propertiesValue;

                    if (parameters.Properties.LoggingDiagnosticSettings != null)
                    {
                        JObject loggingValue = new JObject();
                        propertiesValue["logging"] = loggingValue;

                        loggingValue["delete"] = parameters.Properties.LoggingDiagnosticSettings.Delete;

                        loggingValue["read"] = parameters.Properties.LoggingDiagnosticSettings.Read;

                        loggingValue["write"] = parameters.Properties.LoggingDiagnosticSettings.Write;

                        loggingValue["retention"] = XmlConvert.ToString(parameters.Properties.LoggingDiagnosticSettings.Retention);
                    }

                    if (parameters.Properties.MetricDiagnosticSettings != null)
                    {
                        JObject metricsValue = new JObject();
                        propertiesValue["metrics"] = metricsValue;

                        if (parameters.Properties.MetricDiagnosticSettings.MetricAggregations != null)
                        {
                            if (parameters.Properties.MetricDiagnosticSettings.MetricAggregations is ILazyCollection == false || ((ILazyCollection)parameters.Properties.MetricDiagnosticSettings.MetricAggregations).IsInitialized)
                            {
                                JArray aggregationsArray = new JArray();
                                foreach (StorageMetricAggregation aggregationsItem in parameters.Properties.MetricDiagnosticSettings.MetricAggregations)
                                {
                                    JObject storageMetricAggregationValue = new JObject();
                                    aggregationsArray.Add(storageMetricAggregationValue);

                                    storageMetricAggregationValue["scheduledTransferPeriod"] = XmlConvert.ToString(aggregationsItem.ScheduledTransferPeriod);

                                    storageMetricAggregationValue["retention"] = XmlConvert.ToString(aggregationsItem.Retention);

                                    storageMetricAggregationValue["level"] = aggregationsItem.Level.ToString();
                                }
                                metricsValue["aggregations"] = aggregationsArray;
                            }
                        }
                    }
                }

                requestContent      = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                // 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 && statusCode != HttpStatusCode.Created && statusCode != HttpStatusCode.Accepted)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

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

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

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                        }
                    }
                    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();
                }
            }
        }
 /// <summary>
 /// Creates or update the diagnostic settings for the specified storage
 /// service.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Insights.IStorageDiagnosticSettingsOperations.
 /// </param>
 /// <param name='resourceUri'>
 /// Required. The resource identifier of the storage service.
 /// </param>
 /// <param name='parameters'>
 /// Required. The storage diagnostic settings parameters.
 /// </param>
 /// <returns>
 /// Generic empty response. We only pass it to ensure json error
 /// handling
 /// </returns>
 public static Task <EmptyResponse> PutAsync(this IStorageDiagnosticSettingsOperations operations, string resourceUri, StorageDiagnosticSettingsPutParameters parameters)
 {
     return(operations.PutAsync(resourceUri, parameters, CancellationToken.None));
 }