Skip to content

Commit fccb192

Browse files
gogascasirtorry
authored andcommitted
PurgeOrphan and PurgeProductsInProductSet (GoogleCloudPlatform#953)
* PurgeOrphan and PurgeProductsInProductSet Add PurgeOrphan and PurgeProductsInProductSet features in PHP Update README file with commands Add test cases. * lint
1 parent de2b267 commit fccb192

File tree

6 files changed

+192
-20
lines changed

6 files changed

+192
-20
lines changed

vision/README.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,27 @@ This simple command-line application demonstrates how to invoke
4949
```
5050
For `product_search.php`, the following commands are available:
5151
```
52-
product-create Create a product
53-
product-delete Delete a product
54-
product-get Get information of a product
55-
product-list List information for all products
56-
product-update Update information for a product
57-
product-image-create Create reference image
58-
product-image-delete Delete reference image
59-
product-image-get Get reference image information for a product
60-
product-image-list List all reference image information for a product
61-
product-search-similar Search for similar products to local image
62-
product-search-similar-gcs Search for similar products to GCS image
63-
product-set-create Create a product set
64-
product-set-delete Delete a product set
65-
product-set-get Get information for a product set
66-
product-set-import Import a product set
67-
product-set-list List information for all product sets
68-
product-set-add-product Add product to a product set
69-
product-set-list-products List products in a product set
70-
product-set-remove-product Remove product from a product set
52+
product-create Create a product
53+
product-delete Delete a product
54+
product-get Get information of a product
55+
product-list List information for all products
56+
product-update Update information for a product
57+
product-image-create Create reference image
58+
product-image-delete Delete reference image
59+
product-image-get Get reference image information for a product
60+
product-image-list List all reference image information for a product
61+
product-search-similar Search for similar products to local image
62+
product-search-similar-gcs Search for similar products to GCS image
63+
product-set-create Create a product set
64+
product-set-delete Delete a product set
65+
product-set-get Get information for a product set
66+
product-set-import Import a product set
67+
product-set-list List information for all product sets
68+
product-set-add-product Add product to a product set
69+
product-set-list-products List products in a product set
70+
product-set-remove-product Remove product from a product set
71+
product-purge-orphan Delete all products not in any product sets
72+
product-purge-products-in-product-set Delete all products not in any product set
7173
```
7274

7375
7. Run `php vision.php COMMAND --help` or `php product_search.php COMMAND --help` to print information about the usage of each command.

vision/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
"src/product_set_list.php",
5555
"src/product_set_list_products.php",
5656
"src/product_set_remove_product.php",
57-
"src/product_update.php"
57+
"src/product_update.php",
58+
"src/product_purge_orphan_products.php",
59+
"src/product_purge_products_in_product_set.php"
5860
]
5961
}
6062
}

vision/product_search.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,53 @@
129129
})
130130
);
131131

132+
// Purge orphan products
133+
$application->add((new Command('product-purge-orphan'))
134+
->addArgument('project-id', InputArgument::REQUIRED,
135+
'Project/agent id. Required.')
136+
->addArgument('location', InputArgument::REQUIRED,
137+
'Name of compute region.')
138+
->addArgument('force', InputArgument::REQUIRED,
139+
'Force purge.')
140+
->setDescription('Purge orphan products.')
141+
->setHelp(<<
142+
The %command.name% Purge orphan products
143+
php %command.full_name% PROJECT_ID COMPUTE_REGION
144+
EOF
145+
)
146+
->setCode(function ($input, $output) {
147+
$projectId = $input->getArgument('project-id');
148+
$location = $input->getArgument('location');
149+
$force = $input->getArgument('force');
150+
purge_orphan_products($projectId, $location, $force);
151+
})
152+
);
153+
154+
155+
// Purge orphan products
156+
$application->add((new Command('product-purge-products-in-product-set'))
157+
->addArgument('project-id', InputArgument::REQUIRED,
158+
'Project/agent id. Required.')
159+
->addArgument('location', InputArgument::REQUIRED,
160+
'Name of compute region.')
161+
->addArgument('product-set-id', InputArgument::REQUIRED, 'ID of product set')
162+
->addArgument('force', InputArgument::REQUIRED,
163+
'Force purge.')
164+
->setDescription('Purge orphan products.')
165+
->setHelp(<<
166+
The %command.name% Purge orphan products
167+
php %command.full_name% PROJECT_ID COMPUTE_REGION
168+
EOF
169+
)
170+
->setCode(function ($input, $output) {
171+
$projectId = $input->getArgument('project-id');
172+
$location = $input->getArgument('location');
173+
$productSetId = $input->getArgument('product-set-id');
174+
$force = $input->getArgument('force');
175+
purge_products_in_product_set($projectId, $location, $productSetId, $force);
176+
})
177+
);
178+
132179
// create product
133180
$application->add((new Command('product-create'))
134181
->addArgument('project-id', InputArgument::REQUIRED,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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+
# [START vision_product_search_purge_products_in_product_set]
19+
namespace Google\Cloud\Samples\Vision;
20+
21+
use Google\Cloud\Vision\V1\ProductSearchClient;
22+
23+
/**
24+
* Delete all products not in any product sets.
25+
*
26+
* @param string $projectId Your Google Cloud project ID
27+
* @param string $location Google Cloud compute region name
28+
* @param boolean $force force purge
29+
*/
30+
function purge_orphan_products($projectId, $location, $force)
31+
{
32+
$client = new ProductSearchClient();
33+
34+
$parent = $client->locationName($projectId, $location);
35+
printf("Deleting orphan products" . PHP_EOL);
36+
$operationResponse = $client->purgeProducts($parent, ['deleteOrphanProducts' => true, 'force' => $force]);
37+
$operationResponse->pollUntilComplete();
38+
if ($operationResponse->operationSucceeded()) {
39+
print('Operation succeeded' . PHP_EOL);
40+
# print_r($operationResponse->getResult());
41+
} else {
42+
print('Operation failed' . PHP_EOL);
43+
print_r($operationResponse->getError());
44+
}
45+
$client->close();
46+
}
47+
# [END vision_product_search_purge_products_in_product_set]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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+
# [START vision_product_search_purge_products_in_product_set]
19+
namespace Google\Cloud\Samples\Vision;
20+
21+
use Google\Cloud\Vision\V1\ProductSearchClient;
22+
use Google\Cloud\Vision\V1\ProductSetPurgeConfig;
23+
24+
/**
25+
* Delete all products in a product set.
26+
*
27+
* @param string $projectId Your Google Cloud project ID
28+
* @param string $location Google Cloud compute region name
29+
* @param string $product_set_id ID of the product
30+
* @param boolean $force force purge
31+
*/
32+
function purge_products_in_product_set($projectId, $location, $product_set_id, $force)
33+
{
34+
$client = new ProductSearchClient();
35+
36+
$parent = $client->locationName($projectId, $location);
37+
$product_set_purge_config = (new ProductSetPurgeConfig())->setProductSetId($product_set_id);
38+
printf("Deleting products in product-set: %s" . PHP_EOL, $product_set_id);
39+
$operationResponse = $client->purgeProducts($parent, ['productSetPurgeConfig' => $product_set_purge_config,
40+
'force' => $force]);
41+
$operationResponse->pollUntilComplete();
42+
if ($operationResponse->operationSucceeded()) {
43+
print('Operation succeeded' . PHP_EOL);
44+
# print_r($operationResponse->getResult());
45+
} else {
46+
print('Operation failed' . PHP_EOL);
47+
print_r($operationResponse->getError());
48+
}
49+
$client->close();
50+
}
51+
# [END vision_product_search_purge_products_in_product_set]

vision/test/productSearchTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,29 @@ public function testDeleteProduct()
275275
$this->assertNotContains(self::$productId, $output);
276276
}
277277

278+
/** @depends testCreateProduct */
279+
public function testPurgeOrphan()
280+
{
281+
# test
282+
$this->runCommand('product-purge-orphan', [
283+
'force' => true
284+
]);
285+
$output = $this->runCommand('product-list', []);
286+
# check
287+
$this->assertNotContains(self::$productId, $output);
288+
}
289+
290+
/** @depends testAddProductToProductSet */
291+
public function testPurgeProductsInProductSet()
292+
{
293+
$this->runCommand('product-purge-products-in-product-set', [
294+
'product-set-id' => self::$productSetId,
295+
'force' => true
296+
]);
297+
$output = $this->runCommand('product-list', []);
298+
$this->assertNotContains(self::$productId, $output);
299+
}
300+
278301
/** @depends testCreateProductSet */
279302
public function testDeleteProductSet()
280303
{

0 commit comments

Comments
 (0)