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

Commit 0bcbd18

Browse files
authored
New snippet to delete notification channel [(#1920)](GoogleCloudPlatform/python-docs-samples#1920)
New snippet to delete notification channel
1 parent 7fb37fc commit 0bcbd18

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

samples/snippets/v3/alerts-client/snippets.py

+17
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ def replace_notification_channels(project_name, alert_policy_id, channel_ids):
9090
# [END monitoring_alert_replace_channels]
9191

9292

93+
# [START monitoring_alert_delete_channel]
94+
def delete_notification_channels(project_name, channel_ids, force=None):
95+
channel_client = monitoring_v3.NotificationChannelServiceClient()
96+
for channel_id in channel_ids:
97+
channel_name = '{}/notificationChannels/{}'.format(
98+
project_name, channel_id)
99+
try:
100+
channel_client.delete_notification_channel(
101+
channel_name, force=force)
102+
print('Channel {} deleted'.format(channel_name))
103+
except ValueError:
104+
print('The parameters are invalid')
105+
except Exception as e:
106+
print('API call failed: {}'.format(e))
107+
# [END monitoring_alert_delete_channel]
108+
109+
93110
# [START monitoring_alert_backup_policies]
94111
def backup(project_name):
95112
alert_client = monitoring_v3.AlertPolicyServiceClient()

samples/snippets/v3/alerts-client/snippets_test.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ def __enter__(self):
6363
def __exit__(self, type, value, traceback):
6464
# Delete the policy and channel we created.
6565
self.alert_policy_client.delete_alert_policy(self.alert_policy.name)
66-
self.notification_channel_client.delete_notification_channel(
67-
self.notification_channel.name)
66+
if self.notification_channel.name:
67+
self.notification_channel_client.delete_notification_channel(
68+
self.notification_channel.name)
6869

6970

7071
@pytest.fixture(scope='session')
@@ -114,3 +115,12 @@ def test_backup_and_restore(capsys, pochan):
114115
assert "Updated {0}".format(pochan.alert_policy.name) in out
115116
assert "Updating channel {0}".format(
116117
pochan.notification_channel.display_name) in out
118+
119+
120+
def test_delete_channels(capsys, pochan):
121+
notification_channel_id = pochan.notification_channel.name.split('/')[-1]
122+
snippets.delete_notification_channels(
123+
pochan.project_name, [notification_channel_id], force=True)
124+
out, _ = capsys.readouterr()
125+
assert "{0} deleted".format(notification_channel_id) in out
126+
pochan.notification_channel.name = '' # So teardown is not tried

0 commit comments

Comments
 (0)