@@ -110,16 +110,15 @@ function lookupRegistry (client, registryId, projectId, cloudRegion, cb) {
110
110
// [END iot_lookup_registry]
111
111
}
112
112
113
- // Create a new registry, or look up an existing one if it doesn't exist.
114
- function lookupOrCreateRegistry ( client , registryId , projectId , cloudRegion ,
115
- pubsubTopicId ) {
116
- // [START iot_lookup_or_create_registry]
113
+ function createRegistry ( client , registryId , projectId , cloudRegion ,
114
+ pubsubTopicId , foundCb ) {
115
+ // [START iot_create_registry]
117
116
// Client retrieved in callback
118
117
// getClient(apiKey, serviceAccountJson, function(client) {...});
119
118
// const cloudRegion = 'us-central1';
120
119
// const projectId = 'adjective-noun-123';
121
120
// const registryId = 'my-registry';
122
- // const pubsubTopicId = 'my-iot-topic';
121
+ // function errCb = lookupRegistry; // Lookup registry if already exists.
123
122
const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
124
123
const pubsubTopic = `projects/${ projectId } /topics/${ pubsubTopicId } ` ;
125
124
@@ -137,7 +136,7 @@ function lookupOrCreateRegistry (client, registryId, projectId, cloudRegion,
137
136
if ( err ) {
138
137
if ( err . code === 409 ) {
139
138
// The registry already exists - look it up instead.
140
- lookupRegistry ( client , registryId , projectId , cloudRegion ) ;
139
+ foundCb ( client , registryId , projectId , cloudRegion ) ;
141
140
} else {
142
141
console . log ( 'Could not create registry' ) ;
143
142
console . log ( err ) ;
@@ -147,6 +146,23 @@ function lookupOrCreateRegistry (client, registryId, projectId, cloudRegion,
147
146
console . log ( data ) ;
148
147
}
149
148
} ) ;
149
+ // [END iot_create_registry]
150
+ }
151
+
152
+ // Create a new registry, or look up an existing one if it doesn't exist.
153
+ function lookupOrCreateRegistry ( client , registryId , projectId , cloudRegion ,
154
+ pubsubTopicId ) {
155
+ // [START iot_lookup_or_create_registry]
156
+ // Client retrieved in callback
157
+ // getClient(apiKey, serviceAccountJson, function(client) {...});
158
+ // const cloudRegion = 'us-central1';
159
+ // const projectId = 'adjective-noun-123';
160
+ // const registryId = 'my-registry';
161
+ // const pubsubTopicId = 'my-iot-topic';
162
+
163
+ createRegistry ( client , registryId , projectId , cloudRegion , pubsubTopicId ,
164
+ lookupRegistry ) ;
165
+
150
166
// [END iot_lookup_or_create_registry]
151
167
}
152
168
@@ -189,7 +205,7 @@ function createRsaDevice (client, deviceId, registryId, projectId, cloudRegion,
189
205
// Client retrieved in callback
190
206
// getClient(apiKey, serviceAccountJson, function(client) {...});
191
207
// const cloudRegion = 'us-central1';
192
- // const deviceId = 'my-unauth -device';
208
+ // const deviceId = 'my-rsa -device';
193
209
// const projectId = 'adjective-noun-123';
194
210
// const registryId = 'my-registry';
195
211
const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
@@ -313,7 +329,7 @@ function patchEs256ForAuth (client, deviceId, registryId, esPublicKeyFile,
313
329
// Client retrieved in callback
314
330
// getClient(apiKey, serviceAccountJson, function(client) {...});
315
331
// const cloudRegion = 'us-central1';
316
- // const deviceId = 'my-rsa -device';
332
+ // const deviceId = 'my-es -device';
317
333
// const projectId = 'adjective-noun-123';
318
334
// const registryId = 'my-registry';
319
335
const parentName =
@@ -372,6 +388,30 @@ function listDevices (client, registryId, projectId, cloudRegion) {
372
388
// [END iot_list_devices]
373
389
}
374
390
391
+ // List all of the registries in the given project.
392
+ function listRegistries ( client , projectId , cloudRegion ) {
393
+ // [START iot_list_registries]
394
+ // Client retrieved in callback
395
+ // getClient(apiKey, serviceAccountJson, function(client) {...});
396
+ // const cloudRegion = 'us-central1';
397
+ // const projectId = 'adjective-noun-123';
398
+ const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
399
+
400
+ const request = {
401
+ parent : parentName
402
+ } ;
403
+
404
+ client . projects . locations . registries . list ( request , ( err , data ) => {
405
+ if ( err ) {
406
+ console . log ( 'Could not list registries' ) ;
407
+ console . log ( err ) ;
408
+ } else {
409
+ console . log ( 'Current registries in project:' , data [ 'deviceRegistries' ] ) ;
410
+ }
411
+ } ) ;
412
+ // [END iot_list_registries]
413
+ }
414
+
375
415
// Delete the given device from the registry.
376
416
function deleteDevice ( client , deviceId , registryId , projectId , cloudRegion ,
377
417
cb ) {
@@ -484,6 +524,7 @@ function getDevice (client, deviceId, registryId, projectId, cloudRegion) {
484
524
// Client retrieved in callback
485
525
// getClient(apiKey, serviceAccountJson, function(client) {...});
486
526
// const cloudRegion = 'us-central1';
527
+ // const deviceId = 'my-device';
487
528
// const projectId = 'adjective-noun-123';
488
529
// const registryId = 'my-registry';
489
530
const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
@@ -494,7 +535,7 @@ function getDevice (client, deviceId, registryId, projectId, cloudRegion) {
494
535
495
536
client . projects . locations . registries . devices . get ( request , ( err , data ) => {
496
537
if ( err ) {
497
- console . log ( 'Could not delete device:' , deviceId ) ;
538
+ console . log ( 'Could not find device:' , deviceId ) ;
498
539
console . log ( err ) ;
499
540
} else {
500
541
console . log ( 'Found device:' , deviceId ) ;
@@ -504,6 +545,32 @@ function getDevice (client, deviceId, registryId, projectId, cloudRegion) {
504
545
// [END iot_get_device]
505
546
}
506
547
548
+ // Retrieve the given device from the registry.
549
+ function getRegistry ( client , registryId , projectId , cloudRegion ) {
550
+ // [START iot_get_registry]
551
+ // Client retrieved in callback
552
+ // getClient(apiKey, serviceAccountJson, function(client) {...});
553
+ // const cloudRegion = 'us-central1';
554
+ // const projectId = 'adjective-noun-123';
555
+ // const registryId = 'my-registry';
556
+ const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
557
+ const registryName = `${ parentName } /registries/${ registryId } ` ;
558
+ const request = {
559
+ name : `${ registryName } `
560
+ } ;
561
+
562
+ client . projects . locations . registries . get ( request , ( err , data ) => {
563
+ if ( err ) {
564
+ console . log ( 'Could not find registry:' , registryId ) ;
565
+ console . log ( err ) ;
566
+ } else {
567
+ console . log ( 'Found registry:' , registryId ) ;
568
+ console . log ( data ) ;
569
+ }
570
+ } ) ;
571
+ // [END iot_get_registry]
572
+ }
573
+
507
574
// Returns an authorized API client by discovering the Cloud IoT Core API with
508
575
// the provided API key.
509
576
function getClient ( apiKey , serviceAccountJson , cb ) {
@@ -528,7 +595,7 @@ function getClient (apiKey, serviceAccountJson, cb) {
528
595
}
529
596
530
597
require ( `yargs` ) // eslint-disable-line
531
- . demand ( 4 )
598
+ . demand ( 1 )
532
599
. options ( {
533
600
apiKey : {
534
601
alias : 'a' ,
@@ -666,6 +733,17 @@ require(`yargs`) // eslint-disable-line
666
733
getClient ( opts . apiKey , opts . serviceAccount , cb ) ;
667
734
}
668
735
)
736
+ . command (
737
+ `getRegistry <registryId>` ,
738
+ `Retrieves a registry.` ,
739
+ { } ,
740
+ ( opts ) => {
741
+ const cb = function ( client ) {
742
+ getRegistry ( client , opts . registryId , opts . projectId , opts . cloudRegion ) ;
743
+ } ;
744
+ getClient ( opts . apiKey , opts . serviceAccount , cb ) ;
745
+ }
746
+ )
669
747
. command (
670
748
`listDevices <registryId>` ,
671
749
`Lists the devices in a given registry.` ,
@@ -677,6 +755,17 @@ require(`yargs`) // eslint-disable-line
677
755
getClient ( opts . apiKey , opts . serviceAccount , cb ) ;
678
756
}
679
757
)
758
+ . command (
759
+ `listRegistries` ,
760
+ `Lists the registries in a given project.` ,
761
+ { } ,
762
+ ( opts ) => {
763
+ const cb = function ( client ) {
764
+ listRegistries ( client , opts . projectId , opts . cloudRegion ) ;
765
+ } ;
766
+ getClient ( opts . apiKey , opts . serviceAccount , cb ) ;
767
+ }
768
+ )
680
769
. command (
681
770
`patchEs256 <deviceId> <registryId> <es256Path>` ,
682
771
`Patches a device with ES256 authorization credentials.` ,
@@ -708,7 +797,9 @@ require(`yargs`) // eslint-disable-line
708
797
. example ( `node $0 deleteDevice my-device my-registry` )
709
798
. example ( `node $0 deleteRegistry my-device my-registry` )
710
799
. example ( `node $0 getDevice my-device my-registry` )
800
+ . example ( `node $0 getRegistry my-registry` )
711
801
. example ( `node $0 listDevices my-node-registry` )
802
+ . example ( `node $0 listRegistries` )
712
803
. example ( `node $0 patchRsa256 my-device my-registry ../rsa_cert.pem` )
713
804
. example ( `node $0 patchEs256 my-device my-registry ../ec_public.pem` )
714
805
. example ( `node $0 setupTopic my-iot-topic --service_account_json=$HOME/creds_iot.json --api_key=abc123zz --project_id=my-project-id` )
0 commit comments