示例#1
0
        /// <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;
        }
        /// <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>
        /// 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;
        }