/// <summary> /// Executes the command. /// </summary> public override void ExecuteCmdlet() { base.ExecuteCmdlet(); var httpJobAction = new PSHttpJobActionParams() { RequestMethod = this.Method, Uri = this.Uri, RequestBody = this.RequestBody, RequestHeaders = this.Headers, RequestAuthentication = GetAuthenticationParams(), }; JobActionType jobActionType; if (this.Uri.Scheme.Equals(Constants.HttpScheme, StringComparison.InvariantCultureIgnoreCase) || this.Uri.Scheme.Equals(Constants.HttpsScheme, StringComparison.InvariantCultureIgnoreCase)) { jobActionType = (JobActionType)Enum.Parse(typeof(JobActionType), this.Uri.Scheme, ignoreCase: true); } else { throw new PSArgumentException(string.Format(Resources.SchedulerInvalidUriScheme, this.Uri.Scheme)); } var jobAction = new PSJobActionParams() { JobActionType = jobActionType, HttpJobAction = httpJobAction }; var jobRecurrence = new PSJobRecurrenceParams() { Interval = this.Interval, Frequency = this.Frequency, EndTime = this.EndTime, ExecutionCount = this.ExecutionCount }; var jobParams = new PSJobParams() { ResourceGroupName = this.ResourceGroupName, JobCollectionName = this.JobCollectionName, JobName = this.JobName, JobState = this.JobState, StartTime = this.StartTime, JobAction = jobAction, JobRecurrence = jobRecurrence, JobErrorAction = this.GetErrorActionParamsValue(this.ErrorActionType) }; this.ConfirmAction( processMessage: string.Format(Resources.NewHttpJobResourceDescription, this.JobName), target: this.JobCollectionName, action: () => { this.WriteObject(this.SchedulerClient.CreateJob(jobParams)); } ); }
/// <summary> /// Get job action. /// </summary> /// <param name="updateJobActionParams">Job action properties specified via PowerShell.</param> /// <param name="existingJobAction">Job action properties from existing job.</param> /// <returns>JobAction object.</returns> private JobAction GetExistingJobAction(PSJobActionParams updateJobActionParams, JobAction existingJobAction) { if (updateJobActionParams != null) { if (existingJobAction != null && (existingJobAction.Type == updateJobActionParams.JobActionType || ((existingJobAction.Type == JobActionType.Http || existingJobAction.Type == JobActionType.Https) && (updateJobActionParams.JobActionType == JobActionType.Http || updateJobActionParams.JobActionType == JobActionType.Https)))) { switch (updateJobActionParams.JobActionType) { case JobActionType.Http: case JobActionType.Https: PSHttpJobActionParams httpJobAction = updateJobActionParams.HttpJobAction; HttpRequest existinghHttpRequest = existingJobAction.Request; if (httpJobAction.Uri != null) { existinghHttpRequest.Uri = httpJobAction.Uri.OriginalString; existingJobAction.Type = updateJobActionParams.JobActionType; } existinghHttpRequest.Method = httpJobAction.RequestMethod.GetValueOrDefault(defaultValue: existinghHttpRequest.Method); existinghHttpRequest.Body = httpJobAction.RequestBody.GetValueOrDefault(defaultValue: existinghHttpRequest.Body); existinghHttpRequest.Headers = httpJobAction.RequestHeaders != null?httpJobAction.RequestHeaders.ToDictionary() : existinghHttpRequest.Headers; existinghHttpRequest.Authentication = this.GetExistingAuthentication(httpJobAction.RequestAuthentication, existinghHttpRequest.Authentication); break; case JobActionType.StorageQueue: PSStorageJobActionParams storageJobAction = updateJobActionParams.StorageJobAction; StorageQueueMessage existingStorageQueue = existingJobAction.QueueMessage; storageJobAction.StorageAccount = storageJobAction.StorageAccount.GetValueOrDefault(defaultValue: existingStorageQueue.StorageAccount); storageJobAction.StorageQueueMessage = storageJobAction.StorageQueueMessage.GetValueOrDefault(defaultValue: existingStorageQueue.Message); storageJobAction.StorageQueueName = storageJobAction.StorageQueueName.GetValueOrDefault(defaultValue: existingStorageQueue.QueueName); storageJobAction.StorageSasToken = storageJobAction.StorageSasToken.GetValueOrDefault(defaultValue: existingStorageQueue.SasToken); break; case JobActionType.ServiceBusQueue: PSServiceBusParams serviceBusQueueParams = updateJobActionParams.ServiceBusAction; ServiceBusQueueMessage existingServiceBusQueueMessage = existingJobAction.ServiceBusQueueMessage; this.UpdateServiceBus(serviceBusQueueParams, existingServiceBusQueueMessage); existingServiceBusQueueMessage.QueueName = serviceBusQueueParams.QueueName.GetValueOrDefault(defaultValue: existingServiceBusQueueMessage.QueueName); break; case JobActionType.ServiceBusTopic: PSServiceBusParams serviceBusTopicParams = updateJobActionParams.ServiceBusAction; ServiceBusTopicMessage existingServiceBusTopicMessage = existingJobAction.ServiceBusTopicMessage; this.UpdateServiceBus(serviceBusTopicParams, existingServiceBusTopicMessage); existingServiceBusTopicMessage.TopicPath = serviceBusTopicParams.TopicPath.GetValueOrDefault(defaultValue: existingServiceBusTopicMessage.TopicPath); break; } } else { this.PopulateJobAction(updateJobActionParams, ref existingJobAction); } } return(existingJobAction); }
/// <summary> /// Gets http job action. /// </summary> /// <param name="httpActionParams">Http job aciton properties specified via PowerShell.</param> /// <returns>HttpRequest object.</returns> private HttpRequest GetHttpJobAction(PSHttpJobActionParams httpActionParams) { if (string.IsNullOrWhiteSpace(httpActionParams.RequestMethod) || httpActionParams.Uri == null) { throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidHttpRequest); } var httpRequest = new HttpRequest() { Method = httpActionParams.RequestMethod, Uri = httpActionParams.Uri.ToString(), Authentication = this.PopulateHttpAuthentication(httpActionParams.RequestAuthentication) }; if (httpActionParams.RequestHeaders != null) { httpRequest.Headers = httpActionParams.RequestHeaders.ToDictionary(); } if ((httpActionParams.RequestMethod.Equals(Constants.HttpMethodPOST, StringComparison.InvariantCultureIgnoreCase) || httpActionParams.RequestMethod.Equals(Constants.HttpMethodPUT, StringComparison.InvariantCultureIgnoreCase)) && httpActionParams.RequestBody != null) { if (httpRequest.Headers != null && httpRequest.Headers.ContainsKeyInvariantCultureIgnoreCase("content-type")) { httpRequest.Body = httpActionParams.RequestBody; } else { throw new PSManagement.PSArgumentException(Resources.SchedulerNoJobContentType); } } return(httpRequest); }
/// <summary> /// Populates error action paramenter values to PSJobActionParams. /// </summary> /// <param name="errorActionType">Error action type e.g. http, https, storage etc</param> /// <returns>PSJobActionParams.</returns> internal PSJobActionParams GetErrorActionParamsValue(string errorActionType) { if (!string.IsNullOrWhiteSpace(errorActionType)) { var jobErrorActionType = (SchedulerModels.JobActionType)Enum.Parse(typeof(SchedulerModels.JobActionType), errorActionType, ignoreCase: true); var jobErrorAction = new PSJobActionParams() { JobActionType = jobErrorActionType, }; switch (jobErrorActionType) { case SchedulerModels.JobActionType.Http: case SchedulerModels.JobActionType.Https: var jobErrorActionAuthentication = new PSHttpJobAuthenticationParams() { HttpAuthType = this.JobDynamicParameters.ErrorActionHttpAuthenticationType, ClientCertPfx = string.IsNullOrWhiteSpace(JobDynamicParameters.ErrorActionClientCertificatePfx) ? null : SchedulerUtility.GetCertData(this.ResolvePath(JobDynamicParameters.ErrorActionClientCertificatePfx), JobDynamicParameters.ErrorActionClientCertificatePassword), ClientCertPassword = this.JobDynamicParameters.ErrorActionClientCertificatePassword, Username = this.JobDynamicParameters.ErrorActionBasicUsername, Password = this.JobDynamicParameters.ErrorActionBasicPassword, Secret = this.JobDynamicParameters.ErrorActionOAuthSecret, Tenant = this.JobDynamicParameters.ErrorActionOAuthTenant, Audience = this.JobDynamicParameters.ErrorActionOAuthAudience, ClientId = this.JobDynamicParameters.ErrorActionOAuthClientId }; var httpJobErrorAction = new PSHttpJobActionParams() { RequestMethod = this.JobDynamicParameters.ErrorActionMethod, Uri = this.JobDynamicParameters.ErrorActionUri, RequestBody = this.JobDynamicParameters.ErrorActionRequestBody, RequestHeaders = this.JobDynamicParameters.ErrorActionHeaders, RequestAuthentication = jobErrorActionAuthentication }; jobErrorAction.HttpJobAction = httpJobErrorAction; break; case SchedulerModels.JobActionType.StorageQueue: var storageQueueErrorAction = new PSStorageJobActionParams() { StorageAccount = this.JobDynamicParameters.ErrorActionStorageAccount, StorageQueueName = this.JobDynamicParameters.ErrorActionStorageQueue, StorageSasToken = this.JobDynamicParameters.ErrorActionStorageSASToken, StorageQueueMessage = this.JobDynamicParameters.ErrorActionQueueMessageBody, }; jobErrorAction.StorageJobAction = storageQueueErrorAction; break; case SchedulerModels.JobActionType.ServiceBusQueue: var serviceBusQueueErrorAction = GetServiceBusErrorActionParams(); serviceBusQueueErrorAction.QueueName = this.JobDynamicParameters.ErrorActionServiceBusQueueName; jobErrorAction.ServiceBusAction = serviceBusQueueErrorAction; break; case SchedulerModels.JobActionType.ServiceBusTopic: var serviceBusTopicErrorAction = GetServiceBusErrorActionParams(); serviceBusTopicErrorAction.TopicPath = this.JobDynamicParameters.ErrorActionServiceBusTopicPath; jobErrorAction.ServiceBusAction = serviceBusTopicErrorAction; break; } return(jobErrorAction); } return(null); }