Skip to content

Commit 9bc966e

Browse files
steffnaybcoe
andauthored
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 <[email protected]>
1 parent 3315da9 commit 9bc966e

File tree

6 files changed

+362
-6
lines changed

6 files changed

+362
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(parent) {
18+
// [START data_catalog_ptm_create_policytag]
19+
// Create a policy tag resource under a given parent taxonomy.
20+
21+
// Import the Google Cloud client library.
22+
const {PolicyTagManagerClient} = require('@google-cloud/datacatalog').v1;
23+
const policyClient = new PolicyTagManagerClient();
24+
25+
async function createPolicyTag() {
26+
/**
27+
* TODO(developer): Uncomment the following lines before running the sample.
28+
*/
29+
// const projectId = 'my_project'; // Google Cloud Platform project
30+
// const location = 'us';
31+
// const taxonomy = 'my_existing_taxonomy';
32+
// const parent = `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}`;
33+
34+
const request = {
35+
parent,
36+
policyTag: {
37+
displayName: 'nodejs_samples_tag',
38+
// // It optionally accepts a parent ID, which can be used to create a hierarchical
39+
// // relationship between tags.
40+
// parentPolicyTag: `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}/policyTags/my_existing_policy_tag`
41+
},
42+
};
43+
44+
try {
45+
const [metadata] = await policyClient.createPolicyTag(request);
46+
console.log(`Created policy tag: ${metadata.name}`);
47+
} catch (e) {
48+
console.error(e);
49+
process.exitCode = 1;
50+
}
51+
}
52+
// [END data_catalog_ptm_create_policytag]
53+
createPolicyTag();
54+
}
55+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(projectId, location, displayName) {
18+
// [START data_catalog_ptm_create_taxonomy]
19+
// Import the Google Cloud client library.
20+
const {DataCatalogClient, PolicyTagManagerClient} =
21+
require('@google-cloud/datacatalog').v1;
22+
const dataCatalog = new DataCatalogClient();
23+
const policyTagManager = new PolicyTagManagerClient();
24+
25+
async function createTaxonomy() {
26+
// const location = 'us';
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// const projectId = 'my_project'; // Google Cloud Platform project
31+
// const location = 'us'
32+
// const displayName = 'my_display_name'; // Display name for new taxonomy.
33+
34+
// Parent project location format is `projects/${projectId}/locations/${location}`
35+
const parent = dataCatalog.locationPath(projectId, location);
36+
37+
const request = {
38+
parent: parent,
39+
taxonomy: {
40+
displayName: displayName,
41+
activatedPolicyTypes: ['FINE_GRAINED_ACCESS_CONTROL'],
42+
},
43+
};
44+
45+
try {
46+
const [metadata] = await policyTagManager.createTaxonomy(request);
47+
console.log(`Created taxonomy: ${metadata.name}`);
48+
} catch (e) {
49+
console.error(e);
50+
process.exitCode = 1;
51+
}
52+
}
53+
// [END data_catalog_ptm_create_taxonomy]
54+
createTaxonomy();
55+
}
56+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(taxonomyName) {
18+
// [START data_catalog_ptm_delete_taxonomy]
19+
// Import the Google Cloud client library.
20+
const {PolicyTagManagerClient} = require('@google-cloud/datacatalog').v1;
21+
const policyTagManager = new PolicyTagManagerClient();
22+
23+
async function deleteTaxonomy() {
24+
/**
25+
* TODO(developer): Uncomment the following line before running the sample.
26+
*/
27+
// const projectId = 'my_project'; // Google Cloud Platform project
28+
// const location = 'us';
29+
// const taxonomy = 'my_existing_taxonomy';
30+
// const taxonomyName = `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}`;
31+
32+
const request = {
33+
name: taxonomyName,
34+
};
35+
36+
try {
37+
await policyTagManager.deleteTaxonomy(request);
38+
console.log(`Deleted taxonomy: ${taxonomyName}`);
39+
} catch (e) {
40+
console.error(e);
41+
process.exitCode = 1;
42+
}
43+
}
44+
// [END data_catalog_ptm_delete_taxonomy]
45+
deleteTaxonomy();
46+
}
47+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(taxonomyName) {
18+
// [START data_catalog_ptm_get_taxonomy]
19+
// Import the Google Cloud client library.
20+
const {PolicyTagManagerClient} = require('@google-cloud/datacatalog').v1;
21+
const policyTagManager = new PolicyTagManagerClient();
22+
23+
async function getTaxonomy() {
24+
/**
25+
* TODO(developer): Uncomment the following line before running the sample.
26+
*/
27+
// const projectId = 'my_project'; // Google Cloud Platform project
28+
// const location = 'us';
29+
// const taxonomy = 'my_existing_taxonomy';
30+
// const taxonomyName = `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}`;
31+
32+
const request = {
33+
name: taxonomyName,
34+
};
35+
36+
try {
37+
const [taxonomy] = await policyTagManager.getTaxonomy(request);
38+
console.log(`Retrieved taxonomy: ${taxonomy.name}`);
39+
} catch (e) {
40+
console.error(e);
41+
process.exitCode = 1;
42+
}
43+
}
44+
// [END data_catalog_ptm_get_taxonomy]
45+
getTaxonomy();
46+
}
47+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(projectId, location) {
18+
// [START data_catalog_ptm_list_taxonomies]
19+
// Import the Google Cloud client library.
20+
const {DataCatalogClient, PolicyTagManagerClient} =
21+
require('@google-cloud/datacatalog').v1;
22+
const dataCatalog = new DataCatalogClient();
23+
const policyTagManager = new PolicyTagManagerClient();
24+
25+
async function listTaxonomies() {
26+
/**
27+
* TODO(developer): Uncomment the following lines before running the sample.
28+
*/
29+
// const projectId = 'my_project'; // Google Cloud Platform project
30+
// const location = 'us';
31+
32+
// Parent project location format is `projects/${projectId}/locations/${location}`
33+
const parent = dataCatalog.locationPath(projectId, location);
34+
35+
const request = {
36+
parent: parent,
37+
};
38+
39+
try {
40+
const [taxonomies] = await policyTagManager.listTaxonomies(request);
41+
console.log('Taxonomies:');
42+
taxonomies.forEach(taxonomy => {
43+
console.log(taxonomy.name);
44+
});
45+
} catch (e) {
46+
console.error(e);
47+
process.exitCode = 1;
48+
}
49+
}
50+
// [END data_catalog_ptm_list_taxonomies]
51+
listTaxonomies();
52+
}
53+
main(...process.argv.slice(2));

datacatalog/snippets/test/samples.test.js

+104-6
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,94 @@ const {assert} = require('chai');
1818
const {describe, it, before} = require('mocha');
1919
const uuid = require('uuid');
2020
const cp = require('child_process');
21-
const {DataCatalogClient} = require('@google-cloud/datacatalog').v1;
21+
const {DataCatalogClient, PolicyTagManagerClient} =
22+
require('@google-cloud/datacatalog').v1;
2223
const datacatalog = new DataCatalogClient();
24+
const policyTagManager = new PolicyTagManagerClient();
2325

2426
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2527

26-
const GCLOUD_TESTS_PREFIX = 'nodejs_samples';
28+
const GCLOUD_TESTS_PREFIX = 'nodejs_samples_';
2729
const generateUuid = () =>
2830
`${GCLOUD_TESTS_PREFIX}_${uuid.v4()}`.replace(/-/gi, '_');
2931

30-
describe('Samples', async () => {
31-
const TAG_TEMPLATE_ID = `${GCLOUD_TESTS_PREFIX}_test_tag_template`;
32-
const projectId = process.env.GCLOUD_PROJECT;
33-
const location = 'us-central1';
32+
const TAG_TEMPLATE_ID = `${GCLOUD_TESTS_PREFIX}_test_tag_template`;
33+
const projectId = process.env.GCLOUD_PROJECT;
34+
const location = 'us-central1';
35+
let taxonomyName;
3436

37+
describe('Samples', async () => {
3538
before(async () => {
3639
// Delete stale resources from samples tests.
3740
await deleteEntryGroups();
41+
await deleteTaxonomies();
42+
43+
// Create taxonomy resource
44+
const parent = datacatalog.locationPath(projectId, 'us');
45+
const taxRequest = {
46+
parent,
47+
taxonomy: {
48+
displayName: generateUuid(),
49+
activatedPolicyTypes: ['FINE_GRAINED_ACCESS_CONTROL'],
50+
},
51+
};
52+
const [taxonomy] = await policyTagManager.createTaxonomy(taxRequest);
53+
taxonomyName = taxonomy.name;
54+
});
55+
56+
describe('policyTagManager', async () => {
57+
it('should create a taxonomy', async () => {
58+
const taxLocation = 'us';
59+
const displayName = generateUuid();
60+
const output = execSync(
61+
`node policyTagManager/createTaxonomy ${projectId} ${taxLocation} ${displayName}`
62+
);
63+
assert.include(output, 'Created taxonomy:');
64+
});
65+
66+
it('should get a taxonomy', async () => {
67+
const output = execSync(
68+
`node policyTagManager/getTaxonomy ${taxonomyName}`
69+
);
70+
assert.include(output, `Retrieved taxonomy: ${taxonomyName}`);
71+
});
72+
73+
it('should list taxonomies', async () => {
74+
const taxLocation = 'us';
75+
const output = execSync(
76+
`node policyTagManager/listTaxonomies ${projectId} ${taxLocation}`
77+
);
78+
assert.include(output, 'Taxonomies:');
79+
});
80+
81+
it('should delete a taxonomy', async () => {
82+
const output = execSync(
83+
`node policyTagManager/deleteTaxonomy ${taxonomyName}`
84+
);
85+
assert.include(output, 'Deleted taxonomy:');
86+
});
87+
88+
it('should create a policy tag', async () => {
89+
const tagLocation = 'us';
90+
const displayName = generateUuid();
91+
const parent = datacatalog.locationPath(projectId, tagLocation);
92+
93+
const request = {
94+
parent: parent,
95+
taxonomy: {
96+
displayName: displayName,
97+
activatedPolicyTypes: ['FINE_GRAINED_ACCESS_CONTROL'],
98+
},
99+
};
100+
101+
const [taxonomy] = await policyTagManager.createTaxonomy(request);
102+
103+
const output = execSync(
104+
`node policyTagManager/createPolicyTag ${taxonomy.name}`
105+
);
106+
assert.include(output, 'Created policy tag:');
107+
assert.include(output, taxonomy.name);
108+
});
38109
});
39110

40111
it('should create a custom entry', async () => {
@@ -89,6 +160,33 @@ describe('Samples', async () => {
89160
return now.getTime() - created.getTime() >= oneDayMs;
90161
}
91162

163+
async function deleteTaxonomies() {
164+
const projectId = await policyTagManager.getProjectId();
165+
const location = 'us';
166+
167+
const listTaxonomiesRequest = {
168+
parent: datacatalog.locationPath(projectId, location),
169+
};
170+
171+
let [taxonomies] = await policyTagManager.listTaxonomies(
172+
listTaxonomiesRequest
173+
);
174+
175+
taxonomies = taxonomies.filter(taxonomy => {
176+
return taxonomy.displayName.includes(GCLOUD_TESTS_PREFIX);
177+
});
178+
179+
taxonomies.forEach(async taxonomy => {
180+
if (isResourceStale(taxonomy.taxonomyTimestamps.createTime.seconds)) {
181+
try {
182+
await policyTagManager.deleteTaxonomy({name: taxonomy.name});
183+
} catch (e) {
184+
console.error(e);
185+
}
186+
}
187+
});
188+
}
189+
92190
async function deleteEntries(entryGroupId) {
93191
const [entries] = await datacatalog.listEntries({parent: entryGroupId});
94192
for (const entry of entries) {

0 commit comments

Comments
 (0)