From b8ad30b7c8af202cc8d0ff513c0ee369f0213c4e Mon Sep 17 00:00:00 2001 From: Jeffrey Rennie Date: Tue, 29 May 2018 14:00:00 -0700 Subject: [PATCH 1/2] Add sample to update a cloud monitoring uptime check. --- .../api/v3/uptime-check-client/snippets.py | 41 +++++++++++++++++++ .../v3/uptime-check-client/snippets_test.py | 14 +++++++ 2 files changed, 55 insertions(+) diff --git a/monitoring/api/v3/uptime-check-client/snippets.py b/monitoring/api/v3/uptime-check-client/snippets.py index 43a536ad294b..320508edfd64 100644 --- a/monitoring/api/v3/uptime-check-client/snippets.py +++ b/monitoring/api/v3/uptime-check-client/snippets.py @@ -42,6 +42,22 @@ def create_uptime_check_config(project_name, host_name=None, # [END monitoring_uptime_check_create] +# [START monitoring_uptime_check_update] +def update_uptime_check_config(config_name, new_display_name=None, + new_http_check_path=None): + client = monitoring_v3.UptimeCheckServiceClient() + config = client.get_uptime_check_config(config_name) + field_mask = monitoring_v3.types.FieldMask() + if new_display_name: + field_mask.paths.append('display_name') + config.display_name = new_display_name + if new_http_check_path: + field_mask.paths.append('http_check.path') + config.http_check.path = new_http_check_path + client.update_uptime_check_config(config, field_mask) +# [END monitoring_uptime_check_update] + + # [START monitoring_uptime_check_list_configs] def list_uptime_check_configs(project_name): client = monitoring_v3.UptimeCheckServiceClient() @@ -153,6 +169,23 @@ def project_name(): required=True, ) + update_uptime_check_config_parser = subparsers.add_parser( + 'update-uptime-check-config', + help=update_uptime_check_config.__doc__ + ) + update_uptime_check_config_parser.add_argument( + '-m', '--name', + required=True, + ) + update_uptime_check_config_parser.add_argument( + '-d', '--display_name', + required=False, + ) + update_uptime_check_config_parser.add_argument( + '-p', '--uptime_check_path', + required=False, + ) + args = parser.parse_args() if args.command == 'list-uptime-check-configs': @@ -170,3 +203,11 @@ def project_name(): elif args.command == 'delete-uptime-check-config': delete_uptime_check_config(args.name) + + elif args.command == 'update-uptime-check-config': + if not args.display_name and not args.uptime_check_path: + print("Nothing to update. Pass --display_name or " + "--uptime_check_path.") + else: + update_uptime_check_config(args.name, args.display_name, + args.uptime_check_path) diff --git a/monitoring/api/v3/uptime-check-client/snippets_test.py b/monitoring/api/v3/uptime-check-client/snippets_test.py index 64db9ec636c7..1411607c37c1 100644 --- a/monitoring/api/v3/uptime-check-client/snippets_test.py +++ b/monitoring/api/v3/uptime-check-client/snippets_test.py @@ -58,6 +58,20 @@ def test_create_and_delete(capsys): pass +def test_update_uptime_config(capsys): + # create and delete happen in uptime fixture. + new_display_name = random_name(10) + new_uptime_check_path = '/' + random_name(10) + with UptimeFixture() as fixture: + snippets.update_uptime_check_config( + fixture.config.name, new_display_name, new_uptime_check_path) + out, _ = capsys.readouterr() + snippets.get_uptime_check_config(fixture.config.name) + out, _ = capsys.readouterr() + assert new_display_name in out + assert new_uptime_check_path in out + + def test_get_uptime_check_config(capsys, uptime): snippets.get_uptime_check_config(uptime.config.name) out, _ = capsys.readouterr() From 29de61559438aac2e001a0702a0f663bb386ef58 Mon Sep 17 00:00:00 2001 From: Jeffrey Rennie Date: Wed, 30 May 2018 09:00:28 -0700 Subject: [PATCH 2/2] Replace double quotes with single quotes. --- monitoring/api/v3/uptime-check-client/snippets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monitoring/api/v3/uptime-check-client/snippets.py b/monitoring/api/v3/uptime-check-client/snippets.py index 320508edfd64..78c4a5f63942 100644 --- a/monitoring/api/v3/uptime-check-client/snippets.py +++ b/monitoring/api/v3/uptime-check-client/snippets.py @@ -206,8 +206,8 @@ def project_name(): elif args.command == 'update-uptime-check-config': if not args.display_name and not args.uptime_check_path: - print("Nothing to update. Pass --display_name or " - "--uptime_check_path.") + print('Nothing to update. Pass --display_name or ' + '--uptime_check_path.') else: update_uptime_check_config(args.name, args.display_name, args.uptime_check_path)