Skip to content

Commit b5283e5

Browse files
authored
Cloud client library migration part 2: Device creation / deleti… (#1566)
* Cloud client library migration part 2: Device creation / deletion
1 parent 6260273 commit b5283e5

File tree

1 file changed

+64
-70
lines changed

1 file changed

+64
-70
lines changed

iot/manager/manager.js

+64-70
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,18 @@ const createDevice = async (
175175
publicKeyFile
176176
) => {
177177
// [START iot_create_device]
178-
// Client retrieved in callback
179-
// getClient(serviceAccountJson, function(client) {...});
180178
// const cloudRegion = 'us-central1';
181179
// const deviceId = 'my-device';
182180
// const projectId = 'adjective-noun-123';
183181
// const registryId = 'my-registry';
182+
const iot = require('@google-cloud/iot');
184183

185-
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
186-
const registryName = `${parentName}/registries/${registryId}`;
187-
const body = {
184+
const iotClient = new iot.v1.DeviceManagerClient({
185+
// optional auth parameters.
186+
});
187+
188+
const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
189+
const device = {
188190
id: deviceId,
189191
credentials: [
190192
{
@@ -197,17 +199,13 @@ const createDevice = async (
197199
};
198200

199201
const request = {
200-
parent: registryName,
201-
resource: body,
202+
parent: regPath,
203+
device,
202204
};
203-
204205
try {
205-
const {data} = await client.projects.locations.registries.devices.create(
206-
request
207-
);
208-
209-
console.log('Created device');
210-
console.log(data);
206+
const responses = await iotClient.createDevice(request);
207+
const response = responses[0];
208+
console.log('Created device', response);
211209
} catch (err) {
212210
console.error('Could not create device', err);
213211
}
@@ -224,28 +222,27 @@ const createUnauthDevice = async (
224222
cloudRegion
225223
) => {
226224
// [START iot_create_unauth_device]
227-
// Client retrieved in callback
228-
// getClient(serviceAccountJson, function(client) {...});
229225
// const cloudRegion = 'us-central1';
230226
// const deviceId = 'my-unauth-device';
231227
// const projectId = 'adjective-noun-123';
232228
// const registryId = 'my-registry';
233-
console.log('Creating device:', deviceId);
234-
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
235-
const registryName = `${parentName}/registries/${registryId}`;
229+
const iot = require('@google-cloud/iot');
236230

231+
const iotClient = new iot.v1.DeviceManagerClient({
232+
// optional auth parameters.
233+
});
234+
235+
const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
236+
const device = {id: deviceId};
237237
const request = {
238-
parent: registryName,
239-
resource: {id: deviceId},
238+
parent: regPath,
239+
device,
240240
};
241241

242242
try {
243-
const {data} = await client.projects.locations.registries.devices.create(
244-
request
245-
);
246-
247-
console.log('Created device');
248-
console.log(data);
243+
const responses = await iotClient.createDevice(request);
244+
const response = responses[0];
245+
console.log('Created device', response);
249246
} catch (err) {
250247
console.error('Could not create device', err);
251248
}
@@ -262,15 +259,18 @@ const createRsaDevice = async (
262259
rsaCertificateFile
263260
) => {
264261
// [START iot_create_rsa_device]
265-
// Client retrieved in callback
266-
// getClient(serviceAccountJson, function(client) {...});
267262
// const cloudRegion = 'us-central1';
268263
// const deviceId = 'my-rsa-device';
269264
// const projectId = 'adjective-noun-123';
270265
// const registryId = 'my-registry';
271-
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
272-
const registryName = `${parentName}/registries/${registryId}`;
273-
const body = {
266+
const iot = require('@google-cloud/iot');
267+
268+
const iotClient = new iot.v1.DeviceManagerClient({
269+
// optional auth parameters.
270+
});
271+
272+
const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
273+
const device = {
274274
id: deviceId,
275275
credentials: [
276276
{
@@ -283,19 +283,14 @@ const createRsaDevice = async (
283283
};
284284

285285
const request = {
286-
parent: registryName,
287-
resource: body,
286+
parent: regPath,
287+
device,
288288
};
289289

290-
console.log(JSON.stringify(request));
291-
292290
try {
293-
const {data} = await client.projects.locations.registries.devices.create(
294-
request
295-
);
296-
297-
console.log('Created device');
298-
console.log(data);
291+
const responses = await iotClient.createDevice(request);
292+
const response = responses[0];
293+
console.log('Created device', response);
299294
} catch (err) {
300295
console.error('Could not create device', err);
301296
}
@@ -312,15 +307,18 @@ const createEsDevice = async (
312307
esCertificateFile
313308
) => {
314309
// [START iot_create_es_device]
315-
// Client retrieved in callback
316-
// getClient(serviceAccountJson, function(client) {...});
317310
// const cloudRegion = 'us-central1';
318311
// const deviceId = 'my-es-device';
319312
// const projectId = 'adjective-noun-123';
320313
// const registryId = 'my-registry';
321-
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
322-
const registryName = `${parentName}/registries/${registryId}`;
323-
const body = {
314+
const iot = require('@google-cloud/iot');
315+
316+
const iotClient = new iot.v1.DeviceManagerClient({
317+
// optional auth parameters.
318+
});
319+
320+
const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
321+
const device = {
324322
id: deviceId,
325323
credentials: [
326324
{
@@ -331,19 +329,15 @@ const createEsDevice = async (
331329
},
332330
],
333331
};
334-
335332
const request = {
336-
parent: registryName,
337-
resource: body,
333+
parent: regPath,
334+
device,
338335
};
339336

340337
try {
341-
const {data} = await client.projects.locations.registries.devices.create(
342-
request
343-
);
344-
345-
console.log('Created device');
346-
console.log(data);
338+
const responses = await iotClient.createDevice(request);
339+
const response = responses[0];
340+
console.log('Created device', response);
347341
} catch (err) {
348342
console.error('Could not create device', err);
349343
}
@@ -503,26 +497,26 @@ const deleteDevice = async (
503497
cloudRegion
504498
) => {
505499
// [START iot_delete_device]
506-
// Client retrieved in callback
507-
// getClient(serviceAccountJson, function(client) {...});
508500
// const cloudRegion = 'us-central1';
509501
// const projectId = 'adjective-noun-123';
510502
// const registryId = 'my-registry';
511-
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
512-
const registryName = `${parentName}/registries/${registryId}`;
513-
const request = {
514-
name: `${registryName}/devices/${deviceId}`,
515-
};
503+
const iot = require('@google-cloud/iot');
516504

517-
try {
518-
const {data} = await client.projects.locations.registries.devices.delete(
519-
request
520-
);
505+
const iotClient = new iot.v1.DeviceManagerClient({
506+
// optional auth parameters.
507+
});
521508

522-
console.log('Successfully deleted device:', deviceId);
523-
console.log(data);
509+
const devPath = iotClient.devicePath(
510+
projectId,
511+
cloudRegion,
512+
registryId,
513+
deviceId
514+
);
515+
try {
516+
const responses = await iotClient.deleteDevice({name: devPath});
517+
console.log('Successfully deleted device', responses);
524518
} catch (err) {
525-
console.error('Could not delete device:', deviceId, err);
519+
console.error('Could not delete device', err);
526520
}
527521
// [END iot_delete_device]
528522
};

0 commit comments

Comments
 (0)