-
Notifications
You must be signed in to change notification settings - Fork 6.6k
[Asset] Add quickstart code for BatchGetAssetsHistory API. #1867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f841d67
[Asset] Test: fix bucket clean up logic.
peter-zheng-g 034e132
Merge branch 'master' into master
gguuss 638d8a5
[Asset] Add quickstart code for BatchGetAssetsHistory API.
peter-zheng-g d25ab80
Merge branch 'master' into master
peter-zheng-g 4bdd834
Merge branch 'master' into master
peter-zheng-g 2f4969c
Merge branch 'master' into master
gguuss 539128b
[Asset] Resolve comments.
peter-zheng-g c0f3f6f
[Asset] Correct typo.
peter-zheng-g af07edd
[Asset] Print assets instead of response.
peter-zheng-g f74320f
[Asset] Minor fix: remove a empty line.
peter-zheng-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2018 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 batch_get_assets_history(project_id, asset_names): | ||
# [START asset_quickstart_batch_get_assets_history] | ||
from google.cloud import asset_v1beta1 | ||
from google.cloud.asset_v1beta1.proto import assets_pb2 | ||
from google.cloud.asset_v1beta1 import enums | ||
|
||
# TODO project_id = 'Your Google Cloud Project ID' | ||
# TODO asset_names = 'Your asset names list, e.g.: | ||
# ["//storage.googleapis.com/[BUCKET_NAME]",]' | ||
|
||
client = asset_v1beta1.AssetServiceClient() | ||
parent = client.project_path(project_id) | ||
content_type = enums.ContentType.RESOURCE | ||
read_time_window = assets_pb2.TimeWindow() | ||
read_time_window.start_time.GetCurrentTime() | ||
response = client.batch_get_assets_history( | ||
parent, content_type, read_time_window, asset_names) | ||
print(response) | ||
# [END asset_quickstart_batch_get_assets_history] | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
peter-zheng-g marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument('project_id', help='Your Google Cloud project ID') | ||
parser.add_argument( | ||
'asset_names', | ||
help='The asset names for which history will be fetched, comma ' | ||
'delimited, e.g.: //storage.googleapis.com/[BUCKET_NAME]') | ||
|
||
args = parser.parse_args() | ||
|
||
asset_name_list = args.asset_names.split(',') | ||
|
||
batch_get_assets_history(args.project_id, asset_name_list) |
55 changes: 55 additions & 0 deletions
55
asset/cloud-client/quickstart_batchgetassetshistory_test.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2018 Google LLC. All Rights Reserved. | ||
peter-zheng-g marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# 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 time | ||
|
||
from google.cloud import storage | ||
import pytest | ||
|
||
import quickstart_batchgetassetshistory | ||
|
||
PROJECT = os.environ['GCLOUD_PROJECT'] | ||
BUCKET = 'assets-{}'.format(int(time.time())) | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def storage_client(): | ||
yield storage.Client() | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def asset_bucket(storage_client): | ||
bucket = storage_client.create_bucket(BUCKET) | ||
|
||
yield BUCKET | ||
|
||
try: | ||
bucket.delete(force=True) | ||
except Exception as e: | ||
print('Failed to delete bucket{}'.format(BUCKET)) | ||
raise e | ||
|
||
|
||
def test_batch_get_assets_history(asset_bucket, capsys): | ||
bucket_asset_name = '//storage.googleapis.com/{}'.format(BUCKET) | ||
asset_names = [bucket_asset_name, ] | ||
quickstart_batchgetassetshistory.batch_get_assets_history( | ||
PROJECT, asset_names) | ||
out, _ = capsys.readouterr() | ||
|
||
if not out: | ||
assert bucket_asset_name in out | ||
peter-zheng-g marked this conversation as resolved.
Show resolved
Hide resolved
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.