/// <summary>
        /// Execute the cmdlet
        /// </summary>
        protected override void ProcessRecordInternal()
        {
            if (ShouldProcess(
                    target: string.Format("Delete activity logs alert: {0} from resource group: {1}", this.Name, this.ResourceGroupName),
                    action: "Delete activity logs alert"))
            {
                string resourceGroupName    = this.ResourceGroupName;
                string activityLogAlertName = this.Name;

                // Using value from the pipe
                if (this.MyInvocation.BoundParameters.ContainsKey("InputObject") || this.InputObject != null)
                {
                    ActivityLogAlertUtilities.ProcessPipeObject(
                        inputObject: this.InputObject,
                        resourceGroupName: out resourceGroupName,
                        activityLogAlertName: out activityLogAlertName);
                }
                else if (this.MyInvocation.BoundParameters.ContainsKey("ResourceId") || !string.IsNullOrWhiteSpace(this.ResourceId))
                {
                    ActivityLogAlertUtilities.ProcessPipeObject(
                        resourceId: this.ResourceId,
                        resourceGroupName: out resourceGroupName,
                        activityLogAlertName: out activityLogAlertName);
                }

                var result   = this.MonitorManagementClient.ActivityLogAlerts.DeleteWithHttpMessagesAsync(resourceGroupName: resourceGroupName, activityLogAlertName: activityLogAlertName).Result;
                var response = new AzureOperationResponse
                {
                    RequestId  = result.RequestId,
                    StatusCode = result.Response != null ? result.Response.StatusCode : HttpStatusCode.OK
                };

                WriteObject(response);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Execute the cmdlet
        /// </summary>
        protected override void ProcessRecordInternal()
        {
            if (ShouldProcess(
                    target: string.Format("Patch activity logs alert: {0} from resource group: {1}", this.Name, this.ResourceGroupName),
                    action: "Patch activity logs alert"))
            {
                string resourceGroupName          = this.ResourceGroupName;
                string activityLogAlertName       = this.Name;
                IDictionary <string, string> tags = null;

                // Using value from the pipe
                if (this.MyInvocation.BoundParameters.ContainsKey("InputObject") || this.InputObject != null)
                {
                    ActivityLogAlertUtilities.ProcessPipeObject(
                        inputObject: this.InputObject,
                        resourceGroupName: out resourceGroupName,
                        activityLogAlertName: out activityLogAlertName);

                    tags = this.InputObject.Tags;
                }
                else if (this.MyInvocation.BoundParameters.ContainsKey("ResourceId") || !string.IsNullOrWhiteSpace(this.ResourceId))
                {
                    ActivityLogAlertUtilities.ProcessPipeObject(
                        resourceId: this.ResourceId,
                        resourceGroupName: out resourceGroupName,
                        activityLogAlertName: out activityLogAlertName);
                }

                WriteObject(
                    this.MonitorManagementClient.ActivityLogAlerts.Update(
                        resourceGroupName: resourceGroupName,
                        activityLogAlertName: activityLogAlertName,
                        activityLogAlertPatch: this.CreateActivityLogAlertPatchBody(enableAlert: true, tags: tags)));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Execute the cmdlet
        /// </summary>
        protected override void ProcessRecordInternal()
        {
            if (ShouldProcess(
                    target: string.Format("Create/update an activity logs alert: {0} from resource group: {1}", this.Name, this.ResourceGroupName),
                    action: "Create/update an activity logs alert"))
            {
                string resourceGroupName             = this.ResourceGroupName;
                string activityLogAlertName          = this.Name;
                ActivityLogAlertResource requestBody = null;

                // Using value from the pipe
                if (this.MyInvocation.BoundParameters.ContainsKey("InputObject") || this.InputObject != null)
                {
                    WriteVerboseWithTimestamp("InputObject detected: creating request body based on it.");

                    ActivityLogAlertUtilities.ProcessPipeObject(
                        inputObject: this.InputObject,
                        resourceGroupName: out resourceGroupName,
                        activityLogAlertName: out activityLogAlertName);

                    requestBody = this.UpdateActivityLogAlertPsResource(this.InputObject);
                }
                else if (this.MyInvocation.BoundParameters.ContainsKey("ResourceId") || !string.IsNullOrWhiteSpace(this.ResourceId))
                {
                    WriteVerboseWithTimestamp("ResourceId detected: extracting name and resource group name based on it.");

                    // ResourceId is not enough to set an ActivityLogAlert
                    // First there is the need to try and find an existing alert and modify it
                    ActivityLogAlertUtilities.ProcessPipeObject(
                        resourceId: this.ResourceId,
                        resourceGroupName: out resourceGroupName,
                        activityLogAlertName: out activityLogAlertName);

                    WriteVerboseWithTimestamp("ResourceId detected: checking for the existence the given activity log alert.");

                    requestBody = this.MonitorManagementClient.ActivityLogAlerts.Get(resourceGroupName: resourceGroupName, activityLogAlertName: activityLogAlertName);
                    if (requestBody == null)
                    {
                        WriteVerboseWithTimestamp("ResourceId detected: given activity log alert does not exist.");

                        // This can only happen if the user sent a resourceId of an alert that does not exist yet
                        if (string.IsNullOrWhiteSpace(this.Location))
                        {
                            // The user wants to create an activity log alert given ResourceId as argument, but the Location parameter was empty or null
                            throw new PSArgumentException("With ResourceId parameter used to create a new ActivityLogAlert, Location must contain a value", "Location");
                        }

                        requestBody = this.CreateActivityLogAlertResource(
                            name: activityLogAlertName,
                            location: this.Location);
                    }
                    else
                    {
                        WriteVerboseWithTimestamp("ResourceId detected: given activity log alert found, modifying its values with the parameters.");

                        requestBody = this.UpdateActivityLogAlertResource(requestBody);
                    }
                }
                else
                {
                    WriteVerboseWithTimestamp("No InputObject or ResourceId detected: following standard creation/update process.");

                    requestBody = this.CreateActivityLogAlertResource(
                        name: activityLogAlertName,
                        location: this.Location);
                }

                WriteObject(
                    this.MonitorManagementClient.ActivityLogAlerts.CreateOrUpdate(
                        resourceGroupName: resourceGroupName,
                        activityLogAlertName: activityLogAlertName,
                        activityLogAlert: requestBody));
            }
        }