-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <bencoe@google.com>
- Loading branch information
Showing
6 changed files
with
362 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters