Skip to content

Commit bf1e0d7

Browse files
Add sample to update a cloud monitoring uptime check. (GoogleCloudPlatform#1508)
* Add sample to update a cloud monitoring uptime check. * Replace double quotes with single quotes.
1 parent fee1b1f commit bf1e0d7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

monitoring/api/v3/uptime-check-client/snippets.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ def create_uptime_check_config(project_name, host_name=None,
4242
# [END monitoring_uptime_check_create]
4343

4444

45+
# [START monitoring_uptime_check_update]
46+
def update_uptime_check_config(config_name, new_display_name=None,
47+
new_http_check_path=None):
48+
client = monitoring_v3.UptimeCheckServiceClient()
49+
config = client.get_uptime_check_config(config_name)
50+
field_mask = monitoring_v3.types.FieldMask()
51+
if new_display_name:
52+
field_mask.paths.append('display_name')
53+
config.display_name = new_display_name
54+
if new_http_check_path:
55+
field_mask.paths.append('http_check.path')
56+
config.http_check.path = new_http_check_path
57+
client.update_uptime_check_config(config, field_mask)
58+
# [END monitoring_uptime_check_update]
59+
60+
4561
# [START monitoring_uptime_check_list_configs]
4662
def list_uptime_check_configs(project_name):
4763
client = monitoring_v3.UptimeCheckServiceClient()
@@ -153,6 +169,23 @@ def project_name():
153169
required=True,
154170
)
155171

172+
update_uptime_check_config_parser = subparsers.add_parser(
173+
'update-uptime-check-config',
174+
help=update_uptime_check_config.__doc__
175+
)
176+
update_uptime_check_config_parser.add_argument(
177+
'-m', '--name',
178+
required=True,
179+
)
180+
update_uptime_check_config_parser.add_argument(
181+
'-d', '--display_name',
182+
required=False,
183+
)
184+
update_uptime_check_config_parser.add_argument(
185+
'-p', '--uptime_check_path',
186+
required=False,
187+
)
188+
156189
args = parser.parse_args()
157190

158191
if args.command == 'list-uptime-check-configs':
@@ -170,3 +203,11 @@ def project_name():
170203

171204
elif args.command == 'delete-uptime-check-config':
172205
delete_uptime_check_config(args.name)
206+
207+
elif args.command == 'update-uptime-check-config':
208+
if not args.display_name and not args.uptime_check_path:
209+
print('Nothing to update. Pass --display_name or '
210+
'--uptime_check_path.')
211+
else:
212+
update_uptime_check_config(args.name, args.display_name,
213+
args.uptime_check_path)

monitoring/api/v3/uptime-check-client/snippets_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ def test_create_and_delete(capsys):
5858
pass
5959

6060

61+
def test_update_uptime_config(capsys):
62+
# create and delete happen in uptime fixture.
63+
new_display_name = random_name(10)
64+
new_uptime_check_path = '/' + random_name(10)
65+
with UptimeFixture() as fixture:
66+
snippets.update_uptime_check_config(
67+
fixture.config.name, new_display_name, new_uptime_check_path)
68+
out, _ = capsys.readouterr()
69+
snippets.get_uptime_check_config(fixture.config.name)
70+
out, _ = capsys.readouterr()
71+
assert new_display_name in out
72+
assert new_uptime_check_path in out
73+
74+
6175
def test_get_uptime_check_config(capsys, uptime):
6276
snippets.get_uptime_check_config(uptime.config.name)
6377
out, _ = capsys.readouterr()

0 commit comments

Comments
 (0)