-
Notifications
You must be signed in to change notification settings - Fork 6.5k
[asset] Add sample code for two new RPCs. #4080
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 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b5cdf9b
Add sample code for two new RPCs.
yuyifan-google 34925a9
Merge branch 'master' into master
yuyifan-google 01945fd
Merge branch 'master' into master
yuyifan-google a257851
Use named args when calling client api; Check for NotFound exception …
yuyifan-google 5bd2acc
Merge remote-tracking branch 'origin/master'
yuyifan-google 4d80c95
Merge branch 'master' into master
yuyifan-google 59b00b0
Merge branch 'master' into master
yuyifan-google 0e416e8
Merge branch 'master' into master
yuyifan-google a14c2eb
Wait some time for the dataset creation event to propagate, instead o…
yuyifan-google f949695
Merge branch 'master' into master
yuyifan-google 2d6f3c3
Use backoff to retry the test until the dataset is created.
yuyifan-google 3976277
Merge branch 'master' into master
yuyifan-google 49f2152
Merge branch 'master' into master
yuyifan-google 1818aae
Use backoff to retry the test until the dataset is created.
yuyifan-google 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,48 @@ | ||
#!/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 search_all_iam_policies(scope, query=None, page_size=None): | ||
# [START asset_quickstart_search_all_iam_policies] | ||
from google.cloud import asset_v1 | ||
|
||
client = asset_v1.AssetServiceClient() | ||
response = client.search_all_iam_policies( | ||
scope, query=query, page_size=page_size) | ||
for page in response.pages: | ||
for policy in page: | ||
print(policy) | ||
break | ||
# [END asset_quickstart_search_all_iam_policies] | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
parser.add_argument( | ||
'scope', | ||
help='The search is limited to the resources within the scope.') | ||
parser.add_argument('--query', help='The query statement.') | ||
parser.add_argument( | ||
'--page_size', | ||
type=int, | ||
help='The page size for search result pagination.') | ||
args = parser.parse_args() | ||
search_all_iam_policies(args.scope, args.query, args.page_size) |
29 changes: 29 additions & 0 deletions
29
asset/cloud-client/quickstart_searchalliampolicies_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,29 @@ | ||
#!/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_searchalliampolicies | ||
|
||
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] | ||
|
||
|
||
def test_search_all_iam_policies(capsys): | ||
scope = "projects/{}".format(PROJECT) | ||
query = "policy:roles/owner" | ||
quickstart_searchalliampolicies.search_all_iam_policies(scope, query=query) | ||
out, _ = capsys.readouterr() | ||
assert "roles/owner" in out |
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,62 @@ | ||
#!/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 search_all_resources(scope, | ||
query=None, | ||
asset_types=None, | ||
page_size=None, | ||
order_by=None): | ||
# [START asset_quickstart_search_all_resources] | ||
from google.cloud import asset_v1 | ||
|
||
client = asset_v1.AssetServiceClient() | ||
response = client.search_all_resources( | ||
scope, | ||
query=query, | ||
asset_types=asset_types, | ||
page_size=page_size, | ||
order_by=order_by) | ||
for page in response.pages: | ||
for resource in page: | ||
print(resource) | ||
break | ||
# [END asset_quickstart_search_all_resources] | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
parser.add_argument( | ||
'scope', | ||
help='The search is limited to the resources within the scope.') | ||
parser.add_argument('--query', help='The query statement.') | ||
parser.add_argument( | ||
'--asset_types', nargs='+', help='A list of asset types to search for.') | ||
parser.add_argument( | ||
'--page_size', | ||
type=int, | ||
help='The page size for search result pagination.') | ||
parser.add_argument( | ||
'--order_by', | ||
help='Fields specifying the sorting order of the results.') | ||
args = parser.parse_args() | ||
search_all_resources(args.scope, args.query, args.asset_types, | ||
args.page_size, args.order_by) |
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,56 @@ | ||
#!/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 time | ||
import uuid | ||
|
||
from google.api_core.exceptions import NotFound | ||
from google.cloud import bigquery | ||
import pytest | ||
|
||
import quickstart_searchallresources | ||
|
||
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] | ||
DATASET = 'dataset_{}'.format(uuid.uuid4().hex) | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def bigquery_client(): | ||
yield bigquery.Client() | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def asset_dataset(bigquery_client): | ||
dataset = bigquery_client.create_dataset(DATASET) | ||
|
||
yield DATASET | ||
|
||
try: | ||
bigquery_client.delete_dataset(dataset) | ||
except NotFound as e: | ||
print('Failed to delete dataset {}'.format(DATASET)) | ||
raise e | ||
|
||
|
||
def test_search_all_resources(asset_dataset, capsys): | ||
scope = "projects/{}".format(PROJECT) | ||
query = "name:{}".format(DATASET) | ||
time.sleep(10) | ||
quickstart_searchallresources.search_all_resources(scope, query=query) | ||
out, _ = capsys.readouterr() | ||
|
||
assert DATASET in out |
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.