From ddddadf873e4edb456d5d9cf31dff79c37210419 Mon Sep 17 00:00:00 2001 From: shabirmean Date: Tue, 2 Aug 2022 14:49:21 -0400 Subject: [PATCH 1/2] improvement: add doc strings to explain the project ID format --- samples/snippets/v3/alerts-client/snippets.py | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/samples/snippets/v3/alerts-client/snippets.py b/samples/snippets/v3/alerts-client/snippets.py index d34e0390..9c235405 100644 --- a/samples/snippets/v3/alerts-client/snippets.py +++ b/samples/snippets/v3/alerts-client/snippets.py @@ -27,6 +27,13 @@ # [START monitoring_alert_list_policies] def list_alert_policies(project_name): + """List alert policies in a project. + + Arguments: + project_name (str): The Google Cloud Project to use. The project name + must be in the format - 'projects/'. + """ + client = monitoring_v3.AlertPolicyServiceClient() policies = client.list_alert_policies(name=project_name) print( @@ -44,6 +51,13 @@ def list_alert_policies(project_name): # [START monitoring_alert_list_channels] def list_notification_channels(project_name): + """List alert notification channels in a project. + + Arguments: + project_name (str): The Google Cloud Project to use. The project name + must be in the format - 'projects/'. + """ + client = monitoring_v3.NotificationChannelServiceClient() channels = client.list_notification_channels(name=project_name) print( @@ -62,7 +76,8 @@ def enable_alert_policies(project_name, enable, filter_=None): """Enable or disable alert policies in a project. Arguments: - project_name (str) + project_name (str): The Google Cloud Project to use. The project name + must be in the format - 'projects/'. enable (bool): Enable or disable the policies. filter_ (str, optional): Only enable/disable alert policies that match this filter_. See @@ -95,6 +110,17 @@ def enable_alert_policies(project_name, enable, filter_=None): # [START monitoring_alert_replace_channels] def replace_notification_channels(project_name, alert_policy_id, channel_ids): + """Replace notification channel of an alert. + + Arguments: + project_name (str): The Google Cloud Project to use. The project name + must be in the format - 'projects/'. + alert_policy_id (str): The ID of the alert policy whose notification + channels are to be replaced. + channel_ids list(str): List of IDs of notification channels to be + replaced as channels for the given alert policy. + """ + _, project_id = project_name.split("/") alert_client = monitoring_v3.AlertPolicyServiceClient() channel_client = monitoring_v3.NotificationChannelServiceClient() @@ -119,6 +145,17 @@ def replace_notification_channels(project_name, alert_policy_id, channel_ids): # [START monitoring_alert_delete_channel] def delete_notification_channels(project_name, channel_ids, force=None): + """Delete alert notification channels. + + Arguments: + project_name (str): The Google Cloud Project to use. The project name + must be in the format - 'projects/'. + channel_ids list(str): List of IDs of notification channels to delete. + force (bool): If true, the notification channels are deleted regardless + of its in use by alert policies. If false, channels that are still + referenced by an existing alerting policy will fail to be deleted. + """ + channel_client = monitoring_v3.NotificationChannelServiceClient() for channel_id in channel_ids: channel_name = "{}/notificationChannels/{}".format(project_name, channel_id) @@ -136,6 +173,15 @@ def delete_notification_channels(project_name, channel_ids, force=None): # [START monitoring_alert_backup_policies] def backup(project_name, backup_filename): + """Backup alert policies from a project to a local file. + + Arguments: + project_name (str): The Google Cloud Project to use. The project name + must be in the format - 'projects/' + backup_filename (str): Name of the file (along with its path) to which + the alert policies will be written as backup. + """ + alert_client = monitoring_v3.AlertPolicyServiceClient() channel_client = monitoring_v3.NotificationChannelServiceClient() record = { From 931faa9d1a30ccc410b0b2cacce6af612a23e7d2 Mon Sep 17 00:00:00 2001 From: shabirmean Date: Tue, 2 Aug 2022 15:52:13 -0400 Subject: [PATCH 2/2] fix: argument desc mismatch --- samples/snippets/v3/alerts-client/snippets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/v3/alerts-client/snippets.py b/samples/snippets/v3/alerts-client/snippets.py index 9c235405..aeaa45a1 100644 --- a/samples/snippets/v3/alerts-client/snippets.py +++ b/samples/snippets/v3/alerts-client/snippets.py @@ -117,8 +117,8 @@ def replace_notification_channels(project_name, alert_policy_id, channel_ids): must be in the format - 'projects/'. alert_policy_id (str): The ID of the alert policy whose notification channels are to be replaced. - channel_ids list(str): List of IDs of notification channels to be - replaced as channels for the given alert policy. + channel_ids (str): ID of notification channel to be added as channel + for the given alert policy. """ _, project_id = project_name.split("/")