Skip to content

Instantly share code, notes, and snippets.

@ricardolsmendes
Last active September 18, 2022 15:55
Show Gist options
  • Save ricardolsmendes/c65a5801d555a3af05ebfc82e6fa4d73 to your computer and use it in GitHub Desktop.
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
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