Skip to content

Commit

Permalink
JENKINS-28630
Browse files Browse the repository at this point in the history
Added env var expanding to payload and json parameters
  • Loading branch information
cast committed May 28, 2015
1 parent 42ca5e4 commit 321db78
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 1 deletion.
Expand Up @@ -245,6 +245,7 @@ public String getDisplayName() {
return "Deploy into Lambda";
}


}


}
Expand Up @@ -26,7 +26,9 @@
* #L%
*/

import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.util.FormValidation;
Expand Down Expand Up @@ -64,6 +66,16 @@ public JsonParameter getJsonParameter(){
return new JsonParameter(envVarName, jsonPath);
}

public void expandVariables(EnvVars env) {
envVarName = expand(envVarName, env);
jsonPath = expand(jsonPath, env);
}

private String expand(String value, EnvVars env) {
return Util.replaceMacro(value.trim(), env);
}


@Extension // This indicates to Jenkins that this is an implementation of an extension point.
public static class DescriptorImpl extends Descriptor<JsonParameterVariables> {

Expand Down
Expand Up @@ -126,6 +126,10 @@ public void expandVariables(EnvVars env) {
awsSecretKey = Secret.fromString(expand(Secret.toString(awsSecretKey), env));
awsRegion = expand(awsRegion, env);
functionName = expand(functionName, env);
payload = expand(payload, env);
for (JsonParameterVariables jsonParameter : jsonParameters) {
jsonParameter.expandVariables(env);
}
}

public LambdaInvokeBuildStepVariables getClone(){
Expand Down
Expand Up @@ -133,6 +133,10 @@ public void expandVariables(EnvVars env) {
awsSecretKey = Secret.fromString(expand(Secret.toString(awsSecretKey), env));
awsRegion = expand(awsRegion, env);
functionName = expand(functionName, env);
payload = expand(payload, env);
for (JsonParameterVariables jsonParameter : jsonParameters) {
jsonParameter.expandVariables(env);
}
}

public LambdaInvokeVariables getClone(){
Expand Down
@@ -0,0 +1,72 @@
package com.xti.jenkins.plugin.awslambda;

import com.amazonaws.services.lambda.AWSLambda;
import com.xti.jenkins.plugin.awslambda.upload.DeployConfig;
import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.EnvVars;
import hudson.util.Secret;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class LambdaVariablesTest {

@Test
public void testCloneExpandVariables() throws Exception {
LambdaVariables variables = new LambdaVariables("${ENV_ID}", Secret.fromString("$ENV_SECRET}"), "${ENV_REGION}", "${ENV_FILE}", "description ${ENV_DESCRIPTION}", "${ENV_FUNCTION}", "${ENV_HANDLER}", 1024, "${ENV_ROLE}", "$ENV_RUNTIME", 30, true, "full");
LambdaVariables clone = variables.getClone();

EnvVars envVars = new EnvVars();
envVars.put("ENV_ID", "ID");
envVars.put("ENV_SECRET", "SECRET");
envVars.put("ENV_REGION", "eu-west-1");
envVars.put("ENV_FILE", "FILE");
envVars.put("ENV_DESCRIPTION", "DESCRIPTION");
envVars.put("ENV_FUNCTION", "FUNCTION");
envVars.put("ENV_HANDLER", "HANDLER");
envVars.put("ENV_ROLE", "ROLE");
envVars.put("ENV_RUNTIME", "RUNTIME");
clone.expandVariables(envVars);

LambdaVariables expected = new LambdaVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FILE", "description DESCRIPTION", "FUNCTION", "HANDLER", 1024, "ROLE", "RUNTIME", 30, true, "full");

assertEquals(expected.getAwsAccessKeyId(), clone.getAwsAccessKeyId());
assertEquals(expected.getAwsSecretKey(), clone.getAwsSecretKey());
assertEquals(expected.getAwsRegion(), clone.getAwsRegion());
assertEquals(expected.getArtifactLocation(), clone.getArtifactLocation());
assertEquals(expected.getDescription(), clone.getDescription());
assertEquals(expected.getFunctionName(), clone.getFunctionName());
assertEquals(expected.getMemorySize(), clone.getMemorySize());
assertEquals(expected.getTimeout(), clone.getTimeout());
assertEquals(expected.getHandler(), clone.getHandler());
assertEquals(expected.getRole(), clone.getRole());
assertEquals(expected.getRuntime(), clone.getRuntime());
assertEquals(expected.getSuccessOnly(), clone.getSuccessOnly());
}

@Test
public void testGetUploadConfig() throws Exception {
LambdaVariables variables = new LambdaVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FILE", "description DESCRIPTION", "FUNCTION", "HANDLER", 1024, "ROLE", "RUNTIME", 30, true, "full");
DeployConfig uploadConfig = variables.getUploadConfig();

assertEquals(variables.getArtifactLocation(), uploadConfig.getArtifactLocation());
assertEquals(variables.getDescription(), uploadConfig.getDescription());
assertEquals(variables.getMemorySize(), uploadConfig.getMemorySize());
assertEquals(variables.getTimeout(), uploadConfig.getTimeout());
assertEquals(variables.getHandler(), uploadConfig.getHandler());
assertEquals(variables.getRuntime(), uploadConfig.getRuntime());
assertEquals(variables.getFunctionName(), uploadConfig.getFunctionName());
assertEquals(variables.getRole(), uploadConfig.getRole());
assertEquals(variables.getUpdateMode(), uploadConfig.getUpdateMode());
}

@Test
public void testGetLambdaClientConfig() throws Exception {
LambdaVariables variables = new LambdaVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FILE", "description DESCRIPTION", "FUNCTION", "HANDLER", 1024, "ROLE", "RUNTIME", 30, true, "full");
LambdaClientConfig lambdaClientConfig = variables.getLambdaClientConfig();

AWSLambda lambda = lambdaClientConfig.getClient();
assertNotNull(lambda);
}
}
@@ -0,0 +1,71 @@
package com.xti.jenkins.plugin.awslambda.invoke;

import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.EnvVars;
import hudson.util.Secret;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class LambdaInvokeBuildStepVariablesTest {

@Test
public void testCloneExpandVariables() throws Exception {
List<JsonParameterVariables> jsonParameterVariables = new ArrayList<JsonParameterVariables>();
jsonParameterVariables.add(new JsonParameterVariables("ENV_$ENV_ENV_NAME", "$.$ENV_JSON_PATH"));
LambdaInvokeBuildStepVariables variables = new LambdaInvokeBuildStepVariables("${ENV_ID}", Secret.fromString("$ENV_SECRET}"), "${ENV_REGION}", "${ENV_FUNCTION}", "${\"payload\":\"${ENV_PAYLOAD}\"", true, jsonParameterVariables);
LambdaInvokeBuildStepVariables clone = variables.getClone();

EnvVars envVars = new EnvVars();
envVars.put("ENV_ID", "ID");
envVars.put("ENV_SECRET", "SECRET");
envVars.put("ENV_REGION", "eu-west-1");
envVars.put("ENV_FUNCTION", "FUNCTION");
envVars.put("ENV_PAYLOAD", "hello");
envVars.put("ENV_ENV_NAME", "NAME");
envVars.put("ENV_JSON_PATH", "path");
envVars.put(".", "bad");
clone.expandVariables(envVars);

List<JsonParameterVariables> jsonParameterVariablesExpected = new ArrayList<JsonParameterVariables>();
jsonParameterVariablesExpected.add(new JsonParameterVariables("ENV_NAME", "$.path"));
LambdaInvokeBuildStepVariables expected = new LambdaInvokeBuildStepVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FUNCTION", "${\"payload\":\"hello\"", true, jsonParameterVariablesExpected);

assertEquals(expected.getAwsAccessKeyId(), clone.getAwsAccessKeyId());
assertEquals(expected.getAwsSecretKey(), clone.getAwsSecretKey());
assertEquals(expected.getAwsRegion(), clone.getAwsRegion());
assertEquals(expected.getFunctionName(), clone.getFunctionName());
assertEquals(expected.getPayload(), clone.getPayload());
assertEquals(expected.getSynchronous(), clone.getSynchronous());
assertEquals(expected.getJsonParameters().get(0).getEnvVarName(), clone.getJsonParameters().get(0).getEnvVarName());
assertEquals(expected.getJsonParameters().get(0).getJsonPath(), clone.getJsonParameters().get(0).getJsonPath());
}

@Test
public void testGetInvokeConfig() throws Exception {
List<JsonParameterVariables> jsonParameterVariables = new ArrayList<JsonParameterVariables>();
jsonParameterVariables.add(new JsonParameterVariables("ENV_NAME", "$.path"));
LambdaInvokeBuildStepVariables variables = new LambdaInvokeBuildStepVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FUNCTION", "${\"payload\":\"hello\"", true, jsonParameterVariables);
InvokeConfig invokeConfig = variables.getInvokeConfig();

assertEquals(variables.getFunctionName(), invokeConfig.getFunctionName());
assertEquals(variables.getPayload(), invokeConfig.getPayload());
assertEquals(variables.getSynchronous(), invokeConfig.isSynchronous());
assertEquals(variables.getJsonParameters().get(0).getEnvVarName(), invokeConfig.getJsonParameters().get(0).getEnvVarName());
assertEquals(variables.getJsonParameters().get(0).getJsonPath(), invokeConfig.getJsonParameters().get(0).getJsonPath());
}

@Test
public void testGetLambdaClientConfig() throws Exception {
List<JsonParameterVariables> jsonParameterVariables = new ArrayList<JsonParameterVariables>();
jsonParameterVariables.add(new JsonParameterVariables("ENV_NAME", "$.path"));
LambdaInvokeBuildStepVariables variables = new LambdaInvokeBuildStepVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FUNCTION", "${\"payload\":\"hello\"", true, jsonParameterVariables);

LambdaClientConfig clientConfig = variables.getLambdaClientConfig();
assertNotNull(clientConfig);
}
}
@@ -0,0 +1,72 @@
package com.xti.jenkins.plugin.awslambda.invoke;

import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.EnvVars;
import hudson.util.Secret;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class LambdaInvokeVariablesTest {

@Test
public void testCloneExpandVariables() throws Exception {
List<JsonParameterVariables> jsonParameterVariables = new ArrayList<JsonParameterVariables>();
jsonParameterVariables.add(new JsonParameterVariables("ENV_$ENV_ENV_NAME", "$.$ENV_JSON_PATH"));
LambdaInvokeVariables variables = new LambdaInvokeVariables("${ENV_ID}", Secret.fromString("$ENV_SECRET}"), "${ENV_REGION}", "${ENV_FUNCTION}", "${\"payload\":\"${ENV_PAYLOAD}\"", true, true, jsonParameterVariables);
LambdaInvokeVariables clone = variables.getClone();

EnvVars envVars = new EnvVars();
envVars.put("ENV_ID", "ID");
envVars.put("ENV_SECRET", "SECRET");
envVars.put("ENV_REGION", "eu-west-1");
envVars.put("ENV_FUNCTION", "FUNCTION");
envVars.put("ENV_PAYLOAD", "hello");
envVars.put("ENV_ENV_NAME", "NAME");
envVars.put("ENV_JSON_PATH", "path");
envVars.put(".", "bad");
clone.expandVariables(envVars);

List<JsonParameterVariables> jsonParameterVariablesExpected = new ArrayList<JsonParameterVariables>();
jsonParameterVariablesExpected.add(new JsonParameterVariables("ENV_NAME", "$.path"));
LambdaInvokeVariables expected = new LambdaInvokeVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FUNCTION", "${\"payload\":\"hello\"", true, true, jsonParameterVariablesExpected);

assertEquals(expected.getAwsAccessKeyId(), clone.getAwsAccessKeyId());
assertEquals(expected.getAwsSecretKey(), clone.getAwsSecretKey());
assertEquals(expected.getAwsRegion(), clone.getAwsRegion());
assertEquals(expected.getFunctionName(), clone.getFunctionName());
assertEquals(expected.getPayload(), clone.getPayload());
assertEquals(expected.getSynchronous(), clone.getSynchronous());
assertEquals(expected.getSuccessOnly(), clone.getSuccessOnly());
assertEquals(expected.getJsonParameters().get(0).getEnvVarName(), clone.getJsonParameters().get(0).getEnvVarName());
assertEquals(expected.getJsonParameters().get(0).getJsonPath(), clone.getJsonParameters().get(0).getJsonPath());
}

@Test
public void testGetInvokeConfig() throws Exception {
List<JsonParameterVariables> jsonParameterVariables = new ArrayList<JsonParameterVariables>();
jsonParameterVariables.add(new JsonParameterVariables("ENV_NAME", "$.path"));
LambdaInvokeVariables variables = new LambdaInvokeVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FUNCTION", "${\"payload\":\"hello\"", true, true, jsonParameterVariables);
InvokeConfig invokeConfig = variables.getInvokeConfig();

assertEquals(variables.getFunctionName(), invokeConfig.getFunctionName());
assertEquals(variables.getPayload(), invokeConfig.getPayload());
assertEquals(variables.getSynchronous(), invokeConfig.isSynchronous());
assertEquals(variables.getJsonParameters().get(0).getEnvVarName(), invokeConfig.getJsonParameters().get(0).getEnvVarName());
assertEquals(variables.getJsonParameters().get(0).getJsonPath(), invokeConfig.getJsonParameters().get(0).getJsonPath());
}

@Test
public void testGetLambdaClientConfig() throws Exception {
List<JsonParameterVariables> jsonParameterVariables = new ArrayList<JsonParameterVariables>();
jsonParameterVariables.add(new JsonParameterVariables("ENV_NAME", "$.path"));
LambdaInvokeVariables variables = new LambdaInvokeVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FUNCTION", "${\"payload\":\"hello\"", true, true, jsonParameterVariables);

LambdaClientConfig clientConfig = variables.getLambdaClientConfig();
assertNotNull(clientConfig);
}
}
@@ -0,0 +1,70 @@
package com.xti.jenkins.plugin.awslambda.upload;

import com.amazonaws.services.lambda.AWSLambda;
import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.EnvVars;
import hudson.util.Secret;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class LambdaUploadBuildStepVariablesTest {

@Test
public void testCloneExpandVariables() throws Exception {
LambdaUploadBuildStepVariables variables = new LambdaUploadBuildStepVariables("${ENV_ID}", Secret.fromString("$ENV_SECRET}"), "${ENV_REGION}", "${ENV_FILE}", "description ${ENV_DESCRIPTION}", "${ENV_FUNCTION}", "${ENV_HANDLER}", 1024, "${ENV_ROLE}", "$ENV_RUNTIME", 30, "full");
LambdaUploadBuildStepVariables clone = variables.getClone();

EnvVars envVars = new EnvVars();
envVars.put("ENV_ID", "ID");
envVars.put("ENV_SECRET", "SECRET");
envVars.put("ENV_REGION", "eu-west-1");
envVars.put("ENV_FILE", "FILE");
envVars.put("ENV_DESCRIPTION", "DESCRIPTION");
envVars.put("ENV_FUNCTION", "FUNCTION");
envVars.put("ENV_HANDLER", "HANDLER");
envVars.put("ENV_ROLE", "ROLE");
envVars.put("ENV_RUNTIME", "RUNTIME");
clone.expandVariables(envVars);

LambdaUploadBuildStepVariables expected = new LambdaUploadBuildStepVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FILE", "description DESCRIPTION", "FUNCTION", "HANDLER", 1024, "ROLE", "RUNTIME", 30, "full");

assertEquals(expected.getAwsAccessKeyId(), clone.getAwsAccessKeyId());
assertEquals(expected.getAwsSecretKey(), clone.getAwsSecretKey());
assertEquals(expected.getAwsRegion(), clone.getAwsRegion());
assertEquals(expected.getArtifactLocation(), clone.getArtifactLocation());
assertEquals(expected.getDescription(), clone.getDescription());
assertEquals(expected.getFunctionName(), clone.getFunctionName());
assertEquals(expected.getMemorySize(), clone.getMemorySize());
assertEquals(expected.getTimeout(), clone.getTimeout());
assertEquals(expected.getHandler(), clone.getHandler());
assertEquals(expected.getRole(), clone.getRole());
assertEquals(expected.getRuntime(), clone.getRuntime());
}

@Test
public void testGetUploadConfig() throws Exception {
LambdaUploadBuildStepVariables variables = new LambdaUploadBuildStepVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FILE", "description DESCRIPTION", "FUNCTION", "HANDLER", 1024, "ROLE", "RUNTIME", 30, "full");
DeployConfig uploadConfig = variables.getUploadConfig();

assertEquals(variables.getArtifactLocation(), uploadConfig.getArtifactLocation());
assertEquals(variables.getDescription(), uploadConfig.getDescription());
assertEquals(variables.getMemorySize(), uploadConfig.getMemorySize());
assertEquals(variables.getTimeout(), uploadConfig.getTimeout());
assertEquals(variables.getHandler(), uploadConfig.getHandler());
assertEquals(variables.getRuntime(), uploadConfig.getRuntime());
assertEquals(variables.getFunctionName(), uploadConfig.getFunctionName());
assertEquals(variables.getRole(), uploadConfig.getRole());
assertEquals(variables.getUpdateMode(), uploadConfig.getUpdateMode());
}

@Test
public void testGetLambdaClientConfig() throws Exception {
LambdaUploadBuildStepVariables variables = new LambdaUploadBuildStepVariables("ID", Secret.fromString("SECRET}"), "eu-west-1", "FILE", "description DESCRIPTION", "FUNCTION", "HANDLER", 1024, "ROLE", "RUNTIME", 30, "full");
LambdaClientConfig lambdaClientConfig = variables.getLambdaClientConfig();

AWSLambda lambda = lambdaClientConfig.getClient();
assertNotNull(lambda);
}
}

0 comments on commit 321db78

Please sign in to comment.