From 80b702207e89b0c99f7c508b0208422bf0e42125 Mon Sep 17 00:00:00 2001 From: Andrew Zimmer Date: Mon, 8 Mar 2021 14:20:36 -0500 Subject: [PATCH] DDP-5671 added script to allow bulk cancellation of orders in GBF --- .../dsm/model/gbf/CancelRequest.java | 13 +++++ .../dsm/model/gbf/CancelResponse.java | 34 ++++++++++++ .../externalShipper/CancelGBFOrderCLI.java | 52 +++++++++++++++++++ .../util/externalShipper/GBFRequestUtil.java | 30 +++++++++-- 4 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/broadinstitute/dsm/model/gbf/CancelRequest.java create mode 100644 src/main/java/org/broadinstitute/dsm/model/gbf/CancelResponse.java create mode 100644 src/main/java/org/broadinstitute/dsm/util/externalShipper/CancelGBFOrderCLI.java diff --git a/src/main/java/org/broadinstitute/dsm/model/gbf/CancelRequest.java b/src/main/java/org/broadinstitute/dsm/model/gbf/CancelRequest.java new file mode 100644 index 000000000..e1eee73b8 --- /dev/null +++ b/src/main/java/org/broadinstitute/dsm/model/gbf/CancelRequest.java @@ -0,0 +1,13 @@ +package org.broadinstitute.dsm.model.gbf; + +import com.google.gson.annotations.SerializedName; + +public class CancelRequest { + + @SerializedName("orderNumber") + private String orderNumber; + + public CancelRequest(String orderNumber) { + this.orderNumber = orderNumber; + } +} diff --git a/src/main/java/org/broadinstitute/dsm/model/gbf/CancelResponse.java b/src/main/java/org/broadinstitute/dsm/model/gbf/CancelResponse.java new file mode 100644 index 000000000..44fee729a --- /dev/null +++ b/src/main/java/org/broadinstitute/dsm/model/gbf/CancelResponse.java @@ -0,0 +1,34 @@ +package org.broadinstitute.dsm.model.gbf; + +import com.google.gson.annotations.SerializedName; + +public class CancelResponse { + + @SerializedName("orderNumber") + private String orderNumber; + + @SerializedName("orderStatus") + private String orderStatus; + + @SerializedName("success") + private Boolean success; + + @SerializedName("errorMessage") + private String errorMessage; + + public String getOrderNumber() { + return orderNumber; + } + + public String getOrderStatus() { + return orderStatus; + } + + public boolean wasSuccessful() { + return Boolean.TRUE.equals(success); + } + + public String getErrorMessage() { + return errorMessage; + } +} diff --git a/src/main/java/org/broadinstitute/dsm/util/externalShipper/CancelGBFOrderCLI.java b/src/main/java/org/broadinstitute/dsm/util/externalShipper/CancelGBFOrderCLI.java new file mode 100644 index 000000000..5bc634ef7 --- /dev/null +++ b/src/main/java/org/broadinstitute/dsm/util/externalShipper/CancelGBFOrderCLI.java @@ -0,0 +1,52 @@ +package org.broadinstitute.dsm.util.externalShipper; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.broadinstitute.dsm.model.gbf.CancelResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CancelGBFOrderCLI { + + private static final Logger logger = LoggerFactory.getLogger(CancelGBFOrderCLI.class); + + + public static void main(String[] args) { + String cancelUrl = args[0]; + String apiKey = args[1]; + String ordersFile = args[2]; + FileReader ordersReader = null; + List ordersToCancel = new ArrayList<>(); + + try { + ordersReader = new FileReader(new File(ordersFile)); + ordersToCancel = IOUtils.readLines(ordersReader); + } catch(IOException e) { + logger.error("Could read orders from file " + ordersFile,e); + } + + if (ordersReader != null) { + CancelResponse cancelResponse = null; + for (String orderNumber : ordersToCancel) { + try { + cancelResponse = GBFRequestUtil.cancelOrder(orderNumber, cancelUrl, apiKey); + if (cancelResponse.wasSuccessful()) { + logger.info("Cancelled order {}", orderNumber); + } else { + logger.error("Could not cancel order {} due to {}",orderNumber, cancelResponse.getErrorMessage()); + } + } catch(Exception e) { + logger.error("Could not cancel order {}", orderNumber, e); + } + } + } + + + } +} diff --git a/src/main/java/org/broadinstitute/dsm/util/externalShipper/GBFRequestUtil.java b/src/main/java/org/broadinstitute/dsm/util/externalShipper/GBFRequestUtil.java index 9a776562f..2007e515a 100644 --- a/src/main/java/org/broadinstitute/dsm/util/externalShipper/GBFRequestUtil.java +++ b/src/main/java/org/broadinstitute/dsm/util/externalShipper/GBFRequestUtil.java @@ -2,6 +2,7 @@ import com.google.api.client.http.HttpStatusCodes; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import lombok.NonNull; import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.lang3.StringUtils; @@ -96,15 +97,17 @@ public class GBFRequestUtil implements ExternalShipper { private static Executor blindTrustEverythingExecutor; - public GBFRequestUtil() { + static { + initTrust(); + } + + private static synchronized void initTrust() { try { if (blindTrustEverythingExecutor == null) { blindTrustEverythingExecutor = Executor.newInstance(SecurityUtil.buildHttpClient()); } - } - catch (Exception e) { + } catch (Exception e) { logger.error("Starting up the blindTrustEverythingExecutor ", e); - System.exit(-3); } } @@ -615,4 +618,23 @@ public static void markKitRequestAsOrdered(ArrayList kitRequests) { } + public static CancelResponse cancelOrder(String orderNumber, String cancelUrl, String apiKey) throws Exception { + Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create(); + CancelRequest cancelRequest = new CancelRequest(orderNumber); + logger.info("About to cancel GBF order {}", orderNumber); + CancelResponse cancelResponse = executePost(CancelResponse.class, cancelUrl, gson.toJson(cancelRequest), apiKey); + + if (cancelResponse != null) { + if (cancelResponse.wasSuccessful()) { + logger.info("Cancelled GBF order {}", orderNumber); + } else { + logger.error("Could not cancel GBF order " + orderNumber + " due to " + cancelResponse.getErrorMessage()); + } + } else { + logger.error("Got no response after cancelling GBF order " + orderNumber); + } + return cancelResponse; + } + + }