Last active
September 18, 2022 15:55
-
-
Save ricardolsmendes/c65a5801d555a3af05ebfc82e6fa4d73 to your computer and use it in GitHub Desktop.
Data Catalog, Python: retrieve all Tag Templates for a Given project through catalog search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.cloud import datacatalog | |
# TODO Developer: set the project id. | |
project_id = YOUR-PROJECT-ID | |
client = datacatalog.DataCatalogClient.from_service_account_file(key_file_location) | |
# Search tag templates for a given project. | |
# WARNING: search semantics may not return an exaustive list, | |
# but is the only option currently available. | |
def search_tag_templates_for_project(project_id): | |
scope = datacatalog.types.SearchCatalogRequest.Scope() | |
scope = { | |
"include_project_ids": [project_id] | |
} | |
return [ | |
result for result | |
in client.search_catalog(scope=scope, query='type=tag_template') | |
] | |
# Print search results. | |
search_results = search_tag_templates_for_project(project_id) | |
for search_result in search_results: | |
print(search_result) | |
# Retrive a Tag Template object from its name | |
# (relative_resource_name attribute from a search result) | |
tag_template = client.get_tag_template(name=search_results[0].relative_resource_name) | |
print(tag_template) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment