Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
#249 Adding support for SWAGGER_PROMOTE_HOME
Browse files Browse the repository at this point in the history
containing a conf directory that is  used to load environment properties.
  • Loading branch information
Chris Wiechmann committed Mar 27, 2020
1 parent 3be6f63 commit 49c9276
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +17,7 @@ public class EnvironmentProperties implements Map<String, String> {
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();
Expand All @@ -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();
}

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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
admin_username=stageUserFromSwaggerPromoteHome
admin_password=stageUasswordFromSwaggerPromoteHome

thisKeyExists=stageKeyFromSwaggerPromoteHome
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
admin_username=userFromSwaggerPromoteHome
admin_password=passwordFromSwaggerPromoteHome

thisKeyExists=keyFromSwaggerPromoteHome

0 comments on commit 49c9276

Please sign in to comment.