Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 15feb5d

Browse files
ricardolsmendespartheatswast
authored
docs(samples): Add sample for PolicyTagManagerClient.create_taxonomy (#37)
* fix(samples): add sample for create_taxonomy * lint * typo in import * refactor sample to match expected pattern Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Tim Swast <[email protected]>
1 parent 85b36cc commit 15feb5d

File tree

3 files changed

+102
-5
lines changed

3 files changed

+102
-5
lines changed

samples/snippets/conftest.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
1615
import datetime
1716
import uuid
1817

@@ -22,9 +21,6 @@
2221

2322
import pytest
2423

25-
datacatalog = datacatalog_v1.DataCatalogClient()
26-
27-
2824
LOCATION = "us-central1"
2925

3026

@@ -62,7 +58,7 @@ def valid_member_id(client, project_id, random_existing_tag_template_id):
6258
)
6359

6460
# Retrieve Template's current IAM Policy.
65-
policy = datacatalog.get_iam_policy(resource=template_name)
61+
policy = client.get_iam_policy(resource=template_name)
6662
yield policy.bindings[0].members[0]
6763

6864

@@ -127,3 +123,25 @@ def random_existing_tag_template_id(client, project_id, resources_to_delete):
127123
)
128124
yield random_tag_template_id
129125
resources_to_delete["templates"].append(random_tag_template.name)
126+
127+
128+
@pytest.fixture(scope="session")
129+
def policy_tag_manager_client(credentials):
130+
return datacatalog_v1.PolicyTagManagerClient(credentials=credentials)
131+
132+
133+
@pytest.fixture
134+
def random_taxonomy_display_name(policy_tag_manager_client, project_id):
135+
now = datetime.datetime.now()
136+
random_display_name = f'example_taxonomy' \
137+
f'_{now.strftime("%Y%m%d%H%M%S")}' \
138+
f'_{uuid.uuid4().hex[:8]}'
139+
yield random_display_name
140+
parent = datacatalog_v1.PolicyTagManagerClient.common_location_path(
141+
project_id, 'us'
142+
)
143+
taxonomies = policy_tag_manager_client.list_taxonomies(parent=parent)
144+
taxonomy = next(
145+
(t for t in taxonomies if t.display_name == random_display_name), None)
146+
if taxonomy:
147+
policy_tag_manager_client.delete_taxonomy(name=taxonomy.name)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2021 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+
# https://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+
# [START data_catalog_ptm_create_taxonomy]
16+
from google.cloud import datacatalog_v1
17+
18+
19+
def create_taxonomy(
20+
# TODO(developer): Set project_id to the ID of the project the
21+
# taxonomy will belong to.
22+
project_id: str = "your-project-id",
23+
24+
# TODO(developer): Specify the geographic location where the
25+
# taxonomy should reside.
26+
location_id: str = "us",
27+
28+
# TODO(developer): Set the display name of the taxonomy.
29+
display_name: str = "example-taxonomy",
30+
):
31+
# TODO(developer): Construct a Policy Tag Manager client object. To avoid
32+
# extra delays due to authentication, create a single client for your
33+
# program and share it across operations.
34+
client = datacatalog_v1.PolicyTagManagerClient()
35+
36+
# Construct a full location path to be the parent of the taxonomy.
37+
parent = datacatalog_v1.PolicyTagManagerClient.common_location_path(
38+
project_id, location_id
39+
)
40+
41+
# TODO(developer): Construct a full Taxonomy object to send to the API.
42+
taxonomy = datacatalog_v1.Taxonomy()
43+
taxonomy.display_name = display_name
44+
taxonomy.description = 'This Taxonomy represents ...'
45+
46+
# Send the taxonomy to the API for creation.
47+
taxonomy = client.create_taxonomy(parent=parent, taxonomy=taxonomy)
48+
print(f'Created taxonomy {taxonomy.name}')
49+
50+
# [END data_catalog_ptm_create_taxonomy]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2021 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+
# https://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+
16+
import data_catalog_ptm_create_taxonomy
17+
18+
19+
def test_create_taxonomy(capsys,
20+
project_id: str,
21+
random_taxonomy_display_name: str):
22+
23+
data_catalog_ptm_create_taxonomy.create_taxonomy(
24+
project_id=project_id, location_id="us", display_name=random_taxonomy_display_name)
25+
out, _ = capsys.readouterr()
26+
assert (
27+
f'Created taxonomy projects/{project_id}/locations/us/taxonomies/'
28+
in out
29+
)

0 commit comments

Comments
 (0)