Skip to content

Commit a8c4afb

Browse files
authored
Merge pull request #8508 from GoogleCloudPlatform/python-datacatalog-migration
migrate code from googleapis/python-datacatalog
2 parents 1c76780 + 074f650 commit a8c4afb

37 files changed

+1780
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@
7373
/talent/**/* @GoogleCloudPlatform/python-samples-reviewers
7474
/vision/**/* @GoogleCloudPlatform/python-samples-reviewers
7575
/workflows/**/* @GoogleCloudPlatform/python-samples-reviewers
76+
/datacatalog/**/* @GoogleCloudPlatform/python-samples-reviewers
7677
/kms/**/** @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers

.github/blunderbuss.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ assign_issues_by:
130130
- 'api: monitoring'
131131
to:
132132
- GoogleCloudPlatform/dee-observability
133+
- labels:
134+
- 'api: datacatalog'
135+
to:
136+
- GoogleCloudPlatform/python-samples-reviewers
133137
- labels:
134138
- 'api: kms'
135139
- 'api: cloudkms'

datacatalog/quickstart/conftest.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 google.auth
20+
from google.cloud import bigquery, datacatalog_v1
21+
import pytest
22+
23+
24+
def temp_suffix():
25+
now = datetime.datetime.now()
26+
return "{}_{}".format(now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8])
27+
28+
29+
@pytest.fixture(scope="session")
30+
def client(credentials):
31+
return datacatalog_v1.DataCatalogClient(credentials=credentials)
32+
33+
34+
@pytest.fixture(scope="session")
35+
def bigquery_client(credentials, project_id):
36+
return bigquery.Client(project=project_id, credentials=credentials)
37+
38+
39+
@pytest.fixture(scope="session")
40+
def default_credentials():
41+
return google.auth.default(
42+
scopes=["https://www.googleapis.com/auth/cloud-platform"]
43+
)
44+
45+
46+
@pytest.fixture(scope="session")
47+
def credentials(default_credentials):
48+
return default_credentials[0]
49+
50+
51+
@pytest.fixture(scope="session")
52+
def project_id(default_credentials):
53+
return default_credentials[1]
54+
55+
56+
@pytest.fixture
57+
def dataset_id(bigquery_client):
58+
dataset_id = f"python_data_catalog_sample_{temp_suffix()}"
59+
dataset = bigquery_client.create_dataset(dataset_id)
60+
yield dataset.dataset_id
61+
bigquery_client.delete_dataset(dataset, delete_contents=True, not_found_ok=True)
62+
63+
64+
@pytest.fixture
65+
def table_id(bigquery_client, project_id, dataset_id):
66+
table_id = f"python_data_catalog_sample_{temp_suffix()}"
67+
table = bigquery.Table("{}.{}.{}".format(project_id, dataset_id, table_id))
68+
table = bigquery_client.create_table(table)
69+
yield table.table_id
70+
bigquery_client.delete_table(table, not_found_ok=True)
71+
72+
73+
@pytest.fixture
74+
def random_tag_template_id():
75+
random_tag_template_id = f"python_sample_{temp_suffix()}"
76+
yield random_tag_template_id

datacatalog/quickstart/quickstart.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 quickstart(override_values):
17+
"""Creates a tag template and attach a tag to a BigQuery table."""
18+
# [START data_catalog_quickstart]
19+
# Import required modules.
20+
from google.cloud import datacatalog_v1
21+
22+
# TODO: Set these values before running the sample.
23+
# Google Cloud Platform project.
24+
project_id = "my_project"
25+
# Set dataset_id to the ID of existing dataset.
26+
dataset_id = "demo_dataset"
27+
# Set table_id to the ID of existing table.
28+
table_id = "trips"
29+
# Tag template to create.
30+
tag_template_id = "example_tag_template"
31+
32+
# [END data_catalog_quickstart]
33+
34+
# To facilitate testing, we replace values with alternatives
35+
# provided by the testing harness.
36+
project_id = override_values.get("project_id", project_id)
37+
dataset_id = override_values.get("dataset_id", dataset_id)
38+
table_id = override_values.get("table_id", table_id)
39+
tag_template_id = override_values.get("tag_template_id", tag_template_id)
40+
41+
# [START data_catalog_quickstart]
42+
# For all regions available, see:
43+
# https://cloud.google.com/data-catalog/docs/concepts/regions
44+
location = "us-central1"
45+
46+
# Use Application Default Credentials to create a new
47+
# Data Catalog client. GOOGLE_APPLICATION_CREDENTIALS
48+
# environment variable must be set with the location
49+
# of a service account key file.
50+
datacatalog_client = datacatalog_v1.DataCatalogClient()
51+
52+
# Create a Tag Template.
53+
tag_template = datacatalog_v1.types.TagTemplate()
54+
55+
tag_template.display_name = "Demo Tag Template"
56+
57+
tag_template.fields["source"] = datacatalog_v1.types.TagTemplateField()
58+
tag_template.fields["source"].display_name = "Source of data asset"
59+
tag_template.fields[
60+
"source"
61+
].type_.primitive_type = datacatalog_v1.types.FieldType.PrimitiveType.STRING
62+
63+
tag_template.fields["num_rows"] = datacatalog_v1.types.TagTemplateField()
64+
tag_template.fields["num_rows"].display_name = "Number of rows in data asset"
65+
tag_template.fields[
66+
"num_rows"
67+
].type_.primitive_type = datacatalog_v1.types.FieldType.PrimitiveType.DOUBLE
68+
69+
tag_template.fields["has_pii"] = datacatalog_v1.types.TagTemplateField()
70+
tag_template.fields["has_pii"].display_name = "Has PII"
71+
tag_template.fields[
72+
"has_pii"
73+
].type_.primitive_type = datacatalog_v1.types.FieldType.PrimitiveType.BOOL
74+
75+
tag_template.fields["pii_type"] = datacatalog_v1.types.TagTemplateField()
76+
tag_template.fields["pii_type"].display_name = "PII type"
77+
78+
for display_name in ["EMAIL", "SOCIAL SECURITY NUMBER", "NONE"]:
79+
enum_value = datacatalog_v1.types.FieldType.EnumType.EnumValue(
80+
display_name=display_name
81+
)
82+
tag_template.fields["pii_type"].type_.enum_type.allowed_values.append(
83+
enum_value
84+
)
85+
86+
expected_template_name = datacatalog_v1.DataCatalogClient.tag_template_path(
87+
project_id, location, tag_template_id
88+
)
89+
90+
# Create the Tag Template.
91+
try:
92+
tag_template = datacatalog_client.create_tag_template(
93+
parent=f"projects/{project_id}/locations/{location}",
94+
tag_template_id=tag_template_id,
95+
tag_template=tag_template,
96+
)
97+
print(f"Created template: {tag_template.name}")
98+
except OSError as e:
99+
print(f"Cannot create template: {expected_template_name}")
100+
print(f"{e}")
101+
102+
# Lookup Data Catalog's Entry referring to the table.
103+
resource_name = (
104+
f"//bigquery.googleapis.com/projects/{project_id}"
105+
f"/datasets/{dataset_id}/tables/{table_id}"
106+
)
107+
table_entry = datacatalog_client.lookup_entry(
108+
request={"linked_resource": resource_name}
109+
)
110+
111+
# Attach a Tag to the table.
112+
tag = datacatalog_v1.types.Tag()
113+
114+
tag.template = tag_template.name
115+
tag.name = "my_super_cool_tag"
116+
117+
tag.fields["source"] = datacatalog_v1.types.TagField()
118+
tag.fields["source"].string_value = "Copied from tlc_yellow_trips_2018"
119+
120+
tag.fields["num_rows"] = datacatalog_v1.types.TagField()
121+
tag.fields["num_rows"].double_value = 113496874
122+
123+
tag.fields["has_pii"] = datacatalog_v1.types.TagField()
124+
tag.fields["has_pii"].bool_value = False
125+
126+
tag.fields["pii_type"] = datacatalog_v1.types.TagField()
127+
tag.fields["pii_type"].enum_value.display_name = "NONE"
128+
129+
tag = datacatalog_client.create_tag(parent=table_entry.name, tag=tag)
130+
print(f"Created tag: {tag.name}")
131+
# [END data_catalog_quickstart]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
import quickstart
16+
17+
18+
def test_quickstart(
19+
capsys, client, project_id, dataset_id, table_id, random_tag_template_id
20+
):
21+
location = "us-central1"
22+
override_values = {
23+
"project_id": project_id,
24+
"dataset_id": dataset_id,
25+
"table_id": table_id,
26+
"tag_template_id": random_tag_template_id,
27+
}
28+
tag_template_name = client.tag_template_path(
29+
project_id, location, random_tag_template_id
30+
)
31+
quickstart.quickstart(override_values)
32+
out, err = capsys.readouterr()
33+
assert "Created template: {}".format(tag_template_name) in out
34+
assert "Created tag:" in out
35+
client.delete_tag_template(name=tag_template_name, force=True)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest==7.2.0
2+
google-cloud-bigquery==3.3.5
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-datacatalog==3.9.3

0 commit comments

Comments
 (0)