Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions asset/cloud-client/quickstart_createfeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python

# Copyright 2019 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 create_feed(project_id, feed_id, asset_names, topic):
# [START asset_quickstart_create_feed]
from google.cloud import asset_v1p2beta1
from google.cloud.asset_v1p2beta1.proto import asset_service_pb2

# TODO project_id = 'Your Google Cloud Project ID'
# TODO feed_id = 'Feed ID you want to create'
# TODO asset_names = 'List of asset names the feed listen to'
# TODO topic = "Topic name of the feed"

client = asset_v1p2beta1.AssetServiceClient()
parent = "projects/{}".format(project_id)
feed = asset_service_pb2.Feed()
feed.asset_names.extend(asset_names)
feed.feed_output_config.pubsub_destination.topic = topic
response = client.create_feed(parent, feed_id, feed)
print('feed: {}'.format(response))
# [END asset_quickstart_create_feed]


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('feed_id', help='Feed ID you want to create')
parser.add_argument('asset_names',
help='List of asset names the feed listen to')
parser.add_argument('topic', help='Topic name of the feed')
args = parser.parse_args()
create_feed(args.project_id, args.feed_id, args.asset_names, args.topic)
44 changes: 44 additions & 0 deletions asset/cloud-client/quickstart_createfeed_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/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 json
import os
import time

import quickstart_createfeed
import quickstart_deletefeed
from google.cloud import resource_manager

json_data = open(os.environ["GOOGLE_APPLICATION_CREDENTIALS"]).read()
data = json.loads(json_data)
PROJECT = data['project_id']
ASSET_NAME = 'assets-{}'.format(int(time.time()))
FEED_ID = 'feed-{}'.format(int(time.time()))
TOPIC = 'topic-{}'.format(int(time.time()))


def test_create_feed(capsys):
client = resource_manager.Client()
project_number = client.fetch_project(PROJECT).number
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
quickstart_createfeed.create_feed(
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
out, _ = capsys.readouterr()
assert "feed" in out

# Clean up, delete the feed
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
quickstart_deletefeed.delete_feed(feed_name)
39 changes: 39 additions & 0 deletions asset/cloud-client/quickstart_deletefeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/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 delete_feed(feed_name):
# [START asset_quickstart_delete_feed]
from google.cloud import asset_v1p2beta1

# TODO feed_name = 'Feed name you want to delete'

client = asset_v1p2beta1.AssetServiceClient()
client.delete_feed(feed_name)
print('deleted_feed')
# [END asset_quickstart_delete_feed]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('feed_name', help='Feed name you want to delete')
args = parser.parse_args()
delete_feed(args.feed_name)
42 changes: 42 additions & 0 deletions asset/cloud-client/quickstart_deletefeed_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/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 os
import time

import quickstart_createfeed
import quickstart_deletefeed
from google.cloud import resource_manager

PROJECT = os.environ['GCLOUD_PROJECT']
ASSET_NAME = 'assets-{}'.format(int(time.time()))
FEED_ID = 'feed-{}'.format(int(time.time()))
TOPIC = 'topic-{}'.format(int(time.time()))


def test_delete_feed(capsys):
client = resource_manager.Client()
project_number = client.fetch_project(PROJECT).number
# First create the feed, which will be deleted later
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
quickstart_createfeed.create_feed(
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)

feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
quickstart_deletefeed.delete_feed(feed_name)

out, _ = capsys.readouterr()
assert "deleted_feed" in out
39 changes: 39 additions & 0 deletions asset/cloud-client/quickstart_getfeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/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 get_feed(feed_name):
# [START asset_quickstart_get_feed]
from google.cloud import asset_v1p2beta1

# TODO feed_name = 'Feed Name you want to get'

client = asset_v1p2beta1.AssetServiceClient()
response = client.get_feed(feed_name)
print('gotten_feed: {}'.format(response))
# [START asset_quickstart_get_feed]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('feed_name', help='Feed Name you want to get')
args = parser.parse_args()
get_feed(args.feed_name)
45 changes: 45 additions & 0 deletions asset/cloud-client/quickstart_getfeed_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/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 os
import time

import quickstart_createfeed
import quickstart_deletefeed
import quickstart_getfeed
from google.cloud import resource_manager

PROJECT = os.environ['GCLOUD_PROJECT']
ASSET_NAME = 'assets-{}'.format(int(time.time()))
FEED_ID = 'feed-{}'.format(int(time.time()))
TOPIC = 'topic-{}'.format(int(time.time()))


def test_get_feed(capsys):
client = resource_manager.Client()
project_number = client.fetch_project(PROJECT).number
# First create the feed, which will be gotten later
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
quickstart_createfeed.create_feed(
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)

feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
quickstart_getfeed.get_feed(feed_name)
out, _ = capsys.readouterr()

assert "gotten_feed" in out
# Clean up and delete the feed
quickstart_deletefeed.delete_feed(feed_name)
41 changes: 41 additions & 0 deletions asset/cloud-client/quickstart_listfeeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/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 list_feeds(parent_resource):
# [START asset_quickstart_list_feeds]
from google.cloud import asset_v1p2beta1

# TODO parent_resource = 'Parent resource you want to list all feeds'

client = asset_v1p2beta1.AssetServiceClient()
response = client.list_feeds(parent_resource)
print('feeds: {}'.format(response.feeds))
# [END asset_quickstart_list_feeds]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'parent_resource',
help='Parent resource you want to list all feeds')
args = parser.parse_args()
list_feeds(args.parent_resource)
28 changes: 28 additions & 0 deletions asset/cloud-client/quickstart_listfeeds_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/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 os

import quickstart_listfeeds

PROJECT = os.environ['GCLOUD_PROJECT']


def test_list_feeds(capsys):
parent_resource = "projects/{}".format(PROJECT)
quickstart_listfeeds.list_feeds(parent_resource)
out, _ = capsys.readouterr()
assert "feeds" in out
49 changes: 49 additions & 0 deletions asset/cloud-client/quickstart_updatefeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/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 update_feed(feed_name, topic):
# [START asset_quickstart_update_feed]
from google.cloud import asset_v1p2beta1
from google.cloud.asset_v1p2beta1.proto import asset_service_pb2
from google.protobuf import field_mask_pb2

# TODO feed_name = 'Feed Name you want to update'
# TODO topic = "Topic name you want to update with"

client = asset_v1p2beta1.AssetServiceClient()
feed = asset_service_pb2.Feed()
feed.name = feed_name
feed.feed_output_config.pubsub_destination.topic = topic
update_mask = field_mask_pb2.FieldMask()
# In this example, we update topic of the feed
update_mask.paths.append("feed_output_config.pubsub_destination.topic")
response = client.update_feed(feed, update_mask)
print('updated_feed: {}'.format(response))
# [END asset_quickstart_update_feed]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('feed_name', help='Feed Name you want to update')
parser.add_argument('topic', help='Topic name you want to update with')
args = parser.parse_args()
update_feed(args.feed_name, args.topic)
Loading