23
23
24
24
25
25
# [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 ):
28
27
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 = "/"
35
35
config .http_check .port = 80
36
36
config .timeout .seconds = 10
37
37
config .period .seconds = 300
@@ -40,22 +40,49 @@ def create_uptime_check_config(project_name, host_name=None,
40
40
new_config = client .create_uptime_check_config (project_name , config )
41
41
pprint .pprint (new_config )
42
42
return new_config
43
- # [END monitoring_uptime_check_create]
44
43
45
44
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
+
46
70
# [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
+ ):
49
74
client = monitoring_v3 .UptimeCheckServiceClient ()
50
75
config = client .get_uptime_check_config (config_name )
51
76
field_mask = monitoring_v3 .types .FieldMask ()
52
77
if new_display_name :
53
- field_mask .paths .append (' display_name' )
78
+ field_mask .paths .append (" display_name" )
54
79
config .display_name = new_display_name
55
80
if new_http_check_path :
56
- field_mask .paths .append (' http_check.path' )
81
+ field_mask .paths .append (" http_check.path" )
57
82
config .http_check .path = new_http_check_path
58
83
client .update_uptime_check_config (config , field_mask )
84
+
85
+
59
86
# [END monitoring_uptime_check_update]
60
87
61
88
@@ -66,17 +93,23 @@ def list_uptime_check_configs(project_name):
66
93
67
94
for config in configs :
68
95
pprint .pprint (config )
96
+
97
+
69
98
# [END monitoring_uptime_check_list_configs]
70
99
71
100
72
101
# [START monitoring_uptime_check_list_ips]
73
102
def list_uptime_check_ips ():
74
103
client = monitoring_v3 .UptimeCheckServiceClient ()
75
104
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
+
80
113
# [END monitoring_uptime_check_list_ips]
81
114
82
115
@@ -85,14 +118,20 @@ def get_uptime_check_config(config_name):
85
118
client = monitoring_v3 .UptimeCheckServiceClient ()
86
119
config = client .get_uptime_check_config (config_name )
87
120
pprint .pprint (config )
121
+
122
+
88
123
# [END monitoring_uptime_check_get]
89
124
90
125
91
126
# [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.
92
129
def delete_uptime_check_config (config_name ):
93
130
client = monitoring_v3 .UptimeCheckServiceClient ()
94
131
client .delete_uptime_check_config (config_name )
95
- print ('Deleted ' , config_name )
132
+ print ("Deleted " , config_name )
133
+
134
+
96
135
# [END monitoring_uptime_check_delete]
97
136
98
137
@@ -109,106 +148,110 @@ def project_id():
109
148
Returns:
110
149
str -- the project name
111
150
"""
112
- project_id = os .environ [' GOOGLE_CLOUD_PROJECT' ]
151
+ project_id = os .environ [" GOOGLE_CLOUD_PROJECT" ]
113
152
114
153
if not project_id :
115
154
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
+ )
118
158
return project_id
119
159
120
160
121
161
def project_name ():
122
- return ' projects/' + project_id ()
162
+ return " projects/" + project_id ()
123
163
124
164
125
- if __name__ == ' __main__' :
165
+ if __name__ == " __main__" :
126
166
127
167
parser = argparse .ArgumentParser (
128
- description = 'Demonstrates Uptime Check API operations.' )
168
+ description = "Demonstrates Uptime Check API operations."
169
+ )
129
170
130
- subparsers = parser .add_subparsers (dest = ' command' )
171
+ subparsers = parser .add_subparsers (dest = " command" )
131
172
132
173
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__
135
175
)
136
176
137
177
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 ,
140
189
)
141
190
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__
145
193
)
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 ,
149
196
)
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 ,
153
199
)
154
200
155
201
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__
158
203
)
159
204
get_uptime_check_config_parser .add_argument (
160
- '-m' , '--name' ,
161
- required = True ,
205
+ "-m" , "--name" , required = True ,
162
206
)
163
207
164
208
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__
167
210
)
168
211
delete_uptime_check_config_parser .add_argument (
169
- '-m' , '--name' ,
170
- required = True ,
212
+ "-m" , "--name" , required = True ,
171
213
)
172
214
173
215
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__
176
217
)
177
218
update_uptime_check_config_parser .add_argument (
178
- '-m' , '--name' ,
179
- required = True ,
219
+ "-m" , "--name" , required = True ,
180
220
)
181
221
update_uptime_check_config_parser .add_argument (
182
- '-d' , '--display_name' ,
183
- required = False ,
222
+ "-d" , "--display_name" , required = False ,
184
223
)
185
224
update_uptime_check_config_parser .add_argument (
186
- '-p' , '--uptime_check_path' ,
187
- required = False ,
225
+ "-p" , "--uptime_check_path" , required = False ,
188
226
)
189
227
190
228
args = parser .parse_args ()
191
229
192
- if args .command == ' list-uptime-check-configs' :
230
+ if args .command == " list-uptime-check-configs" :
193
231
list_uptime_check_configs (project_name ())
194
232
195
- elif args .command == ' list-uptime-check-ips' :
233
+ elif args .command == " list-uptime-check-ips" :
196
234
list_uptime_check_ips ()
197
235
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
+ )
201
244
202
- elif args .command == ' get-uptime-check-config' :
245
+ elif args .command == " get-uptime-check-config" :
203
246
get_uptime_check_config (args .name )
204
247
205
- elif args .command == ' delete-uptime-check-config' :
248
+ elif args .command == " delete-uptime-check-config" :
206
249
delete_uptime_check_config (args .name )
207
250
208
- elif args .command == ' update-uptime-check-config' :
251
+ elif args .command == " update-uptime-check-config" :
209
252
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." )
212
254
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
+ )
0 commit comments