Skip to content

Commit 4abfd2a

Browse files
slz250leahecole
andauthored
Create example for POST check. (#4082)
* Add create example for POST check. * Small fixes for POST check stuff. * Have create example be one tag block * Syntax fixes * Small fixes. * Ran black linter. Co-authored-by: Leah E. Cole <[email protected]>
1 parent 346db2f commit 4abfd2a

File tree

2 files changed

+129
-82
lines changed

2 files changed

+129
-82
lines changed

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

Lines changed: 109 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323

2424

2525
# [START monitoring_uptime_check_create]
26-
def create_uptime_check_config(project_name, host_name=None,
27-
display_name=None):
26+
def create_uptime_check_config_get(project_name, host_name=None, display_name=None):
2827
config = monitoring_v3.types.uptime_pb2.UptimeCheckConfig()
29-
config.display_name = display_name or 'New uptime check'
30-
config.monitored_resource.type = 'uptime_url'
31-
config.monitored_resource.labels.update(
32-
{'host': host_name or 'example.com'})
33-
config.http_check.request_method = monitoring_v3.enums.UptimeCheckConfig.HttpCheck.RequestMethod.GET
34-
config.http_check.path = '/'
28+
config.display_name = display_name or "New GET uptime check"
29+
config.monitored_resource.type = "uptime_url"
30+
config.monitored_resource.labels.update({"host": host_name or "example.com"})
31+
config.http_check.request_method = (
32+
monitoring_v3.enums.UptimeCheckConfig.HttpCheck.RequestMethod.GET
33+
)
34+
config.http_check.path = "/"
3535
config.http_check.port = 80
3636
config.timeout.seconds = 10
3737
config.period.seconds = 300
@@ -40,22 +40,49 @@ def create_uptime_check_config(project_name, host_name=None,
4040
new_config = client.create_uptime_check_config(project_name, config)
4141
pprint.pprint(new_config)
4242
return new_config
43-
# [END monitoring_uptime_check_create]
4443

4544

45+
def create_uptime_check_config_post(project_name, host_name=None, display_name=None):
46+
config = monitoring_v3.types.uptime_pb2.UptimeCheckConfig()
47+
config.display_name = display_name or "New POST uptime check"
48+
config.monitored_resource.type = "uptime_url"
49+
config.monitored_resource.labels.update({"host": host_name or "example.com"})
50+
config.http_check.request_method = (
51+
monitoring_v3.enums.UptimeCheckConfig.HttpCheck.RequestMethod.POST
52+
)
53+
config.http_check.content_type = (
54+
monitoring_v3.enums.UptimeCheckConfig.HttpCheck.ContentType.URL_ENCODED
55+
)
56+
config.http_check.body = "foo=bar".encode("utf-8")
57+
config.http_check.path = "/"
58+
config.http_check.port = 80
59+
config.timeout.seconds = 10
60+
config.period.seconds = 300
61+
62+
client = monitoring_v3.UptimeCheckServiceClient()
63+
new_config = client.create_uptime_check_config(project_name, config)
64+
pprint.pprint(new_config)
65+
return new_config
66+
67+
68+
# [END monitoring_uptime_check_create]
69+
4670
# [START monitoring_uptime_check_update]
47-
def update_uptime_check_config(config_name, new_display_name=None,
48-
new_http_check_path=None):
71+
def update_uptime_check_config(
72+
config_name, new_display_name=None, new_http_check_path=None
73+
):
4974
client = monitoring_v3.UptimeCheckServiceClient()
5075
config = client.get_uptime_check_config(config_name)
5176
field_mask = monitoring_v3.types.FieldMask()
5277
if new_display_name:
53-
field_mask.paths.append('display_name')
78+
field_mask.paths.append("display_name")
5479
config.display_name = new_display_name
5580
if new_http_check_path:
56-
field_mask.paths.append('http_check.path')
81+
field_mask.paths.append("http_check.path")
5782
config.http_check.path = new_http_check_path
5883
client.update_uptime_check_config(config, field_mask)
84+
85+
5986
# [END monitoring_uptime_check_update]
6087

6188

@@ -66,17 +93,23 @@ def list_uptime_check_configs(project_name):
6693

6794
for config in configs:
6895
pprint.pprint(config)
96+
97+
6998
# [END monitoring_uptime_check_list_configs]
7099

71100

72101
# [START monitoring_uptime_check_list_ips]
73102
def list_uptime_check_ips():
74103
client = monitoring_v3.UptimeCheckServiceClient()
75104
ips = client.list_uptime_check_ips()
76-
print(tabulate.tabulate(
77-
[(ip.region, ip.location, ip.ip_address) for ip in ips],
78-
('region', 'location', 'ip_address')
79-
))
105+
print(
106+
tabulate.tabulate(
107+
[(ip.region, ip.location, ip.ip_address) for ip in ips],
108+
("region", "location", "ip_address"),
109+
)
110+
)
111+
112+
80113
# [END monitoring_uptime_check_list_ips]
81114

82115

@@ -85,14 +118,20 @@ def get_uptime_check_config(config_name):
85118
client = monitoring_v3.UptimeCheckServiceClient()
86119
config = client.get_uptime_check_config(config_name)
87120
pprint.pprint(config)
121+
122+
88123
# [END monitoring_uptime_check_get]
89124

90125

91126
# [START monitoring_uptime_check_delete]
127+
# `config_name` is the `name` field of an UptimeCheckConfig.
128+
# See https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.uptimeCheckConfigs#UptimeCheckConfig.
92129
def delete_uptime_check_config(config_name):
93130
client = monitoring_v3.UptimeCheckServiceClient()
94131
client.delete_uptime_check_config(config_name)
95-
print('Deleted ', config_name)
132+
print("Deleted ", config_name)
133+
134+
96135
# [END monitoring_uptime_check_delete]
97136

98137

@@ -109,106 +148,110 @@ def project_id():
109148
Returns:
110149
str -- the project name
111150
"""
112-
project_id = os.environ['GOOGLE_CLOUD_PROJECT']
151+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
113152

114153
if not project_id:
115154
raise MissingProjectIdError(
116-
'Set the environment variable ' +
117-
'GCLOUD_PROJECT to your Google Cloud Project Id.')
155+
"Set the environment variable "
156+
+ "GCLOUD_PROJECT to your Google Cloud Project Id."
157+
)
118158
return project_id
119159

120160

121161
def project_name():
122-
return 'projects/' + project_id()
162+
return "projects/" + project_id()
123163

124164

125-
if __name__ == '__main__':
165+
if __name__ == "__main__":
126166

127167
parser = argparse.ArgumentParser(
128-
description='Demonstrates Uptime Check API operations.')
168+
description="Demonstrates Uptime Check API operations."
169+
)
129170

130-
subparsers = parser.add_subparsers(dest='command')
171+
subparsers = parser.add_subparsers(dest="command")
131172

132173
list_uptime_check_configs_parser = subparsers.add_parser(
133-
'list-uptime-check-configs',
134-
help=list_uptime_check_configs.__doc__
174+
"list-uptime-check-configs", help=list_uptime_check_configs.__doc__
135175
)
136176

137177
list_uptime_check_ips_parser = subparsers.add_parser(
138-
'list-uptime-check-ips',
139-
help=list_uptime_check_ips.__doc__
178+
"list-uptime-check-ips", help=list_uptime_check_ips.__doc__
179+
)
180+
181+
create_uptime_check_config_get_parser = subparsers.add_parser(
182+
"create-uptime-check-get", help=create_uptime_check_config_get.__doc__
183+
)
184+
create_uptime_check_config_get_parser.add_argument(
185+
"-d", "--display_name", required=False,
186+
)
187+
create_uptime_check_config_get_parser.add_argument(
188+
"-o", "--host_name", required=False,
140189
)
141190

142-
create_uptime_check_config_parser = subparsers.add_parser(
143-
'create-uptime-check',
144-
help=create_uptime_check_config.__doc__
191+
create_uptime_check_config_post_parser = subparsers.add_parser(
192+
"create-uptime-check-post", help=create_uptime_check_config_post.__doc__
145193
)
146-
create_uptime_check_config_parser.add_argument(
147-
'-d', '--display_name',
148-
required=False,
194+
create_uptime_check_config_post_parser.add_argument(
195+
"-d", "--display_name", required=False,
149196
)
150-
create_uptime_check_config_parser.add_argument(
151-
'-o', '--host_name',
152-
required=False,
197+
create_uptime_check_config_post_parser.add_argument(
198+
"-o", "--host_name", required=False,
153199
)
154200

155201
get_uptime_check_config_parser = subparsers.add_parser(
156-
'get-uptime-check-config',
157-
help=get_uptime_check_config.__doc__
202+
"get-uptime-check-config", help=get_uptime_check_config.__doc__
158203
)
159204
get_uptime_check_config_parser.add_argument(
160-
'-m', '--name',
161-
required=True,
205+
"-m", "--name", required=True,
162206
)
163207

164208
delete_uptime_check_config_parser = subparsers.add_parser(
165-
'delete-uptime-check-config',
166-
help=delete_uptime_check_config.__doc__
209+
"delete-uptime-check-config", help=delete_uptime_check_config.__doc__
167210
)
168211
delete_uptime_check_config_parser.add_argument(
169-
'-m', '--name',
170-
required=True,
212+
"-m", "--name", required=True,
171213
)
172214

173215
update_uptime_check_config_parser = subparsers.add_parser(
174-
'update-uptime-check-config',
175-
help=update_uptime_check_config.__doc__
216+
"update-uptime-check-config", help=update_uptime_check_config.__doc__
176217
)
177218
update_uptime_check_config_parser.add_argument(
178-
'-m', '--name',
179-
required=True,
219+
"-m", "--name", required=True,
180220
)
181221
update_uptime_check_config_parser.add_argument(
182-
'-d', '--display_name',
183-
required=False,
222+
"-d", "--display_name", required=False,
184223
)
185224
update_uptime_check_config_parser.add_argument(
186-
'-p', '--uptime_check_path',
187-
required=False,
225+
"-p", "--uptime_check_path", required=False,
188226
)
189227

190228
args = parser.parse_args()
191229

192-
if args.command == 'list-uptime-check-configs':
230+
if args.command == "list-uptime-check-configs":
193231
list_uptime_check_configs(project_name())
194232

195-
elif args.command == 'list-uptime-check-ips':
233+
elif args.command == "list-uptime-check-ips":
196234
list_uptime_check_ips()
197235

198-
elif args.command == 'create-uptime-check':
199-
create_uptime_check_config(project_name(), args.host_name,
200-
args.display_name)
236+
elif args.command == "create-uptime-check-get":
237+
create_uptime_check_config_get(
238+
project_name(), args.host_name, args.display_name
239+
)
240+
elif args.command == "create-uptime-check-post":
241+
create_uptime_check_config_post(
242+
project_name(), args.host_name, args.display_name
243+
)
201244

202-
elif args.command == 'get-uptime-check-config':
245+
elif args.command == "get-uptime-check-config":
203246
get_uptime_check_config(args.name)
204247

205-
elif args.command == 'delete-uptime-check-config':
248+
elif args.command == "delete-uptime-check-config":
206249
delete_uptime_check_config(args.name)
207250

208-
elif args.command == 'update-uptime-check-config':
251+
elif args.command == "update-uptime-check-config":
209252
if not args.display_name and not args.uptime_check_path:
210-
print('Nothing to update. Pass --display_name or '
211-
'--uptime_check_path.')
253+
print("Nothing to update. Pass --display_name or " "--uptime_check_path.")
212254
else:
213-
update_uptime_check_config(args.name, args.display_name,
214-
args.uptime_check_path)
255+
update_uptime_check_config(
256+
args.name, args.display_name, args.uptime_check_path
257+
)

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

2626

2727
def random_name(length):
28-
return ''.join(
29-
[random.choice(string.ascii_lowercase) for i in range(length)])
28+
return "".join([random.choice(string.ascii_lowercase) for i in range(length)])
3029

3130

3231
class UptimeFixture:
@@ -38,17 +37,23 @@ def __init__(self):
3837
self.project_name = snippets.project_name()
3938

4039
def __enter__(self):
41-
# Create an uptime check config.
42-
self.config = snippets.create_uptime_check_config(
43-
self.project_name, display_name=random_name(10))
40+
# Create an uptime check config (GET request).
41+
self.config_get = snippets.create_uptime_check_config_get(
42+
self.project_name, display_name=random_name(10)
43+
)
44+
# Create an uptime check config (POST request).
45+
self.config_post = snippets.create_uptime_check_config_post(
46+
self.project_name, display_name=random_name(10)
47+
)
4448
return self
4549

4650
def __exit__(self, type, value, traceback):
4751
# Delete the config.
48-
snippets.delete_uptime_check_config(self.config.name)
52+
snippets.delete_uptime_check_config(self.config_get.name)
53+
snippets.delete_uptime_check_config(self.config_post.name)
4954

5055

51-
@pytest.fixture(scope='session')
56+
@pytest.fixture(scope="session")
5257
def uptime():
5358
with UptimeFixture() as uptime:
5459
yield uptime
@@ -63,39 +68,38 @@ def test_create_and_delete(capsys):
6368
def test_update_uptime_config(capsys):
6469
# create and delete happen in uptime fixture.
6570
new_display_name = random_name(10)
66-
new_uptime_check_path = '/' + random_name(10)
71+
new_uptime_check_path = "/" + random_name(10)
6772
with UptimeFixture() as fixture:
68-
6973
# We sometimes see the permission error saying the resource
70-
# may not exist. Weirdly DeadlineExceeded instnace is raised
74+
# may not exist. Weirdly DeadlineExceeded instance is raised
7175
# in this case.
7276
@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=120)
7377
def call_sample():
7478
snippets.update_uptime_check_config(
75-
fixture.config.name, new_display_name, new_uptime_check_path)
79+
fixture.config_get.name, new_display_name, new_uptime_check_path)
7680

7781
call_sample()
7882

7983
out, _ = capsys.readouterr()
80-
snippets.get_uptime_check_config(fixture.config.name)
84+
snippets.get_uptime_check_config(fixture.config_get.name)
8185
out, _ = capsys.readouterr()
8286
assert new_display_name in out
8387
assert new_uptime_check_path in out
8488

8589

8690
def test_get_uptime_check_config(capsys, uptime):
87-
snippets.get_uptime_check_config(uptime.config.name)
91+
snippets.get_uptime_check_config(uptime.config_get.name)
8892
out, _ = capsys.readouterr()
89-
assert uptime.config.display_name in out
93+
assert uptime.config_get.display_name in out
9094

9195

9296
def test_list_uptime_check_configs(capsys, uptime):
9397
snippets.list_uptime_check_configs(uptime.project_name)
9498
out, _ = capsys.readouterr()
95-
assert uptime.config.display_name in out
99+
assert uptime.config_get.display_name in out
96100

97101

98102
def test_list_uptime_check_ips(capsys):
99103
snippets.list_uptime_check_ips()
100104
out, _ = capsys.readouterr()
101-
assert 'Singapore' in out
105+
assert "Singapore" in out

0 commit comments

Comments
 (0)