private async void StartJobButton_Click(object sender, RoutedEventArgs e) { try { StartJobButton.IsEnabled = false; processedStreamIDs.Clear(); cancelOutput = true; refreshTimer.Stop(); jobParams.Time = DateTime.UtcNow.AddDays(-30).ToString("o"); TestJobCreateResponse response = await createTestJob(); if (response != null) { OutputTextBlockParagraph.Inlines.Clear(); JobDetails.FontWeight = FontWeights.Regular; JobDetails.Content = runbookName + " test job created at " + response.TestJob.CreationTime.LocalDateTime; JobStatus.Content = response.TestJob.Status; StopJobButton.IsEnabled = true; } else { StartJobButton.IsEnabled = true; } } catch (Exception exception) { MessageBox.Show(exception.Message, "Job Start Failure", MessageBoxButton.OK, MessageBoxImage.Error); } finally { refreshTimer.Start(); } }
private async void StartJobButton_Click(object sender, RoutedEventArgs e) { try { StartJobButton.IsEnabled = false; refreshTimer.Stop(); TestJobCreateResponse response = await createTestJob(); if (response != null) { OutputTextBlockParagraph.Inlines.Clear(); JobDetails.Content = runbookName + " test job created at " + response.TestJob.CreationTime.LocalDateTime; JobStatus.Content = response.TestJob.Status; } } catch (Exception exception) { MessageBox.Show(exception.Message, "Job Start Failure", MessageBoxButton.OK, MessageBoxImage.Error); } finally { refreshTimer.Start(); StartJobButton.IsEnabled = true; } }
private async Task <TestJobCreateResponse> createTestJob() { RunbookDraft draft = await AutomationRunbookManager.GetRunbookDraft(runbookName, iseClient.automationManagementClient, iseClient.accountResourceGroups[iseClient.currAccount].Name, iseClient.currAccount.Name); if (draft.InEdit == false) { throw new Exception("This runbook has no draft to test because it is in a 'Published' state."); } HybridRunbookWorkerGroupsListResponse hybridGroupResponse = await iseClient.automationManagementClient.HybridRunbookWorkerGroups.ListAsync( iseClient.accountResourceGroups[iseClient.currAccount].Name, iseClient.currAccount.Name, new CancellationToken()); TestJobCreateParameters jobCreationParams = new TestJobCreateParameters(); jobCreationParams.RunbookName = runbookName; if (draft.Parameters.Count > 0 || hybridGroupResponse.HybridRunbookWorkerGroups.Count > 0) { /* User needs to specify some things */ var existingParams = await GetLastTestJobParams(); RunbookParamDialog paramDialog = new RunbookParamDialog(draft.Parameters, existingParams, hybridGroupResponse.HybridRunbookWorkerGroups); if (paramDialog.ShowDialog() == true) { if (draft.Parameters.Count > 0) { jobCreationParams.Parameters = paramDialog.paramValues; } if (!String.IsNullOrEmpty(paramDialog.runOnSelection) && !paramDialog.runOnSelection.Equals("Azure")) { jobCreationParams.RunOn = paramDialog.runOnSelection; } } else { return(null); } } /* start the test job */ CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); TestJobCreateResponse jobResponse = await iseClient.automationManagementClient.TestJobs.CreateAsync( iseClient.accountResourceGroups[iseClient.currAccount].Name, iseClient.currAccount.Name, jobCreationParams, cts.Token); if (jobResponse == null || jobResponse.StatusCode != System.Net.HttpStatusCode.Created) { throw new Exception("The test job could not be created: received HTTP status code " + jobResponse.StatusCode); } return(jobResponse); }
private async Task <TestJobCreateResponse> createTestJob() { RunbookDraft draft = await AutomationRunbookManager.GetRunbookDraft(runbookName, iseClient.automationManagementClient, iseClient.accountResourceGroups[iseClient.currAccount].Name, iseClient.currAccount.Name); if (draft.InEdit == false) { throw new Exception("This runbook has no draft to test because it is in a 'Published' state."); } TestJobCreateParameters jobCreationParams = new TestJobCreateParameters(); jobCreationParams.RunbookName = runbookName; if (draft.Parameters.Count > 0) { /* User needs to specify values for them */ var existingParams = await GetTestJobParams(); RunbookParamDialog paramDialog = new RunbookParamDialog(draft.Parameters, existingParams); if (paramDialog.ShowDialog() == true) { jobCreationParams.Parameters = paramDialog.paramValues; } else { return(null); } } /* start the test job */ TestJobCreateResponse jobResponse = await iseClient.automationManagementClient.TestJobs.CreateAsync( iseClient.accountResourceGroups[iseClient.currAccount].Name, iseClient.currAccount.Name, jobCreationParams, new System.Threading.CancellationToken()); if (jobResponse == null || jobResponse.StatusCode != System.Net.HttpStatusCode.Created) { throw new Exception("The test job could not be created: received HTTP status code " + jobResponse.StatusCode); } return(jobResponse); }
/// <summary> /// Create a test job of the runbook. (see /// http://aka.ms/azureautomationsdk/testjoboperations for more /// information) /// </summary> /// <param name='resourceGroupName'> /// Required. The name of the resource group /// </param> /// <param name='automationAccount'> /// Required. The automation account name. /// </param> /// <param name='parameters'> /// Required. The parameters supplied to the create test job operation. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The response model for the create test job operation. /// </returns> public async Task <TestJobCreateResponse> CreateAsync(string resourceGroupName, string automationAccount, TestJobCreateParameters parameters, CancellationToken cancellationToken) { // Validate if (resourceGroupName == null) { throw new ArgumentNullException("resourceGroupName"); } if (automationAccount == null) { throw new ArgumentNullException("automationAccount"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } if (parameters.RunbookName == null) { throw new ArgumentNullException("parameters.RunbookName"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("resourceGroupName", resourceGroupName); tracingParameters.Add("automationAccount", automationAccount); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/subscriptions/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/resourceGroups/"; url = url + Uri.EscapeDataString(resourceGroupName); url = url + "/providers/"; if (this.Client.ResourceNamespace != null) { url = url + Uri.EscapeDataString(this.Client.ResourceNamespace); } url = url + "/automationAccounts/"; url = url + Uri.EscapeDataString(automationAccount); url = url + "/runbooks/"; url = url + Uri.EscapeDataString(parameters.RunbookName); url = url + "/draft/testJob"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2017-05-15-preview"); 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.Put; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("Accept", "application/json"); httpRequest.Headers.Add("x-ms-version", "2014-06-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; JToken requestDoc = null; JObject testJobCreateParametersValue = new JObject(); requestDoc = testJobCreateParametersValue; if (parameters.Parameters != null) { if (parameters.Parameters is ILazyCollection == false || ((ILazyCollection)parameters.Parameters).IsInitialized) { JObject parametersDictionary = new JObject(); foreach (KeyValuePair <string, string> pair in parameters.Parameters) { string parametersKey = pair.Key; string parametersValue = pair.Value; parametersDictionary[parametersKey] = parametersValue; } testJobCreateParametersValue["parameters"] = parametersDictionary; } } if (parameters.RunOn != null) { testJobCreateParametersValue["runOn"] = parameters.RunOn; } requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // 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.Created) { 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 TestJobCreateResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.Created) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new TestJobCreateResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { TestJob testJobInstance = new TestJob(); result.TestJob = testJobInstance; JToken creationTimeValue = responseDoc["creationTime"]; if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null) { DateTimeOffset creationTimeInstance = ((DateTimeOffset)creationTimeValue); testJobInstance.CreationTime = creationTimeInstance; } JToken statusValue = responseDoc["status"]; if (statusValue != null && statusValue.Type != JTokenType.Null) { string statusInstance = ((string)statusValue); testJobInstance.Status = statusInstance; } JToken statusDetailsValue = responseDoc["statusDetails"]; if (statusDetailsValue != null && statusDetailsValue.Type != JTokenType.Null) { string statusDetailsInstance = ((string)statusDetailsValue); testJobInstance.StatusDetails = statusDetailsInstance; } JToken runOnValue = responseDoc["runOn"]; if (runOnValue != null && runOnValue.Type != JTokenType.Null) { string runOnInstance = ((string)runOnValue); testJobInstance.RunOn = runOnInstance; } JToken startTimeValue = responseDoc["startTime"]; if (startTimeValue != null && startTimeValue.Type != JTokenType.Null) { DateTimeOffset startTimeInstance = ((DateTimeOffset)startTimeValue); testJobInstance.StartTime = startTimeInstance; } JToken endTimeValue = responseDoc["endTime"]; if (endTimeValue != null && endTimeValue.Type != JTokenType.Null) { DateTimeOffset endTimeInstance = ((DateTimeOffset)endTimeValue); testJobInstance.EndTime = endTimeInstance; } JToken exceptionValue = responseDoc["exception"]; if (exceptionValue != null && exceptionValue.Type != JTokenType.Null) { string exceptionInstance = ((string)exceptionValue); testJobInstance.Exception = exceptionInstance; } JToken lastModifiedTimeValue = responseDoc["lastModifiedTime"]; if (lastModifiedTimeValue != null && lastModifiedTimeValue.Type != JTokenType.Null) { DateTimeOffset lastModifiedTimeInstance = ((DateTimeOffset)lastModifiedTimeValue); testJobInstance.LastModifiedTime = lastModifiedTimeInstance; } JToken lastStatusModifiedTimeValue = responseDoc["lastStatusModifiedTime"]; if (lastStatusModifiedTimeValue != null && lastStatusModifiedTimeValue.Type != JTokenType.Null) { DateTimeOffset lastStatusModifiedTimeInstance = ((DateTimeOffset)lastStatusModifiedTimeValue); testJobInstance.LastStatusModifiedTime = lastStatusModifiedTimeInstance; } JToken parametersSequenceElement = ((JToken)responseDoc["parameters"]); if (parametersSequenceElement != null && parametersSequenceElement.Type != JTokenType.Null) { foreach (JProperty property in parametersSequenceElement) { string parametersKey2 = ((string)property.Name); string parametersValue2 = ((string)property.Value); testJobInstance.Parameters.Add(parametersKey2, parametersValue2); } } } } 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(); } } }