Skip to content

Commit

Permalink
Cloud client library migration part 2: Device creation / deleti… (#1566)
Browse files Browse the repository at this point in the history
* Cloud client library migration part 2: Device creation / deletion
  • Loading branch information
gguuss authored Dec 16, 2019
1 parent 6260273 commit b5283e5
Showing 1 changed file with 64 additions and 70 deletions.
134 changes: 64 additions & 70 deletions iot/manager/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,18 @@ const createDevice = async (
publicKeyFile
) => {
// [START iot_create_device]
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const deviceId = 'my-device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
const iot = require('@google-cloud/iot');

const parentName = `projects/${projectId}/locations/${cloudRegion}`;
const registryName = `${parentName}/registries/${registryId}`;
const body = {
const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
const device = {
id: deviceId,
credentials: [
{
Expand All @@ -197,17 +199,13 @@ const createDevice = async (
};

const request = {
parent: registryName,
resource: body,
parent: regPath,
device,
};

try {
const {data} = await client.projects.locations.registries.devices.create(
request
);

console.log('Created device');
console.log(data);
const responses = await iotClient.createDevice(request);
const response = responses[0];
console.log('Created device', response);
} catch (err) {
console.error('Could not create device', err);
}
Expand All @@ -224,28 +222,27 @@ const createUnauthDevice = async (
cloudRegion
) => {
// [START iot_create_unauth_device]
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const deviceId = 'my-unauth-device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
console.log('Creating device:', deviceId);
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
const registryName = `${parentName}/registries/${registryId}`;
const iot = require('@google-cloud/iot');

const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
const device = {id: deviceId};
const request = {
parent: registryName,
resource: {id: deviceId},
parent: regPath,
device,
};

try {
const {data} = await client.projects.locations.registries.devices.create(
request
);

console.log('Created device');
console.log(data);
const responses = await iotClient.createDevice(request);
const response = responses[0];
console.log('Created device', response);
} catch (err) {
console.error('Could not create device', err);
}
Expand All @@ -262,15 +259,18 @@ const createRsaDevice = async (
rsaCertificateFile
) => {
// [START iot_create_rsa_device]
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const deviceId = 'my-rsa-device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
const registryName = `${parentName}/registries/${registryId}`;
const body = {
const iot = require('@google-cloud/iot');

const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
const device = {
id: deviceId,
credentials: [
{
Expand All @@ -283,19 +283,14 @@ const createRsaDevice = async (
};

const request = {
parent: registryName,
resource: body,
parent: regPath,
device,
};

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

try {
const {data} = await client.projects.locations.registries.devices.create(
request
);

console.log('Created device');
console.log(data);
const responses = await iotClient.createDevice(request);
const response = responses[0];
console.log('Created device', response);
} catch (err) {
console.error('Could not create device', err);
}
Expand All @@ -312,15 +307,18 @@ const createEsDevice = async (
esCertificateFile
) => {
// [START iot_create_es_device]
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const deviceId = 'my-es-device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
const registryName = `${parentName}/registries/${registryId}`;
const body = {
const iot = require('@google-cloud/iot');

const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
const device = {
id: deviceId,
credentials: [
{
Expand All @@ -331,19 +329,15 @@ const createEsDevice = async (
},
],
};

const request = {
parent: registryName,
resource: body,
parent: regPath,
device,
};

try {
const {data} = await client.projects.locations.registries.devices.create(
request
);

console.log('Created device');
console.log(data);
const responses = await iotClient.createDevice(request);
const response = responses[0];
console.log('Created device', response);
} catch (err) {
console.error('Could not create device', err);
}
Expand Down Expand Up @@ -503,26 +497,26 @@ const deleteDevice = async (
cloudRegion
) => {
// [START iot_delete_device]
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
const registryName = `${parentName}/registries/${registryId}`;
const request = {
name: `${registryName}/devices/${deviceId}`,
};
const iot = require('@google-cloud/iot');

try {
const {data} = await client.projects.locations.registries.devices.delete(
request
);
const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

console.log('Successfully deleted device:', deviceId);
console.log(data);
const devPath = iotClient.devicePath(
projectId,
cloudRegion,
registryId,
deviceId
);
try {
const responses = await iotClient.deleteDevice({name: devPath});
console.log('Successfully deleted device', responses);
} catch (err) {
console.error('Could not delete device:', deviceId, err);
console.error('Could not delete device', err);
}
// [END iot_delete_device]
};
Expand Down

0 comments on commit b5283e5

Please sign in to comment.