Skip to content

Commit

Permalink
docs(samples): add policyTagManager samples (#330)
Browse files Browse the repository at this point in the history
* 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 <bencoe@google.com>
  • Loading branch information
steffnay and bcoe authored Feb 18, 2022
1 parent 3315da9 commit 9bc966e
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 6 deletions.
55 changes: 55 additions & 0 deletions datacatalog/snippets/policyTagManager/createPolicyTag.js
Original file line number Diff line number Diff line change
@@ -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));
56 changes: 56 additions & 0 deletions datacatalog/snippets/policyTagManager/createTaxonomy.js
Original file line number Diff line number Diff line change
@@ -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));
47 changes: 47 additions & 0 deletions datacatalog/snippets/policyTagManager/deleteTaxonomy.js
Original file line number Diff line number Diff line change
@@ -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));
47 changes: 47 additions & 0 deletions datacatalog/snippets/policyTagManager/getTaxonomy.js
Original file line number Diff line number Diff line change
@@ -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));
53 changes: 53 additions & 0 deletions datacatalog/snippets/policyTagManager/listTaxonomies.js
Original file line number Diff line number Diff line change
@@ -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));
110 changes: 104 additions & 6 deletions datacatalog/snippets/test/samples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9bc966e

Please sign in to comment.