From 9847605b253042d5ec71135f682e72fb40ab408f Mon Sep 17 00:00:00 2001 From: Gus Class Date: Thu, 16 Jan 2020 13:40:13 -0800 Subject: [PATCH 1/5] Stash to WFH --- iot/manager/manager.js | 158 +++++++++++++++++++++++++---------------- 1 file changed, 96 insertions(+), 62 deletions(-) diff --git a/iot/manager/manager.js b/iot/manager/manager.js index ae5b16274b0..4badf79ae51 100644 --- a/iot/manager/manager.js +++ b/iot/manager/manager.js @@ -857,7 +857,6 @@ const sendCommand = async ( const getRegistry = async (client, registryId, projectId, cloudRegion) => { // [START iot_get_registry] // Client retrieved in callback - // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; @@ -1095,18 +1094,22 @@ const bindDeviceToGateway = async ( // const gatewayId = 'my-gateway'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; + const iot = require('@google-cloud/iot'); - console.log(`Binding device: ${deviceId}`); - const parentName = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}`; + const iotClient = new iot.v1.DeviceManagerClient({ + // optional auth parameters. + }); + + const regPath = iotClient.registryPath(projectId, cloudRegion, registryId); const bindRequest = { - parent: parentName, + parent: regPath, deviceId: deviceId, gatewayId: gatewayId, }; try { - await client.projects.locations.registries.bindDeviceToGateway(bindRequest); + await iotClient.bindDeviceToGateway(bindRequest); console.log(`Bound ${deviceId} to`, gatewayId); } catch (err) { @@ -1130,19 +1133,23 @@ const unbindDeviceFromGateway = async ( // const gatewayId = 'my-gateway'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; + console.log(`Unbinding device: ${deviceId}`); - const parentName = `projects/${projectId}/locations/${cloudRegion}/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 unbindRequest = { - parent: parentName, + parent: regPath, deviceId: deviceId, gatewayId: gatewayId, }; try { - await client.projects.locations.registries.unbindDeviceFromGateway( - unbindRequest - ); + await iotClient.bindDeviceToGateway(unbindRequest); console.log(`Unbound ${deviceId} from`, gatewayId); } catch (err) { @@ -1159,21 +1166,33 @@ const unbindDeviceFromAllGateways = async ( registryId, deviceId ) => { - const parentName = `projects/${projectId}/locations/${cloudRegion}`; - const registryName = `${parentName}/registries/${registryId}`; - const request = { - name: `${registryName}/devices/${deviceId}`, - }; + + const iot = require('@google-cloud/iot'); + const iotClient = new iot.v1.DeviceManagerClient({ + // optional auth parameters. + }); // get information about this device let device; + + const devicePath = iotClient.devicePath( + projectId, + cloudRegion, + registryId, + deviceId + ); + try { - const {data} = await client.projects.locations.registries.devices.get( - request - ); - device = data; + const responses = await iotClient.getDevice({ + name: devicePath, + 'gatewayListOptions.associationsDeviceId': deviceId, + }); + const data = responses[0]; + + console.log('Found device:', deviceId, data); } catch (err) { - console.error('Could not get device', err); + console.error('Could not find device:', deviceId); + console.error('Trace:', err); return; } @@ -1277,34 +1296,47 @@ const listGateways = async (client, projectId, cloudRegion, registryId) => { // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; - const parentName = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}`; - const request = { - parent: parentName, - fieldMask: 'config,gatewayConfig', - }; + const iot = require('@google-cloud/iot'); + const iotClient = new iot.v1.DeviceManagerClient({ + // optional auth parameters. + }); + + // get information about this device + let device; + + const registryPath = iotClient.registryPath( + projectId, + cloudRegion, + registryId, + ); - let devices; try { - const {data} = await client.projects.locations.registries.devices.list( - request - ); - devices = data.devices; + console.log('Current gateways in registry:'); + const responses = await iotClient.listDevices({ + parent: registryPath, + fieldMask: {mask: 'config,gatewayConfig'} + }); + const devices = responses[0]; + + console.log('Devices', devices) + devices.forEach(device => { + if ( + device.gatewayConfig !== undefined && + device.gatewayConfig.gatewayType === 'GATEWAY' + ) { + console.log('----\n', device); + } else { + console.log('\t', device); + } + }); + } catch (err) { - console.error('Could not list devices', err); + console.error('Could not list gateways:'); + console.error('Trace:', err); return; } - console.log('Current gateways in registry:'); - devices.forEach(device => { - if ( - device.gatewayConfig !== undefined && - device.gatewayConfig.gatewayType === 'GATEWAY' - ) { - console.log('----\n', device); - } else { - console.log('\t', device); - } - }); + // [END iot_list_gateways] }; @@ -1321,30 +1353,32 @@ const listDevicesForGateway = async ( // const gatewayId = 'my-gateway'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; - const parentName = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}`; - const request = { - parent: parentName, - 'gatewayListOptions.associationsGatewayId': gatewayId, - }; + const iot = require('@google-cloud/iot'); + const iotClient = new iot.v1.DeviceManagerClient({ + // optional auth parameters. + }); + + const parentName = iotClient.registryPath(projectId, cloudRegion, registryId); - let devices; try { - const {data} = await client.projects.locations.registries.devices.list( - request - ); - devices = data.devices; + const responses = await iotClient.listDevices({ + parent: parentName, + gatewayListOptions: {associationsGatewayId: gatewayId} + }); + const devices = responses[0]; + + if (devices.length > 0) { + console.log('Current devices bound to gateway: ', gatewayId); + } else { + console.log('No devices bound to this gateway.'); + } + + for (let i = 0; i < devices.length; i++) { + const device = devices[i]; + console.log(`\tDevice: ${device.numId}: ${device.name}`); + } } catch (err) { console.error('Could not list devices', err); - return; - } - - console.log('Current devices bound to gateway: ', gatewayId); - if (devices && devices.length > 0) { - devices.forEach(device => { - console.log(`\tDevice: ${device.numId} : ${device.id}`); - }); - } else { - console.log('No devices bound to this gateway.'); } // [END iot_list_devices_for_gateway] }; From d33ea853a3b0178eff42f75f8bb5e3eb69342c5b Mon Sep 17 00:00:00 2001 From: Gus Class Date: Thu, 16 Jan 2020 16:21:24 -0800 Subject: [PATCH 2/5] Fix clear --- iot/manager/manager.js | 101 +++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 49 deletions(-) diff --git a/iot/manager/manager.js b/iot/manager/manager.js index 4badf79ae51..2f8f304dd03 100644 --- a/iot/manager/manager.js +++ b/iot/manager/manager.js @@ -1038,6 +1038,14 @@ const createGateway = async ( // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; // const gatewayAuthMethod = 'ASSOCIATION_ONLY'; + const iot = require('@google-cloud/iot'); + + const iotClient = new iot.v1.DeviceManagerClient({ + // optional auth parameters. + }); + + const regPath = iotClient.registryPath(projectId, cloudRegion, registryId); + const parentName = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}`; console.log('Creating gateway:', gatewayId); @@ -1055,26 +1063,26 @@ const createGateway = async ( ]; } - const createRequest = { - parent: parentName, - resource: { - id: gatewayId, - credentials: credentials, - gatewayConfig: { - gatewayType: 'GATEWAY', - gatewayAuthMethod: gatewayAuthMethod, - }, - }, + const device = { + id: gatewayId, + credentials: credentials, + gatewayConfig: { + gatewayType: 'GATEWAY', + gatewayAuthMethod: gatewayAuthMethod, + } }; - try { - const {data} = await client.projects.locations.registries.devices.create( - createRequest - ); + const request = { + parent: regPath, + device, + }; - console.log('Created device', data); + try { + 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 not create gateway', err); } // [END iot_create_gateway] }; @@ -1108,6 +1116,8 @@ const bindDeviceToGateway = async ( gatewayId: gatewayId, }; + console.log(`Binding device: ${deviceId}`); + try { await iotClient.bindDeviceToGateway(bindRequest); @@ -1149,7 +1159,7 @@ const unbindDeviceFromGateway = async ( }; try { - await iotClient.bindDeviceToGateway(unbindRequest); + await iotClient.unbindDeviceFromGateway(unbindRequest); console.log(`Unbound ${deviceId} from`, gatewayId); } catch (err) { @@ -1301,9 +1311,6 @@ const listGateways = async (client, projectId, cloudRegion, registryId) => { // optional auth parameters. }); - // get information about this device - let device; - const registryPath = iotClient.registryPath( projectId, cloudRegion, @@ -1314,29 +1321,23 @@ const listGateways = async (client, projectId, cloudRegion, registryId) => { console.log('Current gateways in registry:'); const responses = await iotClient.listDevices({ parent: registryPath, - fieldMask: {mask: 'config,gatewayConfig'} + fieldMask: {paths: ['config', 'gateway_config']} }); const devices = responses[0]; - console.log('Devices', devices) devices.forEach(device => { if ( device.gatewayConfig !== undefined && device.gatewayConfig.gatewayType === 'GATEWAY' ) { console.log('----\n', device); - } else { - console.log('\t', device); } }); - } catch (err) { console.error('Could not list gateways:'); console.error('Trace:', err); return; } - - // [END iot_list_gateways] }; @@ -1375,7 +1376,7 @@ const listDevicesForGateway = async ( for (let i = 0; i < devices.length; i++) { const device = devices[i]; - console.log(`\tDevice: ${device.numId}: ${device.name}`); + console.log(`\tDevice: ${device.numId}: ${device.id}`); } } catch (err) { console.error('Could not list devices', err); @@ -1396,30 +1397,32 @@ const listGatewaysForDevice = async ( // const deviceId = 'my-device'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; - const parentName = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}`; - const request = { - parent: parentName, - 'gatewayListOptions.associationsDeviceId': deviceId, - }; + const iot = require('@google-cloud/iot'); + const iotClient = new iot.v1.DeviceManagerClient({ + // optional auth parameters. + }); - let devices; - try { - const {data} = await client.projects.locations.registries.devices.list( - request - ); - devices = data.devices; - } catch (err) { - console.error('Could not list gateways for device', err); - return; - } + const parentName = iotClient.registryPath(projectId, cloudRegion, registryId); - console.log('Current gateways for device:', deviceId); - if (devices && devices.length > 0) { - devices.forEach(gateway => { - console.log(`\tDevice: ${gateway.numId} : ${gateway.id}`); + try { + const responses = await iotClient.listDevices({ + parent: parentName, + gatewayListOptions: {associationsDeviceId: deviceId} }); - } else { - console.log('No gateways associated with this device.'); + const devices = responses[0]; + + if (devices.length > 0) { + console.log('Current gateways for: ', deviceId); + } else { + console.log('No gateways associated with this device.'); + } + + for (let i = 0; i < devices.length; i++) { + const device = devices[i]; + console.log(`\tDevice: ${device.numId}: ${device.id}`); + } + } catch (err) { + console.error('Could not list devices', err); } // [END iot_list_gateways_for_device] }; From d1689e772e1ee9ad08a0e8b9e0cc5275a2f2809c Mon Sep 17 00:00:00 2001 From: Gus Class Date: Fri, 17 Jan 2020 13:30:18 -0800 Subject: [PATCH 3/5] Converts cleanup helpers to Cloud --- iot/manager/manager.js | 173 +++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 110 deletions(-) diff --git a/iot/manager/manager.js b/iot/manager/manager.js index 2f8f304dd03..de3d3ee3485 100644 --- a/iot/manager/manager.js +++ b/iot/manager/manager.js @@ -1069,7 +1069,7 @@ const createGateway = async ( gatewayConfig: { gatewayType: 'GATEWAY', gatewayAuthMethod: gatewayAuthMethod, - } + }, }; const request = { @@ -1170,36 +1170,25 @@ const unbindDeviceFromGateway = async ( // Unbinds the given device from all gateways const unbindDeviceFromAllGateways = async ( - client, projectId, cloudRegion, registryId, deviceId ) => { - const iot = require('@google-cloud/iot'); - const iotClient = new iot.v1.DeviceManagerClient({ - // optional auth parameters. - }); + const iotClient = new iot.v1.DeviceManagerClient({}); - // get information about this device let device; - const devicePath = iotClient.devicePath( projectId, cloudRegion, registryId, deviceId ); - try { - const responses = await iotClient.getDevice({ - name: devicePath, - 'gatewayListOptions.associationsDeviceId': deviceId, - }); - const data = responses[0]; - - console.log('Found device:', deviceId, data); + const responses = await iotClient.getDevice({name: devicePath}); + device = responses[0]; + console.log(`Found Device ${device.id}`); } catch (err) { console.error('Could not find device:', deviceId); console.error('Trace:', err); @@ -1207,96 +1196,66 @@ const unbindDeviceFromAllGateways = async ( } if (device) { - const isGateway = device.gatewayConfig.gatewayType === 'GATEWAY'; - - if (!isGateway) { - const listGatewaysForDeviceRequest = { - parent: registryName, - 'gatewayListOptions.associationsDeviceId': deviceId, - }; - - // get list of all gateways this non-gateway device is bound to - let gateways; - try { - const { - devices, - } = await client.projects.locations.registries.devices.list( - listGatewaysForDeviceRequest - ); - gateways = devices; - } catch (err) { - console.error('Could not list gateways', err); - return; - } - - if (gateways && gateways > 0) { - const promises = gateways.map(gateway => { - const unbindRequest = { - parent: registryName, - deviceId: device.id, - gatewayId: gateway.id, - }; - - // for each gateway, make the call to unbind it - return new Promise((resolve, reject) => { - try { - client.projects.locations.registries.unbindDeviceFromGateway( - unbindRequest - ); - console.log('Unbound device from gateways', gateway.id); - resolve(); - } catch (err) { - console.error('Could not unbind device', err); - reject(); - } - }); - }); - - return Promise.all(promises); + try { + const parentName = iotClient.registryPath( + projectId, + cloudRegion, + registryId + ); + const responses = await iotClient.listDevices({ + parent: parentName, + gatewayListOptions: {associationsDeviceId: deviceId}, + }); + const gateways = responses[0]; + if (gateways && gateways.length > 0) { + for (let i = 0; i < gateways.length; i++) { + const gatewayId = gateways[i].id; + unbindDeviceFromGateway( + client, + projectId, + cloudRegion, + registryId, + deviceId, + gatewayId + ); + } } + } catch (err) { + console.error('Could not list gateways', err); + return; } } }; -const unbindAllDevices = async (client, projectId, cloudRegion, registryId) => { - const parentName = `projects/${projectId}/locations/${cloudRegion}`; - const registryName = `${parentName}/registries/${registryId}`; - const request = { - parent: registryName, - }; +const unbindAllDevices = async (projectId, cloudRegion, registryId) => { + const iot = require('@google-cloud/iot'); + const iotClient = new iot.v1.DeviceManagerClient({ + // optional auth parameters. + }); - // get information about this device - let devices; - try { - const {data} = await client.projects.locations.registries.devices.list( - request - ); - devices = data.devices; - } catch (err) { - console.error('Could not list devices', err); - return; - } + const parentName = iotClient.registryPath(projectId, cloudRegion, registryId); - if (devices && devices.length > 0) { - const promises = devices.map(device => { - if (device) { - const isGateway = - device.gatewayConfig && - device.gatewayConfig.gatewayType === 'GATEWAY'; + try { + const responses = await iotClient.listDevices({parent: parentName}); + const devices = responses[0]; - if (!isGateway) { - return unbindDeviceFromAllGateways( - client, - projectId, - cloudRegion, - registryId, - device.id - ); - } - } - }); + if (devices.length > 0) { + console.log('Current devices in registry:'); + } else { + console.log('No devices in registry.'); + } - return Promise.all(promises); + for (let i = 0; i < devices.length; i++) { + const device = devices[i]; + unbindDeviceFromAllGateways( + projectId, + cloudRegion, + registryId, + device.id + ); + } + } catch (err) { + console.error('Could not list devices', err); } }; @@ -1314,14 +1273,14 @@ const listGateways = async (client, projectId, cloudRegion, registryId) => { const registryPath = iotClient.registryPath( projectId, cloudRegion, - registryId, + registryId ); try { console.log('Current gateways in registry:'); const responses = await iotClient.listDevices({ parent: registryPath, - fieldMask: {paths: ['config', 'gateway_config']} + fieldMask: {paths: ['config', 'gateway_config']}, }); const devices = responses[0]; @@ -1363,8 +1322,8 @@ const listDevicesForGateway = async ( try { const responses = await iotClient.listDevices({ - parent: parentName, - gatewayListOptions: {associationsGatewayId: gatewayId} + parent: parentName, + gatewayListOptions: {associationsGatewayId: gatewayId}, }); const devices = responses[0]; @@ -1406,8 +1365,8 @@ const listGatewaysForDevice = async ( try { const responses = await iotClient.listDevices({ - parent: parentName, - gatewayListOptions: {associationsDeviceId: deviceId} + parent: parentName, + gatewayListOptions: {associationsDeviceId: deviceId}, }); const devices = responses[0]; @@ -1900,7 +1859,6 @@ require(`yargs`) // eslint-disable-line async opts => { const client = await getClient(opts.serviceAccount); await unbindDeviceFromAllGateways( - client, opts.projectId, opts.cloudRegion, opts.registryId, @@ -1914,12 +1872,7 @@ require(`yargs`) // eslint-disable-line {}, async opts => { const client = await getClient(opts.serviceAccount); - await unbindAllDevices( - client, - opts.projectId, - opts.cloudRegion, - opts.registryId - ); + await unbindAllDevices(opts.projectId, opts.cloudRegion, opts.registryId); } ) .command( From b5592a7d5f1e731a60486d4cf8df0c9546e7f22b Mon Sep 17 00:00:00 2001 From: Gus Class Date: Fri, 17 Jan 2020 13:53:47 -0800 Subject: [PATCH 4/5] Remove duplicate get_client snippet --- iot/manager/manager.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/iot/manager/manager.js b/iot/manager/manager.js index de3d3ee3485..dbe09c4bde8 100644 --- a/iot/manager/manager.js +++ b/iot/manager/manager.js @@ -888,7 +888,6 @@ const getRegistry = async (client, registryId, projectId, cloudRegion) => { // [END iot_get_registry] }; -// [START iot_get_client] // Returns an authorized API client by discovering the Cloud IoT Core API with // the provided API key. const getClient = async serviceAccountJson => { @@ -911,7 +910,6 @@ const getClient = async serviceAccountJson => { console.error('Error during API discovery.', err); } }; -// [END iot_get_client] // Retrieves the IAM policy for a given registry. const getIamPolicy = async (client, registryId, projectId, cloudRegion) => { From 23b03d38588d2eda03c8ebdf4a9cd41c0b0ba697 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Fri, 17 Jan 2020 14:18:52 -0800 Subject: [PATCH 5/5] Fix lint --- iot/manager/manager.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/iot/manager/manager.js b/iot/manager/manager.js index dbe09c4bde8..34bf0ab7cfc 100644 --- a/iot/manager/manager.js +++ b/iot/manager/manager.js @@ -1044,7 +1044,6 @@ const createGateway = async ( const regPath = iotClient.registryPath(projectId, cloudRegion, registryId); - const parentName = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}`; console.log('Creating gateway:', gatewayId); let credentials = []; @@ -1855,7 +1854,6 @@ require(`yargs`) // eslint-disable-line `Unbinds a device from all gateways`, {}, async opts => { - const client = await getClient(opts.serviceAccount); await unbindDeviceFromAllGateways( opts.projectId, opts.cloudRegion, @@ -1869,7 +1867,6 @@ require(`yargs`) // eslint-disable-line `Unbinds all devices in a given registry. Mainly for clearing registries`, {}, async opts => { - const client = await getClient(opts.serviceAccount); await unbindAllDevices(opts.projectId, opts.cloudRegion, opts.registryId); } )