Skip to content

Commit

Permalink
Add sample to update a cloud monitoring uptime check. (#1508)
Browse files Browse the repository at this point in the history
* Add sample to update a cloud monitoring uptime check.

* Replace double quotes with single quotes.
  • Loading branch information
SurferJeffAtGoogle authored May 30, 2018
1 parent fee1b1f commit bf1e0d7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
41 changes: 41 additions & 0 deletions monitoring/api/v3/uptime-check-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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':
Expand All @@ -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)
14 changes: 14 additions & 0 deletions monitoring/api/v3/uptime-check-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit bf1e0d7

Please sign in to comment.