Skip to content

[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 10 commits into from
Nov 28, 2018
58 changes: 58 additions & 0 deletions asset/cloud-client/quickstart_batchgetassetshistory.py
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__':

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 asset/cloud-client/quickstart_batchgetassetshistory_test.py
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.
#
# 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
4 changes: 2 additions & 2 deletions asset/cloud-client/quickstart_exportassets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def export_assets(project_id, dump_file_path):
# [START asset_quickstart_exportassets]
# [START asset_quickstart_export_assets]
from google.cloud import asset_v1beta1
from google.cloud.asset_v1beta1.proto import asset_service_pb2

Expand All @@ -32,7 +32,7 @@ def export_assets(project_id, dump_file_path):
output_config.gcs_destination.uri = dump_file_path
response = client.export_assets(parent, output_config)
print(response.result())
# [END asset_quickstart_exportassets]
# [END asset_quickstart_export_assets]


if __name__ == '__main__':
Expand Down