From 6bd1bf6ec360ce820725decdf579d5b094b33acd Mon Sep 17 00:00:00 2001 From: Ricardo Mendes Date: Fri, 1 Nov 2019 13:24:01 -0300 Subject: [PATCH 1/2] feat(datacatalog): add sample to create an entry group Closes #9583 --- datacatalog/noxfile.py | 2 +- datacatalog/samples/__init__.py | 0 datacatalog/samples/tests/__init__.py | 0 datacatalog/samples/tests/conftest.py | 45 ++++++++++++++++ .../samples/tests/test_create_entry_group.py | 29 ++++++++++ datacatalog/samples/v1beta1/__init__.py | 0 .../samples/v1beta1/create_entry_group.py | 54 +++++++++++++++++++ 7 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 datacatalog/samples/__init__.py create mode 100644 datacatalog/samples/tests/__init__.py create mode 100644 datacatalog/samples/tests/conftest.py create mode 100644 datacatalog/samples/tests/test_create_entry_group.py create mode 100644 datacatalog/samples/v1beta1/__init__.py create mode 100644 datacatalog/samples/v1beta1/create_entry_group.py diff --git a/datacatalog/noxfile.py b/datacatalog/noxfile.py index 3d92df19084f..9ee6f2cfb3f1 100644 --- a/datacatalog/noxfile.py +++ b/datacatalog/noxfile.py @@ -96,7 +96,7 @@ def unit(session): def system(session): """Run the system test suite.""" system_test_path = os.path.join("tests", "system.py") - system_test_folder_path = os.path.join("tests", "system") + system_test_folder_path = os.path.join("samples", "tests") # Sanity check: Only run tests if the environment variable is set. if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""): session.skip("Credentials must be set via environment variable") diff --git a/datacatalog/samples/__init__.py b/datacatalog/samples/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/datacatalog/samples/tests/__init__.py b/datacatalog/samples/tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/datacatalog/samples/tests/conftest.py b/datacatalog/samples/tests/conftest.py new file mode 100644 index 000000000000..d540fad71240 --- /dev/null +++ b/datacatalog/samples/tests/conftest.py @@ -0,0 +1,45 @@ +# Copyright 2019 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_v1beta1 + + +@pytest.fixture(scope="module") +def client(): + return datacatalog_v1beta1.DataCatalogClient() + + +@pytest.fixture(scope="module") +def project_id(): + return google.auth.default()[1] + + +@pytest.fixture +def random_entry_group_id(client, project_id): + now = datetime.datetime.now() + random_entry_group_id = "example_entry_group_{}_{}".format( + now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] + ) + yield random_entry_group_id + entry_group_name = datacatalog_v1beta1.DataCatalogClient.entry_group_path( + project_id, "us-central1", random_entry_group_id + ) + client.delete_entry_group(entry_group_name) diff --git a/datacatalog/samples/tests/test_create_entry_group.py b/datacatalog/samples/tests/test_create_entry_group.py new file mode 100644 index 000000000000..9c8c33b8cd64 --- /dev/null +++ b/datacatalog/samples/tests/test_create_entry_group.py @@ -0,0 +1,29 @@ +# Copyright 2019 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. + + +from ..v1beta1 import create_entry_group + + +def test_create_entry_group(capsys, client, project_id, random_entry_group_id): + + create_entry_group.create_entry_group(client, project_id, random_entry_group_id) + out, err = capsys.readouterr() + assert ( + "Created entry group" + " projects/{}/locations/{}/entryGroups/{}".format( + project_id, "us-central1", random_entry_group_id + ) + in out + ) diff --git a/datacatalog/samples/v1beta1/__init__.py b/datacatalog/samples/v1beta1/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/datacatalog/samples/v1beta1/create_entry_group.py b/datacatalog/samples/v1beta1/create_entry_group.py new file mode 100644 index 000000000000..24a856d8739c --- /dev/null +++ b/datacatalog/samples/v1beta1/create_entry_group.py @@ -0,0 +1,54 @@ +# Copyright 2019 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_entry_group(client, project_id, entry_group_id): + + # [START datacatalog_create_entry_group_tag] + from google.cloud import datacatalog_v1beta1 + + # TODO(developer): Construct a Data Catalog client object. + # client = datacatalog_v1beta1.DataCatalogClient() + + # TODO(developer): Set entry_group_id to the ID of the + # entry group to create. + # project_id = "your-project-id" + + # TODO(developer): Specify the geographic location where the + # entry group should reside. + # Currently, Data Catalog stores metadata in the us-central1 region. + location_id = "us-central1" + + # TODO(developer): Set entry_group_id to the ID of the + # entry group to create. + # entry_group_id = "your_entry_group_id" + + # Construct a full location path to be the parent of the entry group. + parent = datacatalog_v1beta1.DataCatalogClient.location_path( + project_id, location_id + ) + + # Construct a full EntryGroup object to send to the API. + entry_group = datacatalog_v1beta1.types.EntryGroup() + entry_group.display_name = "My Entry Group" + entry_group.description = "This Entry Group consists of ..." + + # Send the entry group to the API for creation. + # Raises google.api_core.exceptions.AlreadyExists if the Entry Group + # already exists within the project. + entry_group = client.create_entry_group( + parent, entry_group_id, entry_group + ) # Make an API request. + print("Created entry group {}".format(entry_group.name)) + # [END datacatalog_create_entry_group_tag] From e747c2da22d9c0acf99cd75b5d687d8eb5416eeb Mon Sep 17 00:00:00 2001 From: Ricardo Mendes Date: Fri, 1 Nov 2019 16:24:48 -0300 Subject: [PATCH 2/2] feat(datacatalog): add sample to create an entry group Fix pytest fixtures and synth.py. --- datacatalog/noxfile.py | 21 ++++++++++++++++++++- datacatalog/samples/tests/conftest.py | 22 ++++++++++++++++------ datacatalog/synth.py | 6 +++++- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/datacatalog/noxfile.py b/datacatalog/noxfile.py index 9ee6f2cfb3f1..509a565876ed 100644 --- a/datacatalog/noxfile.py +++ b/datacatalog/noxfile.py @@ -96,7 +96,7 @@ def unit(session): def system(session): """Run the system test suite.""" system_test_path = os.path.join("tests", "system.py") - system_test_folder_path = os.path.join("samples", "tests") + system_test_folder_path = os.path.join("tests", "system") # Sanity check: Only run tests if the environment variable is set. if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""): session.skip("Credentials must be set via environment variable") @@ -125,6 +125,25 @@ def system(session): session.run("py.test", "--quiet", system_test_folder_path, *session.posargs) +@nox.session(python=["2.7", "3.7"]) +def samples(session): + requirements_path = os.path.join("samples", "requirements.txt") + requirements_exists = os.path.exists(requirements_path) + + # Sanity check: Only run tests if the environment variable is set. + if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""): + session.skip("Credentials must be set via environment variable") + + session.install("mock", "pytest") + for local_dep in LOCAL_DEPS: + session.install("-e", local_dep) + if requirements_exists: + session.install("-r", requirements_path) + session.install("-e", ".") + + session.run("py.test", "--quiet", "samples", *session.posargs) + + @nox.session(python="3.7") def cover(session): """Run the final coverage report. diff --git a/datacatalog/samples/tests/conftest.py b/datacatalog/samples/tests/conftest.py index d540fad71240..b147413db588 100644 --- a/datacatalog/samples/tests/conftest.py +++ b/datacatalog/samples/tests/conftest.py @@ -22,14 +22,24 @@ from google.cloud import datacatalog_v1beta1 -@pytest.fixture(scope="module") -def client(): - return datacatalog_v1beta1.DataCatalogClient() +@pytest.fixture(scope="session") +def client(credentials): + return datacatalog_v1beta1.DataCatalogClient(credentials=credentials) -@pytest.fixture(scope="module") -def project_id(): - return google.auth.default()[1] +@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] @pytest.fixture diff --git a/datacatalog/synth.py b/datacatalog/synth.py index 468dc63c713d..5f1436288e14 100644 --- a/datacatalog/synth.py +++ b/datacatalog/synth.py @@ -57,7 +57,11 @@ # ---------------------------------------------------------------------------- # Add templated files # ---------------------------------------------------------------------------- -templated_files = common.py_library(unit_cov_level=80, cov_level=80) +templated_files = common.py_library( + unit_cov_level=80, + cov_level=80, + samples_test=True, +) s.move(templated_files) s.shell.run(["nox", "-s", "blacken"], hide_output=False)