diff --git a/asset/asset.php b/asset/asset.php index f7ff4f978e..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 @@ -62,6 +64,104 @@ 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) { + $feedName = $input->getArgument('feedName'); + $assetNames = $input->getArgument('assetNames'); + update_feed($feedName, $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..79766ce0cc --- /dev/null +++ b/asset/src/create_feed.php @@ -0,0 +1,45 @@ +setTopic($topic); + $feedOutputConfig->setPubsubDestination($pubsubDestination); + $feed->setAssetNames($assetNames); + $feed->setFeedOutputConfig($feedOutputConfig); + + $createdFeed = $client->CreateFeed($parent, $feedId, $feed); + + echo $createdFeed->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..c82eff8349 --- /dev/null +++ b/asset/src/delete_feed.php @@ -0,0 +1,34 @@ +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..76eb367095 --- /dev/null +++ b/asset/src/get_feed.php @@ -0,0 +1,34 @@ +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..0bd8a17f36 --- /dev/null +++ b/asset/src/list_feeds.php @@ -0,0 +1,34 @@ +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..3cdb69c629 --- /dev/null +++ b/asset/src/update_feed.php @@ -0,0 +1,42 @@ +setName($feedName); + $new_feed->setAssetNames($assetNames); + $updateMask = new FieldMask(); + $updateMask->setPaths(['asset_names']); + + $updated_feed = $client->UpdateFeed($new_feed, $updateMask); + + echo 'Feed Updated ' . $updated_feed; +} +# [END asset_quickstart_create_feed] diff --git a/asset/test/assetTest.php b/asset/test/assetTest.php index 4f61003c23..62ac7a3d0d 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,17 +37,25 @@ 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() @@ -75,4 +84,55 @@ 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 ($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 () use ($assetName) { + $output = $this->runCommand('update-feed', [ + 'feedName' => self::$feedName, + 'assetNames' => [$assetName."new"], + ]); + + $this->assertContains('Feeds updated', $output); + }, 1, true); + + $this->runEventuallyConsistentTest(function () { + $output = $this->runCommand('delete-feed', [ + 'feedName' => self::$feedName, + ]); + + $this->assertContains('Feed deleted', $output); + }, 1, true); + } }