From 1096751b36b8513cfd543b10e5d18ceeacabc709 Mon Sep 17 00:00:00 2001 From: Shashank Joshi Date: Sun, 16 Apr 2023 10:35:56 -0500 Subject: [PATCH] quickstart list assets from Org & Folders Current code accepts only projects as "parent" for the Asset API, whereas the API can list assets from Orgs and Folders as well. A small change to accept the other two options for "parent" will make the code more useful. Many people use Asset API to search for resources across multiple projects. The current code at has `project_id` as an argument, instead we can have `parent_resource` as an argument, and the `parent_resource` can be either projects/ or folders/ or organizations/. We then can change references to `project_id` with `parent_resource` in other places. --- asset/snippets/quickstart_listassets.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/asset/snippets/quickstart_listassets.py b/asset/snippets/quickstart_listassets.py index 3cd2969196..dfd6769975 100644 --- a/asset/snippets/quickstart_listassets.py +++ b/asset/snippets/quickstart_listassets.py @@ -18,24 +18,16 @@ import argparse -def list_assets(project_id, asset_types, page_size, content_type): +def list_assets(parent_resource, asset_types, page_size, content_type): # [START asset_quickstart_list_assets] from google.cloud import asset_v1 - # TODO project_id = 'Your Google Cloud Project ID' - # TODO asset_types = 'Your asset type list, e.g., - # ["storage.googleapis.com/Bucket","bigquery.googleapis.com/Table"]' - # TODO page_size = 'Num of assets in one page, which must be between 1 and - # 1000 (both inclusively)' - # TODO content_type ="Content type to list" - - project_resource = "projects/{}".format(project_id) client = asset_v1.AssetServiceClient() # Call ListAssets v1 to list assets. response = client.list_assets( request={ - "parent": project_resource, + "parent": parent_resource, "read_time": None, "asset_types": asset_types, "content_type": content_type, @@ -52,7 +44,7 @@ def list_assets(project_id, asset_types, page_size, content_type): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter ) - parser.add_argument("project_id", help="Your Google Cloud project ID") + parser.add_argument("parent_resource", help="Parent resource. Either projects/ or folders/ or organizations/") parser.add_argument( "asset_types", help="The types of the assets to list, comma delimited, e.g., " @@ -63,10 +55,10 @@ def list_assets(project_id, asset_types, page_size, content_type): help="Num of assets in one page, which must be between 1 and 1000 " "(both inclusively)", ) - parser.add_argument("content_type", help="Content type to list") + parser.add_argument("content_type", help="Content type to list, e.g. RESOURCE see more options https://cloud.google.com/asset-inventory/docs/reference/rest/v1/feeds#ContentType") args = parser.parse_args() asset_type_list = args.asset_types.split(",") - list_assets(args.project_id, asset_type_list, int(args.page_size), args.content_type) + list_assets(args.parent_resource, asset_type_list, int(args.page_size), args.content_type)