public void RemoveScheduledQueryRuleCommandParametersProcessing() { cmdlet.ResourceGroupName = Utilities.ResourceGroup; cmdlet.Name = "alert1"; cmdlet.ExecuteCmdlet(); Assert.Equal(Utilities.ResourceGroup, this.resourceGroup); Assert.Equal("alert1", this.ruleName); cmdlet.Name = null; cmdlet.ResourceGroupName = null; Source source = new Source(); Schedule schedule = new Schedule(); Action action = new Action(); var resource = new ScheduledQueryRuleResource(new LogSearchRuleResource(name: "alert2", location: "westus", source: source, schedule: schedule, action: action, id: "/subscriptions/00000000-0000-0000-0000-0000000000000/resourceGroups/Default-Web-EastUS/providers/Microsoft.Insights/scheduledqueryrules/alert2")); cmdlet.InputObject = new OutputClasses.PSScheduledQueryRuleResource(resource); cmdlet.ExecuteCmdlet(); Assert.Equal(Utilities.ResourceGroup, this.resourceGroup); Assert.Equal("alert2", this.ruleName); cmdlet.Name = null; cmdlet.ResourceGroupName = null; cmdlet.InputObject = null; cmdlet.ResourceId = "/subscriptions/00000000-0000-0000-0000-0000000000000/resourceGroups/Default-Web-EastUS/providers/Microsoft.Insights/scheduledqueryrules/alert3"; cmdlet.ExecuteCmdlet(); Assert.Equal(Utilities.ResourceGroup, this.resourceGroup); Assert.Equal("alert3", this.ruleName); }
protected override void ProcessRecordInternal() { ResourceIdentifier resourceIdentifier = null; ScheduledQueryRuleResource requestBody = null; // ByInputObject parameter set if (this.IsParameterBound(c => c.InputObject) || this.InputObject != null) { resourceIdentifier = new ResourceIdentifier(this.InputObject.Id); this.Name = resourceIdentifier.ResourceName; this.ResourceGroupName = resourceIdentifier.ResourceGroupName; requestBody = UpdateScheduledQueryRuleResource(this.InputObject); } else { // ByRuleName and ByResourceId parameter sets if (this.IsParameterBound(c => c.ResourceId) || !string.IsNullOrWhiteSpace(this.ResourceId)) { resourceIdentifier = new ResourceIdentifier(ResourceId); this.Name = resourceIdentifier.ResourceName; this.ResourceGroupName = resourceIdentifier.ResourceGroupName; } try { // Allowing only updates with Set-* command requestBody = new ScheduledQueryRuleResource( this.MonitorManagementClient.ScheduledQueryRules.GetWithHttpMessagesAsync(this.ResourceGroupName, this.Name).Result.Body); } catch (Exception ex) { throw new Exception("Error in retrieving Log Alert Rule", ex); } requestBody = UpdateScheduledQueryRuleResource(requestBody); } try { if (ShouldProcess(this.Name, string.Format("Updating Log Alert Rule '{0}' in resource group '{1}'.", this.Name, this.ResourceGroupName))) { requestBody.Validate(); // Update the Log Alert rule var result = this.MonitorManagementClient.ScheduledQueryRules.CreateOrUpdateWithHttpMessagesAsync( this.ResourceGroupName, this.Name, requestBody).Result; WriteObject(new PSScheduledQueryRuleResource(result.Body)); } } catch (Exception ex) { throw new Exception("Error in updating Log Alert Rule", ex); } }
protected override void ProcessRecordInternal() { ResourceIdentifier resourceIdentifier = null; ScheduledQueryRuleResource resource = null; // ByInputObject parameter set if (this.IsParameterBound(c => c.InputObject) || this.InputObject != null) { resourceIdentifier = new ResourceIdentifier(InputObject.Id); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; } else if (this.IsParameterBound(c => c.ResourceId) || !string.IsNullOrWhiteSpace(this.ResourceId)) { resourceIdentifier = new ResourceIdentifier(this.ResourceId); this.ResourceGroupName = resourceIdentifier.ResourceGroupName; this.Name = resourceIdentifier.ResourceName; } try { resource = new ScheduledQueryRuleResource( this.MonitorManagementClient.ScheduledQueryRules.GetWithHttpMessagesAsync(this.ResourceGroupName, this.Name).Result.Body); } catch (Exception ex) { throw new Exception("Error in getting Log Alert Rule", ex); } // Update of only Enabled field is supported LogSearchRuleResourcePatch parameters = new LogSearchRuleResourcePatch(resource.Tags, this.Enabled ? "true" : "false"); if (ShouldProcess(this.Name, string.Format("Updating Log Alert Rule '{0}' in resource group '{1}'.", this.Name, this.ResourceGroupName))) { try { WriteObject(new PSScheduledQueryRuleResource( this.MonitorManagementClient.ScheduledQueryRules.UpdateWithHttpMessagesAsync(this.ResourceGroupName, this.Name, parameters).Result.Body)); } catch (Exception ex) { throw new Exception("Error in updating Log Alert Rule", ex); } } }
public void UpdateScheduledQueryRuleCommandParametersProcessing() { //testing update of "enabled" field cmdlet.Name = "LogSearchAlertName"; cmdlet.ResourceGroupName = Utilities.ResourceGroup; cmdlet.Enabled = false; cmdlet.ExecuteCmdlet(); Assert.Equal("LogSearchAlertName", this.ruleName); Assert.Equal(Utilities.ResourceGroup, this.resourceGroup); Assert.NotNull(this.updatePrms); Assert.NotNull(this.patchPrms); Assert.Equal("false", this.patchPrms.Enabled); Assert.Null(this.updatePrms.Id); cmdlet.Name = null; cmdlet.ResourceGroupName = null; ScheduledQueryRuleResource sqrResource = new ScheduledQueryRuleResource(); cmdlet.InputObject = new PSScheduledQueryRuleResource(sqrResource); cmdlet.Enabled = true; cmdlet.ExecuteCmdlet(); Assert.NotNull(this.patchPrms); Assert.NotNull(this.updatePrms); Assert.Equal("true", this.patchPrms.Enabled); cmdlet.InputObject = null; cmdlet.ResourceId = "/subscriptions/00000000-0000-0000-0000-0000000000000/resourceGroups/Default-Web-EastUS/providers/Microsoft.Insights/scheduledqueryrules/LogSearchAlertName"; cmdlet.Enabled = false; cmdlet.ExecuteCmdlet(); Assert.NotNull(this.patchPrms); Assert.NotNull(this.updatePrms); Assert.Equal("LogSearchAlertName", this.ruleName); Assert.Equal(Utilities.ResourceGroup, this.resourceGroup); Assert.Equal("false", this.patchPrms.Enabled); }
private ScheduledQueryRuleResource UpdateScheduledQueryRuleResource(ScheduledQueryRuleResource requestBody) { if (this.MyInvocation.BoundParameters.ContainsKey("Location") || this.Location != null) { requestBody.Location = this.Location; } if (this.MyInvocation.BoundParameters.ContainsKey("Action") || this.Action != null) { var alertingAction = new AlertingAction(severity: Action.Severity, aznsAction: Action.AznsAction, trigger: Action.Trigger, throttlingInMin: Action.ThrottlingInMin); requestBody.Action = alertingAction; } if (this.MyInvocation.BoundParameters.ContainsKey("Source") || this.Source != null) { requestBody.Source = this.Source; } if (this.MyInvocation.BoundParameters.ContainsKey("Enabled")) { requestBody.Enabled = this.Enabled? "true" : "false"; } if (this.MyInvocation.BoundParameters.ContainsKey("Description") || this.Description != null) { requestBody.Description = this.Description; } if (this.MyInvocation.BoundParameters.ContainsKey("Schedule") || this.Schedule != null) { requestBody.Schedule = this.Schedule; } if (this.MyInvocation.BoundParameters.ContainsKey("Tags") || this.Tag != null) { Dictionary <string, string> tags = TagsConversionHelper.CreateTagDictionary(Tag, true); requestBody.Tags = tags; } return(requestBody); }