From 9bc966e5eeee73b9b4d62257a42f0721801a9b5c Mon Sep 17 00:00:00 2001 From: Steffany Brown <30247553+steffnay@users.noreply.github.com> Date: Thu, 17 Feb 2022 19:31:46 -0800 Subject: [PATCH] docs(samples): add policyTagManager samples (#330) * docs(samples): add policyManager samples * lint * lint * lint * fix comment typo * docs(samples) add process.exitCode and fix copyright year Co-authored-by: Benjamin E. Coe --- .../policyTagManager/createPolicyTag.js | 55 +++++++++ .../policyTagManager/createTaxonomy.js | 56 +++++++++ .../policyTagManager/deleteTaxonomy.js | 47 ++++++++ .../snippets/policyTagManager/getTaxonomy.js | 47 ++++++++ .../policyTagManager/listTaxonomies.js | 53 +++++++++ datacatalog/snippets/test/samples.test.js | 110 +++++++++++++++++- 6 files changed, 362 insertions(+), 6 deletions(-) create mode 100644 datacatalog/snippets/policyTagManager/createPolicyTag.js create mode 100644 datacatalog/snippets/policyTagManager/createTaxonomy.js create mode 100644 datacatalog/snippets/policyTagManager/deleteTaxonomy.js create mode 100644 datacatalog/snippets/policyTagManager/getTaxonomy.js create mode 100644 datacatalog/snippets/policyTagManager/listTaxonomies.js diff --git a/datacatalog/snippets/policyTagManager/createPolicyTag.js b/datacatalog/snippets/policyTagManager/createPolicyTag.js new file mode 100644 index 0000000000..820a065798 --- /dev/null +++ b/datacatalog/snippets/policyTagManager/createPolicyTag.js @@ -0,0 +1,55 @@ +// Copyright 2022 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. + +'use strict'; + +function main(parent) { + // [START data_catalog_ptm_create_policytag] + // Create a policy tag resource under a given parent taxonomy. + + // Import the Google Cloud client library. + const {PolicyTagManagerClient} = require('@google-cloud/datacatalog').v1; + const policyClient = new PolicyTagManagerClient(); + + async function createPolicyTag() { + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = 'my_project'; // Google Cloud Platform project + // const location = 'us'; + // const taxonomy = 'my_existing_taxonomy'; + // const parent = `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}`; + + const request = { + parent, + policyTag: { + displayName: 'nodejs_samples_tag', + // // It optionally accepts a parent ID, which can be used to create a hierarchical + // // relationship between tags. + // parentPolicyTag: `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}/policyTags/my_existing_policy_tag` + }, + }; + + try { + const [metadata] = await policyClient.createPolicyTag(request); + console.log(`Created policy tag: ${metadata.name}`); + } catch (e) { + console.error(e); + process.exitCode = 1; + } + } + // [END data_catalog_ptm_create_policytag] + createPolicyTag(); +} +main(...process.argv.slice(2)); diff --git a/datacatalog/snippets/policyTagManager/createTaxonomy.js b/datacatalog/snippets/policyTagManager/createTaxonomy.js new file mode 100644 index 0000000000..542d0c9faf --- /dev/null +++ b/datacatalog/snippets/policyTagManager/createTaxonomy.js @@ -0,0 +1,56 @@ +// Copyright 2022 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. + +'use strict'; + +function main(projectId, location, displayName) { + // [START data_catalog_ptm_create_taxonomy] + // Import the Google Cloud client library. + const {DataCatalogClient, PolicyTagManagerClient} = + require('@google-cloud/datacatalog').v1; + const dataCatalog = new DataCatalogClient(); + const policyTagManager = new PolicyTagManagerClient(); + + async function createTaxonomy() { + // const location = 'us'; + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = 'my_project'; // Google Cloud Platform project + // const location = 'us' + // const displayName = 'my_display_name'; // Display name for new taxonomy. + + // Parent project location format is `projects/${projectId}/locations/${location}` + const parent = dataCatalog.locationPath(projectId, location); + + const request = { + parent: parent, + taxonomy: { + displayName: displayName, + activatedPolicyTypes: ['FINE_GRAINED_ACCESS_CONTROL'], + }, + }; + + try { + const [metadata] = await policyTagManager.createTaxonomy(request); + console.log(`Created taxonomy: ${metadata.name}`); + } catch (e) { + console.error(e); + process.exitCode = 1; + } + } + // [END data_catalog_ptm_create_taxonomy] + createTaxonomy(); +} +main(...process.argv.slice(2)); diff --git a/datacatalog/snippets/policyTagManager/deleteTaxonomy.js b/datacatalog/snippets/policyTagManager/deleteTaxonomy.js new file mode 100644 index 0000000000..597ca82633 --- /dev/null +++ b/datacatalog/snippets/policyTagManager/deleteTaxonomy.js @@ -0,0 +1,47 @@ +// Copyright 2022 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. + +'use strict'; + +function main(taxonomyName) { + // [START data_catalog_ptm_delete_taxonomy] + // Import the Google Cloud client library. + const {PolicyTagManagerClient} = require('@google-cloud/datacatalog').v1; + const policyTagManager = new PolicyTagManagerClient(); + + async function deleteTaxonomy() { + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = 'my_project'; // Google Cloud Platform project + // const location = 'us'; + // const taxonomy = 'my_existing_taxonomy'; + // const taxonomyName = `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}`; + + const request = { + name: taxonomyName, + }; + + try { + await policyTagManager.deleteTaxonomy(request); + console.log(`Deleted taxonomy: ${taxonomyName}`); + } catch (e) { + console.error(e); + process.exitCode = 1; + } + } + // [END data_catalog_ptm_delete_taxonomy] + deleteTaxonomy(); +} +main(...process.argv.slice(2)); diff --git a/datacatalog/snippets/policyTagManager/getTaxonomy.js b/datacatalog/snippets/policyTagManager/getTaxonomy.js new file mode 100644 index 0000000000..dbf3a59a3c --- /dev/null +++ b/datacatalog/snippets/policyTagManager/getTaxonomy.js @@ -0,0 +1,47 @@ +// Copyright 2022 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. + +'use strict'; + +function main(taxonomyName) { + // [START data_catalog_ptm_get_taxonomy] + // Import the Google Cloud client library. + const {PolicyTagManagerClient} = require('@google-cloud/datacatalog').v1; + const policyTagManager = new PolicyTagManagerClient(); + + async function getTaxonomy() { + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = 'my_project'; // Google Cloud Platform project + // const location = 'us'; + // const taxonomy = 'my_existing_taxonomy'; + // const taxonomyName = `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}`; + + const request = { + name: taxonomyName, + }; + + try { + const [taxonomy] = await policyTagManager.getTaxonomy(request); + console.log(`Retrieved taxonomy: ${taxonomy.name}`); + } catch (e) { + console.error(e); + process.exitCode = 1; + } + } + // [END data_catalog_ptm_get_taxonomy] + getTaxonomy(); +} +main(...process.argv.slice(2)); diff --git a/datacatalog/snippets/policyTagManager/listTaxonomies.js b/datacatalog/snippets/policyTagManager/listTaxonomies.js new file mode 100644 index 0000000000..fcfa23b8c9 --- /dev/null +++ b/datacatalog/snippets/policyTagManager/listTaxonomies.js @@ -0,0 +1,53 @@ +// Copyright 2022 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. + +'use strict'; + +function main(projectId, location) { + // [START data_catalog_ptm_list_taxonomies] + // Import the Google Cloud client library. + const {DataCatalogClient, PolicyTagManagerClient} = + require('@google-cloud/datacatalog').v1; + const dataCatalog = new DataCatalogClient(); + const policyTagManager = new PolicyTagManagerClient(); + + async function listTaxonomies() { + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = 'my_project'; // Google Cloud Platform project + // const location = 'us'; + + // Parent project location format is `projects/${projectId}/locations/${location}` + const parent = dataCatalog.locationPath(projectId, location); + + const request = { + parent: parent, + }; + + try { + const [taxonomies] = await policyTagManager.listTaxonomies(request); + console.log('Taxonomies:'); + taxonomies.forEach(taxonomy => { + console.log(taxonomy.name); + }); + } catch (e) { + console.error(e); + process.exitCode = 1; + } + } + // [END data_catalog_ptm_list_taxonomies] + listTaxonomies(); +} +main(...process.argv.slice(2)); diff --git a/datacatalog/snippets/test/samples.test.js b/datacatalog/snippets/test/samples.test.js index 1f8398d69e..eed570a373 100644 --- a/datacatalog/snippets/test/samples.test.js +++ b/datacatalog/snippets/test/samples.test.js @@ -18,23 +18,94 @@ const {assert} = require('chai'); const {describe, it, before} = require('mocha'); const uuid = require('uuid'); const cp = require('child_process'); -const {DataCatalogClient} = require('@google-cloud/datacatalog').v1; +const {DataCatalogClient, PolicyTagManagerClient} = + require('@google-cloud/datacatalog').v1; const datacatalog = new DataCatalogClient(); +const policyTagManager = new PolicyTagManagerClient(); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); -const GCLOUD_TESTS_PREFIX = 'nodejs_samples'; +const GCLOUD_TESTS_PREFIX = 'nodejs_samples_'; const generateUuid = () => `${GCLOUD_TESTS_PREFIX}_${uuid.v4()}`.replace(/-/gi, '_'); -describe('Samples', async () => { - const TAG_TEMPLATE_ID = `${GCLOUD_TESTS_PREFIX}_test_tag_template`; - const projectId = process.env.GCLOUD_PROJECT; - const location = 'us-central1'; +const TAG_TEMPLATE_ID = `${GCLOUD_TESTS_PREFIX}_test_tag_template`; +const projectId = process.env.GCLOUD_PROJECT; +const location = 'us-central1'; +let taxonomyName; +describe('Samples', async () => { before(async () => { // Delete stale resources from samples tests. await deleteEntryGroups(); + await deleteTaxonomies(); + + // Create taxonomy resource + const parent = datacatalog.locationPath(projectId, 'us'); + const taxRequest = { + parent, + taxonomy: { + displayName: generateUuid(), + activatedPolicyTypes: ['FINE_GRAINED_ACCESS_CONTROL'], + }, + }; + const [taxonomy] = await policyTagManager.createTaxonomy(taxRequest); + taxonomyName = taxonomy.name; + }); + + describe('policyTagManager', async () => { + it('should create a taxonomy', async () => { + const taxLocation = 'us'; + const displayName = generateUuid(); + const output = execSync( + `node policyTagManager/createTaxonomy ${projectId} ${taxLocation} ${displayName}` + ); + assert.include(output, 'Created taxonomy:'); + }); + + it('should get a taxonomy', async () => { + const output = execSync( + `node policyTagManager/getTaxonomy ${taxonomyName}` + ); + assert.include(output, `Retrieved taxonomy: ${taxonomyName}`); + }); + + it('should list taxonomies', async () => { + const taxLocation = 'us'; + const output = execSync( + `node policyTagManager/listTaxonomies ${projectId} ${taxLocation}` + ); + assert.include(output, 'Taxonomies:'); + }); + + it('should delete a taxonomy', async () => { + const output = execSync( + `node policyTagManager/deleteTaxonomy ${taxonomyName}` + ); + assert.include(output, 'Deleted taxonomy:'); + }); + + it('should create a policy tag', async () => { + const tagLocation = 'us'; + const displayName = generateUuid(); + const parent = datacatalog.locationPath(projectId, tagLocation); + + const request = { + parent: parent, + taxonomy: { + displayName: displayName, + activatedPolicyTypes: ['FINE_GRAINED_ACCESS_CONTROL'], + }, + }; + + const [taxonomy] = await policyTagManager.createTaxonomy(request); + + const output = execSync( + `node policyTagManager/createPolicyTag ${taxonomy.name}` + ); + assert.include(output, 'Created policy tag:'); + assert.include(output, taxonomy.name); + }); }); it('should create a custom entry', async () => { @@ -89,6 +160,33 @@ describe('Samples', async () => { return now.getTime() - created.getTime() >= oneDayMs; } + async function deleteTaxonomies() { + const projectId = await policyTagManager.getProjectId(); + const location = 'us'; + + const listTaxonomiesRequest = { + parent: datacatalog.locationPath(projectId, location), + }; + + let [taxonomies] = await policyTagManager.listTaxonomies( + listTaxonomiesRequest + ); + + taxonomies = taxonomies.filter(taxonomy => { + return taxonomy.displayName.includes(GCLOUD_TESTS_PREFIX); + }); + + taxonomies.forEach(async taxonomy => { + if (isResourceStale(taxonomy.taxonomyTimestamps.createTime.seconds)) { + try { + await policyTagManager.deleteTaxonomy({name: taxonomy.name}); + } catch (e) { + console.error(e); + } + } + }); + } + async function deleteEntries(entryGroupId) { const [entries] = await datacatalog.listEntries({parent: entryGroupId}); for (const entry of entries) {