Skip to content
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

Purge Products #1569

Merged
merged 13 commits into from
Sep 6, 2019
4 changes: 2 additions & 2 deletions vision/product-search/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.52.0</version>
<version>1.88.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.52.0</version>
<version>1.88.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Google LLC
* Copyright 2019 Google LLC
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -157,6 +157,11 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
removeProductFromProductSetParser.addArgument("productId");
removeProductFromProductSetParser.addArgument("productSetId");

Subparser purgeProductsInProductSetParser =
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
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");

Expand All @@ -172,8 +177,9 @@ 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());
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved

} catch (ArgumentParserException e) {
parser.handleError(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Google LLC
* Copyright 2019 Google LLC
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -241,6 +241,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");
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
purgeOrphanProductsParser.addArgument("force");

String projectId = System.getenv("PROJECT_ID");
String computeRegion = System.getenv("REGION_NAME");

Expand Down Expand Up @@ -268,7 +271,6 @@ public void argsHelper(String[] args, PrintStream out) throws Exception {
if (ns.get("command").equals("delete_product")) {
deleteProduct(projectId, computeRegion, ns.getString("productId"));
}

} catch (ArgumentParserException e) {
parser.handleError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
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]
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]
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down
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);
}
}
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);
}
}