public void CreateAndDeleteAlertProcessingRule() { using (MockContext context = MockContext.Start(this.GetType())) { var alertsManagementClient = GetAlertsManagementClient(context, handler); string resourceGroupName = "ActionRules-Powershell-Test"; string alertProcessingRuleName = "ScenarioTest-AlertProcessingRule"; // Create Alert Processing Rule AlertProcessingRule alertProcessingRule = new AlertProcessingRule( location: "Global", tags: new Dictionary <string, string>(), properties: new AlertProcessingRuleProperties( scopes: new List <string> { "/subscriptions/dd91de05-d791-4ceb-b6dc-988682dc7d72/resourceGroups/alertslab", "/subscriptions/dd91de05-d791-4ceb-b6dc-988682dc7d72/resourceGroups/Test-VMs" }, conditions: new List <Condition> { new Condition( field: "Severity", operatorProperty: "Equals", values: new List <string> { "Sev2" }) }, schedule: new Schedule( effectiveFrom: "2019-12-22T11:00:00", effectiveUntil: "2020-09-24T11:00:00", timeZone: "Pacific Standard Time", recurrences: new List <Recurrence> { new DailyRecurrence(startTime: "06:00:00", endTime: "14:00:00") } ), actions: new List <Action> { new RemoveAllActionGroups() }, description: "Test Supression Rule", enabled: true ) ); var createdAlertProcessingRule = alertsManagementClient.AlertProcessingRules.CreateOrUpdate(resourceGroupName: resourceGroupName, alertProcessingRuleName: alertProcessingRuleName, alertProcessingRule: alertProcessingRule); // Get Alert processing rule by name var fetchedAlertProcessingRule = alertsManagementClient.AlertProcessingRules.GetByName(resourceGroupName: resourceGroupName, alertProcessingRuleName: alertProcessingRuleName); if (!this.IsRecording) { ComparisonUtility.AreEqual(createdAlertProcessingRule, fetchedAlertProcessingRule); } // Delete created alert processing rule alertsManagementClient.AlertProcessingRules.Delete(resourceGroupName: resourceGroupName, alertProcessingRuleName: alertProcessingRuleName); } }
/// <summary> /// Initializes a new instance of PSActionRule /// </summary> /// <param name="rule"></param> public PSAlertProcessingRule(AlertProcessingRule rule) { Id = rule.Id; Name = rule.Name; Description = rule.Properties.Description; Enabled = rule.Properties.Enabled.ToString(); CreatedAt = rule.SystemData.CreatedAt; CreatedBy = rule.SystemData.CreatedBy; LastModifiedAt = rule.SystemData.LastModifiedAt; LastModifiedBy = rule.SystemData.LastModifiedBy; Scopes = JsonConvert.SerializeObject(rule.Properties.Scopes); if (rule.Tags != null) { Tags = JsonConvert.SerializeObject(rule.Tags); } if (rule.Properties.Conditions != null) { Conditions = JsonConvert.SerializeObject(rule.Properties.Conditions); } if (rule.Properties.Schedule != null) { Schedule = JsonConvert.SerializeObject(rule.Properties.Schedule); } if (rule.Properties.Actions[0] is AddActionGroups) { AlertProcessingType = "AddActionGroups"; } else { AlertProcessingType = "RemoveAllActionGroups"; } }
private PSAlertProcessingRule TransformOutput(AlertProcessingRule input) { if (input.Properties.Actions[0] is AddActionGroups) { return(new PSActionGroupAlertProcessingRule(input)); } else { return(new PSSuppressionAlertProcessingRule(input)); } }
public static void AreEqual(AlertProcessingRule exp, AlertProcessingRule act) { if (exp != null) { if (exp.Properties.Description != null && act.Properties.Description != null) { Assert.Equal(exp.Properties.Description, act.Properties.Description); } if (exp.Properties.Enabled != null && act.Properties.Enabled != null) { Assert.Equal(exp.Properties.Enabled, act.Properties.Enabled); } AreEqual(exp.Properties.Scopes, act.Properties.Scopes); AreEqual(exp.Properties.Conditions, act.Properties.Conditions); } }
protected override void ProcessRecordInternal() { AlertProcessingRule result = new AlertProcessingRule(); if (ShouldProcess( target: string.Format(Resources.TargetWithRG, this.Name, this.ResourceGroupName), action: Resources.CreateOrUpdateAlertProcessingRule_Action)) { try { switch (ParameterSetName) { case BySimplifiedFormatActionGroupAlertProcessingRuleParameterSet: if (AlertProcessingRuleType != "AddActionGroups") { throw new PSInvalidOperationException(string.Format(Resources.IncorrectActionRuleType_Exception, "AddActionGroups")); } // Create Alert Processing Rule AlertProcessingRule actionGroupAR = new AlertProcessingRule( location: "Global", tags: ParseTags(), properties: new AlertProcessingRuleProperties( scopes: Scope, actions: ParseAddActionGroupsActions(), conditions: ParseConditions(), schedule: ValidateParseSchedule(), description: Description, enabled: Enabled == null ? true : bool.Parse(Enabled) ) ); result = this.AlertsManagementClient.AlertProcessingRules.CreateOrUpdateWithHttpMessagesAsync( resourceGroupName: ResourceGroupName, alertProcessingRuleName: Name, alertProcessingRule: actionGroupAR).Result.Body; break; case BySimplifiedFormatSuppressionAlertProcessingRuleParameterSet: if (AlertProcessingRuleType != "RemoveAllActionGroups") { throw new PSInvalidOperationException(string.Format(Resources.IncorrectActionRuleType_Exception, "RemoveAllActionGroups")); } // Create Action Rule AlertProcessingRule suppressionAR = new AlertProcessingRule( location: "Global", tags: ParseTags(), properties: new AlertProcessingRuleProperties( scopes: Scope, actions: ParseRemoveAllActionGroupsActions(), conditions: ParseConditions(), schedule: ValidateParseSchedule(), description: Description, enabled: Enabled == null ? true : bool.Parse(Enabled) ) ); result = this.AlertsManagementClient.AlertProcessingRules.CreateOrUpdateWithHttpMessagesAsync( resourceGroupName: ResourceGroupName, alertProcessingRuleName: Name, alertProcessingRule: suppressionAR).Result.Body; break; case ByInputObjectParameterSet: ExtractedInfo info = CommonUtils.ExtractFromActionRuleResourceId(InputObject.Id); switch (InputObject.AlertProcessingType) { case "AddActionGroups": // Create AlertProcessing Rule PSActionGroupAlertProcessingRule actionGroupInputObject = (PSActionGroupAlertProcessingRule)InputObject; AlertProcessingRule actionGroupAlertProcessingRuleFromInputObject = new AlertProcessingRule( location: "Global", tags: JsonConvert.DeserializeObject <IDictionary <string, string> >(actionGroupInputObject.Tags), properties: new AlertProcessingRuleProperties( scopes: JsonConvert.DeserializeObject <IList <string> >(actionGroupInputObject.Scopes), actions: ExtractActions(actionGroupInputObject.ActionGroupIds), conditions: JsonConvert.DeserializeObject <IList <Condition> >(actionGroupInputObject.Conditions), schedule: JsonConvert.DeserializeObject <Schedule>(actionGroupInputObject.Schedule), description: actionGroupInputObject.Description, enabled: actionGroupInputObject.Enabled == "True" ? true : false ) ); result = this.AlertsManagementClient.AlertProcessingRules.CreateOrUpdateWithHttpMessagesAsync( resourceGroupName: info.ResourceGroupName, alertProcessingRuleName: info.Resource, alertProcessingRule: actionGroupAlertProcessingRuleFromInputObject).Result.Body; break; case "RemoveAllActionGroups": PSSuppressionAlertProcessingRule suppressionInputObject = (PSSuppressionAlertProcessingRule)InputObject; // Create AlertProcessing Rule AlertProcessingRule suppressionARFromInputObject = new AlertProcessingRule( location: "Global", tags: JsonConvert.DeserializeObject <IDictionary <string, string> >(suppressionInputObject.Tags), properties: new AlertProcessingRuleProperties( scopes: JsonConvert.DeserializeObject <IList <string> >(suppressionInputObject.Scopes), actions: ParseRemoveAllActionGroupsActions(), conditions: JsonConvert.DeserializeObject <IList <Condition> >(suppressionInputObject.Conditions), schedule: JsonConvert.DeserializeObject <Schedule>(suppressionInputObject.Schedule), description: suppressionInputObject.Description, enabled: suppressionInputObject.Enabled == "True" ? true : false ) ); result = this.AlertsManagementClient.AlertProcessingRules.CreateOrUpdateWithHttpMessagesAsync( resourceGroupName: info.ResourceGroupName, alertProcessingRuleName: info.Resource, alertProcessingRule: suppressionARFromInputObject).Result.Body; break; } break; } } catch (System.Exception e) { throw (e); } WriteObject(sendToPipeline: TransformOutput(result)); } }
public PSSuppressionAlertProcessingRule(AlertProcessingRule rule) : base(rule) { }
public PSActionGroupAlertProcessingRule(AlertProcessingRule rule) : base(rule) { ActionGroupIds = JsonConvert.SerializeObject(rule.Properties.Actions); }
public void CreateAndUpdateAlertProcessingRule() { using (MockContext context = MockContext.Start(this.GetType())) { var alertsManagementClient = GetAlertsManagementClient(context, handler); string resourceGroupName = "ActionRules-Powershell-Test"; string alertProcessingRuleName = "ScenarioTest-AlertProcessingRule"; // Create Alert processing Rule AlertProcessingRule alertProcessingRule = new AlertProcessingRule( location: "Global", tags: new Dictionary <string, string>(), properties: new AlertProcessingRuleProperties( scopes: new List <string> { "/subscriptions/dd91de05-d791-4ceb-b6dc-988682dc7d72/resourceGroups/alertslab", "/subscriptions/dd91de05-d791-4ceb-b6dc-988682dc7d72/resourceGroups/Test-VMs" }, conditions: new List <Condition> { new Condition( field: "Severity", operatorProperty: "Equals", values: new List <string> { "Sev2" }) }, schedule: new Schedule( effectiveFrom: "2019-12-22T11:00:00", effectiveUntil: "2020-09-24T11:00:00", timeZone: "Pacific Standard Time", recurrences: new List <Recurrence> { new DailyRecurrence(startTime: "06:00:00", endTime: "14:00:00") } ), actions: new List <Action> { new AddActionGroups(new List <string> { "/subscriptions/dd91de05-d791-4ceb-b6dc-988682dc7d72/resourceGroups/actionrules-powershell-test/providers/microsoft.insights/actionGroups/powershell-test-ag" }) }, description: "Test Add Action Group Rule", enabled: true ) ); var createdAlertProcessingRule = alertsManagementClient.AlertProcessingRules.CreateOrUpdate(resourceGroupName: resourceGroupName, alertProcessingRuleName: alertProcessingRuleName, alertProcessingRule: alertProcessingRule); // Update Alert processing Rule PatchObject patchObject = new PatchObject( enabled: false ); var updatePatch = alertsManagementClient.AlertProcessingRules.Update(resourceGroupName: resourceGroupName, alertProcessingRuleName: alertProcessingRuleName, alertProcessingRulePatch: patchObject); // Get again to verify update var fetchedAlertProcessingRule = alertsManagementClient.AlertProcessingRules.GetByName(resourceGroupName: resourceGroupName, alertProcessingRuleName: alertProcessingRuleName); if (!this.IsRecording) { Assert.False(fetchedAlertProcessingRule.Properties.Enabled); } } }
/// <summary> /// Create or update an alert processing rule. /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='resourceGroupName'> /// Resource group name where the resource is created. /// </param> /// <param name='alertProcessingRuleName'> /// The name of the alert processing rule that needs to be created/updated. /// </param> /// <param name='alertProcessingRule'> /// Alert processing rule to be created/updated. /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <AlertProcessingRule> CreateOrUpdateAsync(this IAlertProcessingRulesOperations operations, string resourceGroupName, string alertProcessingRuleName, AlertProcessingRule alertProcessingRule, CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, alertProcessingRuleName, alertProcessingRule, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
/// <summary> /// Create or update an alert processing rule. /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='resourceGroupName'> /// Resource group name where the resource is created. /// </param> /// <param name='alertProcessingRuleName'> /// The name of the alert processing rule that needs to be created/updated. /// </param> /// <param name='alertProcessingRule'> /// Alert processing rule to be created/updated. /// </param> public static AlertProcessingRule CreateOrUpdate(this IAlertProcessingRulesOperations operations, string resourceGroupName, string alertProcessingRuleName, AlertProcessingRule alertProcessingRule) { return(operations.CreateOrUpdateAsync(resourceGroupName, alertProcessingRuleName, alertProcessingRule).GetAwaiter().GetResult()); }