From 8809a0325ba2e24d4ac344dabe4b02a423e2c1ef Mon Sep 17 00:00:00 2001 From: Gus Class Date: Thu, 12 Dec 2019 15:11:22 -0800 Subject: [PATCH 1/3] Cloud client library migration part 2: Device creation / deletion --- iot/manager/manager.js | 136 ++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 71 deletions(-) diff --git a/iot/manager/manager.js b/iot/manager/manager.js index 7339af5ffb..10c5d5d83d 100644 --- a/iot/manager/manager.js +++ b/iot/manager/manager.js @@ -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: [ { @@ -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); } @@ -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, }; 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); } @@ -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: [ { @@ -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); } @@ -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: [ { @@ -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); } // [END iot_create_es_device] }; @@ -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] }; From 59440621112e3c67b9846e51ac5a165b2f166c17 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 16 Dec 2019 11:31:52 -0800 Subject: [PATCH 2/3] Fix typo and use shorthand for create device calls --- iot/manager/manager.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/iot/manager/manager.js b/iot/manager/manager.js index 10c5d5d83d..d783f16386 100644 --- a/iot/manager/manager.js +++ b/iot/manager/manager.js @@ -200,7 +200,7 @@ const createDevice = async ( const request = { parent: regPath, - device: device, + device }; try { const responses = await iotClient.createDevice(request); @@ -236,7 +236,7 @@ const createUnauthDevice = async ( const device = {id: deviceId}; const request = { parent: regPath, - device: device, + device }; try { @@ -284,7 +284,7 @@ const createRsaDevice = async ( const request = { parent: regPath, - device: device, + device }; try { @@ -331,7 +331,7 @@ const createEsDevice = async ( }; const request = { parent: regPath, - device: device, + device }; try { @@ -339,7 +339,7 @@ const createEsDevice = async ( const response = responses[0]; console.log('Created device', response); } catch (err) { - console.error('Could note create device', err); + console.error('Could not create device', err); } // [END iot_create_es_device] }; From 097371ef0b7241c3ecbfb6fe2710cd9662083abb Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 16 Dec 2019 12:25:39 -0800 Subject: [PATCH 3/3] Prettier fixjsstyle --- iot/manager/manager.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/iot/manager/manager.js b/iot/manager/manager.js index d783f16386..80e33e3e05 100644 --- a/iot/manager/manager.js +++ b/iot/manager/manager.js @@ -200,7 +200,7 @@ const createDevice = async ( const request = { parent: regPath, - device + device, }; try { const responses = await iotClient.createDevice(request); @@ -236,7 +236,7 @@ const createUnauthDevice = async ( const device = {id: deviceId}; const request = { parent: regPath, - device + device, }; try { @@ -284,7 +284,7 @@ const createRsaDevice = async ( const request = { parent: regPath, - device + device, }; try { @@ -331,7 +331,7 @@ const createEsDevice = async ( }; const request = { parent: regPath, - device + device, }; try {