Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
New snippet to delete notification channel
  • Loading branch information
engelke committed Dec 10, 2018
commit 3cdec0d913c2cb0ce33045af2458d41a025210a6
16 changes: 15 additions & 1 deletion monitoring/api/v3/alerts-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ 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):
_, project_id = project_name.split('/')
alert_client = monitoring_v3.AlertPolicyServiceClient()
channel_client = monitoring_v3.NotificationChannelServiceClient()
policy = monitoring_v3.types.alert_pb2.AlertPolicy()
policy.name = alert_client.alert_policy_path(project_id, alert_policy_id)
Expand All @@ -90,6 +89,21 @@ def replace_notification_channels(project_name, alert_policy_id, channel_ids):
# [END monitoring_alert_replace_channels]


# [START monitoring_alert_delete_channel]
def delete_notification_channels(project_name, channel_ids):
channel_client = monitoring_v3.NotificationChannelServiceClient()
for channel_id in channel_ids:
channel_name = '{}/notificationChannels/{}'.format(project_name, channel_id)
try:
channel_client.delete_notification_channel(channel_name)
print('Channel {} deleted').format(channel_name)
except ValueError:
print('The parameters are invalid')
except:
print('API call failed')
# [END monitoring_alert_delete_channel]


# [START monitoring_alert_backup_policies]
def backup(project_name):
alert_client = monitoring_v3.AlertPolicyServiceClient()
Expand Down
8 changes: 8 additions & 0 deletions monitoring/api/v3/alerts-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def test_replace_channels(capsys, pochan):
assert "Updated {0}".format(pochan.alert_policy.name) in out


def test_delete_channels(capsys, pochan):
notification_channel_id = pochan.notification_channel.name.split('/')[-1]
snippets.delete_notification_channels(
pochan.project_name, [notification_channel_id])
out, _ = capsys.readouterr()
assert "{0} deleted".format(notification_channel_id) in out


def test_backup_and_restore(capsys, pochan):
snippets.backup(pochan.project_name)
out, _ = capsys.readouterr()
Expand Down