public void Test_InputOperations_ReferenceBlob() { BasicDelegatingHandler handler = new BasicDelegatingHandler(); using (var undoContext = UndoContext.Current) { undoContext.Start(); string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics"); string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK"); string serviceLocation = TestHelper.GetDefaultLocation(); var resourceClient = TestHelper.GetResourceClient(handler); var client = TestHelper.GetStreamAnalyticsManagementClient(handler); try { ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation }; resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup); // Construct the JobCreateProperties JobCreateOrUpdateParameters jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation)); // Create a streaming job JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode); // Get a streaming job to check JobGetParameters jobGetParameters = new JobGetParameters(string.Empty); JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(serviceLocation, jobGetResponse.Job.Location); Assert.Equal(resourceName, jobGetResponse.Job.Name); // Construct the Input StorageAccount storageAccount = new StorageAccount(); storageAccount.AccountName = TestHelper.AccountName; storageAccount.AccountKey = TestHelper.AccountKey; InputProperties inputProperties = new ReferenceInputProperties() { Serialization = new CsvSerialization() { Properties = new CsvSerializationProperties() { FieldDelimiter = ",", Encoding = "UTF8" } }, DataSource = new BlobReferenceInputDataSource() { Properties = new BlobReferenceInputDataSourceProperties() { StorageAccounts = new[] { storageAccount }, Container = "state", PathPattern = "{date}", DateFormat = "yyyy/MM/dd" } } }; string inputName = TestUtilities.GenerateName("inputtest"); Input input1 = new Input(inputName) { Properties = inputProperties }; // Add an input InputCreateOrUpdateParameters inputCreateOrUpdateParameters = new InputCreateOrUpdateParameters(); inputCreateOrUpdateParameters.Input = input1; InputCreateOrUpdateResponse inputCreateOrUpdateResponse = client.Inputs.CreateOrUpdate(resourceGroupName, resourceName, inputCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, inputCreateOrUpdateResponse.StatusCode); Assert.Equal(inputName, inputCreateOrUpdateResponse.Input.Name); Assert.Equal("Reference", inputCreateOrUpdateResponse.Input.Properties.Type); Assert.True(inputCreateOrUpdateResponse.Input.Properties is ReferenceInputProperties); ReferenceInputProperties referenceInputPropertiesInResponse1 = (ReferenceInputProperties)inputCreateOrUpdateResponse.Input.Properties; Assert.True(referenceInputPropertiesInResponse1.DataSource is BlobReferenceInputDataSource); BlobReferenceInputDataSource blobReferenceInputDataSourceInResponse1 = (BlobReferenceInputDataSource)referenceInputPropertiesInResponse1.DataSource; Assert.Equal("{date}", blobReferenceInputDataSourceInResponse1.Properties.PathPattern); Assert.Equal("yyyy/MM/dd", blobReferenceInputDataSourceInResponse1.Properties.DateFormat); Assert.NotNull(inputCreateOrUpdateResponse.Input.Properties.Etag); // Get the input InputGetResponse inputGetResponse = client.Inputs.Get(resourceGroupName, resourceName, inputName); Assert.Equal(HttpStatusCode.OK, inputGetResponse.StatusCode); Assert.Equal(inputName, inputGetResponse.Input.Name); Assert.True(inputGetResponse.Input.Properties is ReferenceInputProperties); ReferenceInputProperties referenceInputPropertiesInResponse2 = (ReferenceInputProperties)inputGetResponse.Input.Properties; Assert.True(referenceInputPropertiesInResponse2.DataSource is BlobReferenceInputDataSource); BlobReferenceInputDataSource blobReferenceInputDataSourceInResponse2 = (BlobReferenceInputDataSource)referenceInputPropertiesInResponse2.DataSource; Assert.Equal("{date}", blobReferenceInputDataSourceInResponse2.Properties.PathPattern); Assert.Equal("yyyy/MM/dd", blobReferenceInputDataSourceInResponse2.Properties.DateFormat); Assert.Equal(inputCreateOrUpdateResponse.Input.Properties.Etag, inputGetResponse.Input.Properties.Etag); // List inputs InputListResponse inputListResponse = client.Inputs.ListInputInJob(resourceGroupName, resourceName, new InputListParameters()); Assert.Equal(HttpStatusCode.OK, inputListResponse.StatusCode); Assert.Equal(1, inputListResponse.Value.Count); // Test input connectivity DataSourceTestConnectionResponse response = client.Inputs.TestConnection(resourceGroupName, resourceName, inputName); Assert.Equal(OperationStatus.Succeeded, response.Status); Assert.Equal(DataSourceTestStatus.TestSucceeded, response.DataSourceTestStatus); // Update the input BlobReferenceInputDataSource blobReferenceInputDataSource = new BlobReferenceInputDataSource() { Properties = new BlobReferenceInputDataSourceProperties() { StorageAccounts = new[] { storageAccount }, Container = "state", PathPattern = "test.csv", DateFormat = "yyyy/MM/dd" } }; ((ReferenceInputProperties)inputProperties).DataSource = blobReferenceInputDataSource; inputProperties.Etag = inputCreateOrUpdateResponse.Input.Properties.Etag; InputPatchParameters inputPatchParameters = new InputPatchParameters(inputProperties); InputPatchResponse inputPatchResponse = client.Inputs.Patch(resourceGroupName, resourceName, inputName, inputPatchParameters); Assert.Equal(HttpStatusCode.OK, inputPatchResponse.StatusCode); Assert.True(inputPatchResponse.Properties is ReferenceInputProperties); ReferenceInputProperties referenceInputPropertiesInResponse3 = (ReferenceInputProperties)inputPatchResponse.Properties; Assert.True(referenceInputPropertiesInResponse3.DataSource is BlobReferenceInputDataSource); BlobReferenceInputDataSource blobReferenceInputDataSourceInResponse3 = (BlobReferenceInputDataSource)referenceInputPropertiesInResponse3.DataSource; Assert.Equal("test.csv", blobReferenceInputDataSourceInResponse3.Properties.PathPattern); Assert.Equal("yyyy/MM/dd", blobReferenceInputDataSourceInResponse3.Properties.DateFormat); Assert.NotNull(inputPatchResponse.Properties.Etag); Assert.NotEqual(inputCreateOrUpdateResponse.Input.Properties.Etag, inputPatchResponse.Properties.Etag); // Delete the inputs AzureOperationResponse deleteInputOperationResponse = client.Inputs.Delete(resourceGroupName, resourceName, inputName); Assert.Equal(HttpStatusCode.OK, deleteInputOperationResponse.StatusCode); // Check that there are 0 inputs in the job jobGetParameters = new JobGetParameters("inputs"); jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(0, jobGetResponse.Job.Properties.Inputs.Count); } finally { client.StreamingJobs.Delete(resourceGroupName, resourceName); resourceClient.ResourceGroups.Delete(resourceGroupName); } } }
public void Test_InputOperations_IoTHub() { BasicDelegatingHandler handler = new BasicDelegatingHandler(); using (var undoContext = UndoContext.Current) { undoContext.Start(); string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics"); string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK"); string serviceLocation = TestHelper.GetDefaultLocation(); var resourceClient = TestHelper.GetResourceClient(handler); var client = TestHelper.GetStreamAnalyticsManagementClient(handler); try { ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation }; resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup); // Construct the JobCreateProperties JobCreateOrUpdateParameters jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation)); // Create a streaming job JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode); // Get a streaming job to check JobGetParameters jobGetParameters = new JobGetParameters(string.Empty); JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(serviceLocation, jobGetResponse.Job.Location); Assert.Equal(resourceName, jobGetResponse.Job.Name); // Construct the Input string sharedAccessPolicyName = "owner"; InputProperties inputProperties = new StreamInputProperties() { Serialization = new CsvSerialization() { Properties = new CsvSerializationProperties() { FieldDelimiter = ",", Encoding = "UTF8" } }, DataSource = new IoTHubStreamInputDataSource() { Properties = new IoTHubStreamInputDataSourceProperties() { IotHubNamespace = TestHelper.IoTHubNamespace, SharedAccessPolicyName = sharedAccessPolicyName, SharedAccessPolicyKey = TestHelper.IotHubSharedAccessPolicyKey } } }; string inputName = TestUtilities.GenerateName("inputtest"); Input input1 = new Input(inputName) { Properties = inputProperties }; // Add an input InputCreateOrUpdateParameters inputCreateOrUpdateParameters = new InputCreateOrUpdateParameters(); inputCreateOrUpdateParameters.Input = input1; InputCreateOrUpdateResponse inputCreateOrUpdateResponse = client.Inputs.CreateOrUpdate(resourceGroupName, resourceName, inputCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, inputCreateOrUpdateResponse.StatusCode); Assert.Equal(inputName, inputCreateOrUpdateResponse.Input.Name); Assert.Equal("Stream", inputCreateOrUpdateResponse.Input.Properties.Type); Assert.True(inputCreateOrUpdateResponse.Input.Properties.Serialization is CsvSerialization); CsvSerialization csvSerializationInResponse1 = (CsvSerialization)inputCreateOrUpdateResponse.Input.Properties.Serialization; Assert.Equal(",", csvSerializationInResponse1.Properties.FieldDelimiter); Assert.True(inputCreateOrUpdateResponse.Input.Properties is StreamInputProperties); StreamInputProperties streamInputPropertiesInResponse = (StreamInputProperties)inputCreateOrUpdateResponse.Input.Properties; Assert.True(streamInputPropertiesInResponse.DataSource is IoTHubStreamInputDataSource); IoTHubStreamInputDataSource iotHubInputDataSourceInResponse1 = (IoTHubStreamInputDataSource)streamInputPropertiesInResponse.DataSource; Assert.Equal(sharedAccessPolicyName, iotHubInputDataSourceInResponse1.Properties.SharedAccessPolicyName); Assert.NotNull(streamInputPropertiesInResponse.Etag); // Get the input InputGetResponse inputGetResponse = client.Inputs.Get(resourceGroupName, resourceName, inputName); Assert.Equal(HttpStatusCode.OK, inputGetResponse.StatusCode); Assert.Equal(inputName, inputGetResponse.Input.Name); Assert.True(inputGetResponse.Input.Properties is StreamInputProperties); StreamInputProperties streamInputPropertiesInResponse2 = (StreamInputProperties)inputGetResponse.Input.Properties; Assert.True(streamInputPropertiesInResponse2.DataSource is IoTHubStreamInputDataSource); IoTHubStreamInputDataSource iotHubInputDataSourceInResponse2 = (IoTHubStreamInputDataSource)streamInputPropertiesInResponse2.DataSource; Assert.Equal(sharedAccessPolicyName, iotHubInputDataSourceInResponse2.Properties.SharedAccessPolicyName); Assert.Equal(inputCreateOrUpdateResponse.Input.Properties.Etag, inputGetResponse.Input.Properties.Etag); // Test input connectivity DataSourceTestConnectionResponse response = client.Inputs.TestConnection(resourceGroupName, resourceName, inputName); Assert.Equal(OperationStatus.Succeeded, response.Status); Assert.Equal(DataSourceTestStatus.TestSucceeded, response.DataSourceTestStatus); // Update the input string newSharedPolicyName = TestUtilities.GenerateName("owner"); inputProperties = new StreamInputProperties() { Serialization = new CsvSerialization() { Properties = new CsvSerializationProperties() { FieldDelimiter = "|", Encoding = "UTF8" } }, DataSource = new IoTHubStreamInputDataSource() { Properties = new IoTHubStreamInputDataSourceProperties() { IotHubNamespace = TestHelper.IoTHubNamespace, SharedAccessPolicyName = newSharedPolicyName, SharedAccessPolicyKey = TestHelper.IotHubSharedAccessPolicyKey } } }; inputProperties.Etag = inputCreateOrUpdateResponse.Input.Properties.Etag; InputPatchParameters inputPatchParameters = new InputPatchParameters(inputProperties); InputPatchResponse inputPatchResponse = client.Inputs.Patch(resourceGroupName, resourceName, inputName, inputPatchParameters); Assert.Equal(HttpStatusCode.OK, inputPatchResponse.StatusCode); Assert.True(inputPatchResponse.Properties.Serialization is CsvSerialization); CsvSerialization csvSerializationInResponse2 = (CsvSerialization)inputPatchResponse.Properties.Serialization; Assert.Equal("|", csvSerializationInResponse2.Properties.FieldDelimiter); Assert.True(inputPatchResponse.Properties is StreamInputProperties); StreamInputProperties streamInputPropertiesInResponse3 = (StreamInputProperties)inputPatchResponse.Properties; Assert.True(streamInputPropertiesInResponse3.DataSource is IoTHubStreamInputDataSource); IoTHubStreamInputDataSource iotHubInputDataSourceInResponse3 = (IoTHubStreamInputDataSource)streamInputPropertiesInResponse3.DataSource; Assert.Equal(newSharedPolicyName, iotHubInputDataSourceInResponse3.Properties.SharedAccessPolicyName); Assert.NotNull(inputPatchResponse.Properties.Etag); Assert.NotEqual(streamInputPropertiesInResponse.Etag, inputPatchResponse.Properties.Etag); // Delete the input AzureOperationResponse deleteInputOperationResponse = client.Inputs.Delete(resourceGroupName, resourceName, inputName); Assert.Equal(HttpStatusCode.OK, deleteInputOperationResponse.StatusCode); // Check that there are 0 inputs in the job jobGetParameters = new JobGetParameters("inputs"); jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(0, jobGetResponse.Job.Properties.Inputs.Count); } finally { client.StreamingJobs.Delete(resourceGroupName, resourceName); resourceClient.ResourceGroups.Delete(resourceGroupName); } } }
// retrieve the windows azure managment certificate from current user public ApprenticeShipSchedular() { var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, "CA4716F85EC0962E2B69BB7D626CC16B924E550D", false)[0]; store.Close(); // // create management credencials and cloud service management client var credentials = new CertificateCloudCredentials("bd27a79c-de25-4097-a874-3bb35f2b926a", certificate); var cloudServiceMgmCli = new CloudServiceManagementClient(credentials); // create cloud service var cloudServiceCreateParameters = new CloudServiceCreateParameters { Description = "sfaarmscheduler", Email = "*****@*****.**", GeoRegion = "West Europe", Label = "sfaarmscheduler" }; var cloudService = cloudServiceMgmCli.CloudServices.Create("sfaarmscheduler", cloudServiceCreateParameters); // create job collection var schedulerMgmCli = new SchedulerManagementClient(credentials); var jobCollectionIntrinsicSettings = new JobCollectionIntrinsicSettings { //Add Credentials for X.509 Plan = JobCollectionPlan.Free, Quota = new JobCollectionQuota { MaxJobCount = 5, MaxJobOccurrence = 1, MaxRecurrence = new JobCollectionMaxRecurrence { Frequency = JobCollectionRecurrenceFrequency.Hour, Interval = 1 } } }; var jobCollectionCreateParameters = new JobCollectionCreateParameters { IntrinsicSettings = jobCollectionIntrinsicSettings, Label = "sfajobcollection" }; var jobCollectionCreateResponse = schedulerMgmCli.JobCollections.Create("sfaarmscheduler", "sfajobcollection", jobCollectionCreateParameters); var schedulerClient = new SchedulerClient("sfaarmscheduler", "sfajobcollection", credentials); var jobAction = new JobAction { Type = JobActionType.StorageQueue, QueueMessage = new JobQueueMessage() { Message = "test", QueueName = "vacancysummaryqueueitem", SasToken = "?sv=2017-04-17&ss=bfqt&srt=sco&sp=rwdlacup&se=2017-09-01T21:58:06Z&st=2017-09-01T13:58:06Z&spr=https&sig=r0u1A01ytcN213lsWO47Td7DUaU7lo3aQTPNTCrHRxU%3D", StorageAccountName = "sfabetastorage" } //Request = new JobHttpRequest //{ // Uri = new Uri("http://blog.shaunxu.me"), // Method = "GET" //} }; var jobRecurrence = new JobRecurrence { Frequency = JobRecurrenceFrequency.Hour, Interval = 1 }; var jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters { Action = jobAction, Recurrence = jobRecurrence }; var jobCreateResponse = schedulerClient.Jobs.CreateOrUpdate("poll_blog", jobCreateOrUpdateParameters); var jobGetHistoryParameters = new JobGetHistoryParameters { Skip = 0, Top = 100 }; var history = schedulerClient.Jobs.GetHistory("poll_blog", jobGetHistoryParameters); foreach (var action in history) { Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", action.Status, action.Message, action.RetryCount, action.RepeatCount, action.Timestamp); } }
private void DeployASJob(SAJobConfigModel cfg) { // Get authentication token TokenCloudCredentials aadTokenCredentials = new TokenCloudCredentials(cfg.subscriptionID, GetAuthorizationHeader()); // Create Stream Analytics management client StreamAnalyticsManagementClient client = new StreamAnalyticsManagementClient(aadTokenCredentials); // Create a Stream Analytics job JobCreateOrUpdateParameters jobCreateParameters = new JobCreateOrUpdateParameters() { Job = new Job() { Name = cfg.streamAnalyticsJobName, Location = cfg.location, Properties = new JobProperties() { EventsOutOfOrderPolicy = EventsOutOfOrderPolicy.Adjust, Sku = new Sku() { Name = "Standard" } } } }; JobCreateOrUpdateResponse jobCreateResponse = client.StreamingJobs.CreateOrUpdate(cfg.resourceGroupName, jobCreateParameters); TempData["jobName"] = jobCreateResponse.Job.Name; TempData["jobCreationStatus"] = jobCreateResponse.StatusCode; // Create a Stream Analytics input source InputCreateOrUpdateParameters jobInputCreateParameters = new InputCreateOrUpdateParameters() { Input = new Input() { Name = cfg.streamAnalyticsInputName, Properties = new StreamInputProperties() { Serialization = new CsvSerialization { Properties = new CsvSerializationProperties { Encoding = "UTF8", FieldDelimiter = "," } }, DataSource = new EventHubStreamInputDataSource { Properties = new EventHubStreamInputDataSourceProperties { EventHubName = cfg.EventHubName, ServiceBusNamespace = cfg.ServiceBusNamespace, SharedAccessPolicyKey = cfg.SharedAccessPolicyKey, SharedAccessPolicyName = cfg.SharedAccessPolicyName, } } } } }; InputCreateOrUpdateResponse inputCreateResponse = client.Inputs.CreateOrUpdate(cfg.resourceGroupName, cfg.streamAnalyticsJobName, jobInputCreateParameters); TempData["jobInputName"] = inputCreateResponse.Input.Name; TempData["jobInputCreationStatus"] = inputCreateResponse.StatusCode; }
/// <summary> /// If a scheduler job already exists, this will merge the existing job config values with the request /// </summary> /// <param name="job">Existing Scheduler job</param> /// <param name="jobRequest">Request values</param> /// <param name="type">Http or Storage</param> /// <returns>JobCreateOrUpdateParameters object to use when updating Scheduler job</returns> private JobCreateOrUpdateParameters PopulateExistingJobParams(Job job, PSCreateJobParams jobRequest, JobActionType type) { JobCreateOrUpdateParameters jobUpdateParams = new JobCreateOrUpdateParameters(); if (type.Equals(JobActionType.StorageQueue)) { if (jobRequest.IsStorageActionSet()) { jobUpdateParams.Action = new JobAction(); jobUpdateParams.Action.QueueMessage = new JobQueueMessage(); if (job.Action != null) { jobUpdateParams.Action.Type = job.Action.Type; if (job.Action.QueueMessage != null) { jobUpdateParams.Action.QueueMessage.Message = string.IsNullOrEmpty(jobRequest.StorageQueueMessage) ? job.Action.QueueMessage.Message : jobRequest.StorageQueueMessage; jobUpdateParams.Action.QueueMessage.QueueName = jobRequest.QueueName ?? job.Action.QueueMessage.QueueName; jobUpdateParams.Action.QueueMessage.SasToken = jobRequest.SasToken ?? job.Action.QueueMessage.SasToken; jobUpdateParams.Action.QueueMessage.StorageAccountName = job.Action.QueueMessage.StorageAccountName; } else if (job.Action.QueueMessage == null) { jobUpdateParams.Action.QueueMessage.Message = string.IsNullOrEmpty(jobRequest.StorageQueueMessage) ? string.Empty : jobRequest.StorageQueueMessage; jobUpdateParams.Action.QueueMessage.QueueName = jobRequest.QueueName; jobUpdateParams.Action.QueueMessage.SasToken = jobRequest.SasToken; jobUpdateParams.Action.QueueMessage.StorageAccountName = jobRequest.StorageAccount; } } else { jobUpdateParams.Action.QueueMessage.Message = string.IsNullOrEmpty(jobRequest.StorageQueueMessage) ? string.Empty : jobRequest.StorageQueueMessage; jobUpdateParams.Action.QueueMessage.QueueName = jobRequest.QueueName; jobUpdateParams.Action.QueueMessage.SasToken = jobRequest.SasToken; jobUpdateParams.Action.QueueMessage.StorageAccountName = jobRequest.StorageAccount; } } else { jobUpdateParams.Action = job.Action; } } else //If it is a HTTP job action type { if (jobRequest.IsActionSet()) { jobUpdateParams.Action = new JobAction(); jobUpdateParams.Action.Request = new JobHttpRequest(); if (job.Action != null) { jobUpdateParams.Action.Type = job.Action.Type; if (job.Action.Request != null) { jobUpdateParams.Action.Request.Uri = jobRequest.Uri ?? job.Action.Request.Uri; jobUpdateParams.Action.Request.Method = jobRequest.Method ?? job.Action.Request.Method; jobUpdateParams.Action.Request.Headers = jobRequest.Headers == null ? job.Action.Request.Headers : jobRequest.Headers.ToDictionary(); jobUpdateParams.Action.Request.Body = jobRequest.Body ?? job.Action.Request.Body; //Job has existing authentication if (job.Action.Request.Authentication != null) { if (jobRequest.HttpAuthType.Equals("ClientCertificate", StringComparison.OrdinalIgnoreCase)) { jobUpdateParams.Action.Request.Authentication = SetClientCertAuthentication(jobRequest, jobUpdateParams); } else if (jobRequest.HttpAuthType.Equals("None", StringComparison.OrdinalIgnoreCase)) { if (!string.IsNullOrEmpty(jobRequest.ClientCertPfx) || !string.IsNullOrEmpty(jobRequest.ClientCertPassword)) { throw new InvalidOperationException(Resources.SchedulerInvalidNoneAuthRequest); } else { jobUpdateParams.Action.Request.Authentication = null; } } else { jobUpdateParams.Action.Request.Authentication = job.Action.Request.Authentication; } } else if (job.Action.Request.Authentication == null && jobRequest.HttpAuthType.Equals("ClientCertificate", StringComparison.OrdinalIgnoreCase)) { jobUpdateParams.Action.Request.Authentication = SetClientCertAuthentication(jobRequest, jobUpdateParams); } } else if (job.Action.Request == null) { jobUpdateParams.Action.Request.Uri = jobRequest.Uri; jobUpdateParams.Action.Request.Method = jobRequest.Method; jobUpdateParams.Action.Request.Headers = jobRequest.Headers.ToDictionary(); jobUpdateParams.Action.Request.Body = jobRequest.Body; if (jobRequest.HttpAuthType.Equals("ClientCertificate", StringComparison.OrdinalIgnoreCase)) { jobUpdateParams.Action.Request.Authentication = SetClientCertAuthentication(jobRequest, jobUpdateParams); } } } else { jobUpdateParams.Action.Request.Uri = jobRequest.Uri; jobUpdateParams.Action.Request.Method = jobRequest.Method; jobUpdateParams.Action.Request.Headers = jobRequest.Headers.ToDictionary(); jobUpdateParams.Action.Request.Body = jobRequest.Body; if (jobRequest.HttpAuthType.Equals("ClientCertificate", StringComparison.OrdinalIgnoreCase)) { jobUpdateParams.Action.Request.Authentication = SetClientCertAuthentication(jobRequest, jobUpdateParams); } } } else { jobUpdateParams.Action = job.Action; } } if (jobRequest.IsErrorActionSet()) { jobUpdateParams.Action.ErrorAction = new JobErrorAction(); jobUpdateParams.Action.ErrorAction.Request = new JobHttpRequest(); jobUpdateParams.Action.ErrorAction.QueueMessage = new JobQueueMessage(); if (job.Action.ErrorAction != null) { if (job.Action.ErrorAction.Request != null) { jobUpdateParams.Action.ErrorAction.Request.Uri = jobRequest.ErrorActionUri ?? job.Action.ErrorAction.Request.Uri; jobUpdateParams.Action.ErrorAction.Request.Method = jobRequest.ErrorActionMethod ?? job.Action.ErrorAction.Request.Method; jobUpdateParams.Action.ErrorAction.Request.Headers = jobRequest.ErrorActionHeaders == null ? job.Action.ErrorAction.Request.Headers : jobRequest.Headers.ToDictionary(); jobUpdateParams.Action.ErrorAction.Request.Body = jobRequest.ErrorActionBody ?? job.Action.ErrorAction.Request.Body; } else if (job.Action.ErrorAction.Request == null) { jobUpdateParams.Action.ErrorAction.Request.Uri = jobRequest.ErrorActionUri; jobUpdateParams.Action.ErrorAction.Request.Method = jobRequest.ErrorActionMethod; jobUpdateParams.Action.ErrorAction.Request.Headers = jobRequest.ErrorActionHeaders.ToDictionary(); jobUpdateParams.Action.ErrorAction.Request.Body = jobRequest.ErrorActionBody; } if (job.Action.ErrorAction.QueueMessage != null) { jobUpdateParams.Action.ErrorAction.QueueMessage.Message = jobRequest.ErrorActionQueueBody ?? job.Action.ErrorAction.QueueMessage.Message; jobUpdateParams.Action.ErrorAction.QueueMessage.QueueName = jobRequest.ErrorActionQueueName ?? job.Action.ErrorAction.QueueMessage.QueueName; jobUpdateParams.Action.ErrorAction.QueueMessage.SasToken = jobRequest.ErrorActionSasToken ?? job.Action.ErrorAction.QueueMessage.SasToken; jobUpdateParams.Action.ErrorAction.QueueMessage.StorageAccountName = jobRequest.ErrorActionStorageAccount ?? job.Action.ErrorAction.QueueMessage.StorageAccountName; } else if (job.Action.ErrorAction.QueueMessage == null) { jobUpdateParams.Action.ErrorAction.QueueMessage.Message = jobRequest.ErrorActionQueueBody; jobUpdateParams.Action.ErrorAction.QueueMessage.QueueName = jobRequest.ErrorActionQueueName; jobUpdateParams.Action.ErrorAction.QueueMessage.SasToken = jobRequest.ErrorActionSasToken; jobUpdateParams.Action.ErrorAction.QueueMessage.StorageAccountName = jobRequest.ErrorActionStorageAccount; } } else if (job.Action.ErrorAction == null) { jobUpdateParams.Action.ErrorAction.Request.Uri = jobRequest.ErrorActionUri; jobUpdateParams.Action.ErrorAction.Request.Method = jobRequest.ErrorActionMethod; jobUpdateParams.Action.ErrorAction.Request.Headers = jobRequest.ErrorActionHeaders.ToDictionary(); jobUpdateParams.Action.ErrorAction.Request.Body = jobRequest.ErrorActionBody; jobUpdateParams.Action.ErrorAction.QueueMessage.Message = jobRequest.ErrorActionQueueBody; jobUpdateParams.Action.ErrorAction.QueueMessage.QueueName = jobRequest.ErrorActionQueueName; jobUpdateParams.Action.ErrorAction.QueueMessage.SasToken = jobRequest.ErrorActionSasToken; jobUpdateParams.Action.ErrorAction.QueueMessage.StorageAccountName = jobRequest.ErrorActionStorageAccount; } } else { jobUpdateParams.Action.ErrorAction = job.Action.ErrorAction; } if (jobRequest.IsRecurrenceSet()) { jobUpdateParams.Recurrence = new JobRecurrence(); if (job.Recurrence != null) { jobUpdateParams.Recurrence.Count = jobRequest.ExecutionCount ?? job.Recurrence.Count; jobUpdateParams.Recurrence.EndTime = jobRequest.EndTime ?? job.Recurrence.EndTime; jobUpdateParams.Recurrence.Frequency = string.IsNullOrEmpty(jobRequest.Frequency) ? job.Recurrence.Frequency : (JobRecurrenceFrequency)Enum.Parse(typeof(JobRecurrenceFrequency), jobRequest.Frequency); jobUpdateParams.Recurrence.Interval = jobRequest.Interval ?? job.Recurrence.Interval; jobUpdateParams.Recurrence.Schedule = SetRecurrenceSchedule(job.Recurrence.Schedule); } else if (job.Recurrence == null) { jobUpdateParams.Recurrence.Count = jobRequest.ExecutionCount; jobUpdateParams.Recurrence.EndTime = jobRequest.EndTime; jobUpdateParams.Recurrence.Frequency = string.IsNullOrEmpty(jobRequest.Frequency) ? default(JobRecurrenceFrequency) : (JobRecurrenceFrequency)Enum.Parse(typeof(JobRecurrenceFrequency), jobRequest.Frequency); jobUpdateParams.Recurrence.Interval = jobRequest.Interval; jobUpdateParams.Recurrence.Schedule = null; } } else { jobUpdateParams.Recurrence = job.Recurrence; if (jobUpdateParams.Recurrence != null) { jobUpdateParams.Recurrence.Schedule = SetRecurrenceSchedule(job.Recurrence.Schedule); } } jobUpdateParams.Action.RetryPolicy = job.Action.RetryPolicy; jobUpdateParams.StartTime = jobRequest.StartTime ?? job.StartTime; return(jobUpdateParams); }
public void Test_JobOperations_E2E() { BasicDelegatingHandler handler = new BasicDelegatingHandler(); using (var undoContext = UndoContext.Current) { undoContext.Start(); string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics"); string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK"); string serviceLocation = TestHelper.GetDefaultLocation(); var resourceClient = TestHelper.GetResourceClient(handler); var client = TestHelper.GetStreamAnalyticsManagementClient(handler); try { ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation }; resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup); Job job = new Job(); job.Name = resourceName; job.Location = serviceLocation; // Construct the general properties for JobProperties JobProperties jobProperties = new JobProperties(); jobProperties.Sku = new Sku() { Name = "standard" }; jobProperties.EventsOutOfOrderPolicy = EventsOutOfOrderPolicy.Drop; jobProperties.EventsOutOfOrderMaxDelayInSeconds = 0; // Construct the Input StorageAccount storageAccount = new StorageAccount { AccountName = TestHelper.AccountName, AccountKey = TestHelper.AccountKey }; InputProperties inputProperties = new StreamInputProperties() { Serialization = new JsonSerialization() { Properties = new JsonSerializationProperties() { Encoding = "UTF8" } }, DataSource = new BlobStreamInputDataSource() { Properties = new BlobStreamInputDataSourceProperties() { StorageAccounts = new[] { storageAccount }, Container = "state", PathPattern = "" } } }; Input input1 = new Input("inputtest") { Properties = inputProperties }; jobProperties.Inputs = new[] { input1 }; // Construct the Output OutputProperties outputProperties = new OutputProperties(); SqlAzureOutputDataSource sqlAzureOutputDataSource = new SqlAzureOutputDataSource() { Properties = new SqlAzureOutputDataSourceProperties() { Server = TestHelper.Server, Database = TestHelper.Database, User = TestHelper.User, Password = TestHelper.Password, Table = "StateInfo" } }; outputProperties.DataSource = sqlAzureOutputDataSource; Output output1 = new Output("outputtest") { Properties = outputProperties }; jobProperties.Outputs = new Output[] { output1 }; // Construct the transformation Transformation transformation = new Transformation() { Name = "transformationtest", Properties = new TransformationProperties() { Query = "Select Id, Name from inputtest", StreamingUnits = 1 } }; jobProperties.Transformation = transformation; job.Properties = jobProperties; // Construct the JobCreateProperties JobCreateOrUpdateParameters jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters(); jobCreateOrUpdateParameters.Job = job; // Create a streaming job JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode); Assert.NotNull(jobCreateOrUpdateResponse.Job.Properties.Etag); // Get a streaming job to check JobGetParameters jobGetParameters = new JobGetParameters("inputs,transformation,outputs"); JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(serviceLocation, jobGetResponse.Job.Location); Assert.Equal(resourceName, jobGetResponse.Job.Name); Assert.True(jobGetResponse.Job.Properties.Inputs[0].Properties is StreamInputProperties); StreamInputProperties streamInputProperties = jobGetResponse.Job.Properties.Inputs[0].Properties as StreamInputProperties; Assert.Equal("Stream", jobGetResponse.Job.Properties.Inputs[0].Properties.Type); Assert.Equal("Microsoft.Storage/Blob", streamInputProperties.DataSource.Type); Assert.Equal("Json", streamInputProperties.Serialization.Type); Assert.Equal(EventsOutOfOrderPolicy.Drop, jobGetResponse.Job.Properties.EventsOutOfOrderPolicy); Assert.NotNull(jobGetResponse.Job.Properties.Etag); Assert.Equal(jobCreateOrUpdateResponse.Job.Properties.Etag, jobGetResponse.Job.Properties.Etag); // Patch the streaming job JobPatchParameters jobPatchParameters = new JobPatchParameters() { JobPatchRequest = new JobPatchRequest() { Properties = new JobProperties() { EventsOutOfOrderPolicy = EventsOutOfOrderPolicy.Adjust } } }; var jobPatchResponse = client.StreamingJobs.Patch(resourceGroupName, resourceName, jobPatchParameters); jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobPatchResponse.StatusCode); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(EventsOutOfOrderPolicy.Adjust, jobPatchResponse.Job.Properties.EventsOutOfOrderPolicy); Assert.Equal(EventsOutOfOrderPolicy.Adjust, jobGetResponse.Job.Properties.EventsOutOfOrderPolicy); JobListParameters parameters = new JobListParameters(string.Empty); JobListResponse response = client.StreamingJobs.ListJobsInResourceGroup(resourceGroupName, parameters); Assert.Equal(HttpStatusCode.OK, response.StatusCode); // Start a streaming job JobStartParameters jobStartParameters = new JobStartParameters() { OutputStartMode = OutputStartMode.LastOutputEventTime }; CloudException cloudException = Assert.Throws <CloudException>(() => client.StreamingJobs.Start(resourceGroupName, resourceName, jobStartParameters)); Assert.Equal("LastOutputEventTime must be available when OutputStartMode is set to LastOutputEventTime. Please make sure at least one output event has been processed. ", cloudException.Error.Message); jobStartParameters.OutputStartMode = OutputStartMode.CustomTime; jobStartParameters.OutputStartTime = new DateTime(2012, 12, 12, 12, 12, 12, DateTimeKind.Utc); AzureOperationResponse jobStartOperationResponse = client.StreamingJobs.Start(resourceGroupName, resourceName, jobStartParameters); Assert.Equal(HttpStatusCode.OK, jobStartOperationResponse.StatusCode); // Get a streaming job to check jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.True(IsRunning(jobGetResponse.Job.Properties.JobState)); // Check diagnostics InputListResponse inputListResponse = client.Inputs.ListInputInJob(resourceGroupName, resourceName, new InputListParameters("*")); Assert.Equal(HttpStatusCode.OK, inputListResponse.StatusCode); Assert.NotEqual(0, inputListResponse.Value.Count); Assert.NotNull(inputListResponse.Value[0].Properties.Diagnostics); Assert.NotEqual(0, inputListResponse.Value[0].Properties.Diagnostics.Conditions.Count); Assert.NotNull(inputListResponse.Value[0].Properties.Diagnostics.Conditions[0].Code); Assert.NotNull(inputListResponse.Value[0].Properties.Diagnostics.Conditions[0].Message); // Stop a streaming job AzureOperationResponse jobStopOperationResponse = client.StreamingJobs.Stop(resourceGroupName, resourceName); Assert.Equal(HttpStatusCode.OK, jobStopOperationResponse.StatusCode); // Get a streaming job to check jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(JobRunningState.Stopped, jobGetResponse.Job.Properties.JobState); // Delete a streaming job AzureOperationResponse jobDeleteOperationResponse = client.StreamingJobs.Delete(resourceGroupName, resourceName); Assert.Equal(HttpStatusCode.OK, jobDeleteOperationResponse.StatusCode); } finally { client.StreamingJobs.Delete(resourceGroupName, resourceName); resourceClient.ResourceGroups.Delete(resourceGroupName); } } }
protected override async Task OnExecute(SubscriptionCloudCredentials credentials) { if (!String.IsNullOrEmpty(EncodedPayload)) { Payload = Encoding.UTF8.GetString(Convert.FromBase64String(EncodedPayload)); } if (ServiceUri == null) { await Console.WriteErrorLine(Strings.ParameterRequired, "SerivceUri"); } else { Dictionary <string, string> payload = null; if (!String.IsNullOrEmpty(Payload)) { payload = InvocationPayloadSerializer.Deserialize(Payload); } var bodyValue = new InvocationRequest( Job, "Scheduler", payload) { JobInstanceName = InstanceName, UnlessAlreadyRunning = Singleton }; var body = JsonFormat.Serialize(bodyValue); var request = new JobCreateOrUpdateParameters() { StartTime = StartTime, Action = new JobAction() { Type = JobActionType.Https, Request = new JobHttpRequest() { Uri = new Uri(ServiceUri, "work/invocations"), Method = "PUT", Body = body, Headers = new Dictionary <string, string>() { { "Content-Type", "application/json" }, { "Authorization", GenerateAuthHeader(Password) } } } // TODO: Retry Policy }, Recurrence = new JobRecurrence() { Count = Count, EndTime = EndTime, Frequency = Frequency, Interval = Interval // TODO: Schedule field? } }; using (var client = CloudContext.Clients.CreateSchedulerClient(credentials, CloudService, Collection)) { await Console.WriteInfoLine(Strings.Scheduler_NewJobCommand_CreatingJob, Job, CloudService, Collection); if (WhatIf) { await Console.WriteInfoLine(Strings.Scheduler_NewJobCommand_WouldCreateJob, JsonConvert.SerializeObject(request, new JsonSerializerSettings() { Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver() })); } else { var response = await client.Jobs.CreateOrUpdateAsync(InstanceName, request, CancellationToken.None); await Console.WriteObject(response.Job); } await Console.WriteInfoLine(Strings.Scheduler_NewJobCommand_CreatedJob, Job, CloudService, Collection); } } }
/// <summary> /// Creates a new Job with a user-provided job id, or updates an /// existing job, replacing its definition with that specified. /// </summary> /// <param name='operations'> /// Reference to the Microsoft.WindowsAzure.Scheduler.IJobOperations. /// </param> /// <param name='jobId'> /// Required. Id of the job to create or update. /// </param> /// <param name='parameters'> /// Required. Parameters specifying the job definition for a /// CreateOrUpdate Job operation. /// </param> /// <returns> /// The CreateOrUpdate Job operation response. /// </returns> public static Task <JobCreateOrUpdateResponse> CreateOrUpdateAsync(this IJobOperations operations, string jobId, JobCreateOrUpdateParameters parameters) { return(operations.CreateOrUpdateAsync(jobId, parameters, CancellationToken.None)); }
public void Test_FunctionOperations_RetrieveDefaultDefinitionWithRawJsonContent() { BasicDelegatingHandler handler = new BasicDelegatingHandler(); using (var undoContext = UndoContext.Current) { undoContext.Start(); string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics"); string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK"); string serviceLocation = TestHelper.GetDefaultLocation(); var resourceClient = TestHelper.GetResourceClient(handler); var client = TestHelper.GetStreamAnalyticsManagementClient(handler); try { ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation }; resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup); // Construct the JobCreateProperties JobCreateOrUpdateParameters jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation)); // Create a streaming job JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode); // Get a streaming job to check JobGetParameters jobGetParameters = new JobGetParameters(string.Empty); JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(serviceLocation, jobGetResponse.Job.Location); Assert.Equal(resourceName, jobGetResponse.Job.Name); // Retrieve default definition of the function string functionName = TestUtilities.GenerateName("functiontest"); string content = File.ReadAllText(@"Resources\RetrieveDefaultFunctionDefinitionRequest.json"); var retrieveDefaultDefinitionParameters = new FunctionRetrieveDefaultDefinitionWithRawJsonContentParameters () { Content = content }; FunctionRetrieveDefaultDefinitionResponse retrieveDefaultDefinitionResponse = client.Functions.RetrieveDefaultDefinitionWithRawJsonContent(resourceGroupName, resourceName, functionName, retrieveDefaultDefinitionParameters); Assert.Equal(functionName, retrieveDefaultDefinitionResponse.Function.Name); ScalarFunctionProperties scalarFunctionProperties = (ScalarFunctionProperties)retrieveDefaultDefinitionResponse.Function.Properties; AzureMachineLearningWebServiceFunctionBinding azureMachineLearningWebServiceFunctionBinding = (AzureMachineLearningWebServiceFunctionBinding)scalarFunctionProperties.Properties.Binding; Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint); Assert.Equal(1000, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize); Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey); azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey = TestHelper.ApiKey; } finally { client.StreamingJobs.Delete(resourceGroupName, resourceName); resourceClient.ResourceGroups.Delete(resourceGroupName); } } }
// retrieve the windows azure managment certificate from current user public Schedular() { var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, "<ThumbPrint>", false)[0]; store.Close(); // // create management credencials and cloud service management client var credentials = new CertificateCloudCredentials("8cb918af-59d0-4b24-893c-15661d048f16", certificate); var cloudServiceMgmCli = new CloudServiceManagementClient(credentials); // create cloud service var cloudServiceCreateParameters = new CloudServiceCreateParameters() { Description = "schedulerdemo1", Email = "microsoft.com", GeoRegion = "Southeast Asia", Label = "schedulerdemo1" }; var cloudService = cloudServiceMgmCli.CloudServices.Create("schedulerdemo1", cloudServiceCreateParameters); // create job collection var schedulerMgmCli = new SchedulerManagementClient(credentials); var jobCollectionIntrinsicSettings = new JobCollectionIntrinsicSettings() { Plan = JobCollectionPlan.Free, Quota = new JobCollectionQuota() { MaxJobCount = 5, MaxJobOccurrence = 1, MaxRecurrence = new JobCollectionMaxRecurrence() { Frequency = JobCollectionRecurrenceFrequency.Hour, Interval = 1 } } }; var jobCollectionCreateParameters = new JobCollectionCreateParameters() { IntrinsicSettings = jobCollectionIntrinsicSettings, Label = "jc1" }; var jobCollectionCreateResponse = schedulerMgmCli.JobCollections.Create("schedulerdemo1", "jc1", jobCollectionCreateParameters); var schedulerClient = new SchedulerClient("schedulerdemo1", "jc1", credentials); var jobAction = new JobAction() { Type = JobActionType.Http, Request = new JobHttpRequest() { Uri = new Uri("http://blog.shaunxu.me"), Method = "GET" } }; var jobRecurrence = new JobRecurrence() { Frequency = JobRecurrenceFrequency.Hour, Interval = 1 }; var jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters() { Action = jobAction, Recurrence = jobRecurrence }; var jobCreateResponse = schedulerClient.Jobs.CreateOrUpdate("poll_blog", jobCreateOrUpdateParameters); var jobGetHistoryParameters = new JobGetHistoryParameters() { Skip = 0, Top = 100 }; var history = schedulerClient.Jobs.GetHistory("poll_blog", jobGetHistoryParameters); foreach (var action in history) { Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", action.Status, action.Message, action.RetryCount, action.RepeatCount, action.Timestamp); } }
public void Test_FunctionOperations_Full_Scalar_AzureMLWebService() { BasicDelegatingHandler handler = new BasicDelegatingHandler(); using (var undoContext = UndoContext.Current) { undoContext.Start(); string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics"); string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK"); string serviceLocation = TestHelper.GetDefaultLocation(); var resourceClient = TestHelper.GetResourceClient(handler); var client = TestHelper.GetStreamAnalyticsManagementClient(handler); try { ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation }; resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup); // Construct the JobCreateProperties JobCreateOrUpdateParameters jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation)); // Create a streaming job JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode); // Get a streaming job to check JobGetParameters jobGetParameters = new JobGetParameters(string.Empty); JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(serviceLocation, jobGetResponse.Job.Location); Assert.Equal(resourceName, jobGetResponse.Job.Name); // Retrieve default definition of the function string functionName = TestUtilities.GenerateName("functiontest"); var retrieveDefaultDefinitionParameters = new AzureMachineLearningWebServiceFunctionRetrieveDefaultDefinitionParameters() { BindingRetrievalProperties = new AzureMachineLearningWebServiceFunctionBindingRetrievalProperties() { ExecuteEndpoint = TestHelper.ExecuteEndpoint, UdfType = "Scalar" } }; FunctionRetrieveDefaultDefinitionResponse retrieveDefaultDefinitionResponse = client.Functions.RetrieveDefaultDefinition(resourceGroupName, resourceName, functionName, retrieveDefaultDefinitionParameters); Assert.Equal(functionName, retrieveDefaultDefinitionResponse.Function.Name); // Add the function ScalarFunctionProperties scalarFunctionProperties = (ScalarFunctionProperties)retrieveDefaultDefinitionResponse.Function.Properties; AzureMachineLearningWebServiceFunctionBinding azureMachineLearningWebServiceFunctionBinding = (AzureMachineLearningWebServiceFunctionBinding)scalarFunctionProperties.Properties.Binding; Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint); Assert.Equal(1000, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize); Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey); azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey = TestHelper.ApiKey; FunctionCreateOrUpdateParameters functionCreateOrUpdateParameters = new FunctionCreateOrUpdateParameters { Function = retrieveDefaultDefinitionResponse.Function }; FunctionCreateOrUpdateResponse functionCreateOrUpdateResponse = client.Functions.CreateOrUpdate(resourceGroupName, resourceName, functionCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, functionCreateOrUpdateResponse.StatusCode); Assert.Equal(functionName, functionCreateOrUpdateResponse.Function.Name); scalarFunctionProperties = (ScalarFunctionProperties)functionCreateOrUpdateResponse.Function.Properties; azureMachineLearningWebServiceFunctionBinding = (AzureMachineLearningWebServiceFunctionBinding)scalarFunctionProperties.Properties.Binding; Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint); Assert.Equal(1000, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize); Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey); Assert.NotNull(functionCreateOrUpdateResponse.Function.Properties.Etag); // Get the function FunctionGetResponse functionGetResponse = client.Functions.Get(resourceGroupName, resourceName, functionName); Assert.Equal(HttpStatusCode.OK, functionGetResponse.StatusCode); Assert.Equal(functionName, functionGetResponse.Function.Name); scalarFunctionProperties = (ScalarFunctionProperties)functionGetResponse.Function.Properties; azureMachineLearningWebServiceFunctionBinding = (AzureMachineLearningWebServiceFunctionBinding)scalarFunctionProperties.Properties.Binding; Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint); Assert.Equal(1000, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize); Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey); Assert.Equal(functionCreateOrUpdateResponse.Function.Properties.Etag, functionGetResponse.Function.Properties.Etag); // List functions FunctionListResponse functionListResponse = client.Functions.ListFunctionsInJob(resourceGroupName, resourceName); Assert.Equal(HttpStatusCode.OK, functionListResponse.StatusCode); Assert.Equal(1, functionListResponse.Value.Count); // Check that there is 1 function in the job jobGetParameters = new JobGetParameters("functions"); jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(1, jobGetResponse.Job.Properties.Functions.Count); // Test function connectivity ResourceTestConnectionResponse response = client.Functions.TestConnection(resourceGroupName, resourceName, functionName); Assert.Equal(OperationStatus.Succeeded, response.Status); Assert.Equal(ResourceTestStatus.TestSucceeded, response.ResourceTestStatus); // Update the function ScalarFunctionProperties scalarFunctionPropertiesForPatch = new ScalarFunctionProperties() { Properties = new ScalarFunctionConfiguration() { Binding = new AzureMachineLearningWebServiceFunctionBinding() { Properties = new AzureMachineLearningWebServiceFunctionBindingProperties() { BatchSize = 256 } } } }; FunctionPatchParameters functionPatchParameters = new FunctionPatchParameters(scalarFunctionPropertiesForPatch); FunctionPatchResponse functionPatchResponse = client.Functions.Patch(resourceGroupName, resourceName, functionName, functionPatchParameters); Assert.Equal(HttpStatusCode.OK, functionPatchResponse.StatusCode); scalarFunctionProperties = (ScalarFunctionProperties)functionPatchResponse.Properties; azureMachineLearningWebServiceFunctionBinding = (AzureMachineLearningWebServiceFunctionBinding)scalarFunctionProperties.Properties.Binding; Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint); Assert.Equal(256, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize); Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey); Assert.NotNull(functionPatchResponse.Properties.Etag); Assert.NotEqual(functionCreateOrUpdateResponse.Function.Properties.Etag, functionPatchResponse.Properties.Etag); // Add second function string functionName2 = TestUtilities.GenerateName("functiontest"); Function function2 = new Function(functionName2) { Properties = retrieveDefaultDefinitionResponse.Function.Properties }; functionCreateOrUpdateParameters.Function = function2; functionCreateOrUpdateResponse = client.Functions.CreateOrUpdate(resourceGroupName, resourceName, functionCreateOrUpdateParameters); // List functions functionListResponse = client.Functions.ListFunctionsInJob(resourceGroupName, resourceName); Assert.Equal(HttpStatusCode.OK, functionListResponse.StatusCode); Assert.Equal(2, functionListResponse.Value.Count); // Check that there are 2 functions in the job jobGetParameters = new JobGetParameters("functions"); jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(2, jobGetResponse.Job.Properties.Functions.Count); // Delete the functions AzureOperationResponse deleteFunctionOperationResponse = client.Functions.Delete(resourceGroupName, resourceName, functionName); Assert.Equal(HttpStatusCode.OK, deleteFunctionOperationResponse.StatusCode); deleteFunctionOperationResponse = client.Functions.Delete(resourceGroupName, resourceName, functionName2); Assert.Equal(HttpStatusCode.OK, deleteFunctionOperationResponse.StatusCode); // Check that there are 0 functions in the job jobGetParameters = new JobGetParameters("functions"); jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(0, jobGetResponse.Job.Properties.Functions.Count); } finally { client.StreamingJobs.Delete(resourceGroupName, resourceName); resourceClient.ResourceGroups.Delete(resourceGroupName); } } }
public void Test_FunctionOperations_Scalar_JavaScript() { BasicDelegatingHandler handler = new BasicDelegatingHandler(); using (var undoContext = UndoContext.Current) { undoContext.Start(); string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics"); string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK"); string serviceLocation = TestHelper.GetDefaultLocation(); var resourceClient = TestHelper.GetResourceClient(handler); var client = TestHelper.GetStreamAnalyticsManagementClient(handler); try { ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation }; resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup); // Construct the JobCreateProperties JobCreateOrUpdateParameters jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation)); // Create a streaming job JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode); // Get a streaming job to check JobGetParameters jobGetParameters = new JobGetParameters(string.Empty); JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(serviceLocation, jobGetResponse.Job.Location); Assert.Equal(resourceName, jobGetResponse.Job.Name); // Retrieve default definition of the function string functionName = TestUtilities.GenerateName("functiontest"); string javaScriptFunctionCode = @"function (x, y) { return x + y; }"; var retrieveDefaultDefinitionParameters = new JavaScriptFunctionRetrieveDefaultDefinitionParameters () { BindingRetrievalProperties = new JavaScriptFunctionBindingRetrievalProperties() { Script = javaScriptFunctionCode, UdfType = "Scalar" } }; CloudException cloudException = Assert.Throws <CloudException>( () => client.Functions.RetrieveDefaultDefinition(resourceGroupName, resourceName, functionName, retrieveDefaultDefinitionParameters)); Assert.Equal(HttpStatusCode.InternalServerError, cloudException.Response.StatusCode); Assert.Contains("not supported", cloudException.Error.Message, StringComparison.InvariantCulture); // Add the function Function javaScriptFunction = new Function(functionName) { Properties = new ScalarFunctionProperties() { Properties = new ScalarFunctionConfiguration() { Inputs = new List <FunctionInput>() { new FunctionInput() { DataType = "Any" } }, Output = new FunctionOutput() { DataType = "Any" }, Binding = new JavaScriptFunctionBinding() { Properties = new JavaScriptFunctionBindingProperties() { Script = javaScriptFunctionCode } } } } }; FunctionCreateOrUpdateParameters functionCreateOrUpdateParameters = new FunctionCreateOrUpdateParameters { Function = javaScriptFunction }; FunctionCreateOrUpdateResponse functionCreateOrUpdateResponse = client.Functions.CreateOrUpdate(resourceGroupName, resourceName, functionCreateOrUpdateParameters); Assert.Equal(HttpStatusCode.OK, functionCreateOrUpdateResponse.StatusCode); Assert.Equal(functionName, functionCreateOrUpdateResponse.Function.Name); var scalarFunctionProperties = (ScalarFunctionProperties)functionCreateOrUpdateResponse.Function.Properties; var javaScriptFunctionBinding = (JavaScriptFunctionBinding)scalarFunctionProperties.Properties.Binding; Assert.Equal(javaScriptFunctionCode, javaScriptFunctionBinding.Properties.Script); Assert.NotNull(functionCreateOrUpdateResponse.Function.Properties.Etag); // Get the function FunctionGetResponse functionGetResponse = client.Functions.Get(resourceGroupName, resourceName, functionName); Assert.Equal(HttpStatusCode.OK, functionGetResponse.StatusCode); Assert.Equal(functionName, functionGetResponse.Function.Name); scalarFunctionProperties = (ScalarFunctionProperties)functionGetResponse.Function.Properties; javaScriptFunctionBinding = (JavaScriptFunctionBinding)scalarFunctionProperties.Properties.Binding; Assert.Equal(javaScriptFunctionCode, javaScriptFunctionBinding.Properties.Script); Assert.Equal(functionCreateOrUpdateResponse.Function.Properties.Etag, functionGetResponse.Function.Properties.Etag); // List functions FunctionListResponse functionListResponse = client.Functions.ListFunctionsInJob(resourceGroupName, resourceName); Assert.Equal(HttpStatusCode.OK, functionListResponse.StatusCode); Assert.Equal(1, functionListResponse.Value.Count); // Check that there is 1 function in the job jobGetParameters = new JobGetParameters("functions"); jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(1, jobGetResponse.Job.Properties.Functions.Count); // Test function connectivity ResourceTestConnectionResponse response = client.Functions.TestConnection(resourceGroupName, resourceName, functionName); Assert.Equal(OperationStatus.Failed, response.Status); Assert.Equal(ResourceTestStatus.TestFailed, response.ResourceTestStatus); Assert.NotNull(response.Error); Assert.Contains("not supported", response.Error.Message, StringComparison.InvariantCulture); // Update the function string newJavaScriptFunctionCode = @"function (x, y) { return x * y; }"; ScalarFunctionProperties scalarFunctionPropertiesForPatch = new ScalarFunctionProperties() { Properties = new ScalarFunctionConfiguration() { Binding = new JavaScriptFunctionBinding() { Properties = new JavaScriptFunctionBindingProperties() { Script = newJavaScriptFunctionCode } } } }; FunctionPatchParameters functionPatchParameters = new FunctionPatchParameters(scalarFunctionPropertiesForPatch); FunctionPatchResponse functionPatchResponse = client.Functions.Patch(resourceGroupName, resourceName, functionName, functionPatchParameters); Assert.Equal(HttpStatusCode.OK, functionPatchResponse.StatusCode); scalarFunctionProperties = (ScalarFunctionProperties)functionPatchResponse.Properties; javaScriptFunctionBinding = (JavaScriptFunctionBinding)scalarFunctionProperties.Properties.Binding; Assert.Equal(newJavaScriptFunctionCode, javaScriptFunctionBinding.Properties.Script); Assert.NotEqual(javaScriptFunctionCode, javaScriptFunctionBinding.Properties.Script); Assert.NotNull(functionPatchResponse.Properties.Etag); Assert.NotEqual(functionCreateOrUpdateResponse.Function.Properties.Etag, functionPatchResponse.Properties.Etag); // Delete the functions AzureOperationResponse deleteFunctionOperationResponse = client.Functions.Delete(resourceGroupName, resourceName, functionName); Assert.Equal(HttpStatusCode.OK, deleteFunctionOperationResponse.StatusCode); // Check that there are 0 functions in the job jobGetParameters = new JobGetParameters("functions"); jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters); Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode); Assert.Equal(0, jobGetResponse.Job.Properties.Functions.Count); } finally { client.StreamingJobs.Delete(resourceGroupName, resourceName); resourceClient.ResourceGroups.Delete(resourceGroupName); } } }
private HttpAuthentication SetClientCertAuthentication(PSCreateJobParams jobRequest, JobCreateOrUpdateParameters jobUpdateParams) { if (jobRequest.ClientCertPfx != null && jobRequest.ClientCertPassword != null) { return(new ClientCertAuthentication { Type = HttpAuthenticationType.ClientCertificate, Password = jobRequest.ClientCertPassword, Pfx = jobRequest.ClientCertPfx }); } else { throw new InvalidOperationException(Resources.SchedulerInvalidClientCertAuthRequest); } }
/// <summary> /// If a scheduler job already exists, this will merge the existing job config values with the request /// </summary> /// <param name="job">Existing Scheduler job</param> /// <param name="jobRequest">Request values</param> /// <param name="type">Http or Storage</param> /// <returns>JobCreateOrUpdateParameters object to use when updating Scheduler job</returns> private JobCreateOrUpdateParameters PopulateExistingJobParams(Job job, PSCreateJobParams jobRequest, JobActionType type) { JobCreateOrUpdateParameters jobUpdateParams = new JobCreateOrUpdateParameters(); if (type.Equals(JobActionType.StorageQueue)) { if (jobRequest.IsStorageActionSet()) { jobUpdateParams.Action = new JobAction(); jobUpdateParams.Action.QueueMessage = new JobQueueMessage(); if (job.Action != null) { jobUpdateParams.Action.Type = job.Action.Type; if (job.Action.QueueMessage != null) { jobUpdateParams.Action.QueueMessage.Message = string.IsNullOrEmpty(jobRequest.StorageQueueMessage) ? job.Action.QueueMessage.Message : jobRequest.StorageQueueMessage; jobUpdateParams.Action.QueueMessage.QueueName = jobRequest.QueueName ?? job.Action.QueueMessage.QueueName; jobUpdateParams.Action.QueueMessage.SasToken = jobRequest.SasToken ?? job.Action.QueueMessage.SasToken; jobUpdateParams.Action.QueueMessage.StorageAccountName = job.Action.QueueMessage.StorageAccountName; } else if (job.Action.QueueMessage == null) { jobUpdateParams.Action.QueueMessage.Message = string.IsNullOrEmpty(jobRequest.StorageQueueMessage) ? string.Empty : jobRequest.StorageQueueMessage; jobUpdateParams.Action.QueueMessage.QueueName = jobRequest.QueueName; jobUpdateParams.Action.QueueMessage.SasToken = jobRequest.SasToken; jobUpdateParams.Action.QueueMessage.StorageAccountName = jobRequest.StorageAccount; } } else { jobUpdateParams.Action.QueueMessage.Message = string.IsNullOrEmpty(jobRequest.StorageQueueMessage) ? string.Empty : jobRequest.StorageQueueMessage; jobUpdateParams.Action.QueueMessage.QueueName = jobRequest.QueueName; jobUpdateParams.Action.QueueMessage.SasToken = jobRequest.SasToken; jobUpdateParams.Action.QueueMessage.StorageAccountName = jobRequest.StorageAccount; } } else { jobUpdateParams.Action = job.Action; } } else //If it is a HTTP job action type { if (jobRequest.IsActionSet()) { jobUpdateParams.Action = new JobAction(); jobUpdateParams.Action.Request = new JobHttpRequest(); if (job.Action != null) { jobUpdateParams.Action.Type = job.Action.Type; if (job.Action.Request != null) { jobUpdateParams.Action.Request.Uri = jobRequest.Uri ?? job.Action.Request.Uri; jobUpdateParams.Action.Request.Method = jobRequest.Method ?? job.Action.Request.Method; jobUpdateParams.Action.Request.Headers = jobRequest.Headers == null ? job.Action.Request.Headers : jobRequest.Headers.ToDictionary(); jobUpdateParams.Action.Request.Body = jobRequest.Body ?? job.Action.Request.Body; } else if (job.Action.Request == null) { jobUpdateParams.Action.Request.Uri = jobRequest.Uri; jobUpdateParams.Action.Request.Method = jobRequest.Method; jobUpdateParams.Action.Request.Headers = jobRequest.Headers.ToDictionary(); jobUpdateParams.Action.Request.Body = jobRequest.Body; } } else { jobUpdateParams.Action.Request.Uri = jobRequest.Uri; jobUpdateParams.Action.Request.Method = jobRequest.Method; jobUpdateParams.Action.Request.Headers = jobRequest.Headers.ToDictionary(); jobUpdateParams.Action.Request.Body = jobRequest.Body; } } else { jobUpdateParams.Action = job.Action; } } if (jobRequest.IsErrorActionSet()) { jobUpdateParams.Action.ErrorAction = new JobErrorAction(); jobUpdateParams.Action.ErrorAction.Request = new JobHttpRequest(); jobUpdateParams.Action.ErrorAction.QueueMessage = new JobQueueMessage(); if (job.Action.ErrorAction != null) { if (job.Action.ErrorAction.Request != null) { jobUpdateParams.Action.ErrorAction.Request.Uri = jobRequest.ErrorActionUri ?? job.Action.ErrorAction.Request.Uri; jobUpdateParams.Action.ErrorAction.Request.Method = jobRequest.ErrorActionMethod ?? job.Action.ErrorAction.Request.Method; jobUpdateParams.Action.ErrorAction.Request.Headers = jobRequest.ErrorActionHeaders == null ? job.Action.ErrorAction.Request.Headers : jobRequest.Headers.ToDictionary(); jobUpdateParams.Action.ErrorAction.Request.Body = jobRequest.ErrorActionBody ?? job.Action.ErrorAction.Request.Body; } else if (job.Action.ErrorAction.Request == null) { jobUpdateParams.Action.ErrorAction.Request.Uri = jobRequest.ErrorActionUri; jobUpdateParams.Action.ErrorAction.Request.Method = jobRequest.ErrorActionMethod; jobUpdateParams.Action.ErrorAction.Request.Headers = jobRequest.ErrorActionHeaders.ToDictionary(); jobUpdateParams.Action.ErrorAction.Request.Body = jobRequest.ErrorActionBody; } if (job.Action.ErrorAction.QueueMessage != null) { jobUpdateParams.Action.ErrorAction.QueueMessage.Message = jobRequest.ErrorActionQueueBody ?? job.Action.ErrorAction.QueueMessage.Message; jobUpdateParams.Action.ErrorAction.QueueMessage.QueueName = jobRequest.ErrorActionQueueName ?? job.Action.ErrorAction.QueueMessage.QueueName; jobUpdateParams.Action.ErrorAction.QueueMessage.SasToken = jobRequest.ErrorActionSasToken ?? job.Action.ErrorAction.QueueMessage.SasToken; jobUpdateParams.Action.ErrorAction.QueueMessage.StorageAccountName = jobRequest.ErrorActionStorageAccount ?? job.Action.ErrorAction.QueueMessage.StorageAccountName; } else if (job.Action.ErrorAction.QueueMessage == null) { jobUpdateParams.Action.ErrorAction.QueueMessage.Message = jobRequest.ErrorActionQueueBody; jobUpdateParams.Action.ErrorAction.QueueMessage.QueueName = jobRequest.ErrorActionQueueName; jobUpdateParams.Action.ErrorAction.QueueMessage.SasToken = jobRequest.ErrorActionSasToken; jobUpdateParams.Action.ErrorAction.QueueMessage.StorageAccountName = jobRequest.ErrorActionStorageAccount; } } else if (job.Action.ErrorAction == null) { jobUpdateParams.Action.ErrorAction.Request.Uri = jobRequest.ErrorActionUri; jobUpdateParams.Action.ErrorAction.Request.Method = jobRequest.ErrorActionMethod; jobUpdateParams.Action.ErrorAction.Request.Headers = jobRequest.ErrorActionHeaders.ToDictionary(); jobUpdateParams.Action.ErrorAction.Request.Body = jobRequest.ErrorActionBody; jobUpdateParams.Action.ErrorAction.QueueMessage.Message = jobRequest.ErrorActionQueueBody; jobUpdateParams.Action.ErrorAction.QueueMessage.QueueName = jobRequest.ErrorActionQueueName; jobUpdateParams.Action.ErrorAction.QueueMessage.SasToken = jobRequest.ErrorActionSasToken; jobUpdateParams.Action.ErrorAction.QueueMessage.StorageAccountName = jobRequest.ErrorActionStorageAccount; } } else { jobUpdateParams.Action.ErrorAction = job.Action.ErrorAction; } if (jobRequest.IsRecurrenceSet()) { jobUpdateParams.Recurrence = new JobRecurrence(); if (job.Recurrence != null) { jobUpdateParams.Recurrence.Count = jobRequest.ExecutionCount ?? job.Recurrence.Count; jobUpdateParams.Recurrence.EndTime = jobRequest.EndTime ?? job.Recurrence.EndTime; jobUpdateParams.Recurrence.Frequency = string.IsNullOrEmpty(jobRequest.Frequency) ? job.Recurrence.Frequency : (JobRecurrenceFrequency)Enum.Parse(typeof(JobRecurrenceFrequency), jobRequest.Frequency); jobUpdateParams.Recurrence.Interval = jobRequest.Interval ?? job.Recurrence.Interval; jobUpdateParams.Recurrence.Schedule = SetRecurrenceSchedule(job.Recurrence.Schedule); } else if (job.Recurrence == null) { jobUpdateParams.Recurrence.Count = jobRequest.ExecutionCount; jobUpdateParams.Recurrence.EndTime = jobRequest.EndTime; jobUpdateParams.Recurrence.Frequency = string.IsNullOrEmpty(jobRequest.Frequency) ? default(JobRecurrenceFrequency) : (JobRecurrenceFrequency)Enum.Parse(typeof(JobRecurrenceFrequency), jobRequest.Frequency); jobUpdateParams.Recurrence.Interval = jobRequest.Interval; jobUpdateParams.Recurrence.Schedule = null; } } else { jobUpdateParams.Recurrence = job.Recurrence; if (jobUpdateParams.Recurrence != null) { jobUpdateParams.Recurrence.Schedule = SetRecurrenceSchedule(job.Recurrence.Schedule); } } jobUpdateParams.Action.RetryPolicy = job.Action.RetryPolicy; jobUpdateParams.StartTime = jobRequest.StartTime ?? job.StartTime; return(jobUpdateParams); }
private JobCreateOrUpdateParameters GetParams(Task model) { JobCreateOrUpdateParameters p = new JobCreateOrUpdateParameters(); string url; if (Request == null) { url = model.Url; } else { string serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"]; string host = serverName == "localhost" ? defaultHost : serverName; url = string.Format("http://{0}{1}", host, model.Url); } if (Request == null) { p.Action = new JobAction() { Type = JobActionType.Https, Request = new JobHttpRequest() { Method = "GET", Uri = new Uri(url), } }; p.StartTime = model.Start_Date; } else { p.Action = new JobAction() { Type = JobActionType.Http, Request = new JobHttpRequest() { Body = "", Headers = new Dictionary <string, string>() { { "Content-Type", "application/x-www-form-urlencoded" } }, Method = "GET", Uri = new Uri(url), Authentication = new BasicAuthentication() { Username = systemAccountName, Password = systemAccountPass, Type = HttpAuthenticationType.Basic } } }; p.StartTime = model.Start_Date == null ? DateTime.UtcNow : model.Start_Date + model.Start_Time; } JobRecurrence r = new JobRecurrence(); switch (model.Type) { case ScheduleType.DAILY: r.Frequency = JobRecurrenceFrequency.Day; r.Interval = model.Daily_Repeat; break; case ScheduleType.WEEKLY: r.Frequency = JobRecurrenceFrequency.Week; r.Interval = model.Weekly_Repeat; r.Schedule = new JobRecurrenceSchedule() { Days = GetWeeklyDays(model.Weekly_Days), }; break; case ScheduleType.MONTHLY: r.Frequency = JobRecurrenceFrequency.Month; r.Schedule = CreateMonthlySchedule(model); break; case ScheduleType.ONCE: r.Count = 1; break; case ScheduleType.ONIDLE: throw new NotImplementedException("Pro Azure scheduler nelze plánovat OnIdle úlohy"); } if (model.Repeat == true) { JobRecurrenceSchedule s = r.Schedule ?? new JobRecurrenceSchedule(); int minutes = (int)model.Repeat_Minute; int startHour = model.Start_Time == null ? DateTime.Now.Hour : model.Start_Time.Hours; List <int> minuteSchedule = new List <int>(); for (int i = 1; i <= 60; i++) { if ((i % minutes) == 0) { minuteSchedule.Add(i); } } List <int> hourSchedule = new List <int>(); for (int i = 0; i < model.Repeat_Duration; i++) { int hour = startHour + i; hourSchedule.Add(hour > 23 ? hour - 24 : hour); } s.Minutes = minuteSchedule; s.Hours = hourSchedule; } if (model.End_Date != null) { r.EndTime = model.End_Date + model.End_Time; } p.Recurrence = r; return(p); }
/// <summary> /// Creates a new Http Scheduler job /// </summary> /// <param name="jobRequest">Request values</param> /// <param name="status">Status of create action</param> /// <returns>Created Http Scheduler job</returns> public PSJobDetail CreateHttpJob(PSCreateJobParams jobRequest, out string status) { if (!this.AvailableRegions.Contains(jobRequest.Region, StringComparer.OrdinalIgnoreCase)) { throw new Exception(Resources.SchedulerInvalidLocation); } SchedulerClient schedulerClient = this.currentSubscription.CreateClient <SchedulerClient>(false, jobRequest.Region.ToCloudServiceName(), jobRequest.JobCollectionName, csmClient.Credentials, schedulerManagementClient.BaseUri); JobCreateOrUpdateParameters jobCreateParams = new JobCreateOrUpdateParameters { Action = new JobAction { Request = new JobHttpRequest { Uri = jobRequest.Uri, Method = jobRequest.Method }, } }; if (jobRequest.Headers != null) { jobCreateParams.Action.Request.Headers = jobRequest.Headers.ToDictionary(); } if (jobRequest.Method.Equals("PUT", StringComparison.OrdinalIgnoreCase) || jobRequest.Method.Equals("POST", StringComparison.OrdinalIgnoreCase)) { jobCreateParams.Action.Request.Body = jobRequest.Body; } //Populate job error action jobCreateParams.Action.ErrorAction = PopulateErrorAction(jobRequest); jobCreateParams.StartTime = jobRequest.StartTime ?? default(DateTime?); if (jobRequest.Interval != null || jobRequest.ExecutionCount != null || !string.IsNullOrEmpty(jobRequest.Frequency) || jobRequest.EndTime != null) { jobCreateParams.Recurrence = new JobRecurrence(); jobCreateParams.Recurrence.Count = jobRequest.ExecutionCount ?? default(int?); if (!string.IsNullOrEmpty(jobRequest.Frequency)) { jobCreateParams.Recurrence.Frequency = (JobRecurrenceFrequency)Enum.Parse(typeof(JobRecurrenceFrequency), jobRequest.Frequency); } jobCreateParams.Recurrence.Interval = jobRequest.Interval ?? default(int?); jobCreateParams.Recurrence.EndTime = jobRequest.EndTime ?? default(DateTime?); } JobCreateOrUpdateResponse jobCreateResponse = schedulerClient.Jobs.CreateOrUpdate(jobRequest.JobName, jobCreateParams); if (!string.IsNullOrEmpty(jobRequest.JobState) && jobRequest.JobState.Equals("DISABLED", StringComparison.OrdinalIgnoreCase)) { schedulerClient.Jobs.UpdateState(jobRequest.JobName, new JobUpdateStateParameters { State = JobState.Disabled }); } status = jobCreateResponse.StatusCode.ToString().Equals("OK") ? "Job has been updated" : jobCreateResponse.StatusCode.ToString(); return(GetJobDetail(jobRequest.JobCollectionName, jobRequest.JobName, jobRequest.Region.ToCloudServiceName())); }
private HttpAuthentication SetHttpAuthentication(PSCreateJobParams jobRequest, JobCreateOrUpdateParameters jobUpdateParams) { HttpAuthentication httpAuthentication = null; if (!string.IsNullOrEmpty(jobRequest.HttpAuthType)) { switch (jobRequest.HttpAuthType.ToLower()) { case "clientcertificate": if (jobRequest.ClientCertPfx != null && jobRequest.ClientCertPassword != null) { httpAuthentication = new ClientCertAuthentication { Type = HttpAuthenticationType.ClientCertificate, Password = jobRequest.ClientCertPassword, Pfx = jobRequest.ClientCertPfx }; } else { throw new InvalidOperationException(Resources.SchedulerInvalidClientCertAuthRequest); } break; case "activedirectoryoauth": if (jobRequest.Tenant != null && jobRequest.Audience != null && jobRequest.ClientId != null && jobRequest.Secret != null) { httpAuthentication = new AADOAuthAuthentication { Type = HttpAuthenticationType.ActiveDirectoryOAuth, Tenant = jobRequest.Tenant, Audience = jobRequest.Audience, ClientId = jobRequest.ClientId, Secret = jobRequest.Secret }; } else { throw new InvalidOperationException(Resources.SchedulerInvalidAADOAuthRequest); } break; case "basic": if (jobRequest.Username != null && jobRequest.Password != null) { httpAuthentication = new BasicAuthentication { Type = HttpAuthenticationType.Basic, Username = jobRequest.Username, Password = jobRequest.Password }; } else { throw new InvalidOperationException(Resources.SchedulerInvalidBasicAuthRequest); } break; case "none": if (!string.IsNullOrEmpty(jobRequest.ClientCertPfx) || !string.IsNullOrEmpty(jobRequest.ClientCertPassword) || !string.IsNullOrEmpty(jobRequest.Tenant) || !string.IsNullOrEmpty(jobRequest.Secret) || !string.IsNullOrEmpty(jobRequest.ClientId) || !string.IsNullOrEmpty(jobRequest.Audience) || !string.IsNullOrEmpty(jobRequest.Username) || !string.IsNullOrEmpty(jobRequest.Password)) { throw new InvalidOperationException(Resources.SchedulerInvalidNoneAuthRequest); } break; } } return(httpAuthentication); }
/// <summary> /// Creates a new Job with a user-provided job id, or updates an /// existing job, replacing its definition with that specified. /// </summary> /// <param name='operations'> /// Reference to the Microsoft.WindowsAzure.Scheduler.IJobOperations. /// </param> /// <param name='jobId'> /// Required. Id of the job to create or update. /// </param> /// <param name='parameters'> /// Required. Parameters specifying the job definition for a /// CreateOrUpdate Job operation. /// </param> /// <returns> /// The CreateOrUpdate Job operation response. /// </returns> public static JobCreateOrUpdateResponse CreateOrUpdate(this IJobOperations operations, string jobId, JobCreateOrUpdateParameters parameters) { return(Task.Factory.StartNew((object s) => { return ((IJobOperations)s).CreateOrUpdateAsync(jobId, parameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
private static void CreateSchedulerJob(AzureDetails details) { // retrieve the windows azure managment certificate from current user var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, "<THUMBPRINT of CERTIFICATE>", false)[0]; store.Close(); // create management credencials and cloud service management client var credentials = new CertificateCloudCredentials("<Your subscription ID>", certificate); var cloudServiceMgmCli = new CloudServiceManagementClient(credentials); // create job collection var schedulerMgmCli = new SchedulerManagementClient(credentials); var jobCollectionIntrinsicSettings = new JobCollectionIntrinsicSettings() { Plan = JobCollectionPlan.Free, Quota = new JobCollectionQuota() { MaxJobCount = 5, MaxJobOccurrence = 1, MaxRecurrence = new JobCollectionMaxRecurrence() { Frequency = JobCollectionRecurrenceFrequency.Hour, Interval = 24 } } }; var jobCollectionCreateParameters = new JobCollectionCreateParameters() { IntrinsicSettings = jobCollectionIntrinsicSettings, Label = "RunBackupJob" }; var jobCollectionCreateResponse = schedulerMgmCli.JobCollections.Create(details.ResourceGroup, "RunBackupJob", jobCollectionCreateParameters); //generate sas token var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureWebJobsStorage")); //Set the expiry time and permissions for the container. //In this case no start time is specified, so the shared access signature becomes valid immediately. var sasConstraints = new SharedAccessAccountPolicy { SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddYears(1), Permissions = SharedAccessAccountPermissions.Add | SharedAccessAccountPermissions.Create | SharedAccessAccountPermissions.Delete | SharedAccessAccountPermissions.ProcessMessages | SharedAccessAccountPermissions.Read | SharedAccessAccountPermissions.Write }; var token = storageAccount.GetSharedAccessSignature(sasConstraints); // create job var schedulerClient = new SchedulerClient(details.ResourceGroup, "RunBackupJob", credentials); var jobAction = new JobAction() { Type = JobActionType.StorageQueue, QueueMessage = new JobQueueMessage { Message = details.PairName, QueueName = "get-containers", SasToken = sasConstraints.ToString(), StorageAccountName = "queuefunction" } }; var jobRecurrence = new JobRecurrence() { Frequency = JobRecurrenceFrequency.Hour, Interval = 24 }; var jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters() { Action = jobAction, Recurrence = jobRecurrence }; var jobCreateResponse = schedulerClient.Jobs.CreateOrUpdate(details.ResourceGroup, jobCreateOrUpdateParameters); }