protected override void ProcessRecordInternal()
        {
            PSActionRule updatedActionRule = new PSActionRule();

            switch (ParameterSetName)
            {
            case ByNameSimplifiedPatchParameterSet:
                if (ShouldProcess(
                        target: string.Format(Resources.TargetWithRG, this.Name, this.ResourceGroupName),
                        action: Resources.CreateOrUpdateActionRule_Action))
                {
                    updatedActionRule = new PSActionRule(this.AlertsManagementClient.ActionRules.UpdateWithHttpMessagesAsync(
                                                             resourceGroupName: ResourceGroupName,
                                                             actionRuleName: Name,
                                                             actionRulePatch: new PatchObject(
                                                                 status: Status,
                                                                 tags: Tag
                                                                 )
                                                             ).Result.Body);
                }
                break;

            case ByInputObjectParameterSet:
                if (ShouldProcess(
                        target: string.Format(Resources.Target, this.InputObject.Id),
                        action: Resources.CreateOrUpdateActionRule_Action))
                {
                    var extractedInfo = CommonUtils.ExtractFromActionRuleResourceId(InputObject.Id);
                    updatedActionRule = new PSActionRule(this.AlertsManagementClient.ActionRules.UpdateWithHttpMessagesAsync(
                                                             resourceGroupName: extractedInfo.ResourceGroupName,
                                                             actionRuleName: extractedInfo.Resource,
                                                             actionRulePatch: new PatchObject(
                                                                 status: Status,
                                                                 tags: Tag
                                                                 )
                                                             ).Result.Body);
                }
                break;

            case ByResourceIdParameterSet:
                if (ShouldProcess(
                        target: string.Format(Resources.Target, this.ResourceId),
                        action: Resources.CreateOrUpdateActionRule_Action))
                {
                    var info = CommonUtils.ExtractFromActionRuleResourceId(ResourceId);
                    updatedActionRule = new PSActionRule(this.AlertsManagementClient.ActionRules.UpdateWithHttpMessagesAsync(
                                                             resourceGroupName: info.ResourceGroupName,
                                                             actionRuleName: info.Resource,
                                                             actionRulePatch: new PatchObject(
                                                                 status: Status,
                                                                 tags: Tag
                                                                 )
                                                             ).Result.Body);
                }
                break;
            }

            WriteObject(sendToPipeline: updatedActionRule);
        }
Exemple #2
0
        protected override void ProcessRecordInternal()
        {
            ActionRule result = new ActionRule();

            if (ShouldProcess(
                    target: string.Format(Resources.TargetWithRG, this.Name, this.ResourceGroupName),
                    action: Resources.CreateOrUpdateActionRule_Action))
            {
                switch (ParameterSetName)
                {
                case BySimplifiedFormatActionGroupActionRuleParameterSet:
                    if (ActionRuleType != "ActionGroup")
                    {
                        throw new PSInvalidOperationException(string.Format(Resources.IncorrectActionRuleType_Exception, "ActionGroup"));
                    }

                    // Create Action Rule
                    ActionRule actionGroupAR = new ActionRule(
                        location: "Global",
                        tags: new Dictionary <string, string>(),
                        properties: new ActionGroup(
                            scope: ParseScope(),
                            conditions: ParseConditions(),
                            actionGroupId: ActionGroupId,
                            description: Description,
                            status: Status
                            )
                        );

                    result = this.AlertsManagementClient.ActionRules.CreateUpdateWithHttpMessagesAsync(
                        resourceGroupName: ResourceGroupName, actionRuleName: Name, actionRule: actionGroupAR).Result.Body;
                    break;

                case BySimplifiedFormatSuppressionActionRuleParameterSet:

                    if (ActionRuleType != "Suppression")
                    {
                        throw new PSInvalidOperationException(string.Format(Resources.IncorrectActionRuleType_Exception, "Suppression"));
                    }

                    SuppressionConfig config = new SuppressionConfig(recurrenceType: ReccurenceType);
                    if (ReccurenceType != "Always")
                    {
                        config.Schedule = new SuppressionSchedule(
                            startDate: SuppressionStartTime.Split(' ')[0],
                            endDate: SuppressionEndTime.Split(' ')[0],
                            startTime: SuppressionStartTime.Split(' ')[1],
                            endTime: SuppressionEndTime.Split(' ')[1]
                            );

                        if (ReccurentValue.Length > 0)
                        {
                            config.Schedule.RecurrenceValues = ReccurentValue.OfType <int?>().ToList();
                        }
                    }

                    // Create Action Rule
                    ActionRule suppressionAR = new ActionRule(
                        location: "Global",
                        tags: new Dictionary <string, string>(),
                        properties: new Suppression(
                            scope: ParseScope(),
                            conditions: ParseConditions(),
                            description: Description,
                            status: Status,
                            suppressionConfig: config
                            )
                        );

                    result = this.AlertsManagementClient.ActionRules.CreateUpdateWithHttpMessagesAsync(
                        resourceGroupName: ResourceGroupName, actionRuleName: Name, actionRule: suppressionAR).Result.Body;
                    break;

                case BySimplifiedFormatDiagnosticsActionRuleParameterSet:
                    if (ActionRuleType != "Diagnostics")
                    {
                        throw new PSInvalidOperationException(string.Format(Resources.IncorrectActionRuleType_Exception, "Diagnostics"));
                    }

                    // Create Action Rule
                    ActionRule diagnosticsAR = new ActionRule(
                        location: "Global",
                        tags: new Dictionary <string, string>(),
                        properties: new Diagnostics(
                            scope: ParseScope(),
                            conditions: ParseConditions(),
                            description: Description,
                            status: Status
                            )
                        );

                    result = this.AlertsManagementClient.ActionRules.CreateUpdateWithHttpMessagesAsync(
                        resourceGroupName: ResourceGroupName, actionRuleName: Name, actionRule: diagnosticsAR).Result.Body;

                    break;

                case ByInputObjectParameterSet:
                    ExtractedInfo info = CommonUtils.ExtractFromActionRuleResourceId(InputObject.Id);
                    switch (InputObject.ActionRuleType)
                    {
                    case "ActionGroup":
                        // Create Action Rule
                        PSActionGroupActionRule actionGroupInputObject = (PSActionGroupActionRule)InputObject;
                        ActionRule actionGroupARFromInputObject        = new ActionRule(
                            location: "Global",
                            tags: new Dictionary <string, string>(),
                            properties: new ActionGroup(
                                scope: JsonConvert.DeserializeObject <Scope>(actionGroupInputObject.Scope),
                                conditions: JsonConvert.DeserializeObject <Conditions>(actionGroupInputObject.Conditions),
                                actionGroupId: actionGroupInputObject.ActionGroupId,
                                description: actionGroupInputObject.Description,
                                status: actionGroupInputObject.Status
                                )
                            );

                        result = this.AlertsManagementClient.ActionRules.CreateUpdateWithHttpMessagesAsync(
                            resourceGroupName: info.ResourceGroupName, actionRuleName: info.Resource, actionRule: actionGroupARFromInputObject).Result.Body;
                        break;

                    case "Suppression":
                        PSSuppressionActionRule suppressionInputObject = (PSSuppressionActionRule)InputObject;
                        SuppressionConfig       configFromInputObject  = new SuppressionConfig(recurrenceType: suppressionInputObject.RecurrenceType);
                        if (suppressionInputObject.RecurrenceType != "Always")
                        {
                            configFromInputObject.Schedule = new SuppressionSchedule(
                                startDate: suppressionInputObject.StartDate,
                                endDate: suppressionInputObject.EndDate,
                                startTime: suppressionInputObject.StartTime,
                                endTime: suppressionInputObject.EndTime
                                );

                            if (ReccurentValue.Length > 0)
                            {
                                configFromInputObject.Schedule.RecurrenceValues = suppressionInputObject.RecurrenceValues;
                            }
                        }

                        // Create Action Rule
                        ActionRule suppressionARFromInputObject = new ActionRule(
                            location: "Global",
                            tags: new Dictionary <string, string>(),
                            properties: new Suppression(
                                scope: JsonConvert.DeserializeObject <Scope>(suppressionInputObject.Scope),
                                conditions: JsonConvert.DeserializeObject <Conditions>(suppressionInputObject.Conditions),
                                description: suppressionInputObject.Description,
                                status: suppressionInputObject.Status,
                                suppressionConfig: configFromInputObject
                                )
                            );

                        result = this.AlertsManagementClient.ActionRules.CreateUpdateWithHttpMessagesAsync(
                            resourceGroupName: info.ResourceGroupName, actionRuleName: info.Resource, actionRule: suppressionARFromInputObject).Result.Body;
                        break;

                    case "Diagnostics":
                        // Create Action Rule
                        PSDiagnosticsActionRule diagnosticsInputObject = (PSDiagnosticsActionRule)InputObject;
                        ActionRule diagnosticsARFromInputObject        = new ActionRule(
                            location: "Global",
                            tags: new Dictionary <string, string>(),
                            properties: new Diagnostics(
                                scope: JsonConvert.DeserializeObject <Scope>(diagnosticsInputObject.Scope),
                                conditions: JsonConvert.DeserializeObject <Conditions>(diagnosticsInputObject.Conditions),
                                description: diagnosticsInputObject.Description,
                                status: diagnosticsInputObject.Status
                                )
                            );

                        result = this.AlertsManagementClient.ActionRules.CreateUpdateWithHttpMessagesAsync(
                            resourceGroupName: info.ResourceGroupName, actionRuleName: info.Resource, actionRule: diagnosticsARFromInputObject).Result.Body;
                        break;
                    }
                    break;
                }

                WriteObject(sendToPipeline: TransformOutput(result));
            }
        }
        protected override void ProcessRecordInternal()
        {
            try
            {
                switch (ParameterSetName)
                {
                case ListAlertProcessingRulesParameterSet:
                case ListAlertProcessingRulesByResourceGroupParameterSet:
                    IPage <AlertProcessingRule> pageResult = new Page <AlertProcessingRule>();
                    List <AlertProcessingRule>  resultList = new List <AlertProcessingRule>();
                    bool listByResourceGroup = false;

                    if (string.IsNullOrWhiteSpace(ResourceGroupName))
                    {
                        pageResult = this.AlertsManagementClient.AlertProcessingRules.ListBySubscriptionWithHttpMessagesAsync(
                            ).Result.Body;

                        listByResourceGroup = false;
                    }
                    else
                    {
                        pageResult = this.AlertsManagementClient.AlertProcessingRules.ListByResourceGroupWithHttpMessagesAsync(
                            resourceGroupName: ResourceGroupName
                            ).Result.Body;

                        listByResourceGroup = true;
                    }

                    // Deal with paging in response
                    ulong first = MyInvocation.BoundParameters.ContainsKey("First") ? this.PagingParameters.First : ulong.MaxValue;
                    ulong skip  = MyInvocation.BoundParameters.ContainsKey("Skip") ? this.PagingParameters.Skip : 0;

                    // Any items before this count should be return
                    ulong lastCount    = MyInvocation.BoundParameters.ContainsKey("First") ? skip + first : ulong.MaxValue;
                    ulong currentCount = 0;
                    var   nextPageLink = pageResult.NextPageLink;

                    do
                    {
                        nextPageLink = pageResult.NextPageLink;
                        List <AlertProcessingRule> tempList = pageResult.ToList();
                        if (currentCount + (ulong)tempList.Count - 1 < skip)
                        {
                            // skip the whole chunk if they are all in skip
                            currentCount += (ulong)tempList.Count;
                        }
                        else
                        {
                            foreach (AlertProcessingRule currentActionRule in tempList)
                            {
                                // not return "skip" count of items in the begin, and only return "first" count of items after that.
                                if (currentCount >= skip && currentCount < lastCount)
                                {
                                    resultList.Add(currentActionRule);
                                }
                                currentCount++;
                                if (currentCount >= lastCount)
                                {
                                    break;
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(nextPageLink))
                        {
                            if (listByResourceGroup)
                            {
                                pageResult = this.AlertsManagementClient.AlertProcessingRules.ListByResourceGroupNextWithHttpMessagesAsync(nextPageLink).Result.Body;
                            }
                            else
                            {
                                pageResult = this.AlertsManagementClient.AlertProcessingRules.ListBySubscriptionNextWithHttpMessagesAsync(nextPageLink).Result.Body;
                            }
                        }
                    } while (!string.IsNullOrEmpty(nextPageLink) && currentCount < lastCount);

                    WriteObject(resultList.Select((r) => TransformOutput(r)), enumerateCollection: true);
                    break;

                case AlertProcessingRuleByNameParameterSet:
                    var rulebyName = this.AlertsManagementClient.AlertProcessingRules.GetByNameWithHttpMessagesAsync(ResourceGroupName, Name).Result.Body;
                    WriteObject(sendToPipeline: TransformOutput(rulebyName));
                    break;

                case ResourceIdParameterSet:
                    ExtractedInfo info     = CommonUtils.ExtractFromActionRuleResourceId(ResourceId);
                    var           ruleById = this.AlertsManagementClient.AlertProcessingRules.GetByNameWithHttpMessagesAsync(info.ResourceGroupName, info.Resource).Result.Body;
                    WriteObject(sendToPipeline: TransformOutput(ruleById));
                    break;
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
        protected override void ProcessRecordInternal()
        {
            bool?isDeleted = false;

            try
            {
                switch (ParameterSetName)
                {
                case ByResourceIdParameterSet:
                    if (ShouldProcess(
                            target: string.Format(Resources.Target, this.ResourceId),
                            action: Resources.RemoveAlertProcessingRule_Action))
                    {
                        var extractedInfo = CommonUtils.ExtractFromActionRuleResourceId(ResourceId);
                        isDeleted = this.AlertsManagementClient.AlertProcessingRules.DeleteWithHttpMessagesAsync(
                            resourceGroupName: extractedInfo.ResourceGroupName,
                            alertProcessingRuleName: extractedInfo.Resource)
                                    .Result.Response.IsSuccessStatusCode;
                    }

                    if (PassThru.IsPresent)
                    {
                        WriteObject(isDeleted);
                    }
                    break;

                case ByInputObjectParameterSet:
                    if (ShouldProcess(
                            target: string.Format(Resources.Target, this.InputObject.Id),
                            action: Resources.RemoveAlertProcessingRule_Action))
                    {
                        var extractedInfo = CommonUtils.ExtractFromActionRuleResourceId(InputObject.Id);
                        isDeleted = this.AlertsManagementClient.AlertProcessingRules.DeleteWithHttpMessagesAsync(
                            resourceGroupName: extractedInfo.ResourceGroupName,
                            alertProcessingRuleName: extractedInfo.Resource)
                                    .Result.Response.IsSuccessStatusCode;
                    }

                    if (PassThru.IsPresent)
                    {
                        WriteObject(isDeleted);
                    }

                    break;

                case ByNameParameterSet:
                    if (ShouldProcess(
                            target: string.Format(Resources.TargetWithRG, this.Name, this.ResourceGroupName),
                            action: Resources.RemoveAlertProcessingRule_Action))
                    {
                        isDeleted = this.AlertsManagementClient.AlertProcessingRules.DeleteWithHttpMessagesAsync(
                            resourceGroupName: ResourceGroupName,
                            alertProcessingRuleName: Name)
                                    .Result.Response.IsSuccessStatusCode;
                    }

                    if (PassThru.IsPresent)
                    {
                        WriteObject(isDeleted);
                    }
                    break;
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
Exemple #5
0
        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));
            }
        }
Exemple #6
0
        protected override void ProcessRecordInternal()
        {
            PSAlertProcessingRule updatedAlertProcessingRule = new PSAlertProcessingRule();

            try
            {
                switch (ParameterSetName)
                {
                case ByNameSimplifiedPatchParameterSet:
                    if (ShouldProcess(
                            target: string.Format(Resources.TargetWithRG, this.Name, this.ResourceGroupName),
                            action: Resources.CreateOrUpdateAlertProcessingRule_Action))
                    {
                        updatedAlertProcessingRule = new PSAlertProcessingRule(this.AlertsManagementClient.AlertProcessingRules.UpdateWithHttpMessagesAsync(
                                                                                   resourceGroupName: ResourceGroupName,
                                                                                   alertProcessingRuleName: Name,
                                                                                   alertProcessingRulePatch: new PatchObject(
                                                                                       enabled: Enabled != null ? bool.Parse(Enabled) : (bool?)null,
                                                                                       tags: ParseTags()
                                                                                       )
                                                                                   ).Result.Body);
                    }
                    break;

                case ByInputObjectParameterSet:
                    if (ShouldProcess(
                            target: string.Format(Resources.Target, this.InputObject.Id),
                            action: Resources.CreateOrUpdateAlertProcessingRule_Action))
                    {
                        var extractedInfo = CommonUtils.ExtractFromActionRuleResourceId(InputObject.Id);
                        updatedAlertProcessingRule = new PSAlertProcessingRule(this.AlertsManagementClient.AlertProcessingRules.UpdateWithHttpMessagesAsync(
                                                                                   resourceGroupName: extractedInfo.ResourceGroupName,
                                                                                   alertProcessingRuleName: extractedInfo.Resource,
                                                                                   alertProcessingRulePatch: new PatchObject(
                                                                                       enabled: Enabled != null ? bool.Parse(Enabled) : (bool?)null,
                                                                                       tags: ParseTags()

                                                                                       )
                                                                                   ).Result.Body);
                    }
                    break;

                case ByResourceIdParameterSet:
                    if (ShouldProcess(
                            target: string.Format(Resources.Target, this.ResourceId),
                            action: Resources.CreateOrUpdateAlertProcessingRule_Action))
                    {
                        var info = CommonUtils.ExtractFromActionRuleResourceId(ResourceId);
                        updatedAlertProcessingRule = new PSAlertProcessingRule(this.AlertsManagementClient.AlertProcessingRules.UpdateWithHttpMessagesAsync(
                                                                                   resourceGroupName: info.ResourceGroupName,
                                                                                   alertProcessingRuleName: info.Resource,
                                                                                   alertProcessingRulePatch: new PatchObject(
                                                                                       enabled: Enabled != null ? bool.Parse(Enabled) : (bool?)null,
                                                                                       tags: ParseTags()
                                                                                       )
                                                                                   ).Result.Body);
                    }
                    break;
                }
            }
            catch (System.Exception e)
            {
                throw (e);
            }

            WriteObject(sendToPipeline: updatedAlertProcessingRule);
        }