@@ -110,16 +110,15 @@ function lookupRegistry (client, registryId, projectId, cloudRegion, cb) {
110110 // [END iot_lookup_registry]
111111}
112112
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]
117116 // Client retrieved in callback
118117 // getClient(apiKey, serviceAccountJson, function(client) {...});
119118 // const cloudRegion = 'us-central1';
120119 // const projectId = 'adjective-noun-123';
121120 // const registryId = 'my-registry';
122- // const pubsubTopicId = 'my-iot-topic';
121+ // function errCb = lookupRegistry; // Lookup registry if already exists.
123122 const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
124123 const pubsubTopic = `projects/${ projectId } /topics/${ pubsubTopicId } ` ;
125124
@@ -137,7 +136,7 @@ function lookupOrCreateRegistry (client, registryId, projectId, cloudRegion,
137136 if ( err ) {
138137 if ( err . code === 409 ) {
139138 // The registry already exists - look it up instead.
140- lookupRegistry ( client , registryId , projectId , cloudRegion ) ;
139+ foundCb ( client , registryId , projectId , cloudRegion ) ;
141140 } else {
142141 console . log ( 'Could not create registry' ) ;
143142 console . log ( err ) ;
@@ -147,6 +146,23 @@ function lookupOrCreateRegistry (client, registryId, projectId, cloudRegion,
147146 console . log ( data ) ;
148147 }
149148 } ) ;
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+
150166 // [END iot_lookup_or_create_registry]
151167}
152168
@@ -189,7 +205,7 @@ function createRsaDevice (client, deviceId, registryId, projectId, cloudRegion,
189205 // Client retrieved in callback
190206 // getClient(apiKey, serviceAccountJson, function(client) {...});
191207 // const cloudRegion = 'us-central1';
192- // const deviceId = 'my-unauth -device';
208+ // const deviceId = 'my-rsa -device';
193209 // const projectId = 'adjective-noun-123';
194210 // const registryId = 'my-registry';
195211 const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
@@ -313,7 +329,7 @@ function patchEs256ForAuth (client, deviceId, registryId, esPublicKeyFile,
313329 // Client retrieved in callback
314330 // getClient(apiKey, serviceAccountJson, function(client) {...});
315331 // const cloudRegion = 'us-central1';
316- // const deviceId = 'my-rsa -device';
332+ // const deviceId = 'my-es -device';
317333 // const projectId = 'adjective-noun-123';
318334 // const registryId = 'my-registry';
319335 const parentName =
@@ -372,6 +388,30 @@ function listDevices (client, registryId, projectId, cloudRegion) {
372388 // [END iot_list_devices]
373389}
374390
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+
375415// Delete the given device from the registry.
376416function deleteDevice ( client , deviceId , registryId , projectId , cloudRegion ,
377417 cb ) {
@@ -484,6 +524,7 @@ function getDevice (client, deviceId, registryId, projectId, cloudRegion) {
484524 // Client retrieved in callback
485525 // getClient(apiKey, serviceAccountJson, function(client) {...});
486526 // const cloudRegion = 'us-central1';
527+ // const deviceId = 'my-device';
487528 // const projectId = 'adjective-noun-123';
488529 // const registryId = 'my-registry';
489530 const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
@@ -494,7 +535,7 @@ function getDevice (client, deviceId, registryId, projectId, cloudRegion) {
494535
495536 client . projects . locations . registries . devices . get ( request , ( err , data ) => {
496537 if ( err ) {
497- console . log ( 'Could not delete device:' , deviceId ) ;
538+ console . log ( 'Could not find device:' , deviceId ) ;
498539 console . log ( err ) ;
499540 } else {
500541 console . log ( 'Found device:' , deviceId ) ;
@@ -504,6 +545,32 @@ function getDevice (client, deviceId, registryId, projectId, cloudRegion) {
504545 // [END iot_get_device]
505546}
506547
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+
507574// Returns an authorized API client by discovering the Cloud IoT Core API with
508575// the provided API key.
509576function getClient ( apiKey , serviceAccountJson , cb ) {
@@ -528,7 +595,7 @@ function getClient (apiKey, serviceAccountJson, cb) {
528595}
529596
530597require ( `yargs` ) // eslint-disable-line
531- . demand ( 4 )
598+ . demand ( 1 )
532599 . options ( {
533600 apiKey : {
534601 alias : 'a' ,
@@ -666,6 +733,17 @@ require(`yargs`) // eslint-disable-line
666733 getClient ( opts . apiKey , opts . serviceAccount , cb ) ;
667734 }
668735 )
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+ )
669747 . command (
670748 `listDevices <registryId>` ,
671749 `Lists the devices in a given registry.` ,
@@ -677,6 +755,17 @@ require(`yargs`) // eslint-disable-line
677755 getClient ( opts . apiKey , opts . serviceAccount , cb ) ;
678756 }
679757 )
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+ )
680769 . command (
681770 `patchEs256 <deviceId> <registryId> <es256Path>` ,
682771 `Patches a device with ES256 authorization credentials.` ,
@@ -708,7 +797,9 @@ require(`yargs`) // eslint-disable-line
708797 . example ( `node $0 deleteDevice my-device my-registry` )
709798 . example ( `node $0 deleteRegistry my-device my-registry` )
710799 . example ( `node $0 getDevice my-device my-registry` )
800+ . example ( `node $0 getRegistry my-registry` )
711801 . example ( `node $0 listDevices my-node-registry` )
802+ . example ( `node $0 listRegistries` )
712803 . example ( `node $0 patchRsa256 my-device my-registry ../rsa_cert.pem` )
713804 . example ( `node $0 patchEs256 my-device my-registry ../ec_public.pem` )
714805 . 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