@@ -18,23 +18,94 @@ const {assert} = require('chai');
18
18
const { describe, it, before} = require ( 'mocha' ) ;
19
19
const uuid = require ( 'uuid' ) ;
20
20
const cp = require ( 'child_process' ) ;
21
- const { DataCatalogClient} = require ( '@google-cloud/datacatalog' ) . v1 ;
21
+ const { DataCatalogClient, PolicyTagManagerClient} =
22
+ require ( '@google-cloud/datacatalog' ) . v1 ;
22
23
const datacatalog = new DataCatalogClient ( ) ;
24
+ const policyTagManager = new PolicyTagManagerClient ( ) ;
23
25
24
26
const execSync = cmd => cp . execSync ( cmd , { encoding : 'utf-8' } ) ;
25
27
26
- const GCLOUD_TESTS_PREFIX = 'nodejs_samples ' ;
28
+ const GCLOUD_TESTS_PREFIX = 'nodejs_samples_ ' ;
27
29
const generateUuid = ( ) =>
28
30
`${ GCLOUD_TESTS_PREFIX } _${ uuid . v4 ( ) } ` . replace ( / - / gi, '_' ) ;
29
31
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 ;
34
36
37
+ describe ( 'Samples' , async ( ) => {
35
38
before ( async ( ) => {
36
39
// Delete stale resources from samples tests.
37
40
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
+ } ) ;
38
109
} ) ;
39
110
40
111
it ( 'should create a custom entry' , async ( ) => {
@@ -89,6 +160,33 @@ describe('Samples', async () => {
89
160
return now . getTime ( ) - created . getTime ( ) >= oneDayMs ;
90
161
}
91
162
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
+
92
190
async function deleteEntries ( entryGroupId ) {
93
191
const [ entries ] = await datacatalog . listEntries ( { parent : entryGroupId } ) ;
94
192
for ( const entry of entries ) {
0 commit comments