Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions iot/http_example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@
"uuid": "3.1.0",
"yargs": "8.0.2"
},
"testDependencies": {
},
"devDependencies": {}
}
97 changes: 51 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 {
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,24 @@ 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, err) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the second param here would be an error. That's traditionally where you would use a .catch() :)

if (err) {
console.log('Error during API discovery.', err);
} else {
cb(client);
}
});
});
}

Expand All @@ -783,6 +787,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
2 changes: 1 addition & 1 deletion iot/manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"@google-cloud/pubsub": "0.13.2",
"googleapis": "20.1.0",
"googleapis": "^32.0.0",
"yargs": "8.0.2"
},
"devDependencies": {
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": "samples lint",
"pretest": "npm run lint",
"test": "samples 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": "1.4.17",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why downgrade repo tools?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

samples command is in 1.x? This affects the packages.json syntax for test -- couldn't find one for 2.2.1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmdobry whatup?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I blame myself, I didn't really look very hard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found it, will commit with update to runner / repo tools

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"ava": "0.25.0",
"jsonwebtoken": "8.2.0",
"mqtt": "2.16.0",
Expand Down