From 3cdec0d913c2cb0ce33045af2458d41a025210a6 Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 10:06:15 -0800 Subject: [PATCH 01/10] New snippet to delete notification channel --- monitoring/api/v3/alerts-client/snippets.py | 16 +++++++++++++++- monitoring/api/v3/alerts-client/snippets_test.py | 8 ++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/monitoring/api/v3/alerts-client/snippets.py b/monitoring/api/v3/alerts-client/snippets.py index 2790e0f06004..b7a1049884b0 100644 --- a/monitoring/api/v3/alerts-client/snippets.py +++ b/monitoring/api/v3/alerts-client/snippets.py @@ -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) @@ -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() diff --git a/monitoring/api/v3/alerts-client/snippets_test.py b/monitoring/api/v3/alerts-client/snippets_test.py index e58dc39858a4..d4942ce5a619 100644 --- a/monitoring/api/v3/alerts-client/snippets_test.py +++ b/monitoring/api/v3/alerts-client/snippets_test.py @@ -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() From ee19cdefaa59ed9fcf95887d91c77e730f54c241 Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 10:16:20 -0800 Subject: [PATCH 02/10] Restored inadvertently deleted line. --- monitoring/api/v3/alerts-client/snippets.py | 1 + 1 file changed, 1 insertion(+) diff --git a/monitoring/api/v3/alerts-client/snippets.py b/monitoring/api/v3/alerts-client/snippets.py index b7a1049884b0..0188eb6ba8c0 100644 --- a/monitoring/api/v3/alerts-client/snippets.py +++ b/monitoring/api/v3/alerts-client/snippets.py @@ -74,6 +74,7 @@ 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) From 926b8a139a6157e0b8f9a627e56fb341c290ec3f Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 10:28:03 -0800 Subject: [PATCH 03/10] Show message from exception --- monitoring/api/v3/alerts-client/snippets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monitoring/api/v3/alerts-client/snippets.py b/monitoring/api/v3/alerts-client/snippets.py index 0188eb6ba8c0..a69fd7102cd4 100644 --- a/monitoring/api/v3/alerts-client/snippets.py +++ b/monitoring/api/v3/alerts-client/snippets.py @@ -100,8 +100,8 @@ def delete_notification_channels(project_name, channel_ids): print('Channel {} deleted').format(channel_name) except ValueError: print('The parameters are invalid') - except: - print('API call failed') + except Exception as e: + print('API call failed: {}'.format(e.message)) # [END monitoring_alert_delete_channel] From 9a66ef7c4264988f72d21f9cb796f1217753014e Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 10:38:30 -0800 Subject: [PATCH 04/10] Force deletion for test --- monitoring/api/v3/alerts-client/snippets.py | 4 ++-- monitoring/api/v3/alerts-client/snippets_test.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/monitoring/api/v3/alerts-client/snippets.py b/monitoring/api/v3/alerts-client/snippets.py index a69fd7102cd4..8bb2f36ff622 100644 --- a/monitoring/api/v3/alerts-client/snippets.py +++ b/monitoring/api/v3/alerts-client/snippets.py @@ -91,12 +91,12 @@ def replace_notification_channels(project_name, alert_policy_id, channel_ids): # [START monitoring_alert_delete_channel] -def delete_notification_channels(project_name, channel_ids): +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) + channel_client.delete_notification_channel(channel_name, force=force) print('Channel {} deleted').format(channel_name) except ValueError: print('The parameters are invalid') diff --git a/monitoring/api/v3/alerts-client/snippets_test.py b/monitoring/api/v3/alerts-client/snippets_test.py index d4942ce5a619..17b936b95e51 100644 --- a/monitoring/api/v3/alerts-client/snippets_test.py +++ b/monitoring/api/v3/alerts-client/snippets_test.py @@ -108,7 +108,7 @@ def test_replace_channels(capsys, pochan): 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]) + pochan.project_name, [notification_channel_id], force=True) out, _ = capsys.readouterr() assert "{0} deleted".format(notification_channel_id) in out From 17377ac145d6d24df605ff8167cf271c08459076 Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 10:48:56 -0800 Subject: [PATCH 05/10] Reordered tests --- monitoring/api/v3/alerts-client/snippets_test.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/monitoring/api/v3/alerts-client/snippets_test.py b/monitoring/api/v3/alerts-client/snippets_test.py index 17b936b95e51..fb4a20d9f912 100644 --- a/monitoring/api/v3/alerts-client/snippets_test.py +++ b/monitoring/api/v3/alerts-client/snippets_test.py @@ -105,14 +105,6 @@ 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], force=True) - 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() @@ -122,3 +114,11 @@ 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 From 35e6f9a6a48eed290b975059cafca4d8d28b4c60 Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 10:58:20 -0800 Subject: [PATCH 06/10] Avoid teardown of deleted channel --- monitoring/api/v3/alerts-client/snippets_test.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/monitoring/api/v3/alerts-client/snippets_test.py b/monitoring/api/v3/alerts-client/snippets_test.py index fb4a20d9f912..31c28b94371e 100644 --- a/monitoring/api/v3/alerts-client/snippets_test.py +++ b/monitoring/api/v3/alerts-client/snippets_test.py @@ -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') @@ -122,3 +123,4 @@ def test_delete_channels(capsys, pochan): pochan.project_name, [notification_channel_id], force=True) out, _ = capsys.readouterr() assert "{0} deleted".format(notification_channel_id) in out + self.notification_channel.name = None # So teardown is not tried From e0478e7231aa3e4dbb1159f622565f0f812165d3 Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 11:15:44 -0800 Subject: [PATCH 07/10] Typo fixed --- monitoring/api/v3/alerts-client/snippets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monitoring/api/v3/alerts-client/snippets.py b/monitoring/api/v3/alerts-client/snippets.py index 8bb2f36ff622..4e12bde6763a 100644 --- a/monitoring/api/v3/alerts-client/snippets.py +++ b/monitoring/api/v3/alerts-client/snippets.py @@ -97,11 +97,11 @@ def delete_notification_channels(project_name, channel_ids, force=None): channel_name = '{}/notificationChannels/{}'.format(project_name, channel_id) try: channel_client.delete_notification_channel(channel_name, force=force) - print('Channel {} deleted').format(channel_name) + print('Channel {} deleted'.format(channel_name)) except ValueError: print('The parameters are invalid') except Exception as e: - print('API call failed: {}'.format(e.message)) + print('API call failed: {}'.format(e)) # [END monitoring_alert_delete_channel] From 3686fe546405ca2c7593f430f9f11cab34017589 Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 11:24:29 -0800 Subject: [PATCH 08/10] Another typo --- monitoring/api/v3/alerts-client/snippets_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monitoring/api/v3/alerts-client/snippets_test.py b/monitoring/api/v3/alerts-client/snippets_test.py index 31c28b94371e..7ec3debac779 100644 --- a/monitoring/api/v3/alerts-client/snippets_test.py +++ b/monitoring/api/v3/alerts-client/snippets_test.py @@ -123,4 +123,4 @@ def test_delete_channels(capsys, pochan): pochan.project_name, [notification_channel_id], force=True) out, _ = capsys.readouterr() assert "{0} deleted".format(notification_channel_id) in out - self.notification_channel.name = None # So teardown is not tried + pochan.notification_channel.name = None # So teardown is not tried From 6d33fb2c944b29876abded2ef21faaea6ae48a00 Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 13:55:08 -0800 Subject: [PATCH 09/10] Type compatibility --- monitoring/api/v3/alerts-client/snippets_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monitoring/api/v3/alerts-client/snippets_test.py b/monitoring/api/v3/alerts-client/snippets_test.py index 7ec3debac779..8aba3fc59439 100644 --- a/monitoring/api/v3/alerts-client/snippets_test.py +++ b/monitoring/api/v3/alerts-client/snippets_test.py @@ -123,4 +123,4 @@ def test_delete_channels(capsys, pochan): pochan.project_name, [notification_channel_id], force=True) out, _ = capsys.readouterr() assert "{0} deleted".format(notification_channel_id) in out - pochan.notification_channel.name = None # So teardown is not tried + pochan.notification_channel.name = '' # So teardown is not tried From 7bec38e1f82bc53e4cdfda401091fcf8a1c4242b Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Mon, 10 Dec 2018 15:52:28 -0800 Subject: [PATCH 10/10] Okay, no more than 79 characters --- monitoring/api/v3/alerts-client/snippets.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/monitoring/api/v3/alerts-client/snippets.py b/monitoring/api/v3/alerts-client/snippets.py index 4e12bde6763a..62c84bf8d801 100644 --- a/monitoring/api/v3/alerts-client/snippets.py +++ b/monitoring/api/v3/alerts-client/snippets.py @@ -94,9 +94,11 @@ def replace_notification_channels(project_name, alert_policy_id, channel_ids): 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) + channel_name = '{}/notificationChannels/{}'.format( + project_name, channel_id) try: - channel_client.delete_notification_channel(channel_name, force=force) + channel_client.delete_notification_channel( + channel_name, force=force) print('Channel {} deleted'.format(channel_name)) except ValueError: print('The parameters are invalid')