|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2020 Google LLC. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | +import argparse |
| 19 | + |
| 20 | + |
| 21 | +def list_assets(project_id, asset_types, page_size): |
| 22 | + # [START asset_quickstart_list_assets] |
| 23 | + from google.cloud import asset_v1p5beta1 |
| 24 | + from google.cloud.asset_v1p5beta1 import enums |
| 25 | + |
| 26 | + # TODO project_id = 'Your Google Cloud Project ID' |
| 27 | + # TODO asset_types = 'Your asset type list, e.g., |
| 28 | + # ["storage.googleapis.com/Bucket","bigquery.googleapis.com/Table"]' |
| 29 | + # TODO page_size = 'Num of assets in one page, which must be between 1 and |
| 30 | + # 1000 (both inclusively)' |
| 31 | + |
| 32 | + project_resource = 'projects/{}'.format(project_id) |
| 33 | + content_type = enums.ContentType.RESOURCE |
| 34 | + client = asset_v1p5beta1.AssetServiceClient() |
| 35 | + |
| 36 | + # Call ListAssets v1p5beta1 to list assets. |
| 37 | + response = client.list_assets( |
| 38 | + parent=project_resource, read_time=None, asset_types=asset_types, |
| 39 | + content_type=content_type, page_size=page_size) |
| 40 | + |
| 41 | + for page in response.pages: |
| 42 | + for asset in page: |
| 43 | + print(asset) |
| 44 | + # [END asset_quickstart_list_assets] |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == '__main__': |
| 48 | + parser = argparse.ArgumentParser( |
| 49 | + description=__doc__, |
| 50 | + formatter_class=argparse.RawDescriptionHelpFormatter |
| 51 | + ) |
| 52 | + parser.add_argument('project_id', help='Your Google Cloud project ID') |
| 53 | + parser.add_argument( |
| 54 | + 'asset_types', |
| 55 | + help='The types of the assets to list, comma delimited, e.g., ' |
| 56 | + 'storage.googleapis.com/Bucket') |
| 57 | + parser.add_argument( |
| 58 | + 'page_size', |
| 59 | + help='Num of assets in one page, which must be between 1 and 1000 ' |
| 60 | + '(both inclusively)') |
| 61 | + |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + asset_type_list = args.asset_types.split(',') |
| 65 | + |
| 66 | + list_assets(args.project_id, asset_type_list, int(args.page_size)) |
0 commit comments