Skip to content

Commit 713a76e

Browse files
arithmetic1728dandhlee
authored andcommitted
feat!: move to microgenerator (#58)
* feat!: move to microgenerator * update UPGRADING.md * rebase after the samples are moved * update sample * update sample * update sample * update sample * fix linter * fix linter
1 parent 68c031f commit 713a76e

20 files changed

+189
-181
lines changed

asset/snippets/snippets/conftest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import quickstart_deletefeed
2828

2929

30-
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
30+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
3131

3232

3333
@pytest.fixture(scope="module")
3434
def test_topic():
35-
topic_id = f'topic-{uuid.uuid4().hex}'
35+
topic_id = f"topic-{uuid.uuid4().hex}"
3636
publisher = pubsub_v1.PublisherClient()
3737
topic_path = publisher.topic_path(PROJECT, topic_id)
3838
topic = publisher.create_topic(topic_path)
@@ -44,7 +44,7 @@ def test_topic():
4444

4545
@pytest.fixture(scope="module")
4646
def another_topic():
47-
topic_id = f'topic-{uuid.uuid4().hex}'
47+
topic_id = f"topic-{uuid.uuid4().hex}"
4848
publisher = pubsub_v1.PublisherClient()
4949
topic_path = publisher.topic_path(PROJECT, topic_id)
5050
topic = publisher.create_topic(topic_path)
@@ -56,13 +56,14 @@ def another_topic():
5656

5757
@pytest.fixture(scope="module")
5858
def test_feed(test_topic):
59-
feed_id = f'feed-{uuid.uuid4().hex}'
60-
asset_name = f'assets-{uuid.uuid4().hex}'
59+
feed_id = f"feed-{uuid.uuid4().hex}"
60+
asset_name = f"assets-{uuid.uuid4().hex}"
6161

6262
@backoff.on_exception(backoff.expo, InternalServerError, max_time=60)
6363
def create_feed():
6464
return quickstart_createfeed.create_feed(
65-
PROJECT, feed_id, [asset_name, ], test_topic.name)
65+
PROJECT, feed_id, [asset_name], test_topic.name
66+
)
6667

6768
feed = create_feed()
6869

asset/snippets/snippets/noxfile.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,22 @@
3737

3838
TEST_CONFIG = {
3939
# You can opt out from the test for specific Python versions.
40-
'ignored_versions': ["2.7"],
41-
40+
"ignored_versions": ["2.7"],
4241
# An envvar key for determining the project id to use. Change it
4342
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4443
# build specific Cloud project. You can also use your own string
4544
# to use your own Cloud project.
46-
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
45+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
4746
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
48-
4947
# A dictionary you want to inject into your test. Don't put any
5048
# secrets here. These values will override predefined values.
51-
'envs': {},
49+
"envs": {},
5250
}
5351

5452

5553
try:
5654
# Ensure we can import noxfile_config in the project's directory.
57-
sys.path.append('.')
55+
sys.path.append(".")
5856
from noxfile_config import TEST_CONFIG_OVERRIDE
5957
except ImportError as e:
6058
print("No user noxfile_config found: detail: {}".format(e))
@@ -69,12 +67,12 @@ def get_pytest_env_vars():
6967
ret = {}
7068

7169
# Override the GCLOUD_PROJECT and the alias.
72-
env_key = TEST_CONFIG['gcloud_project_env']
70+
env_key = TEST_CONFIG["gcloud_project_env"]
7371
# This should error out if not set.
74-
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
72+
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
7573

7674
# Apply user supplied envs.
77-
ret.update(TEST_CONFIG['envs'])
75+
ret.update(TEST_CONFIG["envs"])
7876
return ret
7977

8078

@@ -83,7 +81,7 @@ def get_pytest_env_vars():
8381
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
8482

8583
# Any default versions that should be ignored.
86-
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
84+
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
8785

8886
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
8987

@@ -138,7 +136,7 @@ def lint(session):
138136
args = FLAKE8_COMMON_ARGS + [
139137
"--application-import-names",
140138
",".join(local_names),
141-
"."
139+
".",
142140
]
143141
session.run("flake8", *args)
144142

@@ -182,9 +180,9 @@ def py(session):
182180
if session.python in TESTED_VERSIONS:
183181
_session_tests(session)
184182
else:
185-
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
186-
session.python
187-
))
183+
session.skip(
184+
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
185+
)
188186

189187

190188
#

asset/snippets/snippets/quickstart_batchgetassetshistory.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,40 @@
2121
def batch_get_assets_history(project_id, asset_names):
2222
# [START asset_quickstart_batch_get_assets_history]
2323
from google.cloud import asset_v1
24-
from google.cloud.asset_v1.proto import assets_pb2
25-
from google.cloud.asset_v1 import enums
2624

2725
# TODO project_id = 'Your Google Cloud Project ID'
2826
# TODO asset_names = 'Your asset names list, e.g.:
2927
# ["//storage.googleapis.com/[BUCKET_NAME]",]'
3028

3129
client = asset_v1.AssetServiceClient()
32-
parent = client.project_path(project_id)
33-
content_type = enums.ContentType.RESOURCE
34-
read_time_window = assets_pb2.TimeWindow()
30+
parent = "projects/{}".format(project_id)
31+
content_type = asset_v1.ContentType.RESOURCE
32+
read_time_window = asset_v1.TimeWindow()
3533
response = client.batch_get_assets_history(
36-
parent, content_type, read_time_window, asset_names)
37-
print('assets: {}'.format(response.assets))
34+
request={
35+
"parent": parent,
36+
"asset_names": asset_names,
37+
"content_type": content_type,
38+
"read_time_window": read_time_window,
39+
}
40+
)
41+
print("assets: {}".format(response.assets))
3842
# [END asset_quickstart_batch_get_assets_history]
3943

4044

41-
if __name__ == '__main__':
45+
if __name__ == "__main__":
4246
parser = argparse.ArgumentParser(
43-
description=__doc__,
44-
formatter_class=argparse.RawDescriptionHelpFormatter
47+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
4548
)
46-
parser.add_argument('project_id', help='Your Google Cloud project ID')
49+
parser.add_argument("project_id", help="Your Google Cloud project ID")
4750
parser.add_argument(
48-
'asset_names',
49-
help='The asset names for which history will be fetched, comma '
50-
'delimited, e.g.: //storage.googleapis.com/[BUCKET_NAME]')
51+
"asset_names",
52+
help="The asset names for which history will be fetched, comma "
53+
"delimited, e.g.: //storage.googleapis.com/[BUCKET_NAME]",
54+
)
5155

5256
args = parser.parse_args()
5357

54-
asset_name_list = args.asset_names.split(',')
58+
asset_name_list = args.asset_names.split(",")
5559

5660
batch_get_assets_history(args.project_id, asset_name_list)

asset/snippets/snippets/quickstart_batchgetassetshistory_test.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424

2525
import quickstart_batchgetassetshistory
2626

27-
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
28-
BUCKET = 'assets-{}'.format(uuid.uuid4().hex)
27+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
28+
BUCKET = "assets-{}".format(uuid.uuid4().hex)
2929

3030

31-
@pytest.fixture(scope='module')
31+
@pytest.fixture(scope="module")
3232
def storage_client():
3333
yield storage.Client()
3434

3535

36-
@pytest.fixture(scope='module')
36+
@pytest.fixture(scope="module")
3737
def asset_bucket(storage_client):
3838
bucket = storage_client.create_bucket(BUCKET)
3939

@@ -42,20 +42,19 @@ def asset_bucket(storage_client):
4242
try:
4343
bucket.delete(force=True)
4444
except Exception as e:
45-
print('Failed to delete bucket{}'.format(BUCKET))
45+
print("Failed to delete bucket{}".format(BUCKET))
4646
raise e
4747

4848

4949
def test_batch_get_assets_history(asset_bucket, capsys):
50-
bucket_asset_name = '//storage.googleapis.com/{}'.format(BUCKET)
51-
asset_names = [bucket_asset_name, ]
50+
bucket_asset_name = "//storage.googleapis.com/{}".format(BUCKET)
51+
asset_names = [
52+
bucket_asset_name,
53+
]
5254

53-
@backoff.on_exception(
54-
backoff.expo, (AssertionError, InvalidArgument), max_time=30
55-
)
55+
@backoff.on_exception(backoff.expo, (AssertionError, InvalidArgument), max_time=30)
5656
def eventually_consistent_test():
57-
quickstart_batchgetassetshistory.batch_get_assets_history(
58-
PROJECT, asset_names)
57+
quickstart_batchgetassetshistory.batch_get_assets_history(PROJECT, asset_names)
5958
out, _ = capsys.readouterr()
6059

6160
assert bucket_asset_name in out

asset/snippets/snippets/quickstart_createfeed.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
def create_feed(project_id, feed_id, asset_names, topic):
2222
# [START asset_quickstart_create_feed]
2323
from google.cloud import asset_v1
24-
from google.cloud.asset_v1.proto import asset_service_pb2
2524

2625
# TODO project_id = 'Your Google Cloud Project ID'
2726
# TODO feed_id = 'Feed ID you want to create'
@@ -30,23 +29,24 @@ def create_feed(project_id, feed_id, asset_names, topic):
3029

3130
client = asset_v1.AssetServiceClient()
3231
parent = "projects/{}".format(project_id)
33-
feed = asset_service_pb2.Feed()
32+
feed = asset_v1.Feed()
3433
feed.asset_names.extend(asset_names)
3534
feed.feed_output_config.pubsub_destination.topic = topic
36-
response = client.create_feed(parent, feed_id, feed)
37-
print('feed: {}'.format(response))
35+
response = client.create_feed(
36+
request={"parent": parent, "feed_id": feed_id, "feed": feed}
37+
)
38+
print("feed: {}".format(response))
3839
# [END asset_quickstart_create_feed]
3940
return response
4041

4142

42-
if __name__ == '__main__':
43+
if __name__ == "__main__":
4344
parser = argparse.ArgumentParser(
44-
description=__doc__,
45-
formatter_class=argparse.RawDescriptionHelpFormatter)
46-
parser.add_argument('project_id', help='Your Google Cloud project ID')
47-
parser.add_argument('feed_id', help='Feed ID you want to create')
48-
parser.add_argument('asset_names',
49-
help='List of asset names the feed listen to')
50-
parser.add_argument('topic', help='Topic name of the feed')
45+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
46+
)
47+
parser.add_argument("project_id", help="Your Google Cloud project ID")
48+
parser.add_argument("feed_id", help="Feed ID you want to create")
49+
parser.add_argument("asset_names", help="List of asset names the feed listen to")
50+
parser.add_argument("topic", help="Topic name of the feed")
5151
args = parser.parse_args()
5252
create_feed(args.project_id, args.feed_id, args.asset_names, args.topic)

asset/snippets/snippets/quickstart_createfeed_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
import quickstart_createfeed
2121

2222

23-
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
24-
ASSET_NAME = 'assets-{}'.format(uuid.uuid4().hex)
25-
FEED_ID = 'feed-{}'.format(uuid.uuid4().hex)
23+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
24+
ASSET_NAME = "assets-{}".format(uuid.uuid4().hex)
25+
FEED_ID = "feed-{}".format(uuid.uuid4().hex)
2626

2727

2828
def test_create_feed(capsys, test_topic, deleter):
2929
feed = quickstart_createfeed.create_feed(
30-
PROJECT, FEED_ID, [ASSET_NAME, ], test_topic.name)
30+
PROJECT, FEED_ID, [ASSET_NAME], test_topic.name
31+
)
3132
deleter.append(feed.name)
3233
out, _ = capsys.readouterr()
3334
assert "feed" in out

asset/snippets/snippets/quickstart_deletefeed.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ def delete_feed(feed_name):
2525
# TODO feed_name = 'Feed name you want to delete'
2626

2727
client = asset_v1.AssetServiceClient()
28-
client.delete_feed(feed_name)
29-
print('deleted_feed')
28+
client.delete_feed(request={"name": feed_name})
29+
print("deleted_feed")
3030
# [END asset_quickstart_delete_feed]
3131

3232

33-
if __name__ == '__main__':
33+
if __name__ == "__main__":
3434
parser = argparse.ArgumentParser(
35-
description=__doc__,
36-
formatter_class=argparse.RawDescriptionHelpFormatter)
37-
parser.add_argument('feed_name', help='Feed name you want to delete')
35+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
36+
)
37+
parser.add_argument("feed_name", help="Feed name you want to delete")
3838
args = parser.parse_args()
3939
delete_feed(args.feed_name)

asset/snippets/snippets/quickstart_deletefeed_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import quickstart_deletefeed
2020

2121

22-
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
22+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
2323

2424

2525
def test_delete_feed(capsys, test_feed):

asset/snippets/snippets/quickstart_exportassets.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,32 @@
2121
def export_assets(project_id, dump_file_path):
2222
# [START asset_quickstart_export_assets]
2323
from google.cloud import asset_v1
24-
from google.cloud.asset_v1.proto import asset_service_pb2
2524

2625
# TODO project_id = 'Your Google Cloud Project ID'
2726
# TODO dump_file_path = 'Your asset dump file path'
2827

2928
client = asset_v1.AssetServiceClient()
30-
parent = client.project_path(project_id)
31-
output_config = asset_service_pb2.OutputConfig()
29+
parent = "projects/{}".format(project_id)
30+
output_config = asset_v1.OutputConfig()
3231
output_config.gcs_destination.uri = dump_file_path
33-
response = client.export_assets(parent, output_config)
32+
response = client.export_assets(
33+
request={"parent": parent, "output_config": output_config}
34+
)
3435
print(response.result())
3536
# [END asset_quickstart_export_assets]
3637

3738

38-
if __name__ == '__main__':
39+
if __name__ == "__main__":
3940

4041
parser = argparse.ArgumentParser(
41-
description=__doc__,
42-
formatter_class=argparse.RawDescriptionHelpFormatter
42+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
4343
)
44-
parser.add_argument('project_id', help='Your Google Cloud project ID')
44+
parser.add_argument("project_id", help="Your Google Cloud project ID")
4545
parser.add_argument(
46-
'dump_file_path',
47-
help='The file ExportAssets API will dump assets to, '
48-
'e.g.: gs://<bucket-name>/asset_dump_file')
46+
"dump_file_path",
47+
help="The file ExportAssets API will dump assets to, "
48+
"e.g.: gs://<bucket-name>/asset_dump_file",
49+
)
4950

5051
args = parser.parse_args()
5152

asset/snippets/snippets/quickstart_exportassets_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222

2323
import quickstart_exportassets
2424

25-
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
26-
BUCKET = 'assets-{}'.format(uuid.uuid4().hex)
25+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
26+
BUCKET = "assets-{}".format(uuid.uuid4().hex)
2727

2828

29-
@pytest.fixture(scope='module')
29+
@pytest.fixture(scope="module")
3030
def storage_client():
3131
yield storage.Client()
3232

3333

34-
@pytest.fixture(scope='module')
34+
@pytest.fixture(scope="module")
3535
def asset_bucket(storage_client):
3636
bucket = storage_client.create_bucket(BUCKET)
3737

@@ -40,12 +40,12 @@ def asset_bucket(storage_client):
4040
try:
4141
bucket.delete(force=True)
4242
except Exception as e:
43-
print('Failed to delete bucket{}'.format(BUCKET))
43+
print("Failed to delete bucket{}".format(BUCKET))
4444
raise e
4545

4646

4747
def test_export_assets(asset_bucket, capsys):
48-
dump_file_path = 'gs://{}/assets-dump.txt'.format(asset_bucket)
48+
dump_file_path = "gs://{}/assets-dump.txt".format(asset_bucket)
4949
quickstart_exportassets.export_assets(PROJECT, dump_file_path)
5050
out, _ = capsys.readouterr()
5151

0 commit comments

Comments
 (0)