Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: add sample code for ListAssets v1p5beta1 [(#4251)](GoogleCloudP…
…latform/python-docs-samples#4251)

## Description

Fixes #4250

Note: It's a good idea to open an issue first for discussion.

## Checklist
- [x] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md)
- [ ] README is updated to include [all relevant information](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#readme-file)
- [x] **Tests** pass:   `nox -s py-3.6` (see [Test Enviroment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup))
- [x] **Lint** pass:   `nox -s lint` (see [Test Enviroment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup))
- [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones)
- [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones)
- [x] Please **merge** this PR for me once it is approved.
  • Loading branch information
Larittic-GG authored and busunkim96 committed Aug 4, 2020
1 parent cb5cdbc commit 187807f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
66 changes: 66 additions & 0 deletions samples/snippets/quickstart_listassets.py
@@ -0,0 +1,66 @@
#!/usr/bin/env python

# Copyright 2020 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse


def list_assets(project_id, asset_types, page_size):
# [START asset_quickstart_list_assets]
from google.cloud import asset_v1p5beta1
from google.cloud.asset_v1p5beta1 import enums

# 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)'

project_resource = 'projects/{}'.format(project_id)
content_type = enums.ContentType.RESOURCE
client = asset_v1p5beta1.AssetServiceClient()

# Call ListAssets v1p5beta1 to list assets.
response = client.list_assets(
parent=project_resource, read_time=None, asset_types=asset_types,
content_type=content_type, page_size=page_size)

for page in response.pages:
for asset in page:
print(asset)
# [END asset_quickstart_list_assets]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('project_id', help='Your Google Cloud project ID')
parser.add_argument(
'asset_types',
help='The types of the assets to list, comma delimited, e.g., '
'storage.googleapis.com/Bucket')
parser.add_argument(
'page_size',
help='Num of assets in one page, which must be between 1 and 1000 '
'(both inclusively)')

args = parser.parse_args()

asset_type_list = args.asset_types.split(',')

list_assets(args.project_id, asset_type_list, int(args.page_size))
27 changes: 27 additions & 0 deletions samples/snippets/quickstart_listassets_test.py
@@ -0,0 +1,27 @@
#!/usr/bin/env python

# Copyright 2020 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import quickstart_listassets

PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']


def test_list_assets(capsys):
quickstart_listassets.list_assets(project_id=PROJECT, asset_types=[], page_size=10)
out, _ = capsys.readouterr()
assert 'asset' in out

0 comments on commit 187807f

Please sign in to comment.