From 49c9276c5cdd43d7309aa0c3fbe701320e6703a0 Mon Sep 17 00:00:00 2001 From: Chris Wiechmann Date: Fri, 27 Mar 2020 09:34:40 +0100 Subject: [PATCH] #249 Adding support for SWAGGER_PROMOTE_HOME containing a conf directory that is used to load environment properties. --- .../src/main/java/com/axway/apim/App.java | 7 ++-- .../com/axway/apim/lib/CommandParameters.java | 2 ++ .../axway/apim/lib/EnvironmentProperties.java | 32 +++++++++++-------- .../test/envProperties/EnvPropertiesTest.java | 28 ++++++++-------- .../envProperties/env.fromPath.properties | 6 ---- .../resources/envProperties/env.properties | 6 ---- .../env.fromSwaggerPromoteHome.properties | 4 +++ .../swaggerPromoteHome/conf/env.properties | 4 +++ 8 files changed, 46 insertions(+), 43 deletions(-) delete mode 100644 modules/swagger-promote-core/src/test/resources/envProperties/env.fromPath.properties delete mode 100644 modules/swagger-promote-core/src/test/resources/envProperties/env.properties create mode 100644 modules/swagger-promote-core/src/test/resources/envPropertiesTest/swaggerPromoteHome/conf/env.fromSwaggerPromoteHome.properties create mode 100644 modules/swagger-promote-core/src/test/resources/envPropertiesTest/swaggerPromoteHome/conf/env.properties diff --git a/modules/swagger-promote-core/src/main/java/com/axway/apim/App.java b/modules/swagger-promote-core/src/main/java/com/axway/apim/App.java index b8bda290..9bc0f80a 100644 --- a/modules/swagger-promote-core/src/main/java/com/axway/apim/App.java +++ b/modules/swagger-promote-core/src/main/java/com/axway/apim/App.java @@ -125,9 +125,10 @@ public static int run(String args[]) { option.setArgName("true/[false]"); options.addOption(option); - option = new Option("confDir", true, "The absolite path to the conf directory containing your stage environment properties."); + option = new Option("swaggerPromoteHome", true, "The absolute path to the Swagger-Promote home directory containing for instance your conf folder.\n" + + "You may also set the environment variable: '"+CommandParameters.SWAGGER_PROMOTE_HOME+"'"); option.setRequired(false); - option.setArgName("/home/chris/swagger-promote/conf"); + option.setArgName("/home/chris/swagger-promote"); options.addOption(option); option = new Option("iq", "ignoreQuotas", true, "Use this flag to ignore configured API quotas."); @@ -201,7 +202,7 @@ public static int run(String args[]) { Transaction.deleteInstance(); RollbackHandler.deleteInstance(); - CommandParameters params = new CommandParameters(cmd, internalCmd, new EnvironmentProperties(cmd.getOptionValue("stage"), cmd.getOptionValue("confDir"))); + CommandParameters params = new CommandParameters(cmd, internalCmd, new EnvironmentProperties(cmd.getOptionValue("stage"), cmd.getOptionValue("swaggerPromoteHome"))); APIManagerAdapter apimAdapter = APIManagerAdapter.getInstance(); diff --git a/modules/swagger-promote-core/src/main/java/com/axway/apim/lib/CommandParameters.java b/modules/swagger-promote-core/src/main/java/com/axway/apim/lib/CommandParameters.java index 3c791ca6..f8dd955e 100644 --- a/modules/swagger-promote-core/src/main/java/com/axway/apim/lib/CommandParameters.java +++ b/modules/swagger-promote-core/src/main/java/com/axway/apim/lib/CommandParameters.java @@ -14,6 +14,8 @@ public class CommandParameters { public static String MODE_IGNORE = "ignore"; public static String MODE_ADD = "add"; + public static String SWAGGER_PROMOTE_HOME = "SWAGGER_PROMOTE_HOME"; + private static CommandParameters instance; int port = 8075; diff --git a/modules/swagger-promote-core/src/main/java/com/axway/apim/lib/EnvironmentProperties.java b/modules/swagger-promote-core/src/main/java/com/axway/apim/lib/EnvironmentProperties.java index 67ce4273..fba97354 100644 --- a/modules/swagger-promote-core/src/main/java/com/axway/apim/lib/EnvironmentProperties.java +++ b/modules/swagger-promote-core/src/main/java/com/axway/apim/lib/EnvironmentProperties.java @@ -1,6 +1,5 @@ package com.axway.apim.lib; -import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.Collection; @@ -18,7 +17,7 @@ public class EnvironmentProperties implements Map { private static Logger LOG = LoggerFactory.getLogger(EnvironmentProperties.class); private String stage; - private String confDir; + private String swaggerPromoteHome; private Properties mainProperties = new Properties(); private Properties stageProperties = new Properties(); @@ -28,10 +27,15 @@ public EnvironmentProperties(String stage) throws AppException { this(stage, null); } - public EnvironmentProperties(String stage, String confDir) throws AppException { + public EnvironmentProperties(String stage, String swaggerPromoteHome) throws AppException { super(); this.stage = stage; - this.confDir = confDir; + this.swaggerPromoteHome = swaggerPromoteHome; + if(swaggerPromoteHome==null) { + // Try to use SWAGGER_PROMOTE_HOME if not given by a parameter + this.swaggerPromoteHome = System.getenv(CommandParameters.SWAGGER_PROMOTE_HOME); + } + if(this.swaggerPromoteHome!=null) this.swaggerPromoteHome += "/conf"; initProperties(); } @@ -44,22 +48,22 @@ private void initProperties() throws AppException { } private Properties loadProperties(String stage) { + /* + * We load properties in the following order: + * SwaggerPromote Home is used + * if ConfDir is not set + * if ConfDir is not set, the Classpath is used + */ String pathToUse = null; InputStream is; Properties props = new Properties(); try { - if(confDir!=null && stage==null) { - pathToUse = confDir + File.separator + "env.properties"; - is = new FileInputStream(pathToUse); - } else if(confDir!=null && stage!=null) { - pathToUse = confDir + File.separator + "env."+stage+".properties"; + if(swaggerPromoteHome!=null) { + pathToUse = (stage==null) ? swaggerPromoteHome + "/env.properties" : swaggerPromoteHome + "/env."+stage+".properties"; is = new FileInputStream(pathToUse); - } else if(confDir==null && stage!=null) { - pathToUse = "env."+stage+".properties"; - is = APIMHttpClient.class.getClassLoader().getResourceAsStream("env."+stage+".properties"); } else { - pathToUse = "env.properties"; - is = APIMHttpClient.class.getClassLoader().getResourceAsStream("env.properties"); + pathToUse = (stage==null) ? "env.properties" : "env."+stage+".properties"; + is = APIMHttpClient.class.getClassLoader().getResourceAsStream(pathToUse); } props.load(is); LOG.info("Loaded environment properties from file: " + pathToUse); diff --git a/modules/swagger-promote-core/src/test/java/com/axway/apim/test/envProperties/EnvPropertiesTest.java b/modules/swagger-promote-core/src/test/java/com/axway/apim/test/envProperties/EnvPropertiesTest.java index 15bf3c1d..6bc27c24 100644 --- a/modules/swagger-promote-core/src/test/java/com/axway/apim/test/envProperties/EnvPropertiesTest.java +++ b/modules/swagger-promote-core/src/test/java/com/axway/apim/test/envProperties/EnvPropertiesTest.java @@ -23,7 +23,7 @@ public void testNoStage() throws AppException, IOException { } @Test - public void testAnyOtherStage() throws AppException, IOException { + public void testStage() throws AppException, IOException { EnvironmentProperties properties = new EnvironmentProperties("anyOtherStage"); Assert.assertEquals(properties.containsKey("thisKeyExists"), true); @@ -33,34 +33,34 @@ public void testAnyOtherStage() throws AppException, IOException { } @Test - public void testNoStageFromConfFolder() throws AppException, IOException, URISyntaxException { + public void testNoStageFromConfDir() throws AppException, IOException, URISyntaxException { // A given path should be used to load the Environent-Config file from String path = EnvPropertiesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); - path += "/envProperties"; - + path += "envPropertiesTest/swaggerPromoteHome"; + EnvironmentProperties properties = new EnvironmentProperties(null, path); Assert.assertEquals(properties.containsKey("thisKeyExists"), true); - Assert.assertEquals(properties.get("thisKeyExists"), "BasicConfigFromPath"); + Assert.assertEquals(properties.get("thisKeyExists"), "keyFromSwaggerPromoteHome"); - Assert.assertEquals(properties.get("admin_username"), "basicUserFromPathProperty"); - Assert.assertEquals(properties.get("admin_password"), "basicPasswordFromPathProperty"); + Assert.assertEquals(properties.get("admin_username"), "userFromSwaggerPromoteHome"); + Assert.assertEquals(properties.get("admin_password"), "passwordFromSwaggerPromoteHome"); } @Test - public void testAnyOtherStageFromConfFolder() throws AppException, IOException, URISyntaxException { + public void testStageFromConfDir() throws AppException, IOException, URISyntaxException { // A given path should be used to load the Environent-Config file from String path = EnvPropertiesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); - path += "/envProperties"; - - EnvironmentProperties properties = new EnvironmentProperties("fromPath", path); + path += "envPropertiesTest/swaggerPromoteHome"; + + EnvironmentProperties properties = new EnvironmentProperties("fromSwaggerPromoteHome", path); Assert.assertEquals(properties.containsKey("thisKeyExists"), true); - Assert.assertEquals(properties.get("thisKeyExists"), "ThisIsComingFromAPath"); + Assert.assertEquals(properties.get("thisKeyExists"), "stageKeyFromSwaggerPromoteHome"); - Assert.assertEquals(properties.get("admin_username"), "userFromPathProperty"); - Assert.assertEquals(properties.get("admin_password"), "passwordFromPathProperty"); + Assert.assertEquals(properties.get("admin_username"), "stageUserFromSwaggerPromoteHome"); + Assert.assertEquals(properties.get("admin_password"), "stageUasswordFromSwaggerPromoteHome"); } } diff --git a/modules/swagger-promote-core/src/test/resources/envProperties/env.fromPath.properties b/modules/swagger-promote-core/src/test/resources/envProperties/env.fromPath.properties deleted file mode 100644 index 0b571718..00000000 --- a/modules/swagger-promote-core/src/test/resources/envProperties/env.fromPath.properties +++ /dev/null @@ -1,6 +0,0 @@ -admin_username=userFromPathProperty -admin_password=passwordFromPathProperty - -thisKeyExists=ThisIsComingFromAPath - -OS_MAIN_AND_STAGE_ENV_PROPERTY=valueFromAnyOtherStageEnv \ No newline at end of file diff --git a/modules/swagger-promote-core/src/test/resources/envProperties/env.properties b/modules/swagger-promote-core/src/test/resources/envProperties/env.properties deleted file mode 100644 index 86400f68..00000000 --- a/modules/swagger-promote-core/src/test/resources/envProperties/env.properties +++ /dev/null @@ -1,6 +0,0 @@ -admin_username=basicUserFromPathProperty -admin_password=basicPasswordFromPathProperty - -thisKeyExists=BasicConfigFromPath - -OS_MAIN_AND_STAGE_ENV_PROPERTY=valueFromAnyOtherStageEnv \ No newline at end of file diff --git a/modules/swagger-promote-core/src/test/resources/envPropertiesTest/swaggerPromoteHome/conf/env.fromSwaggerPromoteHome.properties b/modules/swagger-promote-core/src/test/resources/envPropertiesTest/swaggerPromoteHome/conf/env.fromSwaggerPromoteHome.properties new file mode 100644 index 00000000..c9c1a4c0 --- /dev/null +++ b/modules/swagger-promote-core/src/test/resources/envPropertiesTest/swaggerPromoteHome/conf/env.fromSwaggerPromoteHome.properties @@ -0,0 +1,4 @@ +admin_username=stageUserFromSwaggerPromoteHome +admin_password=stageUasswordFromSwaggerPromoteHome + +thisKeyExists=stageKeyFromSwaggerPromoteHome diff --git a/modules/swagger-promote-core/src/test/resources/envPropertiesTest/swaggerPromoteHome/conf/env.properties b/modules/swagger-promote-core/src/test/resources/envPropertiesTest/swaggerPromoteHome/conf/env.properties new file mode 100644 index 00000000..0314fd8b --- /dev/null +++ b/modules/swagger-promote-core/src/test/resources/envPropertiesTest/swaggerPromoteHome/conf/env.properties @@ -0,0 +1,4 @@ +admin_username=userFromSwaggerPromoteHome +admin_password=passwordFromSwaggerPromoteHome + +thisKeyExists=keyFromSwaggerPromoteHome \ No newline at end of file