Exemple #1
0
        protected override void ProcessRecordInternal()
        {
            if (ShouldProcess(
                    target: string.Format(Resources.Target, string.IsNullOrWhiteSpace(AlertId) ? InputObject.Id : AlertId),
                    action: Resources.UpdateAlertState_Action))
            {
                string id = AlertId;
                switch (ParameterSetName)
                {
                case ByIdParameterSet:
                    id = CommonUtils.GetIdFromARMResourceId(AlertId);
                    break;

                case ByInputObjectParameterSet:
                    id = CommonUtils.GetIdFromARMResourceId(InputObject.Id);
                    break;
                }

                PSAlert alert = new PSAlert(this.AlertsManagementClient.Alerts.ChangeStateWithHttpMessagesAsync(id, State).Result.Body);
                WriteObject(sendToPipeline: alert);
            }
        }
        protected override void ProcessRecordInternal()
        {
            switch (ParameterSetName)
            {
            case AlertsListByFilterParameterSet:
            case AlertsListByTargetResourceIdFilterParameterSet:
                List <Alert>  resultList = new List <Alert>();
                IPage <Alert> pageResult = new Page <Alert>();
                pageResult = this.AlertsManagementClient.Alerts.GetAllWithHttpMessagesAsync(
                    targetResource: TargetResourceId,
                    targetResourceType: TargetResourceType,
                    targetResourceGroup: TargetResourceGroup,
                    monitorService: MonitorService,
                    monitorCondition: MonitorCondition,
                    severity: Severity,
                    alertState: State,
                    alertRule: AlertRuleId,
                    smartGroupId: SmartGroupId,
                    includeContext: IncludeContext,
                    includeEgressConfig: IncludeEgressConfig,
                    pageCount: PageCount,
                    sortBy: SortBy,
                    sortOrder: SortOrder,
                    timeRange: TimeRange,
                    customTimeRange: CustomTimeRange,
                    select: Select
                    ).Result.Body;

                // 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
                {
                    List <Alert> 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 (Alert currentAlert 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(currentAlert);
                            }
                            currentCount++;
                            if (currentCount >= lastCount)
                            {
                                break;
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(nextPageLink))
                    {
                        pageResult   = this.AlertsManagementClient.Alerts.GetAllNextWithHttpMessagesAsync(nextPageLink).Result.Body;
                        nextPageLink = pageResult.NextPageLink;
                    }
                } while (!string.IsNullOrEmpty(nextPageLink) && currentCount < lastCount);

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

            case AlertByIdParameterSet:
                string  id    = CommonUtils.GetIdFromARMResourceId(AlertId);
                PSAlert alert = new PSAlert(this.AlertsManagementClient.Alerts.GetByIdWithHttpMessagesAsync(id).Result.Body);
                WriteObject(sendToPipeline: alert);
                break;
            }
        }