Exemplo n.º 1
0
        /// <param name='incidentId'>
        /// The id of the incident to retrieve.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The Get Incident operation response.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.IncidentGetResponse> GetAsync(string incidentId, CancellationToken cancellationToken)
        {
            // Validate
            if (incidentId == null)
            {
                throw new ArgumentNullException("incidentId");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

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

            // Construct URL
            string url = new Uri(this.Client.BaseUri, "/").ToString() + this.Client.Credentials.SubscriptionId + "/services/monitoring/alertincidents/" + incidentId;

            // 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");
                httpRequest.Headers.Add("x-ms-version", "2013-10-01");

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

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

                    if (shouldTrace)
                    {
                        Tracing.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), CloudExceptionType.Json);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    IncidentGetResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new IncidentGetResponse();
                    JToken responseDoc = JToken.Parse(responseContent);

                    if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                    {
                        Incident incidentInstance = new Incident();
                        result.Incident = incidentInstance;

                        JToken idValue = responseDoc["Id"];
                        if (idValue != null && idValue.Type != JTokenType.Null)
                        {
                            string idInstance = (string)idValue;
                            incidentInstance.Id = idInstance;
                        }

                        JToken ruleIdValue = responseDoc["RuleId"];
                        if (ruleIdValue != null && ruleIdValue.Type != JTokenType.Null)
                        {
                            string ruleIdInstance = (string)ruleIdValue;
                            incidentInstance.RuleId = ruleIdInstance;
                        }

                        JToken isActiveValue = responseDoc["IsActive"];
                        if (isActiveValue != null && isActiveValue.Type != JTokenType.Null)
                        {
                            bool isActiveInstance = (bool)isActiveValue;
                            incidentInstance.IsActive = isActiveInstance;
                        }

                        JToken activatedTimeValue = responseDoc["ActivatedTime"];
                        if (activatedTimeValue != null && activatedTimeValue.Type != JTokenType.Null)
                        {
                            DateTime activatedTimeInstance = (DateTime)activatedTimeValue;
                            incidentInstance.ActivatedTime = activatedTimeInstance;
                        }

                        JToken resolvedTimeValue = responseDoc["ResolvedTime"];
                        if (resolvedTimeValue != null && resolvedTimeValue.Type != JTokenType.Null)
                        {
                            DateTime resolvedTimeInstance = (DateTime)resolvedTimeValue;
                            incidentInstance.ResolvedTime = resolvedTimeInstance;
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        /// <param name='incidentId'>
        /// Required. The id of the incident to retrieve.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The Get Incident operation response.
        /// </returns>
        public async Task <IncidentGetResponse> GetAsync(string incidentId, CancellationToken cancellationToken)
        {
            // Validate
            if (incidentId == null)
            {
                throw new ArgumentNullException("incidentId");
            }

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

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

            // Construct URL
            string url     = "/" + (this.Client.Credentials.SubscriptionId == null ? "" : Uri.EscapeDataString(this.Client.Credentials.SubscriptionId)) + "/services/monitoring/alertincidents/" + Uri.EscapeDataString(incidentId);
            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");
                httpRequest.Headers.Add("x-ms-version", "2013-10-01");

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

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

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            Incident incidentInstance = new Incident();
                            result.Incident = incidentInstance;

                            JToken idValue = responseDoc["Id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                incidentInstance.Id = idInstance;
                            }

                            JToken ruleIdValue = responseDoc["RuleId"];
                            if (ruleIdValue != null && ruleIdValue.Type != JTokenType.Null)
                            {
                                string ruleIdInstance = ((string)ruleIdValue);
                                incidentInstance.RuleId = ruleIdInstance;
                            }

                            JToken isActiveValue = responseDoc["IsActive"];
                            if (isActiveValue != null && isActiveValue.Type != JTokenType.Null)
                            {
                                bool isActiveInstance = ((bool)isActiveValue);
                                incidentInstance.IsActive = isActiveInstance;
                            }

                            JToken activatedTimeValue = responseDoc["ActivatedTime"];
                            if (activatedTimeValue != null && activatedTimeValue.Type != JTokenType.Null)
                            {
                                DateTime activatedTimeInstance = ((DateTime)activatedTimeValue);
                                incidentInstance.ActivatedTime = activatedTimeInstance;
                            }

                            JToken resolvedTimeValue = responseDoc["ResolvedTime"];
                            if (resolvedTimeValue != null && resolvedTimeValue.Type != JTokenType.Null)
                            {
                                DateTime resolvedTimeInstance = ((DateTime)resolvedTimeValue);
                                incidentInstance.ResolvedTime = resolvedTimeInstance;
                            }
                        }
                    }
                    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();
                }
            }
        }