Beispiel #1
0
        //*********************************************************************
        ///
        /// <summary>
        ///
        /// </summary>
        /// <param name="operationText"></param>
        ///
        //*********************************************************************

        private void AzureArmTaskStatus(string operationText)
        {
            List <AzureEventOps.AzureException> azureExceptionList;

            if (AzureEventOps.IsComplete(
                    operationText, "Microsoft.Compute", out azureExceptionList))
            {
                Status       = "Complete";
                ErrorCode    = null;
                ErrorMessage = null;
            }
            else if (null == azureExceptionList)
            {
                Status       = "InProgress";
                ErrorCode    = null;
                ErrorMessage = null;
            }
            else
            {
                Status       = "Failed";
                ErrorCode    = azureExceptionList[0].ErrorCode;
                ErrorMessage = azureExceptionList[0].ErrorMessage;
            }

            ID             = "";
            HttpStatusCode = "";

            switch (Status)
            {
            case "Failed":
                Result = ResultEnum.Failed;
                break;

            default:
                Result = ResultEnum.Success;
                break;
            }
        }
Beispiel #2
0
        //*********************************************************************
        ///
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestType"></param>
        /// <param name="requestUrl"></param>
        /// <param name="bodyContents"></param>
        /// <returns></returns>
        ///
        //*********************************************************************

        public HttpResponse PerformRequestArm(RequestType_Enum requestType,
                                              string requestUrl, string bodyContents = null)
        {
            var responseOut = new HttpResponse();

            try
            {
                var requestUri = new Uri(requestUrl);
                var request    = WebRequest.Create(requestUri) as HttpWebRequest;

                if (null == request)
                {
                    throw new Exception("Unable to create a HttpWebRequest for : " + requestUri);
                }

                request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + Connection.AdToken.AccessToken);

                request.Method      = requestType.ToString();
                request.ContentType = "application/json";
                request.Accept      = "application/json";

                if (requestType == RequestType_Enum.POST | requestType == RequestType_Enum.PUT)
                {
                    var requestStream = request.GetRequestStream();
                    var streamWriter  = new StreamWriter(requestStream, Encoding.UTF8);

                    streamWriter.Write(bodyContents);

                    streamWriter.Close();
                    requestStream.Close();
                }

                var response = (HttpWebResponse)request.GetResponse();

                if (response.ContentLength > 0)
                {
                    var responseStream = response.GetResponseStream();

                    if (null == responseStream)
                    {
                        throw new Exception("Unable to create Stream from HttpWebResponse");
                    }

                    var reader = new StreamReader(responseStream, Encoding.ASCII);
                    responseOut.Body = reader.ReadToEnd();

                    var err = FetchJsonValue(responseOut.Body, "error") as Dictionary <string, dynamic>;

                    if (null != err)
                    {
                        responseOut.HadError = true;
                        responseOut.Body    += string.Format("Web Error making REST API call.\r\nCode: {0}, Message: {1}",
                                                             err["code"], err["code"]);
                    }

                    var props = FetchJsonValue(responseOut.Body, "properties") as Newtonsoft.Json.Linq.JObject;

                    if (null != props)
                    {
                        if (null != props["provisioningState"])
                        {
                            responseOut.ProviderRequestState = props["provisioningState"].ToString();
                        }

                        if (null != props["correlationId"])
                        {
                            var now = DateTime.UtcNow;

                            responseOut.StatusCheckUrl = AzureEventOps.BuildEventFetchUrl(
                                props["correlationId"].ToString(),
                                Connection.SubcriptionID, now.AddMinutes(-5), now.AddHours(4));
                        }

                        responseOut.HadError = false;
                    }

                    reader.Close();
                    responseStream.Close();
                }
                else
                {
                    if (!requestUrl.Contains("/operations/"))
                    {
                        responseOut.Body = "No HTTP Response Body Returned.";
                    }
                }

                foreach (var key in response.Headers.Keys)
                {
                    var keyName = key as string;

                    if (keyName.Equals("Azure-AsyncOperation"))
                    {
                        responseOut.StatusCheckUrl = response.Headers.GetValues(keyName)[0] as string;
                    }
                }

                response.Close();
            }
            catch (WebException exWeb)
            {
                var responseStream = exWeb.Response.GetResponseStream();

                if (null == responseStream)
                {
                    responseOut.HadError = true;
                    responseOut.Body    += "Error making REST API call.\r\n" + exWeb.Message + "\r\n";
                    return(responseOut);
                }

                var reader = new StreamReader(responseStream, Encoding.ASCII);

                var responseStatus = (HttpWebResponse)exWeb.Response;
                responseOut.HTTP = (int)responseStatus.StatusCode + " - " +
                                   responseStatus.StatusCode.ToString();
                responseOut.HadError = true;
                responseOut.Body    += "Web Error making REST API call.\r\nMessage: " +
                                       exWeb.Message + "\r\nResponse:\r\n" + reader.ReadToEnd();

                responseStream.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                responseOut.HadError = true;
                responseOut.Body    += "Error making REST API call.\r\n" + ex.Message + "\r\n";
            }

            return(responseOut);
        }