Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud client library migration part 1: Registry management #1558

Merged
merged 12 commits into from
Dec 10, 2019
259 changes: 152 additions & 107 deletions iot/manager/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,28 @@ const createIotTopic = async topicName => {
// Lookup the registry, assuming that it exists.
const lookupRegistry = async (client, registryId, projectId, cloudRegion) => {
// [START iot_lookup_registry]
// 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,
};
const iot = require('@google-cloud/iot');

try {
const {data} = await client.projects.locations.registries.get(request);
const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

console.log('Looked up existing registry');
console.log(data);
try {
const registryName = iotClient.registryPath(
projectId,
cloudRegion,
registryId
);
try {
const responses = await iotClient.getDeviceRegistry({name: registryName});
const response = responses[0];
console.log(response);
} catch (err) {
console.error(err);
}
} catch (err) {
console.log('Could not look up registry');
console.log(err);
Expand All @@ -120,34 +126,42 @@ const createRegistry = async (
) => {
// [START iot_create_registry]
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
// function errCb = lookupRegistry; // Lookup registry if already exists.
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
const pubsubTopic = `projects/${projectId}/topics/${pubsubTopicId}`;
const iot = require('@google-cloud/iot');

// Lookup the pubsub topc
gguuss marked this conversation as resolved.
Show resolved Hide resolved
const topicPath = `projects/${projectId}/topics/${pubsubTopicId}`;

const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

const newParent = iotClient.locationPath(projectId, cloudRegion);
const deviceRegistry = {
eventNotificationConfigs: [
{
pubsubTopicName: topicPath,
},
],
id: registryId,
};
const request = {
parent: parentName,
resource: {
eventNotificationConfigs: [
{
pubsubTopicName: pubsubTopic,
},
],
id: registryId,
},
parent: newParent,
deviceRegistry: deviceRegistry,
};

try {
const {data} = await client.projects.locations.registries.create(request);
const responses = await iotClient.createDeviceRegistry(request);
const response = responses[0];

console.log('Successfully created registry');
console.log(data);
console.log(response);
} catch (err) {
console.log('Could not create registry');
gguuss marked this conversation as resolved.
Show resolved Hide resolved
console.log(err);
console.error(err);
}
// [END iot_create_registry]
};
Expand Down Expand Up @@ -466,22 +480,25 @@ const listDevices = async (client, registryId, projectId, cloudRegion) => {
// List all of the registries in the given project.
const listRegistries = async (client, projectId, cloudRegion) => {
// [START iot_list_registries]
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
const parentName = `projects/${projectId}/locations/${cloudRegion}`;
const iot = require('@google-cloud/iot');

const request = {
parent: parentName,
};
const newClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

// Iterate over all elements.
const formattedParent = newClient.locationPath(projectId, cloudRegion);

try {
const {data} = await client.projects.locations.registries.list(request);
console.log('Current registries in project:\n', data['deviceRegistries']);
const responses = await newClient.listDeviceRegistries({
parent: formattedParent,
});
const resources = responses[0];
console.log('Current registries in project:\n', resources);
} catch (err) {
console.log('Could not list registries');
console.log(err);
console.error(err);
}
// [END iot_list_registries]
};
Expand Down Expand Up @@ -577,24 +594,30 @@ const clearRegistry = async (client, registryId, projectId, cloudRegion) => {
const deleteRegistry = async (client, registryId, projectId, cloudRegion) => {
// [START iot_delete_registry]
// 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,
};

try {
const res = await client.projects.locations.registries.delete(request);
const iot = require('@google-cloud/iot');

const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

const registryName = iotClient.registryPath(
projectId,
cloudRegion,
registryId
);
try {
const responses = await iotClient.deleteDeviceRegistry({
name: registryName,
});
console.log(responses);
console.log('Successfully deleted registry');
console.log(res);
} catch (err) {
console.log('Could not delete registry');
gguuss marked this conversation as resolved.
Show resolved Hide resolved
console.log(err);
console.error(err);
gguuss marked this conversation as resolved.
Show resolved Hide resolved
}
// [END iot_delete_registry]
};
Expand Down Expand Up @@ -799,17 +822,27 @@ const getRegistry = async (client, registryId, projectId, cloudRegion) => {
// 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}`,
};
const iot = require('@google-cloud/iot');

try {
const {data} = await client.projects.locations.registries.get(request);
const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

console.log('Found registry:', registryId);
console.log(data);
try {
const registryName = iotClient.registryPath(
projectId,
cloudRegion,
registryId
);
try {
const responses = await iotClient.getDeviceRegistry({name: registryName});
const response = responses[0];

console.log('Found registry:', registryId);
console.log(response);
} catch (err) {
console.error(err);
gguuss marked this conversation as resolved.
Show resolved Hide resolved
gguuss marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (err) {
console.log('Could not find registry:', registryId);
console.log(err);
Expand Down Expand Up @@ -845,39 +878,47 @@ const getClient = async serviceAccountJson => {
// Retrieves the IAM policy for a given registry.
const getIamPolicy = async (client, registryId, projectId, cloudRegion) => {
// [START iot_get_iam_policy]
// 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 = {
resource_: `${registryName}`,
};
const iot = require('@google-cloud/iot');

const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

const formattedResource = iotClient.registryPath(
projectId,
cloudRegion,
registryId
);

let bindings, etag;
try {
const {data} = await client.projects.locations.registries.getIamPolicy(
request
);
bindings = data.bindings;
etag = data.etag;
const responses = await iotClient.getIamPolicy({
resource: formattedResource,
});
const response = responses[0];

bindings = response.bindings;
etag = response.etag;

console.log('ETAG:', etag);
bindings = bindings || [];

bindings.forEach(_binding => {
console.log(`Role: ${_binding.role}`);
_binding.members || (_binding.members = []);
_binding.members.forEach(_member => {
console.log(`\t${_member}`);
});
});
} catch (err) {
console.log('Could not find policy for: ', registryId);
console.log('Trace: ', err);
return;
}

console.log(`ETAG: ${etag}`);
bindings = bindings || [];
bindings.forEach(_binding => {
console.log(`Role: ${_binding.role}`);
_binding.members || (_binding.members = []);
_binding.members.forEach(_member => {
console.log(`\t${_member}`);
});
});
// [END iot_get_iam_policy]
};

Expand All @@ -891,50 +932,54 @@ const setIamPolicy = async (
role
) => {
// [START iot_set_iam_policy]
// Client retrieved in callback
// setClient(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 = {
resource_: `${registryName}`,
resource: {
policy: {
bindings: [
{
members: member,
role: role,
},
],

const iot = require('@google-cloud/iot');

const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});

const resource = iotClient.registryPath(projectId, cloudRegion, registryId);

const policy = {
bindings: [
{
members: [member],
role: role,
},
},
],
};

const request = {
resource: resource,
policy: policy,
};

let bindings, etag;
try {
const {data} = await client.projects.locations.registries.setIamPolicy(
request
);
bindings = data.bindings;
etag = data.etag;
const responses = await iotClient.setIamPolicy(request);
const response = responses[0];

bindings = response.bindings;
etag = response.etag;

console.log(JSON.stringify(data));
console.log('ETAG:', etag);
bindings = bindings || [];

bindings.forEach(_binding => {
console.log(`Role: ${_binding.role}`);
_binding.members || (_binding.members = []);
_binding.members.forEach(_member => {
console.log(`\t${_member}`);
});
});
} catch (err) {
console.log('Could not set policy for: ', registryId);
console.log('Trace: ', err);
gguuss marked this conversation as resolved.
Show resolved Hide resolved
}

console.log(`ETAG: ${etag}`);
bindings = bindings || [];
bindings.forEach(_binding => {
console.log(`Role: ${_binding.role}`);
_binding.members || (_binding.members = []);
_binding.members.forEach(_member => {
console.log(`\t${_member}`);
});
});
// [END iot_set_iam_policy]
};

Expand Down