Skip to content

Commit

Permalink
Converts cleanup helpers to Cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss committed Jan 17, 2020
1 parent d33ea85 commit d1689e7
Showing 1 changed file with 63 additions and 110 deletions.
173 changes: 63 additions & 110 deletions iot/manager/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ const createGateway = async (
gatewayConfig: {
gatewayType: 'GATEWAY',
gatewayAuthMethod: gatewayAuthMethod,
}
},
};

const request = {
Expand Down Expand Up @@ -1170,133 +1170,92 @@ 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);
return;
}

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);
}
};

Expand All @@ -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];

Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit d1689e7

Please sign in to comment.