List gateways for device

List all of a device's gateways.

Code sample

Node.js

// const cloudRegion = 'us-central1';
// const deviceId = 'my-device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
const iot = require('@google-cloud/iot');
const iotClient = new iot.v1.DeviceManagerClient({
  // optional auth parameters.
});

async function listDevices() {
  // Construct request
  const parentName = iotClient.registryPath(
    projectId,
    cloudRegion,
    registryId
  );

  const [response] = await iotClient.listDevices({
    parent: parentName,
    gatewayListOptions: {associationsDeviceId: deviceId},
  });
  const devices = response;

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

listDevices();

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser