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
Prev Previous commit
Next Next commit
Ran black linter.
  • Loading branch information
slz250 committed Jul 1, 2020
commit 8fc2c613cc66a882c464e542f4426e59434a6428
170 changes: 88 additions & 82 deletions monitoring/api/v3/uptime-check-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@


# [START monitoring_uptime_check_create]
def create_uptime_check_config_get(project_name, host_name=None,
display_name=None):
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 GET 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.path = '/'
config.display_name = display_name or "New GET 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.path = "/"
config.http_check.port = 80
config.timeout.seconds = 10
config.period.seconds = 300
Expand All @@ -41,17 +41,20 @@ def create_uptime_check_config_get(project_name, host_name=None,
pprint.pprint(new_config)
return new_config

def create_uptime_check_config_post(project_name, host_name=None,
display_name=None):

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 POST 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'.encode('utf-8')
config.http_check.path = '/'
config.display_name = display_name or "New POST 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".encode("utf-8")
config.http_check.path = "/"
config.http_check.port = 80
config.timeout.seconds = 10
config.period.seconds = 300
Expand All @@ -60,21 +63,26 @@ def create_uptime_check_config_post(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]

# [START monitoring_uptime_check_update]
def update_uptime_check_config(config_name, new_display_name=None,
new_http_check_path=None):
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')
field_mask.paths.append("display_name")
config.display_name = new_display_name
if new_http_check_path:
field_mask.paths.append('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]


Expand All @@ -85,17 +93,23 @@ def list_uptime_check_configs(project_name):

for config in configs:
pprint.pprint(config)


# [END monitoring_uptime_check_list_configs]


# [START monitoring_uptime_check_list_ips]
def list_uptime_check_ips():
client = monitoring_v3.UptimeCheckServiceClient()
ips = client.list_uptime_check_ips()
print(tabulate.tabulate(
[(ip.region, ip.location, ip.ip_address) for ip in ips],
('region', 'location', 'ip_address')
))
print(
tabulate.tabulate(
[(ip.region, ip.location, ip.ip_address) for ip in ips],
("region", "location", "ip_address"),
)
)


# [END monitoring_uptime_check_list_ips]


Expand All @@ -104,6 +118,8 @@ def get_uptime_check_config(config_name):
client = monitoring_v3.UptimeCheckServiceClient()
config = client.get_uptime_check_config(config_name)
pprint.pprint(config)


# [END monitoring_uptime_check_get]


Expand All @@ -113,7 +129,9 @@ def get_uptime_check_config(config_name):
def delete_uptime_check_config(config_name):
client = monitoring_v3.UptimeCheckServiceClient()
client.delete_uptime_check_config(config_name)
print('Deleted ', config_name)
print("Deleted ", config_name)


# [END monitoring_uptime_check_delete]


Expand All @@ -130,122 +148,110 @@ def project_id():
Returns:
str -- the project name
"""
project_id = os.environ['GOOGLE_CLOUD_PROJECT']
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]

if not project_id:
raise MissingProjectIdError(
'Set the environment variable ' +
'GCLOUD_PROJECT to your Google Cloud Project Id.')
"Set the environment variable "
+ "GCLOUD_PROJECT to your Google Cloud Project Id."
)
return project_id


def project_name():
return 'projects/' + project_id()
return "projects/" + project_id()


if __name__ == '__main__':
if __name__ == "__main__":

parser = argparse.ArgumentParser(
description='Demonstrates Uptime Check API operations.')
description="Demonstrates Uptime Check API operations."
)

subparsers = parser.add_subparsers(dest='command')
subparsers = parser.add_subparsers(dest="command")

list_uptime_check_configs_parser = subparsers.add_parser(
'list-uptime-check-configs',
help=list_uptime_check_configs.__doc__
"list-uptime-check-configs", help=list_uptime_check_configs.__doc__
)

list_uptime_check_ips_parser = subparsers.add_parser(
'list-uptime-check-ips',
help=list_uptime_check_ips.__doc__
"list-uptime-check-ips", help=list_uptime_check_ips.__doc__
)

create_uptime_check_config_get_parser = subparsers.add_parser(
'create-uptime-check-get',
help=create_uptime_check_config_get.__doc__
"create-uptime-check-get", help=create_uptime_check_config_get.__doc__
)
create_uptime_check_config_get_parser.add_argument(
'-d', '--display_name',
required=False,
"-d", "--display_name", required=False,
)
create_uptime_check_config_get_parser.add_argument(
'-o', '--host_name',
required=False,
"-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-post", help=create_uptime_check_config_post.__doc__
)
create_uptime_check_config_post_parser.add_argument(
'-d', '--display_name',
required=False,
"-d", "--display_name", required=False,
)
create_uptime_check_config_post_parser.add_argument(
'-o', '--host_name',
required=False,
"-o", "--host_name", required=False,
)

get_uptime_check_config_parser = subparsers.add_parser(
'get-uptime-check-config',
help=get_uptime_check_config.__doc__
"get-uptime-check-config", help=get_uptime_check_config.__doc__
)
get_uptime_check_config_parser.add_argument(
'-m', '--name',
required=True,
"-m", "--name", required=True,
)

delete_uptime_check_config_parser = subparsers.add_parser(
'delete-uptime-check-config',
help=delete_uptime_check_config.__doc__
"delete-uptime-check-config", help=delete_uptime_check_config.__doc__
)
delete_uptime_check_config_parser.add_argument(
'-m', '--name',
required=True,
"-m", "--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", help=update_uptime_check_config.__doc__
)
update_uptime_check_config_parser.add_argument(
'-m', '--name',
required=True,
"-m", "--name", required=True,
)
update_uptime_check_config_parser.add_argument(
'-d', '--display_name',
required=False,
"-d", "--display_name", required=False,
)
update_uptime_check_config_parser.add_argument(
'-p', '--uptime_check_path',
required=False,
"-p", "--uptime_check_path", required=False,
)

args = parser.parse_args()

if args.command == 'list-uptime-check-configs':
if args.command == "list-uptime-check-configs":
list_uptime_check_configs(project_name())

elif args.command == 'list-uptime-check-ips':
elif args.command == "list-uptime-check-ips":
list_uptime_check_ips()

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 == "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':
elif args.command == "get-uptime-check-config":
get_uptime_check_config(args.name)

elif args.command == 'delete-uptime-check-config':
elif args.command == "delete-uptime-check-config":
delete_uptime_check_config(args.name)

elif args.command == 'update-uptime-check-config':
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)
update_uptime_check_config(
args.name, args.display_name, args.uptime_check_path
)
23 changes: 13 additions & 10 deletions monitoring/api/v3/uptime-check-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@


def random_name(length):
return ''.join(
[random.choice(string.ascii_lowercase) for i in range(length)])
return "".join([random.choice(string.ascii_lowercase) for i in range(length)])


class UptimeFixture:
Expand All @@ -36,20 +35,23 @@ def __init__(self):
self.project_name = snippets.project_name()

def __enter__(self):
# Create an uptime check config (GET request).
# 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.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))
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_get.name)
snippets.delete_uptime_check_config(self.config_post.name)

@pytest.fixture(scope='session')

@pytest.fixture(scope="session")
def uptime():
with UptimeFixture() as uptime:
yield uptime
Expand All @@ -64,10 +66,11 @@ def test_create_and_delete(capsys):
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)
new_uptime_check_path = "/" + random_name(10)
with UptimeFixture() as fixture:
snippets.update_uptime_check_config(
fixture.config_get.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_get.name)
out, _ = capsys.readouterr()
Expand All @@ -90,4 +93,4 @@ def test_list_uptime_check_configs(capsys, uptime):
def test_list_uptime_check_ips(capsys):
snippets.list_uptime_check_ips()
out, _ = capsys.readouterr()
assert 'Singapore' in out
assert "Singapore" in out