diff --git a/asset/cloud-client/quickstart_createfeed.py b/asset/cloud-client/quickstart_createfeed.py new file mode 100644 index 00000000000..a36d8bd094f --- /dev/null +++ b/asset/cloud-client/quickstart_createfeed.py @@ -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) diff --git a/asset/cloud-client/quickstart_createfeed_test.py b/asset/cloud-client/quickstart_createfeed_test.py new file mode 100644 index 00000000000..c566fe7c73b --- /dev/null +++ b/asset/cloud-client/quickstart_createfeed_test.py @@ -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) diff --git a/asset/cloud-client/quickstart_deletefeed.py b/asset/cloud-client/quickstart_deletefeed.py new file mode 100644 index 00000000000..81a54b4ff5d --- /dev/null +++ b/asset/cloud-client/quickstart_deletefeed.py @@ -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) diff --git a/asset/cloud-client/quickstart_deletefeed_test.py b/asset/cloud-client/quickstart_deletefeed_test.py new file mode 100644 index 00000000000..e4aa8abd651 --- /dev/null +++ b/asset/cloud-client/quickstart_deletefeed_test.py @@ -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 diff --git a/asset/cloud-client/quickstart_getfeed.py b/asset/cloud-client/quickstart_getfeed.py new file mode 100644 index 00000000000..aab32d242e2 --- /dev/null +++ b/asset/cloud-client/quickstart_getfeed.py @@ -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) diff --git a/asset/cloud-client/quickstart_getfeed_test.py b/asset/cloud-client/quickstart_getfeed_test.py new file mode 100644 index 00000000000..7104db47f3c --- /dev/null +++ b/asset/cloud-client/quickstart_getfeed_test.py @@ -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) diff --git a/asset/cloud-client/quickstart_listfeeds.py b/asset/cloud-client/quickstart_listfeeds.py new file mode 100644 index 00000000000..ebcdd46b895 --- /dev/null +++ b/asset/cloud-client/quickstart_listfeeds.py @@ -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) diff --git a/asset/cloud-client/quickstart_listfeeds_test.py b/asset/cloud-client/quickstart_listfeeds_test.py new file mode 100644 index 00000000000..87678f5d233 --- /dev/null +++ b/asset/cloud-client/quickstart_listfeeds_test.py @@ -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 diff --git a/asset/cloud-client/quickstart_updatefeed.py b/asset/cloud-client/quickstart_updatefeed.py new file mode 100644 index 00000000000..5f82b11540a --- /dev/null +++ b/asset/cloud-client/quickstart_updatefeed.py @@ -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) diff --git a/asset/cloud-client/quickstart_updatefeed_test.py b/asset/cloud-client/quickstart_updatefeed_test.py new file mode 100644 index 00000000000..a47c2777a3e --- /dev/null +++ b/asset/cloud-client/quickstart_updatefeed_test.py @@ -0,0 +1,47 @@ +#!/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_updatefeed +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())) +NEW_TOPIC = 'new-topic-{}'.format(int(time.time())) + + +def test_update_feed(capsys): + client = resource_manager.Client() + project_number = client.fetch_project(PROJECT).number + # First create the feed, which will be updated 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) + new_full_topic_name = "projects/" + PROJECT + "/topics/" + NEW_TOPIC + quickstart_updatefeed.update_feed(feed_name, new_full_topic_name) + out, _ = capsys.readouterr() + + assert "updated_feed" in out + # Clean up and delete the feed + quickstart_deletefeed.delete_feed(feed_name) diff --git a/asset/cloud-client/requirements.txt b/asset/cloud-client/requirements.txt index 0e87ed672ee..46cfaa017f7 100644 --- a/asset/cloud-client/requirements.txt +++ b/asset/cloud-client/requirements.txt @@ -1,2 +1,3 @@ -google-cloud-storage==1.13.2 -google-cloud-asset==0.2.0 +google-cloud-storage==1.18.0 +google-cloud-asset==0.4.1 +google-cloud-resource-manager==0.29.2 \ No newline at end of file