Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d9a515b

Browse filesBrowse files
authored
New snippet to delete notification channel (GoogleCloudPlatform#1920)
New snippet to delete notification channel
1 parent 84cdf38 commit d9a515b
Copy full SHA for d9a515b

File tree

Expand file treeCollapse file tree

2 files changed

+29
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+29
-2
lines changed

‎monitoring/api/v3/alerts-client/snippets.py

Copy file name to clipboardExpand all lines: monitoring/api/v3/alerts-client/snippets.py
+17Lines changed: 17 additions & 0 deletions
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()

‎monitoring/api/v3/alerts-client/snippets_test.py

Copy file name to clipboardExpand all lines: monitoring/api/v3/alerts-client/snippets_test.py
+12-2Lines changed: 12 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.