Beispiel #1
0
 /// <summary>
 /// Return job output with newline
 /// </summary>
 /// <param name="js"></param>
 /// <param name="toHTML"></param>
 /// <returns></returns>
 private string GetJobOutput(JobStream js, bool?toHTML)
 {
     if (toHTML != null && toHTML == true)
     {
         return(string.Format("{0}{1}", System.Net.WebUtility.HtmlEncode(js.Summary), "<br>"));
     }
     else
     {
         return(string.Format("{0}{1}", js.Summary, Environment.NewLine));
     }
 }
Beispiel #2
0
        /// <summary>
        /// Gets the next page of job streams using next link.
        /// </summary>
        /// <param name='nextLink'>
        /// Required. NextLink from the previous successful call to List
        /// operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the list job stream operation.
        /// </returns>
        public async Task <JobStreamListResponse> ListNextAsync(string nextLink, CancellationToken cancellationToken)
        {
            // Validate
            if (nextLink == null)
            {
                throw new ArgumentNullException("nextLink");
            }

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

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

            // Construct URL
            string url = "";

            url = url + Uri.EscapeDataString(nextLink);
            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("ocp-referer", url);
                httpRequest.Headers.Add("x-ms-version", "2013-06-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
                    JobStreamListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new JobStreamListResponse();
                        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))
                                {
                                    JobStream jobStreamInstance = new JobStream();
                                    result.JobStreams.Add(jobStreamInstance);

                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        JobStreamProperties propertiesInstance = new JobStreamProperties();
                                        jobStreamInstance.Properties = propertiesInstance;

                                        JToken jobStreamIdValue = propertiesValue["jobStreamId"];
                                        if (jobStreamIdValue != null && jobStreamIdValue.Type != JTokenType.Null)
                                        {
                                            string jobStreamIdInstance = ((string)jobStreamIdValue);
                                            propertiesInstance.JobStreamId = jobStreamIdInstance;
                                        }

                                        JToken timeValue = propertiesValue["time"];
                                        if (timeValue != null && timeValue.Type != JTokenType.Null)
                                        {
                                            DateTimeOffset timeInstance = ((DateTimeOffset)timeValue);
                                            propertiesInstance.Time = timeInstance;
                                        }

                                        JToken streamTypeValue = propertiesValue["streamType"];
                                        if (streamTypeValue != null && streamTypeValue.Type != JTokenType.Null)
                                        {
                                            string streamTypeInstance = ((string)streamTypeValue);
                                            propertiesInstance.StreamType = streamTypeInstance;
                                        }

                                        JToken streamTextValue = propertiesValue["streamText"];
                                        if (streamTextValue != null && streamTextValue.Type != JTokenType.Null)
                                        {
                                            string streamTextInstance = ((string)streamTextValue);
                                            propertiesInstance.StreamText = streamTextInstance;
                                        }

                                        JToken summaryValue = propertiesValue["summary"];
                                        if (summaryValue != null && summaryValue.Type != JTokenType.Null)
                                        {
                                            string summaryInstance = ((string)summaryValue);
                                            propertiesInstance.Summary = summaryInstance;
                                        }

                                        JToken valueSequenceElement = ((JToken)propertiesValue["value"]);
                                        if (valueSequenceElement != null && valueSequenceElement.Type != JTokenType.Null)
                                        {
                                            foreach (JProperty property in valueSequenceElement)
                                            {
                                                string valueKey    = ((string)property.Name);
                                                object valueValue2 = ((string)property.Value);
                                                propertiesInstance.Value.Add(valueKey, valueValue2);
                                            }
                                        }
                                    }
                                }
                            }

                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }

                            JToken odatanextLinkValue = responseDoc["odata.nextLink"];
                            if (odatanextLinkValue != null && odatanextLinkValue.Type != JTokenType.Null)
                            {
                                string odatanextLinkInstance = Regex.Match(((string)odatanextLinkValue), "^.*[&\\?]\\$skiptoken=([^&]*)(&.*)?").Groups[1].Value;
                                result.SkipToken = odatanextLinkInstance;
                            }
                        }
                    }
                    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();
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Retrieve the job stream identified by job stream id.  (see
        /// http://aka.ms/azureautomationsdk/jobstreamoperations for more
        /// information)
        /// </summary>
        /// <param name='automationAccount'>
        /// Required. The automation account name.
        /// </param>
        /// <param name='jobId'>
        /// Required. The job id.
        /// </param>
        /// <param name='jobStreamId'>
        /// Required. The job stream id.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the get job stream operation.
        /// </returns>
        public async Task <JobStreamGetResponse> GetAsync(string automationAccount, Guid jobId, string jobStreamId, CancellationToken cancellationToken)
        {
            // Validate
            if (automationAccount == null)
            {
                throw new ArgumentNullException("automationAccount");
            }
            if (jobStreamId == null)
            {
                throw new ArgumentNullException("jobStreamId");
            }

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

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

            // Construct URL
            string url = "";

            url = url + "/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/cloudservices/OaaSCS/resources/";
            if (this.Client.ResourceNamespace != null)
            {
                url = url + Uri.EscapeDataString(this.Client.ResourceNamespace);
            }
            url = url + "/~/automationAccounts/";
            url = url + Uri.EscapeDataString(automationAccount);
            url = url + "/Jobs/";
            url = url + Uri.EscapeDataString(jobId.ToString());
            url = url + "/streams/";
            url = url + Uri.EscapeDataString(jobStreamId);
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2014-12-08");
            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
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2013-06-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
                    JobStreamGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

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

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

                                JToken jobStreamIdValue = propertiesValue["jobStreamId"];
                                if (jobStreamIdValue != null && jobStreamIdValue.Type != JTokenType.Null)
                                {
                                    string jobStreamIdInstance = ((string)jobStreamIdValue);
                                    propertiesInstance.JobStreamId = jobStreamIdInstance;
                                }

                                JToken timeValue = propertiesValue["time"];
                                if (timeValue != null && timeValue.Type != JTokenType.Null)
                                {
                                    DateTimeOffset timeInstance = ((DateTimeOffset)timeValue);
                                    propertiesInstance.Time = timeInstance;
                                }

                                JToken streamTypeValue = propertiesValue["streamType"];
                                if (streamTypeValue != null && streamTypeValue.Type != JTokenType.Null)
                                {
                                    string streamTypeInstance = ((string)streamTypeValue);
                                    propertiesInstance.StreamType = streamTypeInstance;
                                }

                                JToken streamTextValue = propertiesValue["streamText"];
                                if (streamTextValue != null && streamTextValue.Type != JTokenType.Null)
                                {
                                    string streamTextInstance = ((string)streamTextValue);
                                    propertiesInstance.StreamText = streamTextInstance;
                                }

                                JToken summaryValue = propertiesValue["summary"];
                                if (summaryValue != null && summaryValue.Type != JTokenType.Null)
                                {
                                    string summaryInstance = ((string)summaryValue);
                                    propertiesInstance.Summary = summaryInstance;
                                }

                                JToken valueSequenceElement = ((JToken)propertiesValue["value"]);
                                if (valueSequenceElement != null && valueSequenceElement.Type != JTokenType.Null)
                                {
                                    foreach (JProperty property in valueSequenceElement)
                                    {
                                        string valueKey   = ((string)property.Name);
                                        object valueValue = ((string)property.Value);
                                        propertiesInstance.Value.Add(valueKey, valueValue);
                                    }
                                }
                            }
                        }
                    }
                    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();
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Return job output with newline
        /// </summary>
        /// <param name="js"></param>
        /// <param name="toHTML"></param>
        /// <returns></returns>
        private async Task <string> GetJobOutput(string resourceGroupName, string automationAccountName, JobStream js, Job job, bool?toHTML)
        {
            string output = null;

            if (js.Summary == null)
            {
                //Get long output stream (when summary is null it means the output are kept in the individual stream)
                var stream = await Client.JobStream.GetAsync(resourceGroupName, automationAccountName, job.Name, js.JobStreamId);

                if (stream == null)
                {
                    output = $"No jobstream found for job: {job.Name} and jobstream: {js.JobStreamId}";
                }
                else
                {
                    output = stream.StreamText;
                }
            }
            else
            {
                output = js.Summary;
            }


            if (toHTML != null && toHTML == true)
            {
                if (js.StreamType == StreamType.Error)
                {
                    return(string.Format("{0}", $"<p><pre style='color: red; white-space: pre-wrap'>{System.Net.WebUtility.HtmlEncode(output)}</pre></p>"));
                }
                else
                {
                    //    string[] texts = output.Split("\r\n");
                    //    string text = string.Empty;
                    //    foreach (var t in texts)
                    //    {
                    //        text += $"<p>{System.Net.WebUtility.HtmlEncode(t)}</p>";
                    //    }
                    //    return text;
                    return(string.Format("{0}", $"<p><pre style='white-space: pre-wrap'>{System.Net.WebUtility.HtmlEncode(output)}</pre></p>"));
                }
            }
            else
            {
                return(string.Format("{0}{1}", js.Summary, Environment.NewLine));
            }
        }