Skip to content

Commit

Permalink
Updates client library, cleans up JS, fixes package.js so the tests r…
Browse files Browse the repository at this point in the history
…un (#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
  • Loading branch information
gguuss authored Jul 31, 2018
1 parent 9c15971 commit 146fdd0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 59 deletions.
8 changes: 3 additions & 5 deletions iot/http_example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
"author": "Google Inc.",
"main": "cloudiot_http_example_nodejs.js",
"scripts": {
"lint": "samples lint",
"lint": "repo-tools lint",
"pretest": "npm run lint",
"test": "samples test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
"test": "repo-tools test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
},
"dependencies": {
"@google-cloud/pubsub": "0.13.2",
"@google-cloud/nodejs-repo-tools": "1.4.17",
"@google-cloud/nodejs-repo-tools": "2.2.5",
"ava": "0.22.0",
"jsonwebtoken": "7.4.1",
"retry-request": "3.3.1",
"semistandard": "^12.0.1",
"uuid": "3.1.0",
"yargs": "8.0.2"
},
"testDependencies": {
},
"devDependencies": {}
}
94 changes: 48 additions & 46 deletions iot/manager/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'use strict';

const fs = require('fs');
const google = require('googleapis');
const {google} = require('googleapis');

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

client.projects.locations.registries.get(request, (err, data) => {
client.projects.locations.registries.get(request, (err, res) => {
if (err) {
console.log('Could not look up registry');
console.log(err);
} else {
console.log('Looked up existing registry');
console.log(data);
console.log(res.data);
}
});
// [END iot_lookup_registry]
Expand Down Expand Up @@ -138,7 +138,7 @@ function createRegistry (
}
};

client.projects.locations.registries.create(request, (err, data) => {
client.projects.locations.registries.create(request, (err, res) => {
if (err) {
if (err.code === 409) {
// The registry already exists - look it up instead.
Expand All @@ -149,7 +149,7 @@ function createRegistry (
}
} else {
console.log('Successfully created registry');
console.log(data);
console.log(res.data);
}
});
// [END iot_create_registry]
Expand Down Expand Up @@ -208,13 +208,13 @@ function createUnauthDevice (
resource: {id: deviceId}
};

client.projects.locations.registries.devices.create(request, (err, data) => {
client.projects.locations.registries.devices.create(request, (err, res) => {
if (err) {
console.log('Could not create device');
console.log(err);
} else {
console.log('Created device');
console.log(data);
console.log(res.data);
}
});
// [END iot_create_unauth_device]
Expand Down Expand Up @@ -257,13 +257,13 @@ function createRsaDevice (

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

client.projects.locations.registries.devices.create(request, (err, data) => {
client.projects.locations.registries.devices.create(request, (err, res) => {
if (err) {
console.log('Could not create device');
console.log(err);
} else {
console.log('Created device');
console.log(data);
console.log(res.data);
}
});
// [END iot_create_rsa_device]
Expand Down Expand Up @@ -304,13 +304,13 @@ function createEsDevice (
resource: body
};

client.projects.locations.registries.devices.create(request, (err, data) => {
client.projects.locations.registries.devices.create(request, (err, res) => {
if (err) {
console.log('Could not create device');
console.log(err);
} else {
console.log('Created device');
console.log(data);
console.log(res.data);
}
});
// [END iot_create_es_device]
Expand Down Expand Up @@ -350,13 +350,13 @@ function patchRsa256ForAuth (
}
};

client.projects.locations.registries.devices.patch(request, (err, data) => {
client.projects.locations.registries.devices.patch(request, (err, res) => {
if (err) {
console.log('Error patching device:', deviceId);
console.log(err);
} else {
console.log('Patched device:', deviceId);
console.log(data);
console.log(res.data);
}
});
// [END iot_patch_rsa]
Expand Down Expand Up @@ -396,13 +396,13 @@ function patchEs256ForAuth (
}
};

client.projects.locations.registries.devices.patch(request, (err, data) => {
client.projects.locations.registries.devices.patch(request, (err, res) => {
if (err) {
console.log('Error patching device:', deviceId);
console.log(err);
} else {
console.log('Patched device:', deviceId);
console.log(data);
console.log(res.data);
}
});
// [END iot_patch_es]
Expand All @@ -423,11 +423,12 @@ function listDevices (client, registryId, projectId, cloudRegion) {
parent: registryName
};

client.projects.locations.registries.devices.list(request, (err, data) => {
client.projects.locations.registries.devices.list(request, (err, res) => {
if (err) {
console.log('Could not list devices');
console.log(err);
} else {
let data = res.data;
console.log('Current devices in registry:', data['devices']);
}
});
Expand All @@ -447,11 +448,12 @@ function listRegistries (client, projectId, cloudRegion) {
parent: parentName
};

client.projects.locations.registries.list(request, (err, data) => {
client.projects.locations.registries.list(request, (err, res) => {
if (err) {
console.log('Could not list registries');
console.log(err);
} else {
let data = res.data;
console.log('Current registries in project:', data['deviceRegistries']);
}
});
Expand Down Expand Up @@ -479,13 +481,13 @@ function deleteDevice (
name: `${registryName}/devices/${deviceId}`
};

client.projects.locations.registries.devices.delete(request, (err, data) => {
client.projects.locations.registries.devices.delete(request, (err, res) => {
if (err) {
console.log('Could not delete device:', deviceId);
console.log(err);
} else {
console.log('Successfully deleted device:', deviceId);
console.log(data);
console.log(res.data);
if (cb) {
cb();
}
Expand All @@ -503,13 +505,13 @@ function clearRegistry (client, registryId, projectId, cloudRegion) {
};

const after = function () {
client.projects.locations.registries.delete(requestDelete, (err, data) => {
client.projects.locations.registries.delete(requestDelete, (err, res) => {
if (err) {
console.log('Could not delete registry');
console.log(err);
} else {
console.log(`Successfully deleted registry ${registryName}`);
console.log(data);
console.log(res.data);
}
});
};
Expand All @@ -518,11 +520,12 @@ function clearRegistry (client, registryId, projectId, cloudRegion) {
parent: registryName
};

client.projects.locations.registries.devices.list(request, (err, data) => {
client.projects.locations.registries.devices.list(request, (err, res) => {
if (err) {
console.log('Could not list devices');
console.log(err);
} else {
let data = res.data;
console.log('Current devices in registry:', data['devices']);
let devices = data['devices'];
if (devices) {
Expand Down Expand Up @@ -569,13 +572,13 @@ function deleteRegistry (client, registryId, projectId, cloudRegion) {
name: registryName
};

client.projects.locations.registries.delete(request, (err, data) => {
client.projects.locations.registries.delete(request, (err, res) => {
if (err) {
console.log('Could not delete registry');
console.log(err);
} else {
console.log('Successfully deleted registry');
console.log(data);
console.log(res);
}
});
// [END iot_delete_registry]
Expand All @@ -596,13 +599,13 @@ function getDevice (client, deviceId, registryId, projectId, cloudRegion) {
name: `${registryName}/devices/${deviceId}`
};

client.projects.locations.registries.devices.get(request, (err, data) => {
client.projects.locations.registries.devices.get(request, (err, res) => {
if (err) {
console.log('Could not find device:', deviceId);
console.log(err);
} else {
console.log('Found device:', deviceId);
console.log(data);
console.log(res.data);
}
});
// [END iot_get_device]
Expand Down Expand Up @@ -635,7 +638,7 @@ function getDeviceState (
console.log('Could not find device:', deviceId);
console.log(err);
} else {
console.log('State:', data);
console.log('State:', data.data);
}
});
// [END iot_get_device_state]
Expand Down Expand Up @@ -668,7 +671,7 @@ function getDeviceConfigs (
console.log('Could not find device:', deviceId);
console.log(err);
} else {
console.log('Configs:', data);
console.log('Configs:', data.data);
}
});
// [END iot_get_device_configs]
Expand Down Expand Up @@ -735,7 +738,7 @@ function getRegistry (client, registryId, projectId, cloudRegion) {
console.log(err);
} else {
console.log('Found registry:', registryId);
console.log(data);
console.log(data.data);
}
});
// [END iot_get_registry]
Expand All @@ -744,23 +747,21 @@ function getRegistry (client, registryId, projectId, cloudRegion) {
// Returns an authorized API client by discovering the Cloud IoT Core API with
// the provided API key.
function getClient (serviceAccountJson, cb) {
const serviceAccount = JSON.parse(fs.readFileSync(serviceAccountJson));
const jwtAccess = new google.auth.JWT();
jwtAccess.fromJSON(serviceAccount);
// Note that if you require additional scopes, they should be specified as a
// string, separated by spaces.
jwtAccess.scopes = 'https://www.googleapis.com/auth/cloud-platform';
// Set the default authentication to the above JWT access.
google.options({ auth: jwtAccess });

const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}`;

google.discoverAPI(discoveryUrl, {}, (err, client) => {
if (err) {
console.log('Error during API discovery', err);
return undefined;
}
cb(client);
google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/cloud-platform']
}).then(authClient => {
const discoveryUrl =
`${DISCOVERY_API}?version=${API_VERSION}`;

google.options({
auth: authClient
});

google.discoverAPI(discoveryUrl).then((client) => {
cb(client);
}).catch((err) => {
console.log('Error during API discovery.', err);
});
});
}

Expand All @@ -783,6 +784,7 @@ function getIamPolicy (client, registryId, projectId, cloudRegion) {
console.log('Could not find policy for: ', registryId);
console.log('Trace: ', err);
} else {
data = data.data;
console.log(`ETAG: ${data.etag}`);
data.bindings = data.bindings || [];
data.bindings.forEach((_binding) => {
Expand Down
8 changes: 4 additions & 4 deletions iot/manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
"node": ">=4.3.2"
},
"scripts": {
"lint": "samples lint",
"lint": "repo-tools lint",
"pretest": "npm run lint",
"test": "samples test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
"test": "repo-tools test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
},
"dependencies": {
"@google-cloud/pubsub": "0.13.2",
"googleapis": "20.1.0",
"googleapis": "^32.0.0",
"yargs": "8.0.2"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.4.17",
"@google-cloud/nodejs-repo-tools": "2.2.5",
"ava": "0.22.0",
"semistandard": "^12.0.1",
"uuid": "3.1.0"
Expand Down
13 changes: 9 additions & 4 deletions iot/mqtt_example/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{
"name": "nodejs-docs-samples-iot-mqtt-example",
"author": "Google Inc.",
"version": "0.0.1",
"description": "MQTT Example for Google Cloud IoT Core using NodeJS.",
"main": "cloudiot_mqtt_example_nodejs.js",
"license": "Apache-2.0",
"author": "Google Inc.",
"main": "cloudiot_mqtt_example_nodejs.js",
"name": "nodejs-docs-samples-iot-mqtt-example",
"scripts": {
"lint": "repo-tools lint",
"pretest": "npm run lint",
"test": "repo-tools test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
},
"dependencies": {
"@google-cloud/pubsub": "0.16.4",
"@google-cloud/nodejs-repo-tools": "2.2.1",
"@google-cloud/nodejs-repo-tools": "2.2.5",
"ava": "0.25.0",
"jsonwebtoken": "8.2.0",
"mqtt": "2.16.0",
Expand Down

0 comments on commit 146fdd0

Please sign in to comment.