From f932350a0af5102dd92b732153ddee90dc05fee4 Mon Sep 17 00:00:00 2001 From: Manoj Date: Fri, 9 Nov 2018 15:11:28 +0530 Subject: [PATCH] Add repo and package check connection flow --- .../mlflow_gocd/MLFlowPackagePlugin.java | 91 +++++++++++++------ .../com/indix/mlflow_gocd/MaterialResult.java | 34 +++++++ 2 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/indix/mlflow_gocd/MaterialResult.java diff --git a/src/main/java/com/indix/mlflow_gocd/MLFlowPackagePlugin.java b/src/main/java/com/indix/mlflow_gocd/MLFlowPackagePlugin.java index 11bceb8..3a18474 100644 --- a/src/main/java/com/indix/mlflow_gocd/MLFlowPackagePlugin.java +++ b/src/main/java/com/indix/mlflow_gocd/MLFlowPackagePlugin.java @@ -35,6 +35,8 @@ public class MLFlowPackagePlugin implements GoPlugin { public static final String REQUEST_LATEST_REVISION = "latest-revision"; public static final String REQUEST_LATEST_REVISION_SINCE = "latest-revision-since"; + private static final String MLFLOW_GET_EXPERIMENT_ENDPOINT="/api/2.0/preview/mlflow/experiments/get"; + private static Logger logger = Logger.getLoggerFor(MLFlowPackagePlugin.class); private final HttpRequestFactory requestFactory; @@ -58,7 +60,7 @@ public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws } else if (goPluginApiRequest.requestName().equals(REQUEST_VALIDATE_REPOSITORY_CONFIGURATION)) { return handleRepositoryValidation(goPluginApiRequest); } else if (goPluginApiRequest.requestName().equals(REQUEST_VALIDATE_PACKAGE_CONFIGURATION)) { - return handlePackageValidation(); + return handlePackageValidation(goPluginApiRequest); } else if (goPluginApiRequest.requestName().equals(REQUEST_CHECK_REPOSITORY_CONNECTION)) { return handleRepositoryCheckConnection(goPluginApiRequest); } else if (goPluginApiRequest.requestName().equals(REQUEST_CHECK_PACKAGE_CONNECTION)) { @@ -80,40 +82,77 @@ private GoPluginApiResponse handleGetLatestRevision(GoPluginApiRequest goPluginA } private GoPluginApiResponse handlePackageCheckConnection(GoPluginApiRequest goPluginApiRequest) { - return null; + final Map repositoryConfig = keyValuePairs(goPluginApiRequest, REQUEST_REPOSITORY_CONFIGURATION); + final Map packageConfig = keyValuePairs(goPluginApiRequest, REQUEST_PACKAGE_CONFIGURATION); + + String mlflowUrl = repositoryConfig.get(MLFLOW_URL); + String experimentId = packageConfig.get(EXPERIMENT_ID); + + MaterialResult result; + if(StringUtils.isBlank(mlflowUrl)) { + result = new MaterialResult(false, "Experiment id must be specified"); + } else { + try { + GenericUrl getExperimentUrl = new GenericUrl(String.format("%s%s?experiment_id=%s", mlflowUrl, MLFLOW_GET_EXPERIMENT_ENDPOINT, experimentId)); + HttpResponse response = requestFactory.buildGetRequest(getExperimentUrl).execute(); + if (response.getStatusCode() != 200) { + result = new MaterialResult(false, String.format("Experiment %s not found", experimentId)); + } else { + result = new MaterialResult(true, "Success"); + } + } catch(IOException ex) { + result = new MaterialResult(false, String.format("Unable to reach MLFlow at %s - %s", mlflowUrl, ex.getMessage())); + logger.error("Unable to reach mlflow", ex); + } + } + + return createResponse(result.responseCode(), result.toMap()); } private GoPluginApiResponse handleRepositoryCheckConnection(GoPluginApiRequest goPluginApiRequest) { - return null; + final Map repositoryConfig = keyValuePairs(goPluginApiRequest, REQUEST_REPOSITORY_CONFIGURATION); + + MaterialResult result; + String mlflowUrl = repositoryConfig.get(MLFLOW_URL); + + if(StringUtils.isBlank(mlflowUrl)) { + result = new MaterialResult(false, "MLFlow url must be specified"); + } else { + try { + HttpResponse response = requestFactory.buildGetRequest(new GenericUrl(mlflowUrl)).execute(); + if (response.getStatusCode() != 200) { + result = new MaterialResult(false, String.format("Unable to reach MLFlow at %s", mlflowUrl)); + } else { + result = new MaterialResult(true, "Success"); + } + } catch(IOException ex) { + result = new MaterialResult(false, String.format("Unable to reach MLFlow at %s - %s", mlflowUrl, ex.getMessage())); + logger.error("Unable to reach mlflow", ex); + } + } + + return createResponse(result.responseCode(), result.toMap()); } - private GoPluginApiResponse handlePackageValidation() { - return null; + private GoPluginApiResponse handlePackageValidation(GoPluginApiRequest goPluginApiRequest) { + List> validationResult = new ArrayList<>(); + final Map packageConfig = keyValuePairs(goPluginApiRequest, REQUEST_PACKAGE_CONFIGURATION); + + String experimentId = packageConfig.get(EXPERIMENT_ID); + if(StringUtils.isBlank(experimentId)) { + addError(EXPERIMENT_ID, "Experiment ID must be specified", validationResult); + } + + return createResponse(DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE, validationResult); } private GoPluginApiResponse handleRepositoryValidation(GoPluginApiRequest goPluginApiRequest) { List> validationResult = new ArrayList<>(); - final Map repositoryKeyValuePairs = keyValuePairs(goPluginApiRequest, REQUEST_REPOSITORY_CONFIGURATION); + final Map repositoryConfig = keyValuePairs(goPluginApiRequest, REQUEST_REPOSITORY_CONFIGURATION); - String mlflowUrl = repositoryKeyValuePairs.get(MLFLOW_URL); + String mlflowUrl = repositoryConfig.get(MLFLOW_URL); if(StringUtils.isBlank(mlflowUrl)) { - HashMap errorMap = new HashMap<>(); - errorMap.put("key", MLFLOW_URL); - errorMap.put("message", "MLFlow URL must be specified"); - validationResult.add(errorMap); - } - - try { - HttpResponse response = requestFactory.buildGetRequest(new GenericUrl(mlflowUrl)).execute(); - if (response.getStatusCode() != 200) { - addError(MLFLOW_URL, "Unable to reach MLFlow", validationResult); - HashMap errorMap = new HashMap<>(); - errorMap.put("key", MLFLOW_URL); - errorMap.put("message", "Unable to reach MLFlow"); - validationResult.add(errorMap); - } - } catch(IOException ex) { - addError(MLFLOW_URL, "Unable to reach MLFlow - " + ex.getMessage(), validationResult); + addError(MLFLOW_URL, "MLFlow URL must be specified", validationResult); } return createResponse(DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE, validationResult); @@ -125,10 +164,10 @@ private GoPluginApiResponse handlePackageConfiguration() { createField("Experiment ID", null, true, true, false, "1") ); response.put(PROMOTION_TAG_NAME, - createField("Promotion Tag Name", "promote", true, true, false, "2") + createField("Promotion Tag Name", "promote", true, false, false, "2") ); response.put(PROMOTION_TAG_VALUE, - createField("Promotion Tag Value", "true", true, true, false, "3") + createField("Promotion Tag Value", "true", true, false, false, "3") ); return createResponse(DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE, response); } diff --git a/src/main/java/com/indix/mlflow_gocd/MaterialResult.java b/src/main/java/com/indix/mlflow_gocd/MaterialResult.java new file mode 100644 index 0000000..8d0c6da --- /dev/null +++ b/src/main/java/com/indix/mlflow_gocd/MaterialResult.java @@ -0,0 +1,34 @@ +package com.indix.mlflow_gocd; + +import com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MaterialResult { + private boolean success; + private List messages; + + public MaterialResult(boolean success) { + this.success = success; + messages = new ArrayList<>(); + } + + public MaterialResult(boolean success, String message) { + this(success); + messages.add(message); + } + + public Map toMap() { + final HashMap result = new HashMap(); + result.put("status", success ? "success" : "failure"); + result.put("messages", messages); + return result; + } + + public int responseCode() { + return DefaultGoApiResponse.SUCCESS_RESPONSE_CODE; + } +}