From f004706f2ec491215c0a5f0cc696d1b4d1581f55 Mon Sep 17 00:00:00 2001 From: cwxie-google Date: Mon, 6 Jan 2020 17:29:27 -0800 Subject: [PATCH 1/2] Initial check --- asset/asset.php | 93 ++++++++++++++++++++++++++++++++++++ asset/composer.json | 10 +++- asset/src/create_feed.php | 47 +++++++++++++++++++ asset/src/delete_feed.php | 36 ++++++++++++++ asset/src/get_feed.php | 36 ++++++++++++++ asset/src/list_feeds.php | 36 ++++++++++++++ asset/src/update_feed.php | 44 +++++++++++++++++ asset/test/assetTest.php | 99 +++++++++++++++++++++++++++++++-------- 8 files changed, 379 insertions(+), 22 deletions(-) create mode 100644 asset/src/create_feed.php create mode 100644 asset/src/delete_feed.php create mode 100644 asset/src/get_feed.php create mode 100644 asset/src/list_feeds.php create mode 100644 asset/src/update_feed.php diff --git a/asset/asset.php b/asset/asset.php index f7ff4f978e..69c14c2e21 100644 --- a/asset/asset.php +++ b/asset/asset.php @@ -62,6 +62,99 @@ batch_get_assets_history($projectId, $assetNames); }); +// Create Feed Default ACL command +$application->add(new Command('create-feed')) + ->setDescription('Create real time feed.') + ->setHelp(<<%command.name% command create real time feed. + +php %command.full_name% --help + +EOF + ) + ->addArgument('parent', InputArgument::REQUIRED, 'The parent of the feed') + ->addArgument('feedId', InputArgument::REQUIRED, 'The Id of the feed') + ->addArgument('topic', InputArgument::REQUIRED, 'The topic of the feed') + ->addArgument('assetNames', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'The assets of which the feed will listen to') + + ->setCode(function ($input, $output) { + $parent = $input->getArgument('parent'); + $feedId = $input->getArgument('feedId'); + $topic = $input->getArgument('topic'); + $assetNames = $input->getArgument('assetNames'); + create_feed($parent, $feedId, $topic, $assetNames); + }); + +// Get Feed Default ACL command +$application->add(new Command('get-feed')) + ->setDescription('Get real time feed.') + ->setHelp(<<%command.name% command get real time feed. + +php %command.full_name% --help + +EOF + ) + ->addArgument('feedName', InputArgument::REQUIRED, 'The Name of the feed will be got') + + ->setCode(function ($input, $output) { + $feedName = $input->getArgument('feedName'); + get_feed($feedName); + }); + +// List Feeds Default ACL command +$application->add(new Command('list-feeds')) + ->setDescription('List real time feed under a resource.') + ->setHelp(<<%command.name% command list real time feeds. + +php %command.full_name% --help + +EOF + ) + ->addArgument('parent', InputArgument::REQUIRED, 'The resource parent of the feeds will be got') + + ->setCode(function ($input, $output) { + $parent = $input->getArgument('parent'); + list_feeds($parent); + }); + +// Update Feed Default ACL command +$application->add(new Command('update-feed')) + ->setDescription('Update real time feed.') + ->setHelp(<<%command.name% command update assetNames of a real time feed. + +php %command.full_name% --help + +EOF + ) + ->addArgument('feedName', InputArgument::REQUIRED, 'The Id of the feed') + ->addArgument('assetNames', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'The assets of which the feed will listen to') + + ->setCode(function ($input, $output) { + $feedId = $input->getArgument('feedId'); + $assetNames = $input->getArgument('assetNames'); + update_feed($feedId, $assetNames); + }); + +// Delete Feed Default ACL command +$application->add(new Command('delete-feed')) + ->setDescription('Delete real time feed.') + ->setHelp(<<%command.name% command delete real time feed. + +php %command.full_name% --help + +EOF + ) + ->addArgument('feedName', InputArgument::REQUIRED, 'The Name of the feed to be deleted') + + ->setCode(function ($input, $output) { + $feedName = $input->getArgument('feedName'); + delete_feed($feedName); + }); + // for testing if (getenv('PHPUNIT_TESTS') === '1') { return $application; diff --git a/asset/composer.json b/asset/composer.json index 0e93bd3327..00fddfc785 100644 --- a/asset/composer.json +++ b/asset/composer.json @@ -1,13 +1,19 @@ { "require": { "google/cloud-storage": "^1.9", - "google/cloud-asset": "^0.4.0", + "google/cloud-pubsub": "^1.11.1", + "google/cloud-asset": "^0.5.0", "symfony/console": " ^3.0" }, "autoload": { "files": [ "src/export_assets.php", - "src/batch_get_assets_history.php" + "src/batch_get_assets_history.php", + "src/create_feed.php", + "src/get_feed.php", + "src/list_feeds.php", + "src/delete_feed.php", + "src/update_feed.php" ] } } diff --git a/asset/src/create_feed.php b/asset/src/create_feed.php new file mode 100644 index 0000000000..f311d6160e --- /dev/null +++ b/asset/src/create_feed.php @@ -0,0 +1,47 @@ +setTopic($topic); + $feed_output_config->setPubsubDestination($pubsub_destination); + $feed->setAssetNames($asset_names); + $feed->setFeedOutputConfig($feed_output_config); + + $created_feed = $client->CreateFeed($parent, $feedId, $feed); + + echo $created_feed->getName(); +} +# [END asset_quickstart_create_feed] diff --git a/asset/src/delete_feed.php b/asset/src/delete_feed.php new file mode 100644 index 0000000000..7e52e62926 --- /dev/null +++ b/asset/src/delete_feed.php @@ -0,0 +1,36 @@ +DeleteFeed($feedName); + + echo 'Feed deleted'; +} +# [END asset_quickstart_delete_feed] diff --git a/asset/src/get_feed.php b/asset/src/get_feed.php new file mode 100644 index 0000000000..1783453124 --- /dev/null +++ b/asset/src/get_feed.php @@ -0,0 +1,36 @@ +GetFeed($feedName); + + echo 'Feed ' . $feed->getName() . ' got.'; +} +# [END asset_quickstart_get_feed] diff --git a/asset/src/list_feeds.php b/asset/src/list_feeds.php new file mode 100644 index 0000000000..d889122f26 --- /dev/null +++ b/asset/src/list_feeds.php @@ -0,0 +1,36 @@ +ListFeeds($parent); + + echo 'Feeds listed'; +} +# [END asset_quickstart_list_feeds] diff --git a/asset/src/update_feed.php b/asset/src/update_feed.php new file mode 100644 index 0000000000..8b5934c10f --- /dev/null +++ b/asset/src/update_feed.php @@ -0,0 +1,44 @@ +setName($feedName); + $new_feed->setAssetNames($asset_names); + $updateMask = new FieldMask(); + $updateMask->setPaths(['asset_names']); + + $updated_feed = $client->UpdateFeed($new_feed, $updateMask); + + echo 'Feeds updated'; +} +# [END asset_quickstart_create_feed] diff --git a/asset/test/assetTest.php b/asset/test/assetTest.php index 4f61003c23..4463e67a1f 100644 --- a/asset/test/assetTest.php +++ b/asset/test/assetTest.php @@ -18,6 +18,7 @@ namespace Google\Cloud\Samples\Asset; use Google\Cloud\Storage\StorageClient; +use Google\Cloud\PubSub\PubSubClient; use Google\Cloud\TestUtils\TestTrait; use Google\Cloud\TestUtils\ExecuteCommandTrait; use Google\Cloud\TestUtils\EventuallyConsistentTestTrait; @@ -36,43 +37,101 @@ class assetTest extends TestCase private static $storage; private static $bucketName; private static $bucket; + private static $topicName; + private static $pubsub; + private static $topic; + private static $feedName; public static function setUpBeforeClass() { self::$storage = new StorageClient(); self::$bucketName = sprintf('assets-bucket-%s-%s', time(), rand()); self::$bucket = self::$storage->createBucket(self::$bucketName); + self::$pubsub = new PubSubClient(['projectId' => self::$projectId]); + self::$topicName = sprintf('topic-%s-%s', time(), rand()); + self::$topic = self::$pubsub->createTopic(self::$topicName); } public static function tearDownAfterClass() { self::$bucket->delete(); + self::$topic->delete(); } - public function testExportAssets() - { - $fileName = 'my-assets.txt'; - $dumpFilePath = 'gs://' . self::$bucketName . '/' . $fileName; - $output = $this->runCommand('export', [ - 'project' => self::$projectId, - 'filePath' => $dumpFilePath, - ]); - $assetFile = self::$bucket->object($fileName); - $this->assertEquals($assetFile->name(), $fileName); - $assetFile->delete(); - } + // public function testExportAssets() + // { + // $fileName = 'my-assets.txt'; + // $dumpFilePath = 'gs://' . self::$bucketName . '/' . $fileName; + // $output = $this->runCommand('export', [ + // 'project' => self::$projectId, + // 'filePath' => $dumpFilePath, + // ]); + // $assetFile = self::$bucket->object($fileName); + // $this->assertEquals($assetFile->name(), $fileName); + // $assetFile->delete(); + // } + + // public function testBatchGetAssetsHistory() + // { + // $assetName = '//storage.googleapis.com/' . self::$bucketName; + + // $this->runEventuallyConsistentTest(function () use ($assetName) { + // $output = $this->runCommand('batch-get-history', [ + // 'project' => self::$projectId, + // 'assetNames' => [$assetName], + // ]); - public function testBatchGetAssetsHistory() + // $this->assertContains($assetName, $output); + // }, 10, true); + // } + + public function testRealTimeFeed() { + $feedId = sprintf('feed-%s-%s', time(), rand()); $assetName = '//storage.googleapis.com/' . self::$bucketName; - $this->runEventuallyConsistentTest(function () use ($assetName) { - $output = $this->runCommand('batch-get-history', [ - 'project' => self::$projectId, - 'assetNames' => [$assetName], - ]); + $this->runEventuallyConsistentTest(function () use ($feedId, $assetName) { + $output = $this->runCommand('create-feed', [ + 'parent' => sprintf('projects/%s', self::$projectId), + 'feedId' => $feedId, + 'topic' => sprintf('projects/%s/topics/%s', self::$projectId, self::$topicName), + 'assetNames' => [$assetName], + ]); + self::$feedName = $output; + + $this->assertContains($feedId, $output); + }, 1, true); + + $this->runEventuallyConsistentTest(function () use ($feedId) { + $output = $this->runCommand('get-feed', [ + 'feedName' => self::$feedName, + ]); + + $this->assertContains($feedId, $output); + }, 1, true); + + $this->runEventuallyConsistentTest(function () { + $output = $this->runCommand('list-feeds', [ + 'parent' => sprintf('projects/%s', self::$projectId), + ]); + + $this->assertContains('Feeds listed', $output); + }, 1, true); + + $this->runEventuallyConsistentTest(function () { + $output = $this->runCommand('update-feed', [ + 'parent' => sprintf('projects/%s', self::$projectId), + ]); + + $this->assertContains('Feeds listed', $output); + }, 1, true); + + $this->runEventuallyConsistentTest(function () { + $output = $this->runCommand('delete-feed', [ + 'feedName' => self::$feedName, + ]); - $this->assertContains($assetName, $output); - }, 10, true); + $this->assertContains('Feed deleted', $output); + }, 1, true); } } From 89a352e65407ccd437bf0ec5b499a73c075cc1aa Mon Sep 17 00:00:00 2001 From: cwxie-google Date: Mon, 6 Jan 2020 19:02:12 -0800 Subject: [PATCH 2/2] Add real time feed api sample code --- asset/asset.php | 25 +++++++---- asset/src/create_feed.php | 20 ++++----- asset/src/delete_feed.php | 2 - asset/src/get_feed.php | 2 - asset/src/list_feeds.php | 2 - asset/src/update_feed.php | 8 ++-- asset/test/assetTest.php | 89 ++++++++++++++++++++------------------- 7 files changed, 73 insertions(+), 75 deletions(-) diff --git a/asset/asset.php b/asset/asset.php index 69c14c2e21..4d99799c4f 100644 --- a/asset/asset.php +++ b/asset/asset.php @@ -28,7 +28,8 @@ // Create Bucket ACL command $application->add(new Command('export')) ->setDescription('Export assets for given projec to specified path.') - ->setHelp(<<setHelp( + <<%command.name% command exports assets for given project to specified path. php %command.full_name% --help @@ -46,7 +47,8 @@ // Create Bucket Default ACL command $application->add(new Command('batch-get-history')) ->setDescription('Batch get the history of assets.') - ->setHelp(<<setHelp( + <<%command.name% command batch gets history of assets. php %command.full_name% --help @@ -65,7 +67,8 @@ // Create Feed Default ACL command $application->add(new Command('create-feed')) ->setDescription('Create real time feed.') - ->setHelp(<<setHelp( + <<%command.name% command create real time feed. php %command.full_name% --help @@ -88,7 +91,8 @@ // Get Feed Default ACL command $application->add(new Command('get-feed')) ->setDescription('Get real time feed.') - ->setHelp(<<setHelp( + <<%command.name% command get real time feed. php %command.full_name% --help @@ -105,7 +109,8 @@ // List Feeds Default ACL command $application->add(new Command('list-feeds')) ->setDescription('List real time feed under a resource.') - ->setHelp(<<setHelp( + <<%command.name% command list real time feeds. php %command.full_name% --help @@ -122,7 +127,8 @@ // Update Feed Default ACL command $application->add(new Command('update-feed')) ->setDescription('Update real time feed.') - ->setHelp(<<setHelp( + <<%command.name% command update assetNames of a real time feed. php %command.full_name% --help @@ -133,15 +139,16 @@ ->addArgument('assetNames', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'The assets of which the feed will listen to') ->setCode(function ($input, $output) { - $feedId = $input->getArgument('feedId'); + $feedName = $input->getArgument('feedName'); $assetNames = $input->getArgument('assetNames'); - update_feed($feedId, $assetNames); + update_feed($feedName, $assetNames); }); // Delete Feed Default ACL command $application->add(new Command('delete-feed')) ->setDescription('Delete real time feed.') - ->setHelp(<<setHelp( + <<%command.name% command delete real time feed. php %command.full_name% --help diff --git a/asset/src/create_feed.php b/asset/src/create_feed.php index f311d6160e..79766ce0cc 100644 --- a/asset/src/create_feed.php +++ b/asset/src/create_feed.php @@ -25,23 +25,21 @@ /** * Create a real time feed. - * - * @param string $parent of feeds. */ -function create_feed($parent, $feedId, $topic, $asset_names) +function create_feed($parent, $feedId, $topic, $assetNames) { $client = new AssetServiceClient(); $feed = new Feed(); - $feed_output_config = new FeedOutputConfig(); - $pubsub_destination = new PubsubDestination(); - $pubsub_destination->setTopic($topic); - $feed_output_config->setPubsubDestination($pubsub_destination); - $feed->setAssetNames($asset_names); - $feed->setFeedOutputConfig($feed_output_config); + $feedOutputConfig = new FeedOutputConfig(); + $pubsubDestination = new PubsubDestination(); + $pubsubDestination->setTopic($topic); + $feedOutputConfig->setPubsubDestination($pubsubDestination); + $feed->setAssetNames($assetNames); + $feed->setFeedOutputConfig($feedOutputConfig); - $created_feed = $client->CreateFeed($parent, $feedId, $feed); + $createdFeed = $client->CreateFeed($parent, $feedId, $feed); - echo $created_feed->getName(); + echo $createdFeed->getName(); } # [END asset_quickstart_create_feed] diff --git a/asset/src/delete_feed.php b/asset/src/delete_feed.php index 7e52e62926..c82eff8349 100644 --- a/asset/src/delete_feed.php +++ b/asset/src/delete_feed.php @@ -22,8 +22,6 @@ /** * Get real time feed given its name. - * - * @param string $feedId the Name of feed. */ function delete_feed($feedName) { diff --git a/asset/src/get_feed.php b/asset/src/get_feed.php index 1783453124..76eb367095 100644 --- a/asset/src/get_feed.php +++ b/asset/src/get_feed.php @@ -22,8 +22,6 @@ /** * Get real time feed given its name. - * - * @param string $feedId the name of feed. */ function get_feed($feedName) { diff --git a/asset/src/list_feeds.php b/asset/src/list_feeds.php index d889122f26..0bd8a17f36 100644 --- a/asset/src/list_feeds.php +++ b/asset/src/list_feeds.php @@ -22,8 +22,6 @@ /** * List real time feeds sgiven their parents. - * - * @param string $parent of feeds. */ function list_feeds($parent) { diff --git a/asset/src/update_feed.php b/asset/src/update_feed.php index 8b5934c10f..3cdb69c629 100644 --- a/asset/src/update_feed.php +++ b/asset/src/update_feed.php @@ -24,21 +24,19 @@ /** * Create a real time feed. - * - * @param string $parent of feeds. */ -function update_feed($feedName, $asset_names) +function update_feed($feedName, $assetNames) { $client = new AssetServiceClient(); $new_feed = new Feed(); $new_feed->setName($feedName); - $new_feed->setAssetNames($asset_names); + $new_feed->setAssetNames($assetNames); $updateMask = new FieldMask(); $updateMask->setPaths(['asset_names']); $updated_feed = $client->UpdateFeed($new_feed, $updateMask); - echo 'Feeds updated'; + echo 'Feed Updated ' . $updated_feed; } # [END asset_quickstart_create_feed] diff --git a/asset/test/assetTest.php b/asset/test/assetTest.php index 4463e67a1f..62ac7a3d0d 100644 --- a/asset/test/assetTest.php +++ b/asset/test/assetTest.php @@ -58,32 +58,32 @@ public static function tearDownAfterClass() self::$topic->delete(); } - // public function testExportAssets() - // { - // $fileName = 'my-assets.txt'; - // $dumpFilePath = 'gs://' . self::$bucketName . '/' . $fileName; - // $output = $this->runCommand('export', [ - // 'project' => self::$projectId, - // 'filePath' => $dumpFilePath, - // ]); - // $assetFile = self::$bucket->object($fileName); - // $this->assertEquals($assetFile->name(), $fileName); - // $assetFile->delete(); - // } - - // public function testBatchGetAssetsHistory() - // { - // $assetName = '//storage.googleapis.com/' . self::$bucketName; - - // $this->runEventuallyConsistentTest(function () use ($assetName) { - // $output = $this->runCommand('batch-get-history', [ - // 'project' => self::$projectId, - // 'assetNames' => [$assetName], - // ]); - - // $this->assertContains($assetName, $output); - // }, 10, true); - // } + public function testExportAssets() + { + $fileName = 'my-assets.txt'; + $dumpFilePath = 'gs://' . self::$bucketName . '/' . $fileName; + $output = $this->runCommand('export', [ + 'project' => self::$projectId, + 'filePath' => $dumpFilePath, + ]); + $assetFile = self::$bucket->object($fileName); + $this->assertEquals($assetFile->name(), $fileName); + $assetFile->delete(); + } + + public function testBatchGetAssetsHistory() + { + $assetName = '//storage.googleapis.com/' . self::$bucketName; + + $this->runEventuallyConsistentTest(function () use ($assetName) { + $output = $this->runCommand('batch-get-history', [ + 'project' => self::$projectId, + 'assetNames' => [$assetName], + ]); + + $this->assertContains($assetName, $output); + }, 10, true); + } public function testRealTimeFeed() { @@ -91,47 +91,48 @@ public function testRealTimeFeed() $assetName = '//storage.googleapis.com/' . self::$bucketName; $this->runEventuallyConsistentTest(function () use ($feedId, $assetName) { - $output = $this->runCommand('create-feed', [ + $output = $this->runCommand('create-feed', [ 'parent' => sprintf('projects/%s', self::$projectId), 'feedId' => $feedId, 'topic' => sprintf('projects/%s/topics/%s', self::$projectId, self::$topicName), 'assetNames' => [$assetName], ]); - self::$feedName = $output; + self::$feedName = $output; - $this->assertContains($feedId, $output); - }, 1, true); + $this->assertContains($feedId, $output); + }, 1, true); $this->runEventuallyConsistentTest(function () use ($feedId) { - $output = $this->runCommand('get-feed', [ + $output = $this->runCommand('get-feed', [ 'feedName' => self::$feedName, ]); - $this->assertContains($feedId, $output); - }, 1, true); + $this->assertContains($feedId, $output); + }, 1, true); $this->runEventuallyConsistentTest(function () { - $output = $this->runCommand('list-feeds', [ + $output = $this->runCommand('list-feeds', [ 'parent' => sprintf('projects/%s', self::$projectId), ]); - $this->assertContains('Feeds listed', $output); - }, 1, true); + $this->assertContains('Feeds listed', $output); + }, 1, true); - $this->runEventuallyConsistentTest(function () { - $output = $this->runCommand('update-feed', [ - 'parent' => sprintf('projects/%s', self::$projectId), + $this->runEventuallyConsistentTest(function () use ($assetName) { + $output = $this->runCommand('update-feed', [ + 'feedName' => self::$feedName, + 'assetNames' => [$assetName."new"], ]); - $this->assertContains('Feeds listed', $output); - }, 1, true); + $this->assertContains('Feeds updated', $output); + }, 1, true); $this->runEventuallyConsistentTest(function () { - $output = $this->runCommand('delete-feed', [ + $output = $this->runCommand('delete-feed', [ 'feedName' => self::$feedName, ]); - $this->assertContains('Feed deleted', $output); - }, 1, true); + $this->assertContains('Feed deleted', $output); + }, 1, true); } }