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