Skip to content

Commit

Permalink
feat(datacatalog): add sample to create an entry group (#9584)
Browse files Browse the repository at this point in the history
* feat(datacatalog): add sample to create an entry group

Closes #9583

* feat(datacatalog): add sample to create an entry group

Fix pytest fixtures and synth.py.
  • Loading branch information
ricardolsmendes authored and tswast committed Nov 1, 2019
1 parent 1ddb894 commit a5e5344
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/google-cloud-datacatalog/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Empty file.
Empty file.
55 changes: 55 additions & 0 deletions packages/google-cloud-datacatalog/samples/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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="session")
def client(credentials):
return datacatalog_v1beta1.DataCatalogClient(credentials=credentials)


@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
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)
Original file line number Diff line number Diff line change
@@ -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
)
Empty file.
Original file line number Diff line number Diff line change
@@ -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]
6 changes: 5 additions & 1 deletion packages/google-cloud-datacatalog/synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit a5e5344

Please sign in to comment.