-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Purge Products #1569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Purge Products #1569
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
087c64e
Batch delete & tests
munkhuushmgl e15ed70
purge products
munkhuushmgl dc71dd1
Merge branch 'master' into purge-products
munkhuushmgl 28ed6be
removed TODOs comments, added UUID for tests
munkhuushmgl d9b6276
removed TODOs comments, added UUID for tests
munkhuushmgl 8821314
Merge branch 'purge-products' of https://github.com/GoogleCloudPlatfo…
munkhuushmgl d0a6e77
applied new format for purge products
munkhuushmgl 03ae5a7
fixed nits
munkhuushmgl 384f430
removed javadocs headers & renamed package names
munkhuushmgl ad94cdd
fixed checkstyle
munkhuushmgl 486dee7
deleted unncessary parsers
munkhuushmgl 12f141e
removed force params
munkhuushmgl 125a5c8
Merge branch 'master' into purge-products
nnegrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package vision.snippets; | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.vision.v1.LocationName; | ||
import com.google.cloud.vision.v1.ProductSearchClient; | ||
import com.google.cloud.vision.v1.PurgeProductsRequest; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
// [START vision_product_search_purge_orphan_products] | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class PurgeProducts { | ||
|
||
/** | ||
* Delete the product and all its reference images. | ||
* | ||
* @param projectId - Id of the project. | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param computeRegion - A compute region name. | ||
* @param force - Perform the purge only when force is set to True. | ||
* @throws Exception - on I/O and API errors. | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public static void purgeOrphanProducts(String projectId, String computeRegion, boolean force) | ||
throws Exception { | ||
try (ProductSearchClient client = ProductSearchClient.create()) { | ||
|
||
String parent = LocationName.format(projectId, computeRegion); | ||
|
||
// The purge operation is async. | ||
PurgeProductsRequest req = PurgeProductsRequest | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.newBuilder() | ||
.setDeleteOrphanProducts(true) | ||
.setForce(force) | ||
.setParent(parent) | ||
.build(); | ||
|
||
OperationFuture response = client.purgeProductsAsync(req); | ||
response.getPollingFuture().get(90, TimeUnit.SECONDS); | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
System.out.println("Orphan products deleted."); | ||
} | ||
} | ||
} | ||
// [END vision_product_search_purge_orphan_products] |
67 changes: 67 additions & 0 deletions
67
.../product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package vision.snippets; | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.vision.v1.*; | ||
import com.google.protobuf.Empty; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
// [START vision_product_search_purge_products_in_product_set] | ||
public class PurgeProductsInProductSet { | ||
|
||
/** | ||
* Delete all products in a product set. | ||
* | ||
* @param projectId - Id of the project. | ||
* @param location - Region name. | ||
* @param productSetId - Id of the product set. | ||
* @param force - Perform the purge only when force is set to True. | ||
* @throws Exception - any error. | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public static void purgeProductsInProductSet( | ||
String projectId, String location, String productSetId, boolean force) | ||
throws Exception { | ||
try (ProductSearchClient client = ProductSearchClient.create()) { | ||
|
||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String parent = LocationName.format(projectId, location); | ||
ProductSetPurgeConfig productSetPurgeConfig = ProductSetPurgeConfig | ||
.newBuilder() | ||
.setProductSetId(productSetId) | ||
.build(); | ||
|
||
PurgeProductsRequest req = PurgeProductsRequest | ||
.newBuilder() | ||
.setParent(parent) | ||
.setProductSetPurgeConfig(productSetPurgeConfig) | ||
// The operation is irreversible and removes multiple products. | ||
// The user is required to pass in force=True to actually perform the | ||
// purge. | ||
// If force is not set to True, the service raises an exception. | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.setForce(force) | ||
.build(); | ||
|
||
OperationFuture<Empty, BatchOperationMetadata> response = client.purgeProductsAsync(req); | ||
response.getPollingFuture().get(90, TimeUnit.SECONDS); | ||
|
||
System.out.println("Products removed from product set."); | ||
} | ||
|
||
} | ||
} | ||
// [END vision_product_search_purge_products_in_product_set] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package vision.snippets; | ||
|
||
import com.example.vision.ProductInProductSetManagement; | ||
import com.example.vision.ProductManagement; | ||
import com.example.vision.ProductSetManagement; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
public class ProductInProductSetManagementTests { | ||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private static final String COMPUTE_REGION = "us-west1"; | ||
private static final String PRODUCT_SET_DISPLAY_NAME = | ||
"fake_pdt_set_display_name_for_testing"; | ||
private static final String PRODUCT_SET_ID = "fake_pdt_set_id_for_testing" + UUID.randomUUID(); | ||
private static final String PRODUCT_DISPLAY_NAME = "fake_pdt_display_name_for_testing"; | ||
private static final String PRODUCT_CATEGORY = "apparel"; | ||
private static final String PRODUCT_ID = "fake_pdt_id_for_testing"; | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
ProductSetManagement.createProductSet( | ||
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME); | ||
ProductManagement.createProduct( | ||
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY); | ||
bout.reset(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); | ||
ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); | ||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testPurgeProductsInProductSet() throws Exception { | ||
// Act | ||
ProductInProductSetManagement.addProductToProductSet( | ||
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID); | ||
ProductManagement.listProducts( | ||
PROJECT_ID, COMPUTE_REGION); | ||
|
||
// Assert | ||
String got = bout.toString(); | ||
assertThat(got).contains(PRODUCT_ID); | ||
|
||
bout.reset(); | ||
PurgeProductsInProductSet.purgeProductsInProductSet( | ||
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, true); | ||
|
||
ProductManagement.listProducts( | ||
PROJECT_ID, COMPUTE_REGION); | ||
|
||
// Assert | ||
got = bout.toString(); | ||
assertThat(got).doesNotContain(PRODUCT_ID); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package vision.snippets; | ||
|
||
import com.example.vision.ProductManagement; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
@RunWith(JUnit4.class) | ||
public class ProductManagementTests { | ||
|
||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private static final String COMPUTE_REGION = "us-west1"; | ||
private static final String PRODUCT_DISPLAY_NAME = "fake_prod_display_name_for_testing"; | ||
private static final String PRODUCT_CATEGORY = "homegoods"; | ||
private static final String PRODUCT_ID = "fake_prod_id_for_testing"; | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); | ||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testPurgeOrphanProducts() throws Exception { | ||
// Act | ||
ProductManagement.createProduct( | ||
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY); | ||
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); | ||
|
||
// Assert | ||
String got = bout.toString(); | ||
assertThat(got).contains(PRODUCT_ID); | ||
|
||
bout.reset(); | ||
|
||
// Act | ||
PurgeProducts.purgeOrphanProducts(PROJECT_ID, COMPUTE_REGION, true); | ||
|
||
// Assert | ||
got = bout.toString(); | ||
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); | ||
assertThat(got).doesNotContain(PRODUCT_ID); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.