Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud client library migration part 2: Device creation / deletion #1566

Merged
merged 7 commits into from
Dec 16, 2019
136 changes: 65 additions & 71 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: 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: device,
gguuss marked this conversation as resolved.
Show resolved Hide resolved
};

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: 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,21 +329,17 @@ const createEsDevice = async (
},
],
};

const request = {
parent: registryName,
resource: body,
parent: regPath,
device: 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);
console.error('Could note create device', err);
gguuss marked this conversation as resolved.
Show resolved Hide resolved
}
// [END iot_create_es_device]
};
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