Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample to update a cloud monitoring uptime check. #1508

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: I find it slightly annoying that the autogen client doesn't take a list of strings for field_mask.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

# [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