diff --git a/healthcare/datasets/README.md b/healthcare/datasets/README.md index 379e419d5c..f5c2fb81fd 100644 --- a/healthcare/datasets/README.md +++ b/healthcare/datasets/README.md @@ -21,6 +21,9 @@ Run the following command to install the library dependencies for Node.js: patchDataset.js Updates dataset details. deidentifyDataset.js Creates a new dataset containing de-identified data from the source dataset. + getDatasetIamPolicy.js Gets the IAM policy for the dataset. + setDatasetIamPolicy.js Sets the IAM policy for the dataset. + Options: --version Show version number [boolean] diff --git a/healthcare/datasets/getDatasetIamPolicy.js b/healthcare/datasets/getDatasetIamPolicy.js new file mode 100644 index 0000000000..617aaef526 --- /dev/null +++ b/healthcare/datasets/getDatasetIamPolicy.js @@ -0,0 +1,56 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId +) { + // [START healthcare_get_dataset_iam_policy] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function getDatasetIamPolicy() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + const resource_ = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}`; + const request = {resource_}; + + const dataset = await healthcare.projects.locations.datasets.getIamPolicy( + request + ); + console.log( + 'Got dataset IAM policy:', + JSON.stringify(dataset.data, null, 2) + ); + } + + getDatasetIamPolicy(); + // [END healthcare_get_dataset_iam_policy] +} + +// node getDatasetIamPolicy.js +main(...process.argv.slice(2)); diff --git a/healthcare/datasets/setDatasetIamPolicy.js b/healthcare/datasets/setDatasetIamPolicy.js new file mode 100644 index 0000000000..0d8bccb41d --- /dev/null +++ b/healthcare/datasets/setDatasetIamPolicy.js @@ -0,0 +1,72 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the `License`); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an `AS IS` BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable no-warning-comments */ + +'use strict'; + +function main( + projectId = process.env.GCLOUD_PROJECT, + cloudRegion = 'us-central1', + datasetId, + member, + role +) { + // [START healthcare_set_dataset_iam_policy] + const {google} = require('googleapis'); + const healthcare = google.healthcare('v1beta1'); + + async function setDatasetIamPolicy() { + const auth = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + google.options({auth}); + + // TODO(developer): uncomment these lines before running the sample + // const cloudRegion = 'us-central1'; + // const projectId = 'adjective-noun-123'; + // const datasetId = 'my-dataset'; + // const member = 'user:example@gmail.com'; + // const role = 'roles/healthcare.datasetViewer'; + const resource_ = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}`; + const request = { + resource_, + resource: { + policy: { + bindings: [ + { + members: member, + role: role, + }, + ], + }, + }, + }; + + const dataset = await healthcare.projects.locations.datasets.setIamPolicy( + request + ); + console.log( + 'Set dataset IAM policy:', + JSON.stringify(dataset.data, null, 2) + ); + } + + setDatasetIamPolicy(); + // [END healthcare_set_dataset_iam_policy] +} + +// node setDatasetIamPolicy.js +main(...process.argv.slice(2)); diff --git a/healthcare/datasets/system-test/datasets.test.js b/healthcare/datasets/system-test/datasets.test.js index fa03eb2023..31e3342142 100644 --- a/healthcare/datasets/system-test/datasets.test.js +++ b/healthcare/datasets/system-test/datasets.test.js @@ -85,6 +85,22 @@ it('should de-identify data in a dataset and write to a new dataset', async () = ); }); +it('should create and get a dataset IAM policy', async () => { + const localMember = 'group:dpebot@google.com'; + const localRole = 'roles/viewer'; + + let output = await tools.runAsync( + `node setDatasetIamPolicy.js ${projectId} ${cloudRegion} ${datasetId} ${localMember} ${localRole}`, + cwd + ); + assert.ok(output.includes, 'ETAG'); + + output = await tools.runAsync( + `node getDatasetIamPolicy.js ${projectId} ${cloudRegion} ${datasetId}` + ); + assert.ok(output.includes('dpebot')); +}); + it('should delete a dataset', async () => { const output = await tools.runAsync( `node deleteDataset.js ${projectId} ${cloudRegion} ${datasetId}`,