Skip to content

Commit 0a19b43

Browse files
lvvvvvfdandhlee
authored andcommitted
feat: add relationship samples (#293)
* Add export to BigQuery * Add comments to variables * fix: fix lint * fix: fix the test * feat: add relationship samples * fix: fix build errors in main * fix: fix feed tests
1 parent 56e7c16 commit 0a19b43

7 files changed

+44
-13
lines changed

asset/snippets/snippets/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ def another_topic():
5656

5757
@pytest.fixture(scope="module")
5858
def test_feed(test_topic):
59+
from google.cloud import asset_v1
60+
5961
feed_id = f"feed-{uuid.uuid4().hex}"
6062
asset_name = f"assets-{uuid.uuid4().hex}"
6163

6264
@backoff.on_exception(backoff.expo, InternalServerError, max_time=60)
6365
def create_feed():
6466
return quickstart_createfeed.create_feed(
65-
PROJECT, feed_id, [asset_name], test_topic.name
67+
PROJECT, feed_id, [asset_name], test_topic.name, asset_v1.ContentType.RESOURCE
6668
)
6769

6870
feed = create_feed()

asset/snippets/snippets/quickstart_createfeed.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@
1818
import argparse
1919

2020

21-
def create_feed(project_id, feed_id, asset_names, topic):
21+
def create_feed(project_id, feed_id, asset_names, topic, content_type):
2222
# [START asset_quickstart_create_feed]
2323
from google.cloud import asset_v1
2424

2525
# TODO project_id = 'Your Google Cloud Project ID'
2626
# TODO feed_id = 'Feed ID you want to create'
2727
# TODO asset_names = 'List of asset names the feed listen to'
2828
# TODO topic = "Topic name of the feed"
29+
# TODO content_type ="Content type of the feed"
2930

3031
client = asset_v1.AssetServiceClient()
3132
parent = "projects/{}".format(project_id)
3233
feed = asset_v1.Feed()
3334
feed.asset_names.extend(asset_names)
3435
feed.feed_output_config.pubsub_destination.topic = topic
36+
feed.content_type = content_type
3537
response = client.create_feed(
3638
request={"parent": parent, "feed_id": feed_id, "feed": feed}
3739
)
@@ -48,5 +50,6 @@ def create_feed(project_id, feed_id, asset_names, topic):
4850
parser.add_argument("feed_id", help="Feed ID you want to create")
4951
parser.add_argument("asset_names", help="List of asset names the feed listen to")
5052
parser.add_argument("topic", help="Topic name of the feed")
53+
parser.add_argument("content_type", help="Content type of the feed")
5154
args = parser.parse_args()
52-
create_feed(args.project_id, args.feed_id, args.asset_names, args.topic)
55+
create_feed(args.project_id, args.feed_id, args.asset_names, args.topic, args.content_type)

asset/snippets/snippets/quickstart_createfeed_test.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@
2323
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
2424
ASSET_NAME = "assets-{}".format(uuid.uuid4().hex)
2525
FEED_ID = "feed-{}".format(uuid.uuid4().hex)
26+
FEED_ID_R = "feed-{}".format(uuid.uuid4().hex)
2627

2728

2829
def test_create_feed(capsys, test_topic, deleter):
30+
from google.cloud import asset_v1
31+
2932
feed = quickstart_createfeed.create_feed(
30-
PROJECT, FEED_ID, [ASSET_NAME], test_topic.name
31-
)
33+
PROJECT, FEED_ID, [ASSET_NAME], test_topic.name,
34+
asset_v1.ContentType.RESOURCE)
3235
deleter.append(feed.name)
3336
out, _ = capsys.readouterr()
3437
assert "feed" in out
38+
39+
feed_r = quickstart_createfeed.create_feed(
40+
PROJECT, FEED_ID_R, [ASSET_NAME], test_topic.name,
41+
asset_v1.ContentType.RELATIONSHIP)
42+
deleter.append(feed_r.name)
43+
out_r, _ = capsys.readouterr()
44+
assert "feed" in out_r

asset/snippets/snippets/quickstart_exportassets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ def export_assets(project_id, dump_file_path):
3636
# [END asset_quickstart_export_assets]
3737

3838

39-
def export_assets_bigquery(project_id, dataset, table):
39+
def export_assets_bigquery(project_id, dataset, table, content_type):
4040
# [START asset_quickstart_export_assets_bigquery]
4141
from google.cloud import asset_v1
4242

4343
# TODO project_id = 'Your Google Cloud Project ID'
4444
# TODO dataset = 'Your BigQuery dataset path'
4545
# TODO table = 'Your BigQuery table name'
46+
# TODO content_type ="Content type to export"
4647

4748
client = asset_v1.AssetServiceClient()
4849
parent = "projects/{}".format(project_id)
49-
content_type = asset_v1.ContentType.RESOURCE
5050
output_config = asset_v1.OutputConfig()
5151
output_config.bigquery_destination.dataset = dataset
5252
output_config.bigquery_destination.table = table

asset/snippets/snippets/quickstart_exportassets_test.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import uuid
1919

20+
from google.cloud import asset_v1
2021
from google.cloud import bigquery
2122
from google.cloud import storage
2223
import pytest
@@ -70,8 +71,15 @@ def test_export_assets(asset_bucket, dataset, capsys):
7071
out, _ = capsys.readouterr()
7172
assert dump_file_path in out
7273

74+
content_type = asset_v1.ContentType.RESOURCE
7375
dataset_id = "projects/{}/datasets/{}".format(PROJECT, dataset)
7476
quickstart_exportassets.export_assets_bigquery(
75-
PROJECT, dataset_id, "assettable")
77+
PROJECT, dataset_id, "assettable", content_type)
7678
out, _ = capsys.readouterr()
7779
assert dataset_id in out
80+
81+
content_type_r = asset_v1.ContentType.RELATIONSHIP
82+
quickstart_exportassets.export_assets_bigquery(
83+
PROJECT, dataset_id, "assettable", content_type_r)
84+
out_r, _ = capsys.readouterr()
85+
assert dataset_id in out_r

asset/snippets/snippets/quickstart_listassets.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import argparse
1919

2020

21-
def list_assets(project_id, asset_types, page_size):
21+
def list_assets(project_id, asset_types, page_size, content_type):
2222
# [START asset_quickstart_list_assets]
2323
from google.cloud import asset_v1
2424

@@ -27,9 +27,9 @@ def list_assets(project_id, asset_types, page_size):
2727
# ["storage.googleapis.com/Bucket","bigquery.googleapis.com/Table"]'
2828
# TODO page_size = 'Num of assets in one page, which must be between 1 and
2929
# 1000 (both inclusively)'
30+
# TODO content_type ="Content type to list"
3031

3132
project_resource = "projects/{}".format(project_id)
32-
content_type = asset_v1.ContentType.RESOURCE
3333
client = asset_v1.AssetServiceClient()
3434

3535
# Call ListAssets v1 to list assets.
@@ -63,9 +63,10 @@ def list_assets(project_id, asset_types, page_size):
6363
help="Num of assets in one page, which must be between 1 and 1000 "
6464
"(both inclusively)",
6565
)
66+
parser.add_argument("content_type", help="Content type to list")
6667

6768
args = parser.parse_args()
6869

6970
asset_type_list = args.asset_types.split(",")
7071

71-
list_assets(args.project_id, asset_type_list, int(args.page_size))
72+
list_assets(args.project_id, asset_type_list, int(args.page_size), args.content_type)

asset/snippets/snippets/quickstart_listassets_test.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@
2222

2323

2424
def test_list_assets(capsys):
25+
from google.cloud import asset_v1
26+
2527
quickstart_listassets.list_assets(
26-
project_id=PROJECT, asset_types=["iam.googleapis.com/Role"], page_size=10
27-
)
28+
project_id=PROJECT, asset_types=["iam.googleapis.com/Role"], page_size=10,
29+
content_type=asset_v1.ContentType.RESOURCE)
2830
out, _ = capsys.readouterr()
2931
assert "asset" in out
32+
33+
quickstart_listassets.list_assets(
34+
project_id=PROJECT, asset_types=[], page_size=10, content_type=asset_v1.ContentType.RELATIONSHIP)
35+
out_r, _ = capsys.readouterr()
36+
assert "asset" in out_r

0 commit comments

Comments
 (0)