Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add create example for POST check.
  • Loading branch information
slz250 committed Jun 12, 2020
commit 6a304f4d9b3f22567c4c9aabc5fd8d95f024499f
88 changes: 76 additions & 12 deletions monitoring/api/v3/uptime-check-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
import tabulate


# [START monitoring_uptime_check_create]
def create_uptime_check_config(project_name, host_name=None,
# [START monitoring_uptime_check_create_get]
def create_uptime_check_config_get(project_name, host_name=None,
display_name=None):
config = monitoring_v3.types.uptime_pb2.UptimeCheckConfig()
config.display_name = display_name or 'New uptime check'
config.monitored_resource.type = 'uptime_url'
config.monitored_resource.labels.update(
{'host': host_name or 'example.com'})
config.http_check.request_method = monitoring_v3.enums.UptimeCheckConfig.HttpCheck.RequestMethod.GET
# config.http_check.body = 'foo=bar'
config.http_check.path = '/'
config.http_check.port = 80
config.timeout.seconds = 10
Expand All @@ -39,12 +41,36 @@ def create_uptime_check_config(project_name, host_name=None,
new_config = client.create_uptime_check_config(project_name, config)
pprint.pprint(new_config)
return new_config
# [END monitoring_uptime_check_create]
# [END monitoring_uptime_check_create_get]

# [START monitoring_uptime_check_create_post]
def create_uptime_check_config_post(project_name, host_name=None,
display_name=None):
config = monitoring_v3.types.uptime_pb2.UptimeCheckConfig()
config.display_name = display_name or 'New uptime check'
config.monitored_resource.type = 'uptime_url'
config.monitored_resource.labels.update(
{'host': host_name or 'example.com'})
config.http_check.request_method = monitoring_v3.enums.UptimeCheckConfig.HttpCheck.RequestMethod.POST
config.http_check.content_type = monitoring_v3.enums.UptimeCheckConfig.HttpCheck.ContentType.URL_ENCODED
config.http_check.body = 'foo=bar'
config.http_check.path = '/'
config.http_check.port = 80
config.timeout.seconds = 10
config.period.seconds = 300

client = monitoring_v3.UptimeCheckServiceClient()
new_config = client.create_uptime_check_config(project_name, config)
pprint.pprint(new_config)
return new_config
# [END monitoring_uptime_check_create_post]

# [START monitoring_uptime_check_update]
def update_uptime_check_config(config_name, new_display_name=None,
new_http_check_path=None):
new_http_check_path=None,
new_request_method=None,
new_content_type=None,
new_body=None):
client = monitoring_v3.UptimeCheckServiceClient()
config = client.get_uptime_check_config(config_name)
field_mask = monitoring_v3.types.FieldMask()
Expand All @@ -54,6 +80,15 @@ def update_uptime_check_config(config_name, new_display_name=None,
if new_http_check_path:
field_mask.paths.append('http_check.path')
config.http_check.path = new_http_check_path
if new_request_method:
field_mask.paths.append('http_check.request_method')
config.http_check.request_method = monitoring_v3.enums.UptimeCheckConfig.HttpCheck.RequestMethod.POST
if new_content_type:
field_mask.paths.append('http_check.content_type')
config.http_check.content_type = monitoring_v3.enums.UptimeCheckConfig.HttpCheck.ContentType.URL_ENCODED
if new_body:
field_mask.paths.append('http_check.body')
config.http_check.body = 'foo=bar'
client.update_uptime_check_config(config, field_mask)
# [END monitoring_uptime_check_update]

Expand Down Expand Up @@ -138,15 +173,28 @@ def project_name():
help=list_uptime_check_ips.__doc__
)

create_uptime_check_config_parser = subparsers.add_parser(
'create-uptime-check',
help=create_uptime_check_config.__doc__
create_uptime_check_config_get_parser = subparsers.add_parser(
'create-uptime-check-get',
help=create_uptime_check_config_get.__doc__
)
create_uptime_check_config_get_parser.add_argument(
'-d', '--display_name',
required=False,
)
create_uptime_check_config_get_parser.add_argument(
'-o', '--host_name',
required=False,
)

create_uptime_check_config_post_parser = subparsers.add_parser(
'create-uptime-check-post',
help=create_uptime_check_config_post.__doc__
)
create_uptime_check_config_parser.add_argument(
create_uptime_check_config_post_parser.add_argument(
'-d', '--display_name',
required=False,
)
create_uptime_check_config_parser.add_argument(
create_uptime_check_config_post_parser.add_argument(
'-o', '--host_name',
required=False,
)
Expand Down Expand Up @@ -185,6 +233,18 @@ def project_name():
'-p', '--uptime_check_path',
required=False,
)
update_uptime_check_config_parser.add_argument(
'-r_m', '--request_method',
required=False,
)
update_uptime_check_config_parser.add_argument(
'-c_t', '--content_type',
required=False,
)
update_uptime_check_config_parser.add_argument(
'-b', '--body',
required=False,
)

args = parser.parse_args()

Expand All @@ -194,8 +254,11 @@ def project_name():
elif args.command == 'list-uptime-check-ips':
list_uptime_check_ips()

elif args.command == 'create-uptime-check':
create_uptime_check_config(project_name(), args.host_name,
elif args.command == 'create-uptime-check-get':
create_uptime_check_config_get(project_name(), args.host_name,
args.display_name)
elif args.command == 'create-uptime-check-post':
create_uptime_check_config_post(project_name(), args.host_name,
args.display_name)

elif args.command == 'get-uptime-check-config':
Expand All @@ -207,7 +270,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.')
'--uptime_check_path or --request_method or '
'--content_type or --body.')
else:
update_uptime_check_config(args.name, args.display_name,
args.uptime_check_path)
22 changes: 13 additions & 9 deletions monitoring/api/v3/uptime-check-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ def __init__(self):
self.project_name = snippets.project_name()

def __enter__(self):
# Create an uptime check config.
self.config = snippets.create_uptime_check_config(
print('test');
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove?

# Create an uptime check config (GET request).
self.config_get = snippets.create_uptime_check_config_get(
self.project_name, display_name=random_name(10))
# Create an uptime check config (POST request).
self.config_post = snippets.create_uptime_check_config_post(
self.project_name, display_name=random_name(10))
return self

def __exit__(self, type, value, traceback):
# Delete the config.
snippets.delete_uptime_check_config(self.config.name)

snippets.delete_uptime_check_config(self.config_get.name)
snippets.delete_uptime_check_config(self.config_post.name)

@pytest.fixture(scope='session')
def uptime():
Expand All @@ -64,24 +68,24 @@ def test_update_uptime_config(capsys):
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)
fixture.config_get.name, new_display_name, new_uptime_check_path)
out, _ = capsys.readouterr()
snippets.get_uptime_check_config(fixture.config.name)
snippets.get_uptime_check_config(fixture.config_get.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)
snippets.get_uptime_check_config(uptime.config_get.name)
out, _ = capsys.readouterr()
assert uptime.config.display_name in out
assert uptime.config_get.display_name in out


def test_list_uptime_check_configs(capsys, uptime):
snippets.list_uptime_check_configs(uptime.project_name)
out, _ = capsys.readouterr()
assert uptime.config.display_name in out
assert uptime.config_get.display_name in out


def test_list_uptime_check_ips(capsys):
Expand Down