Skip to content

Commit 6996c8e

Browse files
cwxie-googlechenyumic
authored andcommitted
Asset: Add Real Time Feed API Sample Code (GoogleCloudPlatform#2352)
1 parent 41161fb commit 6996c8e

11 files changed

+428
-2
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
import argparse
19+
20+
21+
def create_feed(project_id, feed_id, asset_names, topic):
22+
# [START asset_quickstart_create_feed]
23+
from google.cloud import asset_v1p2beta1
24+
from google.cloud.asset_v1p2beta1.proto import asset_service_pb2
25+
26+
# TODO project_id = 'Your Google Cloud Project ID'
27+
# TODO feed_id = 'Feed ID you want to create'
28+
# TODO asset_names = 'List of asset names the feed listen to'
29+
# TODO topic = "Topic name of the feed"
30+
31+
client = asset_v1p2beta1.AssetServiceClient()
32+
parent = "projects/{}".format(project_id)
33+
feed = asset_service_pb2.Feed()
34+
feed.asset_names.extend(asset_names)
35+
feed.feed_output_config.pubsub_destination.topic = topic
36+
response = client.create_feed(parent, feed_id, feed)
37+
print('feed: {}'.format(response))
38+
# [END asset_quickstart_create_feed]
39+
40+
41+
if __name__ == '__main__':
42+
parser = argparse.ArgumentParser(
43+
description=__doc__,
44+
formatter_class=argparse.RawDescriptionHelpFormatter)
45+
parser.add_argument('project_id', help='Your Google Cloud project ID')
46+
parser.add_argument('feed_id', help='Feed ID you want to create')
47+
parser.add_argument('asset_names',
48+
help='List of asset names the feed listen to')
49+
parser.add_argument('topic', help='Topic name of the feed')
50+
args = parser.parse_args()
51+
create_feed(args.project_id, args.feed_id, args.asset_names, args.topic)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import json
18+
import os
19+
import time
20+
21+
import quickstart_createfeed
22+
import quickstart_deletefeed
23+
from google.cloud import resource_manager
24+
25+
json_data = open(os.environ["GOOGLE_APPLICATION_CREDENTIALS"]).read()
26+
data = json.loads(json_data)
27+
PROJECT = data['project_id']
28+
ASSET_NAME = 'assets-{}'.format(int(time.time()))
29+
FEED_ID = 'feed-{}'.format(int(time.time()))
30+
TOPIC = 'topic-{}'.format(int(time.time()))
31+
32+
33+
def test_create_feed(capsys):
34+
client = resource_manager.Client()
35+
project_number = client.fetch_project(PROJECT).number
36+
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
37+
quickstart_createfeed.create_feed(
38+
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
39+
out, _ = capsys.readouterr()
40+
assert "feed" in out
41+
42+
# Clean up, delete the feed
43+
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
44+
quickstart_deletefeed.delete_feed(feed_name)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
import argparse
19+
20+
21+
def delete_feed(feed_name):
22+
# [START asset_quickstart_delete_feed]
23+
from google.cloud import asset_v1p2beta1
24+
25+
# TODO feed_name = 'Feed name you want to delete'
26+
27+
client = asset_v1p2beta1.AssetServiceClient()
28+
client.delete_feed(feed_name)
29+
print('deleted_feed')
30+
# [END asset_quickstart_delete_feed]
31+
32+
33+
if __name__ == '__main__':
34+
parser = argparse.ArgumentParser(
35+
description=__doc__,
36+
formatter_class=argparse.RawDescriptionHelpFormatter)
37+
parser.add_argument('feed_name', help='Feed name you want to delete')
38+
args = parser.parse_args()
39+
delete_feed(args.feed_name)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
import time
19+
20+
import quickstart_createfeed
21+
import quickstart_deletefeed
22+
from google.cloud import resource_manager
23+
24+
PROJECT = os.environ['GCLOUD_PROJECT']
25+
ASSET_NAME = 'assets-{}'.format(int(time.time()))
26+
FEED_ID = 'feed-{}'.format(int(time.time()))
27+
TOPIC = 'topic-{}'.format(int(time.time()))
28+
29+
30+
def test_delete_feed(capsys):
31+
client = resource_manager.Client()
32+
project_number = client.fetch_project(PROJECT).number
33+
# First create the feed, which will be deleted later
34+
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
35+
quickstart_createfeed.create_feed(
36+
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
37+
38+
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
39+
quickstart_deletefeed.delete_feed(feed_name)
40+
41+
out, _ = capsys.readouterr()
42+
assert "deleted_feed" in out
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
import argparse
19+
20+
21+
def get_feed(feed_name):
22+
# [START asset_quickstart_get_feed]
23+
from google.cloud import asset_v1p2beta1
24+
25+
# TODO feed_name = 'Feed Name you want to get'
26+
27+
client = asset_v1p2beta1.AssetServiceClient()
28+
response = client.get_feed(feed_name)
29+
print('gotten_feed: {}'.format(response))
30+
# [START asset_quickstart_get_feed]
31+
32+
33+
if __name__ == '__main__':
34+
parser = argparse.ArgumentParser(
35+
description=__doc__,
36+
formatter_class=argparse.RawDescriptionHelpFormatter)
37+
parser.add_argument('feed_name', help='Feed Name you want to get')
38+
args = parser.parse_args()
39+
get_feed(args.feed_name)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
import time
19+
20+
import quickstart_createfeed
21+
import quickstart_deletefeed
22+
import quickstart_getfeed
23+
from google.cloud import resource_manager
24+
25+
PROJECT = os.environ['GCLOUD_PROJECT']
26+
ASSET_NAME = 'assets-{}'.format(int(time.time()))
27+
FEED_ID = 'feed-{}'.format(int(time.time()))
28+
TOPIC = 'topic-{}'.format(int(time.time()))
29+
30+
31+
def test_get_feed(capsys):
32+
client = resource_manager.Client()
33+
project_number = client.fetch_project(PROJECT).number
34+
# First create the feed, which will be gotten later
35+
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
36+
quickstart_createfeed.create_feed(
37+
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
38+
39+
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
40+
quickstart_getfeed.get_feed(feed_name)
41+
out, _ = capsys.readouterr()
42+
43+
assert "gotten_feed" in out
44+
# Clean up and delete the feed
45+
quickstart_deletefeed.delete_feed(feed_name)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
import argparse
19+
20+
21+
def list_feeds(parent_resource):
22+
# [START asset_quickstart_list_feeds]
23+
from google.cloud import asset_v1p2beta1
24+
25+
# TODO parent_resource = 'Parent resource you want to list all feeds'
26+
27+
client = asset_v1p2beta1.AssetServiceClient()
28+
response = client.list_feeds(parent_resource)
29+
print('feeds: {}'.format(response.feeds))
30+
# [END asset_quickstart_list_feeds]
31+
32+
33+
if __name__ == '__main__':
34+
parser = argparse.ArgumentParser(
35+
description=__doc__,
36+
formatter_class=argparse.RawDescriptionHelpFormatter)
37+
parser.add_argument(
38+
'parent_resource',
39+
help='Parent resource you want to list all feeds')
40+
args = parser.parse_args()
41+
list_feeds(args.parent_resource)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
19+
import quickstart_listfeeds
20+
21+
PROJECT = os.environ['GCLOUD_PROJECT']
22+
23+
24+
def test_list_feeds(capsys):
25+
parent_resource = "projects/{}".format(PROJECT)
26+
quickstart_listfeeds.list_feeds(parent_resource)
27+
out, _ = capsys.readouterr()
28+
assert "feeds" in out
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
import argparse
19+
20+
21+
def update_feed(feed_name, topic):
22+
# [START asset_quickstart_update_feed]
23+
from google.cloud import asset_v1p2beta1
24+
from google.cloud.asset_v1p2beta1.proto import asset_service_pb2
25+
from google.protobuf import field_mask_pb2
26+
27+
# TODO feed_name = 'Feed Name you want to update'
28+
# TODO topic = "Topic name you want to update with"
29+
30+
client = asset_v1p2beta1.AssetServiceClient()
31+
feed = asset_service_pb2.Feed()
32+
feed.name = feed_name
33+
feed.feed_output_config.pubsub_destination.topic = topic
34+
update_mask = field_mask_pb2.FieldMask()
35+
# In this example, we update topic of the feed
36+
update_mask.paths.append("feed_output_config.pubsub_destination.topic")
37+
response = client.update_feed(feed, update_mask)
38+
print('updated_feed: {}'.format(response))
39+
# [END asset_quickstart_update_feed]
40+
41+
42+
if __name__ == '__main__':
43+
parser = argparse.ArgumentParser(
44+
description=__doc__,
45+
formatter_class=argparse.RawDescriptionHelpFormatter)
46+
parser.add_argument('feed_name', help='Feed Name you want to update')
47+
parser.add_argument('topic', help='Topic name you want to update with')
48+
args = parser.parse_args()
49+
update_feed(args.feed_name, args.topic)

0 commit comments

Comments
 (0)