16
16
'use strict' ;
17
17
18
18
const fs = require ( 'fs' ) ;
19
- const google = require ( 'googleapis' ) ;
19
+ const { google} = require ( 'googleapis' ) ;
20
20
21
21
const API_VERSION = 'v1' ;
22
22
const DISCOVERY_API = 'https://cloudiot.googleapis.com/$discovery/rest' ;
@@ -98,13 +98,13 @@ function lookupRegistry (client, registryId, projectId, cloudRegion, cb) {
98
98
name : registryName
99
99
} ;
100
100
101
- client . projects . locations . registries . get ( request , ( err , data ) => {
101
+ client . projects . locations . registries . get ( request , ( err , res ) => {
102
102
if ( err ) {
103
103
console . log ( 'Could not look up registry' ) ;
104
104
console . log ( err ) ;
105
105
} else {
106
106
console . log ( 'Looked up existing registry' ) ;
107
- console . log ( data ) ;
107
+ console . log ( res . data ) ;
108
108
}
109
109
} ) ;
110
110
// [END iot_lookup_registry]
@@ -138,7 +138,7 @@ function createRegistry (
138
138
}
139
139
} ;
140
140
141
- client . projects . locations . registries . create ( request , ( err , data ) => {
141
+ client . projects . locations . registries . create ( request , ( err , res ) => {
142
142
if ( err ) {
143
143
if ( err . code === 409 ) {
144
144
// The registry already exists - look it up instead.
@@ -149,7 +149,7 @@ function createRegistry (
149
149
}
150
150
} else {
151
151
console . log ( 'Successfully created registry' ) ;
152
- console . log ( data ) ;
152
+ console . log ( res . data ) ;
153
153
}
154
154
} ) ;
155
155
// [END iot_create_registry]
@@ -208,13 +208,13 @@ function createUnauthDevice (
208
208
resource : { id : deviceId }
209
209
} ;
210
210
211
- client . projects . locations . registries . devices . create ( request , ( err , data ) => {
211
+ client . projects . locations . registries . devices . create ( request , ( err , res ) => {
212
212
if ( err ) {
213
213
console . log ( 'Could not create device' ) ;
214
214
console . log ( err ) ;
215
215
} else {
216
216
console . log ( 'Created device' ) ;
217
- console . log ( data ) ;
217
+ console . log ( res . data ) ;
218
218
}
219
219
} ) ;
220
220
// [END iot_create_unauth_device]
@@ -257,13 +257,13 @@ function createRsaDevice (
257
257
258
258
console . log ( JSON . stringify ( request ) ) ;
259
259
260
- client . projects . locations . registries . devices . create ( request , ( err , data ) => {
260
+ client . projects . locations . registries . devices . create ( request , ( err , res ) => {
261
261
if ( err ) {
262
262
console . log ( 'Could not create device' ) ;
263
263
console . log ( err ) ;
264
264
} else {
265
265
console . log ( 'Created device' ) ;
266
- console . log ( data ) ;
266
+ console . log ( res . data ) ;
267
267
}
268
268
} ) ;
269
269
// [END iot_create_rsa_device]
@@ -304,13 +304,13 @@ function createEsDevice (
304
304
resource : body
305
305
} ;
306
306
307
- client . projects . locations . registries . devices . create ( request , ( err , data ) => {
307
+ client . projects . locations . registries . devices . create ( request , ( err , res ) => {
308
308
if ( err ) {
309
309
console . log ( 'Could not create device' ) ;
310
310
console . log ( err ) ;
311
311
} else {
312
312
console . log ( 'Created device' ) ;
313
- console . log ( data ) ;
313
+ console . log ( res . data ) ;
314
314
}
315
315
} ) ;
316
316
// [END iot_create_es_device]
@@ -350,13 +350,13 @@ function patchRsa256ForAuth (
350
350
}
351
351
} ;
352
352
353
- client . projects . locations . registries . devices . patch ( request , ( err , data ) => {
353
+ client . projects . locations . registries . devices . patch ( request , ( err , res ) => {
354
354
if ( err ) {
355
355
console . log ( 'Error patching device:' , deviceId ) ;
356
356
console . log ( err ) ;
357
357
} else {
358
358
console . log ( 'Patched device:' , deviceId ) ;
359
- console . log ( data ) ;
359
+ console . log ( res . data ) ;
360
360
}
361
361
} ) ;
362
362
// [END iot_patch_rsa]
@@ -396,13 +396,13 @@ function patchEs256ForAuth (
396
396
}
397
397
} ;
398
398
399
- client . projects . locations . registries . devices . patch ( request , ( err , data ) => {
399
+ client . projects . locations . registries . devices . patch ( request , ( err , res ) => {
400
400
if ( err ) {
401
401
console . log ( 'Error patching device:' , deviceId ) ;
402
402
console . log ( err ) ;
403
403
} else {
404
404
console . log ( 'Patched device:' , deviceId ) ;
405
- console . log ( data ) ;
405
+ console . log ( res . data ) ;
406
406
}
407
407
} ) ;
408
408
// [END iot_patch_es]
@@ -423,11 +423,12 @@ function listDevices (client, registryId, projectId, cloudRegion) {
423
423
parent : registryName
424
424
} ;
425
425
426
- client . projects . locations . registries . devices . list ( request , ( err , data ) => {
426
+ client . projects . locations . registries . devices . list ( request , ( err , res ) => {
427
427
if ( err ) {
428
428
console . log ( 'Could not list devices' ) ;
429
429
console . log ( err ) ;
430
430
} else {
431
+ let data = res . data ;
431
432
console . log ( 'Current devices in registry:' , data [ 'devices' ] ) ;
432
433
}
433
434
} ) ;
@@ -447,11 +448,12 @@ function listRegistries (client, projectId, cloudRegion) {
447
448
parent : parentName
448
449
} ;
449
450
450
- client . projects . locations . registries . list ( request , ( err , data ) => {
451
+ client . projects . locations . registries . list ( request , ( err , res ) => {
451
452
if ( err ) {
452
453
console . log ( 'Could not list registries' ) ;
453
454
console . log ( err ) ;
454
455
} else {
456
+ let data = res . data ;
455
457
console . log ( 'Current registries in project:' , data [ 'deviceRegistries' ] ) ;
456
458
}
457
459
} ) ;
@@ -479,13 +481,13 @@ function deleteDevice (
479
481
name : `${ registryName } /devices/${ deviceId } `
480
482
} ;
481
483
482
- client . projects . locations . registries . devices . delete ( request , ( err , data ) => {
484
+ client . projects . locations . registries . devices . delete ( request , ( err , res ) => {
483
485
if ( err ) {
484
486
console . log ( 'Could not delete device:' , deviceId ) ;
485
487
console . log ( err ) ;
486
488
} else {
487
489
console . log ( 'Successfully deleted device:' , deviceId ) ;
488
- console . log ( data ) ;
490
+ console . log ( res . data ) ;
489
491
if ( cb ) {
490
492
cb ( ) ;
491
493
}
@@ -503,13 +505,13 @@ function clearRegistry (client, registryId, projectId, cloudRegion) {
503
505
} ;
504
506
505
507
const after = function ( ) {
506
- client . projects . locations . registries . delete ( requestDelete , ( err , data ) => {
508
+ client . projects . locations . registries . delete ( requestDelete , ( err , res ) => {
507
509
if ( err ) {
508
510
console . log ( 'Could not delete registry' ) ;
509
511
console . log ( err ) ;
510
512
} else {
511
513
console . log ( `Successfully deleted registry ${ registryName } ` ) ;
512
- console . log ( data ) ;
514
+ console . log ( res . data ) ;
513
515
}
514
516
} ) ;
515
517
} ;
@@ -518,11 +520,12 @@ function clearRegistry (client, registryId, projectId, cloudRegion) {
518
520
parent : registryName
519
521
} ;
520
522
521
- client . projects . locations . registries . devices . list ( request , ( err , data ) => {
523
+ client . projects . locations . registries . devices . list ( request , ( err , res ) => {
522
524
if ( err ) {
523
525
console . log ( 'Could not list devices' ) ;
524
526
console . log ( err ) ;
525
527
} else {
528
+ let data = res . data ;
526
529
console . log ( 'Current devices in registry:' , data [ 'devices' ] ) ;
527
530
let devices = data [ 'devices' ] ;
528
531
if ( devices ) {
@@ -569,13 +572,13 @@ function deleteRegistry (client, registryId, projectId, cloudRegion) {
569
572
name : registryName
570
573
} ;
571
574
572
- client . projects . locations . registries . delete ( request , ( err , data ) => {
575
+ client . projects . locations . registries . delete ( request , ( err , res ) => {
573
576
if ( err ) {
574
577
console . log ( 'Could not delete registry' ) ;
575
578
console . log ( err ) ;
576
579
} else {
577
580
console . log ( 'Successfully deleted registry' ) ;
578
- console . log ( data ) ;
581
+ console . log ( res ) ;
579
582
}
580
583
} ) ;
581
584
// [END iot_delete_registry]
@@ -596,13 +599,13 @@ function getDevice (client, deviceId, registryId, projectId, cloudRegion) {
596
599
name : `${ registryName } /devices/${ deviceId } `
597
600
} ;
598
601
599
- client . projects . locations . registries . devices . get ( request , ( err , data ) => {
602
+ client . projects . locations . registries . devices . get ( request , ( err , res ) => {
600
603
if ( err ) {
601
604
console . log ( 'Could not find device:' , deviceId ) ;
602
605
console . log ( err ) ;
603
606
} else {
604
607
console . log ( 'Found device:' , deviceId ) ;
605
- console . log ( data ) ;
608
+ console . log ( res . data ) ;
606
609
}
607
610
} ) ;
608
611
// [END iot_get_device]
@@ -635,7 +638,7 @@ function getDeviceState (
635
638
console . log ( 'Could not find device:' , deviceId ) ;
636
639
console . log ( err ) ;
637
640
} else {
638
- console . log ( 'State:' , data ) ;
641
+ console . log ( 'State:' , data . data ) ;
639
642
}
640
643
} ) ;
641
644
// [END iot_get_device_state]
@@ -668,7 +671,7 @@ function getDeviceConfigs (
668
671
console . log ( 'Could not find device:' , deviceId ) ;
669
672
console . log ( err ) ;
670
673
} else {
671
- console . log ( 'Configs:' , data ) ;
674
+ console . log ( 'Configs:' , data . data ) ;
672
675
}
673
676
} ) ;
674
677
// [END iot_get_device_configs]
@@ -735,7 +738,7 @@ function getRegistry (client, registryId, projectId, cloudRegion) {
735
738
console . log ( err ) ;
736
739
} else {
737
740
console . log ( 'Found registry:' , registryId ) ;
738
- console . log ( data ) ;
741
+ console . log ( data . data ) ;
739
742
}
740
743
} ) ;
741
744
// [END iot_get_registry]
@@ -744,23 +747,21 @@ function getRegistry (client, registryId, projectId, cloudRegion) {
744
747
// Returns an authorized API client by discovering the Cloud IoT Core API with
745
748
// the provided API key.
746
749
function getClient ( serviceAccountJson , cb ) {
747
- const serviceAccount = JSON . parse ( fs . readFileSync ( serviceAccountJson ) ) ;
748
- const jwtAccess = new google . auth . JWT ( ) ;
749
- jwtAccess . fromJSON ( serviceAccount ) ;
750
- // Note that if you require additional scopes, they should be specified as a
751
- // string, separated by spaces.
752
- jwtAccess . scopes = 'https://www.googleapis.com/auth/cloud-platform' ;
753
- // Set the default authentication to the above JWT access.
754
- google . options ( { auth : jwtAccess } ) ;
755
-
756
- const discoveryUrl = `${ DISCOVERY_API } ?version=${ API_VERSION } ` ;
757
-
758
- google . discoverAPI ( discoveryUrl , { } , ( err , client ) => {
759
- if ( err ) {
760
- console . log ( 'Error during API discovery' , err ) ;
761
- return undefined ;
762
- }
763
- cb ( client ) ;
750
+ google . auth . getClient ( {
751
+ scopes : [ 'https://www.googleapis.com/auth/cloud-platform' ]
752
+ } ) . then ( authClient => {
753
+ const discoveryUrl =
754
+ `${ DISCOVERY_API } ?version=${ API_VERSION } ` ;
755
+
756
+ google . options ( {
757
+ auth : authClient
758
+ } ) ;
759
+
760
+ google . discoverAPI ( discoveryUrl ) . then ( ( client ) => {
761
+ cb ( client ) ;
762
+ } ) . catch ( ( err ) => {
763
+ console . log ( 'Error during API discovery.' , err ) ;
764
+ } ) ;
764
765
} ) ;
765
766
}
766
767
@@ -783,6 +784,7 @@ function getIamPolicy (client, registryId, projectId, cloudRegion) {
783
784
console . log ( 'Could not find policy for: ' , registryId ) ;
784
785
console . log ( 'Trace: ' , err ) ;
785
786
} else {
787
+ data = data . data ;
786
788
console . log ( `ETAG: ${ data . etag } ` ) ;
787
789
data . bindings = data . bindings || [ ] ;
788
790
data . bindings . forEach ( ( _binding ) => {
0 commit comments