Skip to content

Commit 5571911

Browse files
ricardolsmendestswast
authored andcommitted
feat(datacatalog): add sample to create an entry group (#9584)
* 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.
1 parent 3e8fbae commit 5571911

File tree

8 files changed

+162
-1
lines changed

8 files changed

+162
-1
lines changed

datacatalog/noxfile.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ def system(session):
125125
session.run("py.test", "--quiet", system_test_folder_path, *session.posargs)
126126

127127

128+
@nox.session(python=["2.7", "3.7"])
129+
def samples(session):
130+
requirements_path = os.path.join("samples", "requirements.txt")
131+
requirements_exists = os.path.exists(requirements_path)
132+
133+
# Sanity check: Only run tests if the environment variable is set.
134+
if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""):
135+
session.skip("Credentials must be set via environment variable")
136+
137+
session.install("mock", "pytest")
138+
for local_dep in LOCAL_DEPS:
139+
session.install("-e", local_dep)
140+
if requirements_exists:
141+
session.install("-r", requirements_path)
142+
session.install("-e", ".")
143+
144+
session.run("py.test", "--quiet", "samples", *session.posargs)
145+
146+
128147
@nox.session(python="3.7")
129148
def cover(session):
130149
"""Run the final coverage report.

datacatalog/samples/__init__.py

Whitespace-only changes.

datacatalog/samples/tests/__init__.py

Whitespace-only changes.

datacatalog/samples/tests/conftest.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2019 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 datetime
17+
import uuid
18+
19+
import pytest
20+
21+
import google.auth
22+
from google.cloud import datacatalog_v1beta1
23+
24+
25+
@pytest.fixture(scope="session")
26+
def client(credentials):
27+
return datacatalog_v1beta1.DataCatalogClient(credentials=credentials)
28+
29+
30+
@pytest.fixture(scope="session")
31+
def default_credentials():
32+
return google.auth.default()
33+
34+
35+
@pytest.fixture(scope="session")
36+
def credentials(default_credentials):
37+
return default_credentials[0]
38+
39+
40+
@pytest.fixture(scope="session")
41+
def project_id(default_credentials):
42+
return default_credentials[1]
43+
44+
45+
@pytest.fixture
46+
def random_entry_group_id(client, project_id):
47+
now = datetime.datetime.now()
48+
random_entry_group_id = "example_entry_group_{}_{}".format(
49+
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
50+
)
51+
yield random_entry_group_id
52+
entry_group_name = datacatalog_v1beta1.DataCatalogClient.entry_group_path(
53+
project_id, "us-central1", random_entry_group_id
54+
)
55+
client.delete_entry_group(entry_group_name)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019 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+
from ..v1beta1 import create_entry_group
17+
18+
19+
def test_create_entry_group(capsys, client, project_id, random_entry_group_id):
20+
21+
create_entry_group.create_entry_group(client, project_id, random_entry_group_id)
22+
out, err = capsys.readouterr()
23+
assert (
24+
"Created entry group"
25+
" projects/{}/locations/{}/entryGroups/{}".format(
26+
project_id, "us-central1", random_entry_group_id
27+
)
28+
in out
29+
)

datacatalog/samples/v1beta1/__init__.py

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2019 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+
def create_entry_group(client, project_id, entry_group_id):
17+
18+
# [START datacatalog_create_entry_group_tag]
19+
from google.cloud import datacatalog_v1beta1
20+
21+
# TODO(developer): Construct a Data Catalog client object.
22+
# client = datacatalog_v1beta1.DataCatalogClient()
23+
24+
# TODO(developer): Set entry_group_id to the ID of the
25+
# entry group to create.
26+
# project_id = "your-project-id"
27+
28+
# TODO(developer): Specify the geographic location where the
29+
# entry group should reside.
30+
# Currently, Data Catalog stores metadata in the us-central1 region.
31+
location_id = "us-central1"
32+
33+
# TODO(developer): Set entry_group_id to the ID of the
34+
# entry group to create.
35+
# entry_group_id = "your_entry_group_id"
36+
37+
# Construct a full location path to be the parent of the entry group.
38+
parent = datacatalog_v1beta1.DataCatalogClient.location_path(
39+
project_id, location_id
40+
)
41+
42+
# Construct a full EntryGroup object to send to the API.
43+
entry_group = datacatalog_v1beta1.types.EntryGroup()
44+
entry_group.display_name = "My Entry Group"
45+
entry_group.description = "This Entry Group consists of ..."
46+
47+
# Send the entry group to the API for creation.
48+
# Raises google.api_core.exceptions.AlreadyExists if the Entry Group
49+
# already exists within the project.
50+
entry_group = client.create_entry_group(
51+
parent, entry_group_id, entry_group
52+
) # Make an API request.
53+
print("Created entry group {}".format(entry_group.name))
54+
# [END datacatalog_create_entry_group_tag]

datacatalog/synth.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@
5757
# ----------------------------------------------------------------------------
5858
# Add templated files
5959
# ----------------------------------------------------------------------------
60-
templated_files = common.py_library(unit_cov_level=80, cov_level=80)
60+
templated_files = common.py_library(
61+
unit_cov_level=80,
62+
cov_level=80,
63+
samples_test=True,
64+
)
6165
s.move(templated_files)
6266

6367
s.shell.run(["nox", "-s", "blacken"], hide_output=False)

0 commit comments

Comments
 (0)