From 087c64ec5ae201d984b1593f584806c3fac6c6a3 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Tue, 20 Aug 2019 12:37:04 -0700 Subject: [PATCH 1/9] Batch delete & tests --- vision/product-search/cloud-client/pom.xml | 4 +- .../vision/ProductInProductSetManagement.java | 64 ++++++++++++++++++- .../com/example/vision/ProductManagement.java | 41 +++++++++++- .../vision/ReferenceImageManagement.java | 6 +- .../ProductInProductSetManagementIT.java | 24 +++++++ .../example/vision/ProductManagementIT.java | 20 ++++++ 6 files changed, 153 insertions(+), 6 deletions(-) diff --git a/vision/product-search/cloud-client/pom.xml b/vision/product-search/cloud-client/pom.xml index ac4a7d15392..179994dbd54 100644 --- a/vision/product-search/cloud-client/pom.xml +++ b/vision/product-search/cloud-client/pom.xml @@ -40,12 +40,12 @@ com.google.cloud google-cloud-vision - 1.52.0 + 1.87.0 com.google.cloud google-cloud-storage - 1.52.0 + 1.87.0 net.sourceforge.argparse4j diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index d770c71a44d..d2738419faa 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -16,12 +16,21 @@ package com.example.vision; +import com.google.api.gax.longrunning.OperationFuture; + +import com.google.cloud.vision.v1.BatchOperationMetadata; +import com.google.cloud.vision.v1.LocationName; import com.google.cloud.vision.v1.Product; import com.google.cloud.vision.v1.ProductName; import com.google.cloud.vision.v1.ProductSearchClient; +import com.google.cloud.vision.v1.ProductSetPurgeConfig; +import com.google.cloud.vision.v1.PurgeProductsRequest; + +import com.google.protobuf.Empty; import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.TimeUnit; import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.inf.ArgumentParser; @@ -135,6 +144,48 @@ public static void removeProductFromProductSet( } // [END vision_product_search_remove_product_from_product_set] + // [START vision_product_search_purge_products_in_product_set] + /** + * 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. + */ + public static void purgeProductsInProductSet( + String projectId, String location, String productSetId, boolean force) + throws Exception { + try (ProductSearchClient client = ProductSearchClient.create()) { + + LocationName parent = LocationName.of(projectId, location); + ProductSetPurgeConfig productSetPurgeConfig = ProductSetPurgeConfig + .newBuilder() + .setProductSetId(productSetId) + .build(); + + PurgeProductsRequest req = PurgeProductsRequest + .newBuilder() + .setProductSetPurgeConfig(productSetPurgeConfig) + .setParent(parent.toString()) + // 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. + .setForce(force) + .build(); + + OperationFuture response = client.purgeProductsAsync(req); + + response.get(60, TimeUnit.SECONDS); + + System.out.println("Products removed from product set."); + } + + } + // [END vision_product_search_purge_products_in_product_set] + public static void main(String[] args) throws Exception { ProductInProductSetManagement productInProductSetManagement = new ProductInProductSetManagement(); @@ -157,6 +208,11 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception { removeProductFromProductSetParser.addArgument("productId"); removeProductFromProductSetParser.addArgument("productSetId"); + Subparser purgeProductsInProductSetParser = + subparsers.addParser("purge_products_in_product_set"); + purgeProductsInProductSetParser.addArgument("productSetId"); + purgeProductsInProductSetParser.addArgument("force"); + String projectId = System.getenv("PROJECT_ID"); String computeRegion = System.getenv("REGION_NAME"); @@ -172,7 +228,13 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception { } if (ns.get("command").equals("remove_product_from_product_set")) { removeProductFromProductSet( - projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId")); + projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId")); + } + System.out.println(ns.getAttrs()); + if (ns.get("command").equals("purge_products_in_product_set")) { + purgeProductsInProductSet( + projectId, computeRegion, ns.getString("productSetId"), + Boolean.parseBoolean(ns.getString("force"))); } } catch (ArgumentParserException e) { diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index 198ab5525e5..280538ce513 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -16,13 +16,17 @@ package com.example.vision; +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.vision.v1.LocationName; import com.google.cloud.vision.v1.Product; import com.google.cloud.vision.v1.Product.KeyValue; import com.google.cloud.vision.v1.ProductSearchClient; +import com.google.cloud.vision.v1.PurgeProductsRequest; import com.google.protobuf.FieldMask; import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.TimeUnit; import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.inf.ArgumentParser; @@ -213,6 +217,36 @@ public static void deleteProduct(String projectId, String computeRegion, String } // [END vision_product_search_delete_product] + // [START vision_product_search_purge_orphan_products] + /** + * Delete the product and all its reference images. + * + * @param projectId - Id of the project. + * @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. + */ + 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 + .newBuilder() + .setDeleteOrphanProducts(true) + .setParent(parent) + .build(); + + OperationFuture response = client.purgeProductsAsync(req); + + response.get(60, TimeUnit.SECONDS); + System.out.println("Orphan products deleted."); + } + } + // [END vision_product_search_purge_orphan_products] + public static void main(String[] args) throws Exception { ProductManagement productManagement = new ProductManagement(); productManagement.argsHelper(args, System.out); @@ -241,6 +275,9 @@ public void argsHelper(String[] args, PrintStream out) throws Exception { Subparser deleteProductParser = subparsers.addParser("delete_product"); deleteProductParser.addArgument("productId"); + Subparser purgeOrphanProductsParser = subparsers.addParser("purge_orphan_products"); + purgeOrphanProductsParser.addArgument("force"); + String projectId = System.getenv("PROJECT_ID"); String computeRegion = System.getenv("REGION_NAME"); @@ -268,7 +305,9 @@ public void argsHelper(String[] args, PrintStream out) throws Exception { if (ns.get("command").equals("delete_product")) { deleteProduct(projectId, computeRegion, ns.getString("productId")); } - + if (ns.get("command").equals("purge_orphan_products")) { + purgeOrphanProducts(projectId, computeRegion, Boolean.parseBoolean(ns.getString("force"))); + } } catch (ArgumentParserException e) { parser.handleError(e); } diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java index cf04dfc85f2..7598950d5bb 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ReferenceImageManagement.java @@ -16,6 +16,8 @@ package com.example.vision; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageName; import com.google.cloud.vision.v1.ProductSearchClient; import com.google.cloud.vision.v1.ReferenceImage; @@ -123,7 +125,7 @@ public static void getReferenceImage( // Get the full path of the reference image. String formattedName = - ProductSearchClient.formatImageName( + ImageName.format( projectId, computeRegion, productId, referenceImageId); // Get complete detail of the reference image. ReferenceImage image = client.getReferenceImage(formattedName); @@ -158,7 +160,7 @@ public static void deleteReferenceImage( // Get the full path of the reference image. String formattedName = - ProductSearchClient.formatImageName( + ImageName.format( projectId, computeRegion, productId, referenceImageId); // Delete the reference image. client.deleteReferenceImage(formattedName); diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java index 4143b7abb74..55c6c3458ea 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java @@ -106,4 +106,28 @@ public void testRemoveProductFromProductSet() throws Exception { got = bout.toString(); assertThat(got).doesNotContain(PRODUCT_ID); } + + @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(); + ProductInProductSetManagement.purgeProductsInProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, true); + + ProductManagement.listProducts( + PROJECT_ID, COMPUTE_REGION); + + // Assert + got = bout.toString(); + assertThat(got).doesNotContain(PRODUCT_ID); + } } diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java index 9f3fecd1d70..720e4a0035e 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java @@ -120,4 +120,24 @@ public void testUpdateProductLabels() throws Exception { assertThat(got).contains(KEY); assertThat(got).contains(VALUE); } + + @Test + public void testPurgeOrphanProducts() throws Exception { + // Act + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + + // Assert + String got = bout.toString(); + assertThat(got).contains(PRODUCT_ID); + + bout.reset(); + + // Act + ProductManagement.purgeOrphanProducts(PROJECT_ID, COMPUTE_REGION, true); + + // Assert + got = bout.toString(); + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + assertThat(got).doesNotContain(PRODUCT_ID); + } } From e15ed700c4dc582815b4630196564ea9048b075d Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Thu, 29 Aug 2019 14:50:18 -0700 Subject: [PATCH 2/9] purge products --- vision/product-search/cloud-client/pom.xml | 4 ++-- .../example/vision/ProductInProductSetManagement.java | 10 ++++++---- .../java/com/example/vision/ProductManagement.java | 6 +++++- .../java/com/example/vision/ProductManagementIT.java | 2 ++ 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/vision/product-search/cloud-client/pom.xml b/vision/product-search/cloud-client/pom.xml index 179994dbd54..6cc1d1f5b96 100644 --- a/vision/product-search/cloud-client/pom.xml +++ b/vision/product-search/cloud-client/pom.xml @@ -40,12 +40,12 @@ com.google.cloud google-cloud-vision - 1.87.0 + 1.88.0 com.google.cloud google-cloud-storage - 1.87.0 + 1.88.0 net.sourceforge.argparse4j diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index d2738419faa..6b25ca3a13f 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -159,7 +159,7 @@ public static void purgeProductsInProductSet( throws Exception { try (ProductSearchClient client = ProductSearchClient.create()) { - LocationName parent = LocationName.of(projectId, location); + String parent = LocationName.format(projectId, location); ProductSetPurgeConfig productSetPurgeConfig = ProductSetPurgeConfig .newBuilder() .setProductSetId(productSetId) @@ -167,8 +167,8 @@ public static void purgeProductsInProductSet( PurgeProductsRequest req = PurgeProductsRequest .newBuilder() + .setParent(parent) .setProductSetPurgeConfig(productSetPurgeConfig) - .setParent(parent.toString()) // The operation is irreversible and removes multiple products. // The user is required to pass in force=True to actually perform the // purge. @@ -176,9 +176,11 @@ public static void purgeProductsInProductSet( .setForce(force) .build(); - OperationFuture response = client.purgeProductsAsync(req); + //TODO: once its supported in all regions, will change it to 60 sec. + // testing method with region asia-east1 seems bit slower than normal. - response.get(60, TimeUnit.SECONDS); + OperationFuture response = client.purgeProductsAsync(req); + response.getPollingFuture().get(90, TimeUnit.MINUTES); System.out.println("Products removed from product set."); } diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index 280538ce513..d382f37a340 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -236,12 +236,16 @@ public static void purgeOrphanProducts(String projectId, String computeRegion, b PurgeProductsRequest req = PurgeProductsRequest .newBuilder() .setDeleteOrphanProducts(true) + .setForce(force) .setParent(parent) .build(); OperationFuture response = client.purgeProductsAsync(req); - response.get(60, TimeUnit.SECONDS); + //TODO: once its supported in all regions, will change it to 60 sec. + // testing method with region asia-east1 seems bit slower than normal. + response.getPollingFuture().get(90, TimeUnit.SECONDS); + System.out.println("Orphan products deleted."); } } diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java index 720e4a0035e..6c99efa6f83 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java @@ -124,6 +124,8 @@ public void testUpdateProductLabels() throws Exception { @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 From 28ed6be4be3f33be59e857d780025f6b7707a519 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Fri, 30 Aug 2019 15:36:40 -0700 Subject: [PATCH 3/9] removed TODOs comments, added UUID for tests --- .../com/example/vision/ProductInProductSetManagement.java | 5 +---- .../src/main/java/com/example/vision/ProductManagement.java | 3 --- .../com/example/vision/ProductInProductSetManagementIT.java | 4 +++- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index 6b25ca3a13f..b19bd6090f0 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -176,11 +176,8 @@ public static void purgeProductsInProductSet( .setForce(force) .build(); - //TODO: once its supported in all regions, will change it to 60 sec. - // testing method with region asia-east1 seems bit slower than normal. - OperationFuture response = client.purgeProductsAsync(req); - response.getPollingFuture().get(90, TimeUnit.MINUTES); + response.getPollingFuture().get(90, TimeUnit.SECONDS); System.out.println("Products removed from product set."); } diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index d382f37a340..f9864695665 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -241,9 +241,6 @@ public static void purgeOrphanProducts(String projectId, String computeRegion, b .build(); OperationFuture response = client.purgeProductsAsync(req); - - //TODO: once its supported in all regions, will change it to 60 sec. - // testing method with region asia-east1 seems bit slower than normal. response.getPollingFuture().get(90, TimeUnit.SECONDS); System.out.println("Orphan products deleted."); diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java index 55c6c3458ea..e97b3247986 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java @@ -21,6 +21,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.UUID; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -35,7 +37,7 @@ public class ProductInProductSetManagementIT { 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"; + 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"; From d0a6e772384d7be0990029b3962ba7c878ffd01b Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Tue, 3 Sep 2019 15:22:59 -0700 Subject: [PATCH 4/9] applied new format for purge products --- .../vision/ProductInProductSetManagement.java | 57 +-------------- .../com/example/vision/ProductManagement.java | 40 +---------- .../java/vision/snippets/PurgeProducts.java | 58 +++++++++++++++ .../snippets/PurgeProductsInProductSet.java | 67 +++++++++++++++++ .../ProductInProductSetManagementIT.java | 24 ------- .../example/vision/ProductManagementIT.java | 22 ------ .../ProductInProductSetManagementTests.java | 71 +++++++++++++++++++ .../snippets/ProductManagementTests.java | 61 ++++++++++++++++ 8 files changed, 259 insertions(+), 141 deletions(-) create mode 100644 vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java create mode 100644 vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java create mode 100644 vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java create mode 100644 vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index b19bd6090f0..8b66fe94688 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 Google LLC + * 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. @@ -16,21 +16,12 @@ package com.example.vision; -import com.google.api.gax.longrunning.OperationFuture; - -import com.google.cloud.vision.v1.BatchOperationMetadata; -import com.google.cloud.vision.v1.LocationName; import com.google.cloud.vision.v1.Product; import com.google.cloud.vision.v1.ProductName; import com.google.cloud.vision.v1.ProductSearchClient; -import com.google.cloud.vision.v1.ProductSetPurgeConfig; -import com.google.cloud.vision.v1.PurgeProductsRequest; - -import com.google.protobuf.Empty; import java.io.IOException; import java.io.PrintStream; -import java.util.concurrent.TimeUnit; import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.inf.ArgumentParser; @@ -144,47 +135,6 @@ public static void removeProductFromProductSet( } // [END vision_product_search_remove_product_from_product_set] - // [START vision_product_search_purge_products_in_product_set] - /** - * 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. - */ - public static void purgeProductsInProductSet( - String projectId, String location, String productSetId, boolean force) - throws Exception { - try (ProductSearchClient client = ProductSearchClient.create()) { - - 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. - .setForce(force) - .build(); - - OperationFuture 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] - public static void main(String[] args) throws Exception { ProductInProductSetManagement productInProductSetManagement = new ProductInProductSetManagement(); @@ -230,11 +180,6 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception { projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId")); } System.out.println(ns.getAttrs()); - if (ns.get("command").equals("purge_products_in_product_set")) { - purgeProductsInProductSet( - projectId, computeRegion, ns.getString("productSetId"), - Boolean.parseBoolean(ns.getString("force"))); - } } catch (ArgumentParserException e) { parser.handleError(e); diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index f9864695665..6ace43a52f3 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 Google LLC + * 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. @@ -16,17 +16,13 @@ package com.example.vision; -import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.vision.v1.LocationName; import com.google.cloud.vision.v1.Product; import com.google.cloud.vision.v1.Product.KeyValue; import com.google.cloud.vision.v1.ProductSearchClient; -import com.google.cloud.vision.v1.PurgeProductsRequest; import com.google.protobuf.FieldMask; import java.io.IOException; import java.io.PrintStream; -import java.util.concurrent.TimeUnit; import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.inf.ArgumentParser; @@ -217,37 +213,6 @@ public static void deleteProduct(String projectId, String computeRegion, String } // [END vision_product_search_delete_product] - // [START vision_product_search_purge_orphan_products] - /** - * Delete the product and all its reference images. - * - * @param projectId - Id of the project. - * @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. - */ - 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 - .newBuilder() - .setDeleteOrphanProducts(true) - .setForce(force) - .setParent(parent) - .build(); - - OperationFuture response = client.purgeProductsAsync(req); - response.getPollingFuture().get(90, TimeUnit.SECONDS); - - System.out.println("Orphan products deleted."); - } - } - // [END vision_product_search_purge_orphan_products] - public static void main(String[] args) throws Exception { ProductManagement productManagement = new ProductManagement(); productManagement.argsHelper(args, System.out); @@ -306,9 +271,6 @@ public void argsHelper(String[] args, PrintStream out) throws Exception { if (ns.get("command").equals("delete_product")) { deleteProduct(projectId, computeRegion, ns.getString("productId")); } - if (ns.get("command").equals("purge_orphan_products")) { - purgeOrphanProducts(projectId, computeRegion, Boolean.parseBoolean(ns.getString("force"))); - } } catch (ArgumentParserException e) { parser.handleError(e); } diff --git a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java b/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java new file mode 100644 index 00000000000..8d442b822c8 --- /dev/null +++ b/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java @@ -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; + +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] +public class PurgeProducts { + + /** + * Delete the product and all its reference images. + * + * @param projectId - Id of the project. + * @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. + */ + 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 + .newBuilder() + .setDeleteOrphanProducts(true) + .setForce(force) + .setParent(parent) + .build(); + + OperationFuture response = client.purgeProductsAsync(req); + response.getPollingFuture().get(90, TimeUnit.SECONDS); + + System.out.println("Orphan products deleted."); + } + } +} +// [END vision_product_search_purge_orphan_products] \ No newline at end of file diff --git a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java b/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java new file mode 100644 index 00000000000..21fa0c10da4 --- /dev/null +++ b/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java @@ -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. + */ + public static void purgeProductsInProductSet( + String projectId, String location, String productSetId, boolean force) + throws Exception { + try (ProductSearchClient client = ProductSearchClient.create()) { + + 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. + .setForce(force) + .build(); + + OperationFuture 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] diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java index e97b3247986..16ca689bf40 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java @@ -108,28 +108,4 @@ public void testRemoveProductFromProductSet() throws Exception { got = bout.toString(); assertThat(got).doesNotContain(PRODUCT_ID); } - - @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(); - ProductInProductSetManagement.purgeProductsInProductSet( - PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, true); - - ProductManagement.listProducts( - PROJECT_ID, COMPUTE_REGION); - - // Assert - got = bout.toString(); - assertThat(got).doesNotContain(PRODUCT_ID); - } } diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java index 6c99efa6f83..9f3fecd1d70 100644 --- a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java @@ -120,26 +120,4 @@ public void testUpdateProductLabels() throws Exception { assertThat(got).contains(KEY); assertThat(got).contains(VALUE); } - - @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 - ProductManagement.purgeOrphanProducts(PROJECT_ID, COMPUTE_REGION, true); - - // Assert - got = bout.toString(); - ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); - assertThat(got).doesNotContain(PRODUCT_ID); - } } diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java new file mode 100644 index 00000000000..e1151692e92 --- /dev/null +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java @@ -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); + } +} diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java new file mode 100644 index 00000000000..dba5a99cfaf --- /dev/null +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java @@ -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); + } +} From 03ae5a77cc9edb3ceb910f4ecb82f4feae732389 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Tue, 3 Sep 2019 16:30:25 -0700 Subject: [PATCH 5/9] fixed nits --- .../vision/ProductInProductSetManagement.java | 2 +- .../com/example/vision/ProductManagement.java | 2 +- .../java/vision/snippets/PurgeProducts.java | 12 ++++++-- .../snippets/PurgeProductsInProductSet.java | 19 ++++++++----- .../ProductInProductSetManagementTests.java | 28 +++++++++++++++---- .../snippets/ProductManagementTests.java | 28 +++++++++++++++---- 6 files changed, 68 insertions(+), 23 deletions(-) diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index 8b66fe94688..29c8f49ff1e 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Google LLC + * 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. diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index 6ace43a52f3..329ec2ae894 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Google LLC + * 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. diff --git a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java b/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java index 8d442b822c8..90c2dc20ca0 100644 --- a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java +++ b/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java @@ -16,6 +16,7 @@ package vision.snippets; +// [START vision_product_search_purge_orphan_products] import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.vision.v1.LocationName; import com.google.cloud.vision.v1.ProductSearchClient; @@ -23,7 +24,7 @@ import java.util.concurrent.TimeUnit; -// [START vision_product_search_purge_orphan_products] + public class PurgeProducts { /** @@ -36,19 +37,24 @@ public class PurgeProducts { */ public static void purgeOrphanProducts(String projectId, String computeRegion, boolean force) throws Exception { + + // String projectId = "YOUR_PROJECT_ID"; + // String computeRegion = "us-central1"; + // boolean force = true; + try (ProductSearchClient client = ProductSearchClient.create()) { String parent = LocationName.format(projectId, computeRegion); // The purge operation is async. - PurgeProductsRequest req = PurgeProductsRequest + PurgeProductsRequest request = PurgeProductsRequest .newBuilder() .setDeleteOrphanProducts(true) .setForce(force) .setParent(parent) .build(); - OperationFuture response = client.purgeProductsAsync(req); + OperationFuture response = client.purgeProductsAsync(request); response.getPollingFuture().get(90, TimeUnit.SECONDS); System.out.println("Orphan products deleted."); diff --git a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java b/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java index 21fa0c10da4..4605817e323 100644 --- a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java +++ b/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java @@ -17,7 +17,11 @@ package vision.snippets; import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.vision.v1.*; +import com.google.cloud.vision.v1.BatchOperationMetadata; +import com.google.cloud.vision.v1.LocationName; +import com.google.cloud.vision.v1.ProductSearchClient; +import com.google.cloud.vision.v1.ProductSetPurgeConfig; +import com.google.cloud.vision.v1.PurgeProductsRequest; import com.google.protobuf.Empty; import java.util.concurrent.TimeUnit; @@ -28,6 +32,11 @@ public class PurgeProductsInProductSet { /** * Delete all products in a product set. * + * 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. + * * @param projectId - Id of the project. * @param location - Region name. * @param productSetId - Id of the product set. @@ -45,18 +54,14 @@ public static void purgeProductsInProductSet( .setProductSetId(productSetId) .build(); - PurgeProductsRequest req = PurgeProductsRequest + PurgeProductsRequest request = 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. .setForce(force) .build(); - OperationFuture response = client.purgeProductsAsync(req); + OperationFuture response = client.purgeProductsAsync(request); response.getPollingFuture().get(90, TimeUnit.SECONDS); System.out.println("Products removed from product set."); diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java index e1151692e92..a5fb0e9a9bb 100644 --- a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java @@ -1,18 +1,36 @@ +/* + * 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 static com.google.common.truth.Truth.assertThat; + 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; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + public class ProductInProductSetManagementTests { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java index dba5a99cfaf..9a9bddf6c7e 100644 --- a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java @@ -1,18 +1,34 @@ +/* + * 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 static com.google.common.truth.Truth.assertThat; + import com.example.vision.ProductManagement; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + 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 { From 384f43006345705aca8d9fa7fb7e843b54872d54 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Wed, 4 Sep 2019 12:28:28 -0700 Subject: [PATCH 6/9] removed javadocs headers & renamed package names --- .../vision/snippets/PurgeProducts.java | 16 ++++------ .../snippets/PurgeProductsInProductSet.java | 30 ++++++++----------- .../ProductInProductSetManagementTests.java | 3 +- .../snippets/ProductManagementTests.java | 1 + 4 files changed, 21 insertions(+), 29 deletions(-) rename vision/product-search/cloud-client/src/main/java/{ => com/example}/vision/snippets/PurgeProducts.java (83%) rename vision/product-search/cloud-client/src/main/java/{ => com/example}/vision/snippets/PurgeProductsInProductSet.java (79%) diff --git a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProducts.java similarity index 83% rename from vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java rename to vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProducts.java index 90c2dc20ca0..04a5ec0b143 100644 --- a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProducts.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProducts.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package vision.snippets; +package com.example.vision.snippets; // [START vision_product_search_purge_orphan_products] import com.google.api.gax.longrunning.OperationFuture; @@ -27,14 +27,7 @@ public class PurgeProducts { - /** - * Delete the product and all its reference images. - * - * @param projectId - Id of the project. - * @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. - */ + // Delete the product and all its reference images. public static void purgeOrphanProducts(String projectId, String computeRegion, boolean force) throws Exception { @@ -43,13 +36,16 @@ public static void purgeOrphanProducts(String projectId, String computeRegion, b // boolean force = true; try (ProductSearchClient client = ProductSearchClient.create()) { - String parent = LocationName.format(projectId, computeRegion); // The purge operation is async. PurgeProductsRequest request = PurgeProductsRequest .newBuilder() .setDeleteOrphanProducts(true) + // 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. .setForce(force) .setParent(parent) .build(); diff --git a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java similarity index 79% rename from vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java rename to vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java index 4605817e323..49dc18dcd68 100644 --- a/vision/product-search/cloud-client/src/main/java/vision/snippets/PurgeProductsInProductSet.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package vision.snippets; +package com.example.vision.snippets; +// [START vision_product_search_purge_products_in_product_set] import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.vision.v1.BatchOperationMetadata; import com.google.cloud.vision.v1.LocationName; @@ -26,26 +27,18 @@ import java.util.concurrent.TimeUnit; -// [START vision_product_search_purge_products_in_product_set] public class PurgeProductsInProductSet { - /** - * Delete all products in a product set. - * - * 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. - * - * @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. - */ + // Delete all products in a product set. public static void purgeProductsInProductSet( String projectId, String location, String productSetId, boolean force) throws Exception { + + // String projectId = "YOUR_PROJECT_ID"; + // String location = "us-central1"; + // String productSetId = "YOUR_PRODUCT_SET_ID"; + // boolean force = true; + try (ProductSearchClient client = ProductSearchClient.create()) { String parent = LocationName.format(projectId, location); @@ -58,6 +51,10 @@ public static void purgeProductsInProductSet( .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. .setForce(force) .build(); @@ -66,7 +63,6 @@ public static void purgeProductsInProductSet( System.out.println("Products removed from product set."); } - } } // [END vision_product_search_purge_products_in_product_set] diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java index a5fb0e9a9bb..c218db3be82 100644 --- a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java @@ -26,12 +26,11 @@ import java.io.PrintStream; import java.util.UUID; +import com.example.vision.snippets.PurgeProductsInProductSet; import org.junit.After; import org.junit.Before; import org.junit.Test; - - public class ProductInProductSetManagementTests { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String COMPUTE_REGION = "us-west1"; diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java index 9a9bddf6c7e..8f28b8db43d 100644 --- a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.PrintStream; +import com.example.vision.snippets.PurgeProducts; import org.junit.After; import org.junit.Before; import org.junit.Test; From ad94cdd3a89c480249eb6e283a3145db5ed24145 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Wed, 4 Sep 2019 12:42:55 -0700 Subject: [PATCH 7/9] fixed checkstyle --- .../vision/snippets/ProductInProductSetManagementTests.java | 2 +- .../src/test/java/vision/snippets/ProductManagementTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java index c218db3be82..f01fdea7ac0 100644 --- a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java @@ -21,12 +21,12 @@ import com.example.vision.ProductInProductSetManagement; import com.example.vision.ProductManagement; import com.example.vision.ProductSetManagement; +import com.example.vision.snippets.PurgeProductsInProductSet; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.UUID; -import com.example.vision.snippets.PurgeProductsInProductSet; import org.junit.After; import org.junit.Before; import org.junit.Test; diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java index 8f28b8db43d..792cc046c2f 100644 --- a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java @@ -19,11 +19,11 @@ import static com.google.common.truth.Truth.assertThat; import com.example.vision.ProductManagement; +import com.example.vision.snippets.PurgeProducts; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; -import com.example.vision.snippets.PurgeProducts; import org.junit.After; import org.junit.Before; import org.junit.Test; From 486dee7598a1ee3466a8ed6da9970382814ef777 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Thu, 5 Sep 2019 15:27:56 -0700 Subject: [PATCH 8/9] deleted unncessary parsers --- .../com/example/vision/ProductInProductSetManagement.java | 6 ------ .../src/main/java/com/example/vision/ProductManagement.java | 3 --- 2 files changed, 9 deletions(-) diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index 29c8f49ff1e..cfd21998ec8 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -157,11 +157,6 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception { removeProductFromProductSetParser.addArgument("productId"); removeProductFromProductSetParser.addArgument("productSetId"); - Subparser purgeProductsInProductSetParser = - subparsers.addParser("purge_products_in_product_set"); - purgeProductsInProductSetParser.addArgument("productSetId"); - purgeProductsInProductSetParser.addArgument("force"); - String projectId = System.getenv("PROJECT_ID"); String computeRegion = System.getenv("REGION_NAME"); @@ -179,7 +174,6 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception { removeProductFromProductSet( projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId")); } - System.out.println(ns.getAttrs()); } catch (ArgumentParserException e) { parser.handleError(e); diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index 329ec2ae894..ce3cc9bb6f7 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -241,9 +241,6 @@ public void argsHelper(String[] args, PrintStream out) throws Exception { Subparser deleteProductParser = subparsers.addParser("delete_product"); deleteProductParser.addArgument("productId"); - Subparser purgeOrphanProductsParser = subparsers.addParser("purge_orphan_products"); - purgeOrphanProductsParser.addArgument("force"); - String projectId = System.getenv("PROJECT_ID"); String computeRegion = System.getenv("REGION_NAME"); From 12f141ef264fdc2118f58af38df7cae356b325e6 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Fri, 6 Sep 2019 11:26:40 -0700 Subject: [PATCH 9/9] removed force params --- .../main/java/com/example/vision/snippets/PurgeProducts.java | 4 ++-- .../example/vision/snippets/PurgeProductsInProductSet.java | 4 ++-- .../vision/snippets/ProductInProductSetManagementTests.java | 2 +- .../src/test/java/vision/snippets/ProductManagementTests.java | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProducts.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProducts.java index 04a5ec0b143..03ecafd4685 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProducts.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProducts.java @@ -28,7 +28,7 @@ public class PurgeProducts { // Delete the product and all its reference images. - public static void purgeOrphanProducts(String projectId, String computeRegion, boolean force) + public static void purgeOrphanProducts(String projectId, String computeRegion) throws Exception { // String projectId = "YOUR_PROJECT_ID"; @@ -46,7 +46,7 @@ public static void purgeOrphanProducts(String projectId, String computeRegion, b // 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. - .setForce(force) + .setForce(true) .setParent(parent) .build(); diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java index 49dc18dcd68..975f89f11bd 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java @@ -31,7 +31,7 @@ public class PurgeProductsInProductSet { // Delete all products in a product set. public static void purgeProductsInProductSet( - String projectId, String location, String productSetId, boolean force) + String projectId, String location, String productSetId) throws Exception { // String projectId = "YOUR_PROJECT_ID"; @@ -55,7 +55,7 @@ public static void purgeProductsInProductSet( // 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. - .setForce(force) + .setForce(true) .build(); OperationFuture response = client.purgeProductsAsync(request); diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java index f01fdea7ac0..bb3b7b3f7ff 100644 --- a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductInProductSetManagementTests.java @@ -76,7 +76,7 @@ public void testPurgeProductsInProductSet() throws Exception { bout.reset(); PurgeProductsInProductSet.purgeProductsInProductSet( - PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, true); + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); ProductManagement.listProducts( PROJECT_ID, COMPUTE_REGION); diff --git a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java index 792cc046c2f..73404ca13d3 100644 --- a/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java +++ b/vision/product-search/cloud-client/src/test/java/vision/snippets/ProductManagementTests.java @@ -68,7 +68,7 @@ public void testPurgeOrphanProducts() throws Exception { bout.reset(); // Act - PurgeProducts.purgeOrphanProducts(PROJECT_ID, COMPUTE_REGION, true); + PurgeProducts.purgeOrphanProducts(PROJECT_ID, COMPUTE_REGION); // Assert got = bout.toString();