Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
New snippet to delete notification channel [(#1920)](GoogleCloudPlatf…
Browse files Browse the repository at this point in the history
…orm/python-docs-samples#1920)

New snippet to delete notification channel
  • Loading branch information
engelke committed Dec 11, 2018
1 parent 7fb37fc commit 0bcbd18
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
17 changes: 17 additions & 0 deletions samples/snippets/v3/alerts-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ 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, force=None):
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, force=force)
print('Channel {} deleted'.format(channel_name))
except ValueError:
print('The parameters are invalid')
except Exception as e:
print('API call failed: {}'.format(e))
# [END monitoring_alert_delete_channel]


# [START monitoring_alert_backup_policies]
def backup(project_name):
alert_client = monitoring_v3.AlertPolicyServiceClient()
Expand Down
14 changes: 12 additions & 2 deletions samples/snippets/v3/alerts-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def __enter__(self):
def __exit__(self, type, value, traceback):
# Delete the policy and channel we created.
self.alert_policy_client.delete_alert_policy(self.alert_policy.name)
self.notification_channel_client.delete_notification_channel(
self.notification_channel.name)
if self.notification_channel.name:
self.notification_channel_client.delete_notification_channel(
self.notification_channel.name)


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -114,3 +115,12 @@ def test_backup_and_restore(capsys, pochan):
assert "Updated {0}".format(pochan.alert_policy.name) in out
assert "Updating channel {0}".format(
pochan.notification_channel.display_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], force=True)
out, _ = capsys.readouterr()
assert "{0} deleted".format(notification_channel_id) in out
pochan.notification_channel.name = '' # So teardown is not tried

0 comments on commit 0bcbd18

Please sign in to comment.