Skip to content

Commit 3e8fe49

Browse files
jacspa96Jacek Spalinski
andauthored
feat(dataplex): add code samples for Entry Types and Groups (#12749)
* feat(dataplex): add sample for list Entry Types * feat(dataplex): add sample for get Entry Type * feat(dataplex): add sample for delete Entry Type * feat(dataplex): add sample for create Entry Type * feat(dataplex): add sample for update Entry Type * feat(dataplex): add integration test for Entry Types * feat(dataplex): add sample for list Entry Groups * feat(dataplex): add sample for get Entry Group * feat(dataplex): add sample for delete Entry Group * feat(dataplex): add sample for create Entry Group * feat(dataplex): add sample for update Entry Group * feat(dataplex): add integration test for Entry Groups * feat(dataplex): opt out from not required python builds * feat(dataplex): use default noxfile config --------- Co-authored-by: Jacek Spalinski <jspa@google.com>
1 parent 5db0862 commit 3e8fe49

13 files changed

+699
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2024 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+
# http://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+
# [START dataplex_create_entry_group]
16+
from google.cloud import dataplex_v1
17+
18+
19+
# Method to create Entry Group located in project_id, location and with entry_group_id
20+
def create_entry_group(
21+
project_id: str, location: str, entry_group_id: str
22+
) -> dataplex_v1.EntryGroup:
23+
# Initialize client that will be used to send requests across threads. This
24+
# client only needs to be created once, and can be reused for multiple requests.
25+
# After completing all of your requests, call the "__exit__()" method to safely
26+
# clean up any remaining background resources. Alternatively, use the client as
27+
# a context manager.
28+
with dataplex_v1.CatalogServiceClient() as client:
29+
# The resource name of the Entry Group location
30+
parent = f"projects/{project_id}/locations/{location}"
31+
entry_group = dataplex_v1.EntryGroup(
32+
description="description of the entry group"
33+
)
34+
create_operation = client.create_entry_group(
35+
parent=parent, entry_group=entry_group, entry_group_id=entry_group_id
36+
)
37+
return create_operation.result(60)
38+
39+
40+
if __name__ == "__main__":
41+
# TODO(developer): Replace these variables before running the sample.
42+
project_id = "MY_PROJECT_ID"
43+
# Available locations: https://cloud.google.com/dataplex/docs/locations
44+
location = "MY_LOCATION"
45+
entry_group_id = "MY_ENTRY_GROUP_ID"
46+
47+
created_entry_group = create_entry_group(project_id, location, entry_group_id)
48+
print(f"Successfully created entry group: {created_entry_group.name}")
49+
# [END dataplex_create_entry_group]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2024 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+
# http://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+
# [START dataplex_create_entry_type]
16+
from google.cloud import dataplex_v1
17+
18+
19+
# Method to create Entry Type located in project_id, location and with entry_type_id
20+
def create_entry_type(
21+
project_id: str, location: str, entry_type_id: str
22+
) -> dataplex_v1.EntryType:
23+
# Initialize client that will be used to send requests across threads. This
24+
# client only needs to be created once, and can be reused for multiple requests.
25+
# After completing all of your requests, call the "__exit__()" method to safely
26+
# clean up any remaining background resources. Alternatively, use the client as
27+
# a context manager.
28+
with dataplex_v1.CatalogServiceClient() as client:
29+
# The resource name of the Entry Type location
30+
parent = f"projects/{project_id}/locations/{location}"
31+
entry_type = dataplex_v1.EntryType(
32+
description="description of the entry type",
33+
# Required aspects will need to be attached to every entry created for this entry type.
34+
# You cannot change required aspects for entry type once it is created.
35+
required_aspects=[
36+
dataplex_v1.EntryType.AspectInfo(
37+
# Example of system aspect type.
38+
# It is also possible to specify custom aspect type.
39+
type="projects/dataplex-types/locations/global/aspectTypes/generic"
40+
)
41+
],
42+
)
43+
create_operation = client.create_entry_type(
44+
parent=parent, entry_type=entry_type, entry_type_id=entry_type_id
45+
)
46+
return create_operation.result(60)
47+
48+
49+
if __name__ == "__main__":
50+
# TODO(developer): Replace these variables before running the sample.
51+
project_id = "MY_PROJECT_ID"
52+
# Available locations: https://cloud.google.com/dataplex/docs/locations
53+
location = "MY_LOCATION"
54+
entry_type_id = "MY_ENTRY_TYPE_ID"
55+
56+
created_entry_type = create_entry_type(project_id, location, entry_type_id)
57+
print(f"Successfully created entry type: {created_entry_type.name}")
58+
# [END dataplex_create_entry_type]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2024 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+
# http://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+
# [START dataplex_delete_entry_group]
16+
from google.cloud import dataplex_v1
17+
18+
19+
# Method to delete Entry Group located in project_id, location and with entry_group_id
20+
def delete_entry_group(project_id: str, location: str, entry_group_id: str) -> None:
21+
# Initialize client that will be used to send requests across threads. This
22+
# client only needs to be created once, and can be reused for multiple requests.
23+
# After completing all of your requests, call the "__exit__()" method to safely
24+
# clean up any remaining background resources. Alternatively, use the client as
25+
# a context manager.
26+
with dataplex_v1.CatalogServiceClient() as client:
27+
# The resource name of the Entry Type
28+
name = (
29+
f"projects/{project_id}/locations/{location}/entryGroups/{entry_group_id}"
30+
)
31+
client.delete_entry_group(name=name)
32+
33+
34+
if __name__ == "__main__":
35+
# TODO(developer): Replace these variables before running the sample.
36+
project_id = "MY_PROJECT_ID"
37+
# Available locations: https://cloud.google.com/dataplex/docs/locations
38+
location = "MY_LOCATION"
39+
entry_group_id = "MY_ENTRY_GROUP_ID"
40+
41+
delete_entry_group(project_id, location, entry_group_id)
42+
print("Successfully deleted entry group")
43+
# [END dataplex_delete_entry_group]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2024 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+
# http://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+
# [START dataplex_delete_entry_type]
16+
from google.cloud import dataplex_v1
17+
18+
19+
# Method to delete Entry Type located in project_id, location and with entry_type_id
20+
def delete_entry_type(project_id: str, location: str, entry_type_id: str) -> None:
21+
# Initialize client that will be used to send requests across threads. This
22+
# client only needs to be created once, and can be reused for multiple requests.
23+
# After completing all of your requests, call the "__exit__()" method to safely
24+
# clean up any remaining background resources. Alternatively, use the client as
25+
# a context manager.
26+
with dataplex_v1.CatalogServiceClient() as client:
27+
# The resource name of the Entry Type
28+
name = f"projects/{project_id}/locations/{location}/entryTypes/{entry_type_id}"
29+
client.delete_entry_type(name=name)
30+
31+
32+
if __name__ == "__main__":
33+
# TODO(developer): Replace these variables before running the sample.
34+
project_id = "MY_PROJECT_ID"
35+
# Available locations: https://cloud.google.com/dataplex/docs/locations
36+
location = "MY_LOCATION"
37+
entry_type_id = "MY_ENTRY_TYPE_ID"
38+
39+
delete_entry_type(project_id, location, entry_type_id)
40+
print("Successfully deleted entry type")
41+
# [END dataplex_delete_entry_type]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2024 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+
# http://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+
import os
15+
16+
import uuid
17+
18+
from google.api_core.retry import Retry
19+
20+
import pytest
21+
22+
import create_entry_group
23+
import delete_entry_group
24+
import get_entry_group
25+
import list_entry_groups
26+
import update_entry_group
27+
28+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
29+
LOCATION = "us-central1"
30+
ENTRY_GROUP_ID = f"test-entry-group-{str(uuid.uuid4()).split('-')[0]}"
31+
EXPECTED_ENTRY_GROUP = (
32+
f"projects/{PROJECT_ID}/locations/{LOCATION}/entryGroups/{ENTRY_GROUP_ID}"
33+
)
34+
35+
36+
@pytest.fixture(autouse=True, scope="session")
37+
def setup_and_teardown_entry_group() -> None:
38+
try:
39+
# Create Entry Group resource that will be used in tests for "get", "list" and "update" methods
40+
create_entry_group.create_entry_group(PROJECT_ID, LOCATION, ENTRY_GROUP_ID)
41+
yield
42+
finally:
43+
# Clean-up Entry Group resource created above
44+
delete_entry_group.delete_entry_group(PROJECT_ID, LOCATION, ENTRY_GROUP_ID)
45+
46+
47+
@Retry()
48+
def test_list_entry_groups() -> None:
49+
entry_groups = list_entry_groups.list_entry_groups(PROJECT_ID, LOCATION)
50+
assert EXPECTED_ENTRY_GROUP in [entry_group.name for entry_group in entry_groups]
51+
52+
53+
@Retry()
54+
def test_get_entry_group() -> None:
55+
entry_group = get_entry_group.get_entry_group(PROJECT_ID, LOCATION, ENTRY_GROUP_ID)
56+
assert EXPECTED_ENTRY_GROUP == entry_group.name
57+
58+
59+
@Retry()
60+
def test_update_entry_group() -> None:
61+
entry_group = update_entry_group.update_entry_group(
62+
PROJECT_ID, LOCATION, ENTRY_GROUP_ID
63+
)
64+
assert EXPECTED_ENTRY_GROUP == entry_group.name
65+
66+
67+
@Retry()
68+
def test_create_entry_group() -> None:
69+
entry_group_id_to_create = f"test-entry-group-{str(uuid.uuid4()).split('-')[0]}"
70+
expected_entry_group_to_create = f"projects/{PROJECT_ID}/locations/{LOCATION}/entryGroups/{entry_group_id_to_create}"
71+
try:
72+
entry_group = create_entry_group.create_entry_group(
73+
PROJECT_ID, LOCATION, entry_group_id_to_create
74+
)
75+
assert expected_entry_group_to_create == entry_group.name
76+
finally:
77+
# Clean-up created Entry Group
78+
delete_entry_group.delete_entry_group(
79+
PROJECT_ID, LOCATION, entry_group_id_to_create
80+
)
81+
82+
83+
@Retry()
84+
def test_delete_entry_group() -> None:
85+
entry_group_id_to_delete = f"test-entry-group-{str(uuid.uuid4()).split('-')[0]}"
86+
# Create Entry Group to be deleted
87+
create_entry_group.create_entry_group(
88+
PROJECT_ID, LOCATION, entry_group_id_to_delete
89+
)
90+
# No exception means successful call
91+
delete_entry_group.delete_entry_group(
92+
PROJECT_ID, LOCATION, entry_group_id_to_delete
93+
)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright 2024 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+
# http://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+
import os
15+
16+
import uuid
17+
18+
from google.api_core.retry import Retry
19+
20+
import pytest
21+
22+
import create_entry_type
23+
import delete_entry_type
24+
import get_entry_type
25+
import list_entry_types
26+
import update_entry_type
27+
28+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
29+
LOCATION = "us-central1"
30+
ENTRY_TYPE_ID = f"test-entry-type-{str(uuid.uuid4()).split('-')[0]}"
31+
EXPECTED_ENTRY_TYPE = (
32+
f"projects/{PROJECT_ID}/locations/{LOCATION}/entryTypes/{ENTRY_TYPE_ID}"
33+
)
34+
35+
36+
@pytest.fixture(autouse=True, scope="session")
37+
def setup_and_teardown_entry_type() -> None:
38+
try:
39+
# Create Entry Type resource that will be used in tests for "get", "list" and "update" methods
40+
create_entry_type.create_entry_type(PROJECT_ID, LOCATION, ENTRY_TYPE_ID)
41+
yield
42+
finally:
43+
# Clean-up Entry Type resource created above
44+
delete_entry_type.delete_entry_type(PROJECT_ID, LOCATION, ENTRY_TYPE_ID)
45+
46+
47+
@Retry()
48+
def test_list_entry_types() -> None:
49+
entry_types = list_entry_types.list_entry_types(PROJECT_ID, LOCATION)
50+
assert EXPECTED_ENTRY_TYPE in [entry_type.name for entry_type in entry_types]
51+
52+
53+
@Retry()
54+
def test_get_entry_type() -> None:
55+
entry_type = get_entry_type.get_entry_type(PROJECT_ID, LOCATION, ENTRY_TYPE_ID)
56+
assert EXPECTED_ENTRY_TYPE == entry_type.name
57+
58+
59+
@Retry()
60+
def test_update_entry_type() -> None:
61+
entry_type = update_entry_type.update_entry_type(
62+
PROJECT_ID, LOCATION, ENTRY_TYPE_ID
63+
)
64+
assert EXPECTED_ENTRY_TYPE == entry_type.name
65+
66+
67+
@Retry()
68+
def test_create_entry_type() -> None:
69+
entry_type_id_to_create = f"test-entry-type-{str(uuid.uuid4()).split('-')[0]}"
70+
expected_entry_type_to_create = f"projects/{PROJECT_ID}/locations/{LOCATION}/entryTypes/{entry_type_id_to_create}"
71+
try:
72+
entry_type = create_entry_type.create_entry_type(
73+
PROJECT_ID, LOCATION, entry_type_id_to_create
74+
)
75+
assert expected_entry_type_to_create == entry_type.name
76+
finally:
77+
# Clean-up created Entry Type
78+
delete_entry_type.delete_entry_type(
79+
PROJECT_ID, LOCATION, entry_type_id_to_create
80+
)
81+
82+
83+
@Retry()
84+
def test_delete_entry_type() -> None:
85+
entry_type_id_to_delete = f"test-entry-type-{str(uuid.uuid4()).split('-')[0]}"
86+
# Create Entry Type to be deleted
87+
create_entry_type.create_entry_type(PROJECT_ID, LOCATION, entry_type_id_to_delete)
88+
# No exception means successful call
89+
delete_entry_type.delete_entry_type(PROJECT_ID, LOCATION, entry_type_id_to_delete)

0 commit comments

Comments
 (0)