diff --git a/samples/snippets/conftest.py b/samples/snippets/conftest.py new file mode 100644 index 00000000..3e47036f --- /dev/null +++ b/samples/snippets/conftest.py @@ -0,0 +1,60 @@ +# 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(policy_tag_manager_client, project_id): + random_taxonomy = datacatalog.Taxonomy() + now = datetime.datetime.now() + display_name = f'example_taxonomy' \ + f'_{now.strftime("%Y%m%d%H%M%S")}' \ + f'_{uuid.uuid4().hex[:8]}' + random_taxonomy.display_name = display_name + yield random_taxonomy + 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 == 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] diff --git a/samples/snippets/data_catalog_ptm_create_taxonomy.py b/samples/snippets/data_catalog_ptm_create_taxonomy.py new file mode 100644 index 00000000..14550c27 --- /dev/null +++ b/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, taxonomy): + + # [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 + ) + + # TODO(developer): Construct a full Taxonomy object to send to the API. + # taxonomy = datacatalog.Taxonomy() + # taxonomy.display_name = 'My Taxonomy' + # 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] diff --git a/samples/snippets/data_catalog_ptm_create_taxonomy_test.py b/samples/snippets/data_catalog_ptm_create_taxonomy_test.py new file mode 100644 index 00000000..00ecbbed --- /dev/null +++ b/samples/snippets/data_catalog_ptm_create_taxonomy_test.py @@ -0,0 +1,28 @@ +# 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): + + data_catalog_ptm_create_taxonomy.create_taxonomy( + policy_tag_manager_client, project_id, random_taxonomy) + out, err = capsys.readouterr() + assert ( + f'Created taxonomy projects/{project_id}/locations/us/taxonomies/' + in out + )