@@ -52,8 +52,12 @@ def create_iot_topic(topic_name):
52
52
topic = pubsub_client .topic (topic_name )
53
53
policy = topic .get_iam_policy ()
54
54
publishers = policy .get ('roles/pubsub.publisher' , [])
55
- publishers .add (policy .service_account (
56
- 'cloud-iot@system.gserviceaccount.com' ))
55
+ if hasattr (publishers , "append" ):
56
+ publishers .append (policy .service_account (
57
+ 'cloud-iot@system.gserviceaccount.com' ))
58
+ else :
59
+ publishers .add (policy .service_account (
60
+ 'cloud-iot@system.gserviceaccount.com' ))
57
61
policy ['roles/pubsub.publisher' ] = publishers
58
62
topic .set_iam_policy (policy )
59
63
@@ -64,7 +68,6 @@ def get_client(service_account_json, api_key):
64
68
"""Returns an authorized API client by discovering the IoT API using the
65
69
provided API key and creating a service object using the service account
66
70
credentials JSON."""
67
- # [START authorize]
68
71
api_scopes = ['https://www.googleapis.com/auth/cloud-platform' ]
69
72
api_version = 'v1beta1'
70
73
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
@@ -82,15 +85,13 @@ def get_client(service_account_json, api_key):
82
85
api_version ,
83
86
discoveryServiceUrl = discovery_url ,
84
87
credentials = scoped_credentials )
85
- # [END authorize]
86
88
87
89
88
90
def create_rs256_device (
89
91
service_account_json , api_key , project_id , cloud_region , registry_id ,
90
92
device_id , certificate_file ):
91
93
"""Create a new device with the given id, using RS256 for
92
94
authentication."""
93
- # [START create_rs256_device]
94
95
registry_name = 'projects/{}/locations/{}/registries/{}' .format (
95
96
project_id , cloud_region , registry_id )
96
97
@@ -111,15 +112,13 @@ def create_rs256_device(
111
112
112
113
devices = client .projects ().locations ().registries ().devices ()
113
114
return devices .create (parent = registry_name , body = device_template ).execute ()
114
- # [END create_rs256_device]
115
115
116
116
117
117
def create_es256_device (
118
118
service_account_json , api_key , project_id , cloud_region , registry_id ,
119
119
device_id , public_key_file ):
120
120
"""Create a new device with the given id, using ES256 for
121
121
authentication."""
122
- # [START create_rs256_device]
123
122
registry_name = 'projects/{}/locations/{}/registries/{}' .format (
124
123
project_id , cloud_region , registry_id )
125
124
@@ -140,14 +139,12 @@ def create_es256_device(
140
139
141
140
devices = client .projects ().locations ().registries ().devices ()
142
141
return devices .create (parent = registry_name , body = device_template ).execute ()
143
- # [END create_rs256_device]
144
142
145
143
146
144
def create_unauth_device (
147
145
service_account_json , api_key , project_id , cloud_region , registry_id ,
148
146
device_id ):
149
147
"""Create a new device without authentication."""
150
- # [START create_noauth_device]
151
148
registry_name = 'projects/{}/locations/{}/registries/{}' .format (
152
149
project_id , cloud_region , registry_id )
153
150
@@ -158,14 +155,12 @@ def create_unauth_device(
158
155
159
156
devices = client .projects ().locations ().registries ().devices ()
160
157
return devices .create (parent = registry_name , body = device_template ).execute ()
161
- # [END create_noauth_device]
162
158
163
159
164
160
def delete_device (
165
161
service_account_json , api_key , project_id , cloud_region , registry_id ,
166
162
device_id ):
167
163
"""Delete the device with the given id."""
168
- # [START delete_device]
169
164
print ('Delete device' )
170
165
client = get_client (service_account_json , api_key )
171
166
registry_name = 'projects/{}/locations/{}/registries/{}' .format (
@@ -175,28 +170,24 @@ def delete_device(
175
170
176
171
devices = client .projects ().locations ().registries ().devices ()
177
172
return devices .delete (name = device_name ).execute ()
178
- # [END delete_device]
179
173
180
174
181
175
def delete_registry (
182
176
service_account_json , api_key , project_id , cloud_region , registry_id ):
183
177
"""Deletes the specified registry."""
184
- # [START delete_registry]
185
178
print ('Delete registry' )
186
179
client = get_client (service_account_json , api_key )
187
180
registry_name = 'projects/{}/locations/{}/registries/{}' .format (
188
181
project_id , cloud_region , registry_id )
189
182
190
183
registries = client .projects ().locations ().registries ()
191
184
return registries .delete (name = registry_name ).execute ()
192
- # [END delete_registry]
193
185
194
186
195
187
def get_device (
196
188
service_account_json , api_key , project_id , cloud_region , registry_id ,
197
189
device_id ):
198
190
"""Retrieve the device with the given id."""
199
- # [START delete_device]
200
191
print ('Getting device' )
201
192
client = get_client (service_account_json , api_key )
202
193
registry_name = 'projects/{}/locations/{}/registries/{}' .format (
@@ -223,13 +214,11 @@ def get_device(
223
214
'cloudUpdateTime' )))
224
215
225
216
return device
226
- # [END delete_device]
227
217
228
218
229
219
def list_devices (
230
220
service_account_json , api_key , project_id , cloud_region , registry_id ):
231
221
"""List all devices in the registry."""
232
- # [START list_devices]
233
222
print ('Listing devices' )
234
223
registry_path = 'projects/{}/locations/{}/registries/{}' .format (
235
224
project_id , cloud_region , registry_id )
@@ -243,14 +232,30 @@ def list_devices(
243
232
device .get ('id' )))
244
233
245
234
return devices
246
- # [list_devices]
247
235
248
236
249
- def open_registry (
237
+ def list_registries (service_account_json , api_key , project_id , cloud_region ):
238
+ """List all registries in the project."""
239
+ print ('Listing Registries' )
240
+ registry_path = 'projects/{}/locations/{}' .format (
241
+ project_id , cloud_region )
242
+ client = get_client (service_account_json , api_key )
243
+ registries = client .projects ().locations ().registries ().list (
244
+ parent = registry_path ).execute ().get ('deviceRegistries' , [])
245
+
246
+ for registry in registries :
247
+ print ('id: {}\n \t name: {}' .format (
248
+ registry .get ('id' ),
249
+ registry .get ('name' )))
250
+
251
+ return registries
252
+
253
+
254
+ def create_registry (
250
255
service_account_json , api_key , project_id , cloud_region , pubsub_topic ,
251
256
registry_id ):
252
- """Gets or creates a device registry."""
253
- print ( 'Creating registry' )
257
+ """ Creates a registry and returns the result. Returns an empty result if
258
+ the registry already exists."""
254
259
client = get_client (service_account_json , api_key )
255
260
registry_parent = 'projects/{}/locations/{}' .format (
256
261
project_id ,
@@ -266,19 +271,45 @@ def open_registry(
266
271
267
272
try :
268
273
response = request .execute ()
269
- print ('Created registry' , registry_id )
270
- print (response )
271
- except HttpError as e :
272
- if e .resp .status == 409 :
273
- # Device registry already exists
274
- print (
275
- 'Registry' , registry_id ,
276
- 'already exists - looking it up instead.' )
277
- topic_name = '{}/registries/{}' .format (
278
- registry_parent , registry_id )
279
- request = client .projects ().locations ().registries (
280
- ).get (name = topic_name )
281
- request .execute ()
274
+ print ('Created registry' )
275
+ return response
276
+ except HttpError :
277
+ return ""
278
+
279
+
280
+ def get_registry (
281
+ service_account_json , api_key , project_id , cloud_region , registry_id ):
282
+ """ Retrieves a device registry."""
283
+ client = get_client (service_account_json , api_key )
284
+ registry_parent = 'projects/{}/locations/{}' .format (
285
+ project_id ,
286
+ cloud_region )
287
+ topic_name = '{}/registries/{}' .format (registry_parent , registry_id )
288
+ request = client .projects ().locations ().registries ().get (name = topic_name )
289
+ return request .execute ()
290
+
291
+
292
+ def open_registry (
293
+ service_account_json , api_key , project_id , cloud_region , pubsub_topic ,
294
+ registry_id ):
295
+ """Gets or creates a device registry."""
296
+ print ('Creating registry' )
297
+
298
+ response = create_registry (
299
+ service_account_json , api_key , project_id , cloud_region ,
300
+ pubsub_topic , registry_id )
301
+
302
+ if (response is "" ):
303
+ # Device registry already exists
304
+ print (
305
+ 'Registry {} already exists - looking it up instead.' .format (
306
+ registry_id ))
307
+ response = get_registry (
308
+ service_account_json , api_key , project_id , cloud_region ,
309
+ registry_id )
310
+
311
+ print ('Registry {} opened: ' .format (response .get ('name' )))
312
+ print (response )
282
313
283
314
284
315
def patch_es256_auth (
@@ -394,7 +425,9 @@ def parse_command_line_args():
394
425
command .add_parser ('delete-device' , help = delete_device .__doc__ )
395
426
command .add_parser ('delete-registry' , help = delete_registry .__doc__ )
396
427
command .add_parser ('get' , help = get_device .__doc__ )
428
+ command .add_parser ('get-registry' , help = get_device .__doc__ )
397
429
command .add_parser ('list' , help = list_devices .__doc__ )
430
+ command .add_parser ('list-registries' , help = list_registries .__doc__ )
398
431
command .add_parser ('patch-es256' , help = patch_es256_auth .__doc__ )
399
432
command .add_parser ('patch-rs256' , help = patch_rsa256_auth .__doc__ )
400
433
@@ -448,6 +481,16 @@ def run_command(args):
448
481
args .service_account_json , args .api_key , args .project_id ,
449
482
args .cloud_region , args .registry_id )
450
483
484
+ elif args .command == 'get-registry' :
485
+ print (get_registry (
486
+ args .service_account_json , args .api_key , args .project_id ,
487
+ args .cloud_region , args .registry_id ))
488
+
489
+ elif args .command == 'list-registries' :
490
+ list_registries (
491
+ args .service_account_json , args .api_key , args .project_id ,
492
+ args .cloud_region )
493
+
451
494
elif args .command == 'patch-es256' :
452
495
if (args .ec_public_key_file is None ):
453
496
sys .exit ('Error: specify --ec_public_key_file' )
0 commit comments