Skip to content

Commit 146fdd0

Browse files
authored
Updates client library, cleans up JS, fixes package.js so the tests run (#698)
* Updates client library, cleans up JS, fixes package.js so the tests run * Removes unused clause in package.json * Fixes error handling on discovery and adds missing variable declarations * Upgrades repo-tools
1 parent 9c15971 commit 146fdd0

File tree

4 files changed

+64
-59
lines changed

4 files changed

+64
-59
lines changed

iot/http_example/package.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@
66
"author": "Google Inc.",
77
"main": "cloudiot_http_example_nodejs.js",
88
"scripts": {
9-
"lint": "samples lint",
9+
"lint": "repo-tools lint",
1010
"pretest": "npm run lint",
11-
"test": "samples test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
11+
"test": "repo-tools test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
1212
},
1313
"dependencies": {
1414
"@google-cloud/pubsub": "0.13.2",
15-
"@google-cloud/nodejs-repo-tools": "1.4.17",
15+
"@google-cloud/nodejs-repo-tools": "2.2.5",
1616
"ava": "0.22.0",
1717
"jsonwebtoken": "7.4.1",
1818
"retry-request": "3.3.1",
1919
"semistandard": "^12.0.1",
2020
"uuid": "3.1.0",
2121
"yargs": "8.0.2"
2222
},
23-
"testDependencies": {
24-
},
2523
"devDependencies": {}
2624
}

iot/manager/manager.js

+48-46
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
'use strict';
1717

1818
const fs = require('fs');
19-
const google = require('googleapis');
19+
const {google} = require('googleapis');
2020

2121
const API_VERSION = 'v1';
2222
const DISCOVERY_API = 'https://cloudiot.googleapis.com/$discovery/rest';
@@ -98,13 +98,13 @@ function lookupRegistry (client, registryId, projectId, cloudRegion, cb) {
9898
name: registryName
9999
};
100100

101-
client.projects.locations.registries.get(request, (err, data) => {
101+
client.projects.locations.registries.get(request, (err, res) => {
102102
if (err) {
103103
console.log('Could not look up registry');
104104
console.log(err);
105105
} else {
106106
console.log('Looked up existing registry');
107-
console.log(data);
107+
console.log(res.data);
108108
}
109109
});
110110
// [END iot_lookup_registry]
@@ -138,7 +138,7 @@ function createRegistry (
138138
}
139139
};
140140

141-
client.projects.locations.registries.create(request, (err, data) => {
141+
client.projects.locations.registries.create(request, (err, res) => {
142142
if (err) {
143143
if (err.code === 409) {
144144
// The registry already exists - look it up instead.
@@ -149,7 +149,7 @@ function createRegistry (
149149
}
150150
} else {
151151
console.log('Successfully created registry');
152-
console.log(data);
152+
console.log(res.data);
153153
}
154154
});
155155
// [END iot_create_registry]
@@ -208,13 +208,13 @@ function createUnauthDevice (
208208
resource: {id: deviceId}
209209
};
210210

211-
client.projects.locations.registries.devices.create(request, (err, data) => {
211+
client.projects.locations.registries.devices.create(request, (err, res) => {
212212
if (err) {
213213
console.log('Could not create device');
214214
console.log(err);
215215
} else {
216216
console.log('Created device');
217-
console.log(data);
217+
console.log(res.data);
218218
}
219219
});
220220
// [END iot_create_unauth_device]
@@ -257,13 +257,13 @@ function createRsaDevice (
257257

258258
console.log(JSON.stringify(request));
259259

260-
client.projects.locations.registries.devices.create(request, (err, data) => {
260+
client.projects.locations.registries.devices.create(request, (err, res) => {
261261
if (err) {
262262
console.log('Could not create device');
263263
console.log(err);
264264
} else {
265265
console.log('Created device');
266-
console.log(data);
266+
console.log(res.data);
267267
}
268268
});
269269
// [END iot_create_rsa_device]
@@ -304,13 +304,13 @@ function createEsDevice (
304304
resource: body
305305
};
306306

307-
client.projects.locations.registries.devices.create(request, (err, data) => {
307+
client.projects.locations.registries.devices.create(request, (err, res) => {
308308
if (err) {
309309
console.log('Could not create device');
310310
console.log(err);
311311
} else {
312312
console.log('Created device');
313-
console.log(data);
313+
console.log(res.data);
314314
}
315315
});
316316
// [END iot_create_es_device]
@@ -350,13 +350,13 @@ function patchRsa256ForAuth (
350350
}
351351
};
352352

353-
client.projects.locations.registries.devices.patch(request, (err, data) => {
353+
client.projects.locations.registries.devices.patch(request, (err, res) => {
354354
if (err) {
355355
console.log('Error patching device:', deviceId);
356356
console.log(err);
357357
} else {
358358
console.log('Patched device:', deviceId);
359-
console.log(data);
359+
console.log(res.data);
360360
}
361361
});
362362
// [END iot_patch_rsa]
@@ -396,13 +396,13 @@ function patchEs256ForAuth (
396396
}
397397
};
398398

399-
client.projects.locations.registries.devices.patch(request, (err, data) => {
399+
client.projects.locations.registries.devices.patch(request, (err, res) => {
400400
if (err) {
401401
console.log('Error patching device:', deviceId);
402402
console.log(err);
403403
} else {
404404
console.log('Patched device:', deviceId);
405-
console.log(data);
405+
console.log(res.data);
406406
}
407407
});
408408
// [END iot_patch_es]
@@ -423,11 +423,12 @@ function listDevices (client, registryId, projectId, cloudRegion) {
423423
parent: registryName
424424
};
425425

426-
client.projects.locations.registries.devices.list(request, (err, data) => {
426+
client.projects.locations.registries.devices.list(request, (err, res) => {
427427
if (err) {
428428
console.log('Could not list devices');
429429
console.log(err);
430430
} else {
431+
let data = res.data;
431432
console.log('Current devices in registry:', data['devices']);
432433
}
433434
});
@@ -447,11 +448,12 @@ function listRegistries (client, projectId, cloudRegion) {
447448
parent: parentName
448449
};
449450

450-
client.projects.locations.registries.list(request, (err, data) => {
451+
client.projects.locations.registries.list(request, (err, res) => {
451452
if (err) {
452453
console.log('Could not list registries');
453454
console.log(err);
454455
} else {
456+
let data = res.data;
455457
console.log('Current registries in project:', data['deviceRegistries']);
456458
}
457459
});
@@ -479,13 +481,13 @@ function deleteDevice (
479481
name: `${registryName}/devices/${deviceId}`
480482
};
481483

482-
client.projects.locations.registries.devices.delete(request, (err, data) => {
484+
client.projects.locations.registries.devices.delete(request, (err, res) => {
483485
if (err) {
484486
console.log('Could not delete device:', deviceId);
485487
console.log(err);
486488
} else {
487489
console.log('Successfully deleted device:', deviceId);
488-
console.log(data);
490+
console.log(res.data);
489491
if (cb) {
490492
cb();
491493
}
@@ -503,13 +505,13 @@ function clearRegistry (client, registryId, projectId, cloudRegion) {
503505
};
504506

505507
const after = function () {
506-
client.projects.locations.registries.delete(requestDelete, (err, data) => {
508+
client.projects.locations.registries.delete(requestDelete, (err, res) => {
507509
if (err) {
508510
console.log('Could not delete registry');
509511
console.log(err);
510512
} else {
511513
console.log(`Successfully deleted registry ${registryName}`);
512-
console.log(data);
514+
console.log(res.data);
513515
}
514516
});
515517
};
@@ -518,11 +520,12 @@ function clearRegistry (client, registryId, projectId, cloudRegion) {
518520
parent: registryName
519521
};
520522

521-
client.projects.locations.registries.devices.list(request, (err, data) => {
523+
client.projects.locations.registries.devices.list(request, (err, res) => {
522524
if (err) {
523525
console.log('Could not list devices');
524526
console.log(err);
525527
} else {
528+
let data = res.data;
526529
console.log('Current devices in registry:', data['devices']);
527530
let devices = data['devices'];
528531
if (devices) {
@@ -569,13 +572,13 @@ function deleteRegistry (client, registryId, projectId, cloudRegion) {
569572
name: registryName
570573
};
571574

572-
client.projects.locations.registries.delete(request, (err, data) => {
575+
client.projects.locations.registries.delete(request, (err, res) => {
573576
if (err) {
574577
console.log('Could not delete registry');
575578
console.log(err);
576579
} else {
577580
console.log('Successfully deleted registry');
578-
console.log(data);
581+
console.log(res);
579582
}
580583
});
581584
// [END iot_delete_registry]
@@ -596,13 +599,13 @@ function getDevice (client, deviceId, registryId, projectId, cloudRegion) {
596599
name: `${registryName}/devices/${deviceId}`
597600
};
598601

599-
client.projects.locations.registries.devices.get(request, (err, data) => {
602+
client.projects.locations.registries.devices.get(request, (err, res) => {
600603
if (err) {
601604
console.log('Could not find device:', deviceId);
602605
console.log(err);
603606
} else {
604607
console.log('Found device:', deviceId);
605-
console.log(data);
608+
console.log(res.data);
606609
}
607610
});
608611
// [END iot_get_device]
@@ -635,7 +638,7 @@ function getDeviceState (
635638
console.log('Could not find device:', deviceId);
636639
console.log(err);
637640
} else {
638-
console.log('State:', data);
641+
console.log('State:', data.data);
639642
}
640643
});
641644
// [END iot_get_device_state]
@@ -668,7 +671,7 @@ function getDeviceConfigs (
668671
console.log('Could not find device:', deviceId);
669672
console.log(err);
670673
} else {
671-
console.log('Configs:', data);
674+
console.log('Configs:', data.data);
672675
}
673676
});
674677
// [END iot_get_device_configs]
@@ -735,7 +738,7 @@ function getRegistry (client, registryId, projectId, cloudRegion) {
735738
console.log(err);
736739
} else {
737740
console.log('Found registry:', registryId);
738-
console.log(data);
741+
console.log(data.data);
739742
}
740743
});
741744
// [END iot_get_registry]
@@ -744,23 +747,21 @@ function getRegistry (client, registryId, projectId, cloudRegion) {
744747
// Returns an authorized API client by discovering the Cloud IoT Core API with
745748
// the provided API key.
746749
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+
});
764765
});
765766
}
766767

@@ -783,6 +784,7 @@ function getIamPolicy (client, registryId, projectId, cloudRegion) {
783784
console.log('Could not find policy for: ', registryId);
784785
console.log('Trace: ', err);
785786
} else {
787+
data = data.data;
786788
console.log(`ETAG: ${data.etag}`);
787789
data.bindings = data.bindings || [];
788790
data.bindings.forEach((_binding) => {

iot/manager/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
"node": ">=4.3.2"
1414
},
1515
"scripts": {
16-
"lint": "samples lint",
16+
"lint": "repo-tools lint",
1717
"pretest": "npm run lint",
18-
"test": "samples test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
18+
"test": "repo-tools test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
1919
},
2020
"dependencies": {
2121
"@google-cloud/pubsub": "0.13.2",
22-
"googleapis": "20.1.0",
22+
"googleapis": "^32.0.0",
2323
"yargs": "8.0.2"
2424
},
2525
"devDependencies": {
26-
"@google-cloud/nodejs-repo-tools": "1.4.17",
26+
"@google-cloud/nodejs-repo-tools": "2.2.5",
2727
"ava": "0.22.0",
2828
"semistandard": "^12.0.1",
2929
"uuid": "3.1.0"

iot/mqtt_example/package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
{
2-
"name": "nodejs-docs-samples-iot-mqtt-example",
2+
"author": "Google Inc.",
33
"version": "0.0.1",
44
"description": "MQTT Example for Google Cloud IoT Core using NodeJS.",
5-
"main": "cloudiot_mqtt_example_nodejs.js",
65
"license": "Apache-2.0",
7-
"author": "Google Inc.",
6+
"main": "cloudiot_mqtt_example_nodejs.js",
7+
"name": "nodejs-docs-samples-iot-mqtt-example",
8+
"scripts": {
9+
"lint": "repo-tools lint",
10+
"pretest": "npm run lint",
11+
"test": "repo-tools test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
12+
},
813
"dependencies": {
914
"@google-cloud/pubsub": "0.16.4",
10-
"@google-cloud/nodejs-repo-tools": "2.2.1",
15+
"@google-cloud/nodejs-repo-tools": "2.2.5",
1116
"ava": "0.25.0",
1217
"jsonwebtoken": "8.2.0",
1318
"mqtt": "2.16.0",

0 commit comments

Comments
 (0)