protected override void ProcessRecordInternal() { try { // Convert Tag parameter from Hashtable to Dictionary<string, string> Dictionary <string, string> tags = TagsConversionHelper.CreateTagDictionary(Tag, true); var alertingAction = new AlertingAction(severity: Action.Severity, aznsAction: Action.AznsAction, trigger: Action.Trigger, throttlingInMin: Action.ThrottlingInMin); var parameters = new LogSearchRuleResource(location: Location, source: Source, schedule: Schedule, action: alertingAction, tags: tags, description: Description, enabled: Enabled? "true" : "false"); parameters.Validate(); if (this.ShouldProcess(this.Name, string.Format("Creating Log Alert Rule '{0}' in resource group {0}", this.Name, this.ResourceGroupName))) { var result = this.MonitorManagementClient.ScheduledQueryRules .CreateOrUpdateWithHttpMessagesAsync(resourceGroupName: ResourceGroupName, ruleName: Name, parameters: parameters).Result; WriteObject(new PSScheduledQueryRuleResource(result.Body)); } } catch (Exception ex) { throw new Exception("Error occured while creating Log Alert rule", ex); } }
public async Task <Response <LogSearchRuleResource> > CreateOrUpdateAsync(string resourceGroupName, string ruleName, LogSearchRuleResource parameters, CancellationToken cancellationToken = default) { if (resourceGroupName == null) { throw new ArgumentNullException(nameof(resourceGroupName)); } if (ruleName == null) { throw new ArgumentNullException(nameof(ruleName)); } if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } using var message = CreateCreateOrUpdateRequest(resourceGroupName, ruleName, parameters); await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); switch (message.Response.Status) { case 200: case 201: { LogSearchRuleResource value = default; using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false); value = LogSearchRuleResource.DeserializeLogSearchRuleResource(document.RootElement); return(Response.FromValue(value, message.Response)); }
public UpdateScheduledQueryRuleTests(Xunit.Abstractions.ITestOutputHelper output) { ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagement.Common.Models.XunitTracingInterceptor(output)); TestExecutionHelpers.SetUpSessionAndProfile(); sqrOperationsMock = new Mock <IScheduledQueryRulesOperations>(); monitorManagementClientMock = new Mock <MonitorManagementClient>() { CallBase = true }; commandRuntimeMock = new Mock <ICommandRuntime>(); //testing update of "enabled" field cmdlet = new UpdateScheduledQueryRuleCommand { CommandRuntime = commandRuntimeMock.Object, MonitorManagementClient = monitorManagementClientMock.Object, Enabled = true }; response = new AzureOperationResponse <LogSearchRuleResource>() { Body = new LogSearchRuleResource() }; sqrOperationsMock.Setup(f => f.GetWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult <Microsoft.Rest.Azure.AzureOperationResponse <LogSearchRuleResource> >(response)) .Callback((string resourceGrp, string name, Dictionary <string, List <string> > customHeaders, CancellationToken cancellationToken) => { this.resourceGroup = resourceGrp; this.ruleName = name; this.updatePrms = response.Body; }); sqrOperationsMock.Setup(f => f.UpdateWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <LogSearchRuleResourcePatch>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult <Microsoft.Rest.Azure.AzureOperationResponse <LogSearchRuleResource> >(response)) .Callback((string resourceGrp, string name, LogSearchRuleResourcePatch patchPrms, Dictionary <string, List <string> > headers, CancellationToken t) => { this.resourceGroup = resourceGrp; this.ruleName = name; this.patchPrms = patchPrms; this.updatePrms = response.Body; }); monitorManagementClientMock.SetupGet(f => f.ScheduledQueryRules).Returns(this.sqrOperationsMock.Object); // Setup Confirmation commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>())).Returns(true); commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true); commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(true); commandRuntimeMock.Setup(f => f.ShouldContinue(It.IsAny <string>(), It.IsAny <string>())).Returns(true); }
private void AreEqual(LogSearchRuleResource exp, LogSearchRuleResource act) { Assert.AreEqual(exp.Description, act.Description); Assert.AreEqual(exp.Enabled.Value, act.Enabled.Value); Assert.AreEqual(exp.Id, act.Id); Assert.AreEqual(exp.LastUpdatedTime, act.LastUpdatedTime); Assert.AreEqual(exp.Location, act.Location); Assert.AreEqual(exp.Name, act.Name); Assert.AreEqual(exp.ProvisioningState.Value, act.ProvisioningState.Value); Assert.AreEqual(exp.Type, act.Type); Assert.AreEqual(exp.Action.OdataType, act.Action.OdataType); AreEqual(exp.Schedule, act.Schedule); AreEqual(exp.Source, act.Source); AreEqual(exp.Tags, act.Tags); }
public virtual Response <LogSearchRuleResource> CreateOrUpdate(string resourceGroupName, string ruleName, LogSearchRuleResource parameters, CancellationToken cancellationToken = default) { using var scope = _clientDiagnostics.CreateScope("ScheduledQueryRulesOperations.CreateOrUpdate"); scope.Start(); try { return(RestClient.CreateOrUpdate(resourceGroupName, ruleName, parameters, cancellationToken)); } catch (Exception e) { scope.Failed(e); throw; } }
public SetScheduledQueryRuleTests(Xunit.Abstractions.ITestOutputHelper output) { ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagement.Common.Models.XunitTracingInterceptor(output)); TestExecutionHelpers.SetUpSessionAndProfile(); sqrOperationsMock = new Mock <IScheduledQueryRulesOperations>(); monitorManagementClientMock = new Mock <MonitorManagementClient>() { CallBase = true }; commandRuntimeMock = new Mock <ICommandRuntime>(); ScheduledQueryRuleAznsAction aznsAction = new ScheduledQueryRuleAznsAction(new AzNsActionGroup()); ScheduledQueryRuleTriggerCondition triggerCondition = new ScheduledQueryRuleTriggerCondition(new TriggerCondition("GreaterThan", 15)); ScheduledQueryRuleAlertingAction alertingAction = new ScheduledQueryRuleAlertingAction(new AlertingAction("2", aznsAction, triggerCondition)); ScheduledQueryRuleSchedule schedule = new ScheduledQueryRuleSchedule(new Schedule(5, 5)); ScheduledQueryRuleSource source = new ScheduledQueryRuleSource(new Source("union *", "dataSourceId", new string[] { "authResource1", "authResource2" }, "ResultCount")); //testing update of "description" field cmdlet = new SetScheduledQueryRuleCommand { CommandRuntime = commandRuntimeMock.Object, MonitorManagementClient = monitorManagementClientMock.Object, Source = new PSScheduledQueryRuleSource(source), Schedule = new PSScheduledQueryRuleSchedule(schedule), Action = new PSScheduledQueryRuleAlertingAction(alertingAction), Description = "A Log Search Alert description" }; response = new AzureOperationResponse <LogSearchRuleResource>() { Body = new LogSearchRuleResource() }; sqrOperationsMock.Setup(f => f.GetWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult <Microsoft.Rest.Azure.AzureOperationResponse <LogSearchRuleResource> >(response)) .Callback((string resourceGrp, string name, Dictionary <string, List <string> > customHeaders, CancellationToken cancellationToken) => { this.resourceGroup = resourceGrp; this.ruleName = name; this.updatePrms = response.Body; }); sqrOperationsMock.Setup(f => f.CreateOrUpdateWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <LogSearchRuleResource>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult <Microsoft.Rest.Azure.AzureOperationResponse <LogSearchRuleResource> >(response)) .Callback((string resourceGrp, string name, LogSearchRuleResource updateParams, Dictionary <string, List <string> > headers, CancellationToken t) => { this.resourceGroup = resourceGrp; this.ruleName = name; this.updatePrms = updateParams; }); monitorManagementClientMock.SetupGet(f => f.ScheduledQueryRules).Returns(this.sqrOperationsMock.Object); // Setup Confirmation commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>())).Returns(true); commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true); commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(true); commandRuntimeMock.Setup(f => f.ShouldContinue(It.IsAny <string>(), It.IsAny <string>())).Returns(true); }
/// <summary> /// Creates or updates an log search rule. /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='resourceGroupName'> /// The name of the resource group. The name is case insensitive. /// </param> /// <param name='ruleName'> /// The name of the rule. /// </param> /// <param name='parameters'> /// The parameters of the rule to create or update. /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <LogSearchRuleResource> CreateOrUpdateAsync(this IScheduledQueryRulesOperations operations, string resourceGroupName, string ruleName, LogSearchRuleResource parameters, CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, ruleName, parameters, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
/// <summary> /// Creates or updates an log search rule. /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='resourceGroupName'> /// The name of the resource group. The name is case insensitive. /// </param> /// <param name='ruleName'> /// The name of the rule. /// </param> /// <param name='parameters'> /// The parameters of the rule to create or update. /// </param> public static LogSearchRuleResource CreateOrUpdate(this IScheduledQueryRulesOperations operations, string resourceGroupName, string ruleName, LogSearchRuleResource parameters) { return(operations.CreateOrUpdateAsync(resourceGroupName, ruleName, parameters).GetAwaiter().GetResult()); }
internal HttpMessage CreateCreateOrUpdateRequest(string resourceGroupName, string ruleName, LogSearchRuleResource parameters) { var message = _pipeline.CreateMessage(); var request = message.Request; request.Method = RequestMethod.Put; var uri = new RawRequestUriBuilder(); uri.Reset(endpoint); uri.AppendPath("/subscriptions/", false); uri.AppendPath(subscriptionId, true); uri.AppendPath("/resourcegroups/", false); uri.AppendPath(resourceGroupName, true); uri.AppendPath("/providers/microsoft.insights/scheduledQueryRules/", false); uri.AppendPath(ruleName, true); uri.AppendQuery("api-version", "2018-04-16", true); request.Uri = uri; request.Headers.Add("Accept", "application/json"); request.Headers.Add("Content-Type", "application/json"); var content = new Utf8JsonRequestContent(); content.JsonWriter.WriteObjectValue(parameters); request.Content = content; return(message); }
public GetScheduledQueryRulesTests(Xunit.Abstractions.ITestOutputHelper output) { ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagement.Common.Models.XunitTracingInterceptor(output)); TestExecutionHelpers.SetUpSessionAndProfile(); sqrOperationsMock = new Mock <IScheduledQueryRulesOperations>(); monitorManagementClientMock = new Mock <MonitorManagementClient>() { CallBase = true }; commandRuntimeMock = new Mock <ICommandRuntime>(); cmdlet = new GetScheduledQueryRuleCommand() { CommandRuntime = commandRuntimeMock.Object, MonitorManagementClient = monitorManagementClientMock.Object }; Source source = new Source(); Schedule schedule = new Schedule(); Action action = new Action(); //ScheduledQueryRuleResource responseObject = new ScheduledQueryRuleResource(new LogSearchRuleResource(name: "alert2", location: "westus", source: source, schedule: schedule, action: action)); LogSearchRuleResource responseObject = new LogSearchRuleResource(name: "alert2", location: "westus", source: source, schedule: schedule, action: action); responseSingle = new AzureOperationResponse <LogSearchRuleResource>() { Body = responseObject }; responseList = new AzureOperationResponse <IEnumerable <LogSearchRuleResource> >() { Body = new List <LogSearchRuleResource> { responseObject } }; sqrOperationsMock.Setup(f => f.GetWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult <AzureOperationResponse <LogSearchRuleResource> >(responseSingle)) .Callback((string resourceGrp, string name, Dictionary <string, List <string> > customHeaders, CancellationToken cancellationToken) => { this.resourceGroup = resourceGrp; this.ruleName = name; this.retrieved = new List <LogSearchRuleResource> { responseSingle.Body }; }); sqrOperationsMock.Setup(f => f.ListByResourceGroupWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <ODataQuery <LogSearchRuleResource> >(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult <AzureOperationResponse <IEnumerable <LogSearchRuleResource> > >(responseList)) .Callback((string resourceGrp, ODataQuery <LogSearchRuleResource> odataQuery, Dictionary <string, List <string> > customHeaders, CancellationToken cancellationToken) => { this.resourceGroup = resourceGrp; this.retrieved = responseList.Body; }); sqrOperationsMock.Setup(f => f.ListBySubscriptionWithHttpMessagesAsync(It.IsAny <ODataQuery <LogSearchRuleResource> >(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult <AzureOperationResponse <IEnumerable <LogSearchRuleResource> > >(responseList)) .Callback(() => { this.retrieved = responseList.Body; }); monitorManagementClientMock.SetupGet(f => f.ScheduledQueryRules).Returns(this.sqrOperationsMock.Object); }