From 5f613ea780e9db4ccdad38e5162cf5a575674f8d Mon Sep 17 00:00:00 2001 From: Viraj Date: Mon, 29 Apr 2019 17:56:55 +0530 Subject: [PATCH] persist etcd_variable at init command --- .../wso2/apimgt/gateway/cli/cmd/BuildCmd.java | 6 ++++ .../apimgt/gateway/cli/cmd/ImportCmd.java | 4 +++ .../wso2/apimgt/gateway/cli/cmd/InitCmd.java | 5 ++++ .../gateway/cli/utils/GatewayCmdUtils.java | 28 +++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/BuildCmd.java b/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/BuildCmd.java index af9a84ee0a..5520769e7e 100644 --- a/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/BuildCmd.java +++ b/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/BuildCmd.java @@ -32,6 +32,7 @@ import org.wso2.apimgt.gateway.cli.exception.*; import org.wso2.apimgt.gateway.cli.model.config.Config; import org.wso2.apimgt.gateway.cli.model.config.ContainerConfig; +import org.wso2.apimgt.gateway.cli.model.config.Etcd; import org.wso2.apimgt.gateway.cli.utils.GatewayCmdUtils; import org.wso2.apimgt.gateway.cli.utils.MgwDefinitionUtils; import org.wso2.apimgt.gateway.cli.utils.ToolkitLibExtractionUtils; @@ -109,6 +110,11 @@ public void execute() { try{ String toolkitConfigPath = GatewayCmdUtils.getMainConfigLocation(); init(projectName, toolkitConfigPath, deploymentConfigPath); + + Etcd etcd = new Etcd(); + etcd.setEtcdEnabled(GatewayCmdUtils.getEtcdEnabled(projectName)); + GatewayCmdUtils.setEtcd(etcd); + MgwDefinitionUtils.configureMgwDefinition(projectName); CodeGenerator codeGenerator = new CodeGenerator(); ThrottlePolicyGenerator policyGenerator = new ThrottlePolicyGenerator(); diff --git a/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/ImportCmd.java b/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/ImportCmd.java index ea2aeb14f7..59b4d210d7 100644 --- a/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/ImportCmd.java +++ b/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/ImportCmd.java @@ -299,6 +299,10 @@ public void execute() { throw new CLIRuntimeException(errorMsg); } + Etcd etcd = new Etcd(); + etcd.setEtcdEnabled(GatewayCmdUtils.getEtcdEnabled(projectName)); + GatewayCmdUtils.setEtcd(etcd); + List applicationPolicies = service.getApplicationPolicies(accessToken); List subscriptionPolicies = service.getSubscriptionPolicies(accessToken); diff --git a/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/InitCmd.java b/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/InitCmd.java index bb3d131f28..224fbb23b8 100644 --- a/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/InitCmd.java +++ b/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/cmd/InitCmd.java @@ -54,6 +54,9 @@ public class InitCmd implements GatewayLauncherCmd { @Parameter(names = {"-d", "--deployment-config"}, hidden = true) private String deploymentConfigPath; + @Parameter(names = { "-etcd", "--enable-etcd" }, hidden = true, arity = 0) + private boolean isEtcdEnabled; + @Parameter(names = {"--help", "-h", "?"}, hidden = true, description = "for more information", help = true) private boolean helpFlag; @@ -83,6 +86,8 @@ public void execute() { // Extract the zipped ballerina platform and runtime ToolkitLibExtractionUtils.extractPlatformAndRuntime(); init(projectName, deploymentConfigPath); + //todo: remove the temporary solution + GatewayCmdUtils.saveEtcdEnabled(projectName, isEtcdEnabled); OUT.println("Project '" + projectName + "' is initialized successfully."); } diff --git a/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/utils/GatewayCmdUtils.java b/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/utils/GatewayCmdUtils.java index b826dc4ff3..bdd209b982 100644 --- a/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/utils/GatewayCmdUtils.java +++ b/components/micro-gateway-cli/src/main/java/org/wso2/apimgt/gateway/cli/utils/GatewayCmdUtils.java @@ -54,6 +54,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -75,6 +76,33 @@ public static void setEtcd(Etcd etcd) { GatewayCmdUtils.etcd = etcd; } + //todo: change this later after being finalized + public static void saveEtcdEnabled(String projectName, boolean isEnabled){ + createFileIfNotExist(getProjectGenDirectoryPath(projectName), "internal.conf"); + Map confMap = new HashMap<>(1); + confMap.put("isEtcdEnabled", String.valueOf(isEnabled)); + try { + writeMapToFile(confMap, getProjectGenDirectoryPath(projectName) + File.separator + "internal.conf"); + } catch (IOException e){ + throw new CLIInternalException("Error while writing etcdEnabled to the internal.conf file", e); + } + } + + public static boolean getEtcdEnabled(String projectName){ + String internalConfPath = getProjectGenDirectoryPath(projectName)+ File.separator + "internal.conf"; + if(!(new File(internalConfPath)).exists()){ + return false; + } + try{ + return Boolean.valueOf(readFileToMap(internalConfPath).get("isEtcdEnabled")); + } catch (IOException e){ + throw new CLIInternalException("Error while reading the internal.conf file", e); + } catch (ClassNotFoundException e) { + throw new CLIInternalException("Error while while reading the internal.conf file", e); + } + + } + public static Config getConfig() { return config; }