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

Commit

Permalink
fix(samples): add sample for create_taxonomy
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardolsmendes committed Jan 8, 2021
1 parent fda528a commit c83216c
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
58 changes: 58 additions & 0 deletions samples/snippets/conftest.py
@@ -0,0 +1,58 @@
# Copyright 2021 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
#
# https://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.

import datetime
import uuid

import pytest

import google.auth
from google.cloud import datacatalog


@pytest.fixture(scope="session")
def policy_tag_manager_client(credentials):
return datacatalog.PolicyTagManagerClient(credentials=credentials)


@pytest.fixture
def random_taxonomy_display_name(policy_tag_manager_client, project_id):
now = datetime.datetime.now()
random_display_name = f'example_taxonomy' \
f'_{now.strftime("%Y%m%d%H%M%S")}' \
f'_{uuid.uuid4().hex[:8]}'
yield random_display_name
parent = datacatalog.PolicyTagManagerClient.common_location_path(
project_id, 'us'
)
taxonomies = policy_tag_manager_client.list_taxonomies(parent=parent)
taxonomy = next(
(t for t in taxonomies if t.display_name == random_display_name), None)
if taxonomy:
policy_tag_manager_client.delete_taxonomy(name=taxonomy.name)


@pytest.fixture(scope="session")
def default_credentials():
return google.auth.default()


@pytest.fixture(scope="session")
def credentials(default_credentials):
return default_credentials[0]


@pytest.fixture(scope="session")
def project_id(default_credentials):
return default_credentials[1]
45 changes: 45 additions & 0 deletions samples/snippets/data_catalog_ptm_create_taxonomy.py
@@ -0,0 +1,45 @@
# Copyright 2021 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
#
# https://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.


def create_taxonomy(client, project_id, display_name):

# [START data_catalog_create_taxonomy_tag]
from google.cloud import datacatalog

# TODO(developer): Construct a Policy Tag Manager client object.
# client = datacatalog.PolicyTagManagerClient()

# TODO(developer): Set project_id to the ID of the project the
# taxonomy will belong to.
# project_id = 'your-project-id'

# TODO(developer): Specify the geographic location where the
# taxonomy should reside.
location_id = 'us'

# Construct a full location path to be the parent of the taxonomy.
parent = datacatalog.PolicyTagManagerClient.common_location_path(
project_id, location_id
)

taxonomy = datacatalog.Taxonomy()
# TODO(developer): Construct a full Taxonomy object to send to the API.
taxonomy.display_name = display_name
taxonomy.description = 'This Taxonomy represents ...'

# Send the taxonomy to the API for creation.
taxonomy = client.create_taxonomy(parent=parent, taxonomy=taxonomy)
print(f'Created taxonomy {taxonomy.name}')
# [END data_catalog_create_taxonomy_tag]
30 changes: 30 additions & 0 deletions samples/snippets/data_catalog_ptm_create_taxonomy_test.py
@@ -0,0 +1,30 @@
# Copyright 2021 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
#
# https://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.


import data_catalog_ptm_create_taxonomy


def test_create_taxonomy(capsys,
policy_tag_manager_client,
project_id,
random_taxonomy_display_name):

data_catalog_ptm_create_taxonomy.create_taxonomy(
policy_tag_manager_client, project_id, random_taxonomy_display_name)
out, err = capsys.readouterr()
assert (
f'Created taxonomy projects/{project_id}/locations/us/taxonomies/'
in out
)

0 comments on commit c83216c

Please sign in to comment.