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]
73102def 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.
92129def 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
121161def 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+ )
0 commit comments