@@ -18,23 +18,94 @@ const {assert} = require('chai');
1818const { describe, it, before} = require ( 'mocha' ) ;
1919const uuid = require ( 'uuid' ) ;
2020const cp = require ( 'child_process' ) ;
21- const { DataCatalogClient} = require ( '@google-cloud/datacatalog' ) . v1 ;
21+ const { DataCatalogClient, PolicyTagManagerClient} =
22+ require ( '@google-cloud/datacatalog' ) . v1 ;
2223const datacatalog = new DataCatalogClient ( ) ;
24+ const policyTagManager = new PolicyTagManagerClient ( ) ;
2325
2426const execSync = cmd => cp . execSync ( cmd , { encoding : 'utf-8' } ) ;
2527
26- const GCLOUD_TESTS_PREFIX = 'nodejs_samples ' ;
28+ const GCLOUD_TESTS_PREFIX = 'nodejs_samples_ ' ;
2729const 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