Skip to content

Commit

Permalink
JENKINS-28441
Browse files Browse the repository at this point in the history
TDD refactoring
  • Loading branch information
cast committed May 24, 2015
1 parent 00a35c9 commit 1f80544
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 152 deletions.
Expand Up @@ -26,9 +26,13 @@
* #L%
*/

import com.xti.jenkins.plugin.awslambda.service.JenkinsLogger;
import com.xti.jenkins.plugin.awslambda.service.LambdaDeployService;
import com.xti.jenkins.plugin.awslambda.service.WorkSpaceZipper;
import com.xti.jenkins.plugin.awslambda.upload.LambdaUploadAction;
import com.xti.jenkins.plugin.awslambda.upload.LambdaUploader;
import com.xti.jenkins.plugin.awslambda.upload.DeployConfig;
import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
Expand Down Expand Up @@ -89,10 +93,14 @@ public boolean perform(LambdaVariables lambdaVariables,AbstractBuild<?, ?> build
LambdaVariables executionVariables = lambdaVariables.getClone();
executionVariables.expandVariables(build.getEnvironment(listener));
DeployConfig deployConfig = executionVariables.getUploadConfig();
LambdaClientConfig clientConfig = executionVariables.getLambdaClientConfig();
JenkinsLogger logger = new JenkinsLogger(listener.getLogger());
LambdaDeployService service = new LambdaDeployService(clientConfig.getClient(), logger);
WorkSpaceZipper workSpaceZipper = new WorkSpaceZipper(build.getWorkspace(), logger);

LambdaUploader lambdaUploader = new LambdaUploader(deployConfig, build, listener);
LambdaUploader lambdaUploader = new LambdaUploader(service, workSpaceZipper, logger);

Boolean lambdaSuccess = lambdaUploader.upload();
Boolean lambdaSuccess = lambdaUploader.upload(deployConfig);
if(!lambdaSuccess){
build.setResult(Result.FAILURE);
}
Expand Down
Expand Up @@ -28,6 +28,7 @@

import com.xti.jenkins.plugin.awslambda.upload.DeployConfig;
import com.xti.jenkins.plugin.awslambda.upload.UpdateModeValue;
import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
Expand Down Expand Up @@ -194,7 +195,11 @@ public LambdaVariables getClone(){
}

public DeployConfig getUploadConfig(){
return new DeployConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion, artifactLocation, description, functionName, handler, memorySize, role, runtime, timeout, successOnly, updateMode);
return new DeployConfig(artifactLocation, description, functionName, handler, memorySize, role, runtime, timeout, updateMode);
}

public LambdaClientConfig getLambdaClientConfig(){
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
}

private String expand(String value, EnvVars env) {
Expand Down
Expand Up @@ -3,50 +3,18 @@
import java.util.List;

public class InvokeConfig {
private String awsAccessKeyId;
private String awsSecretKey;
private String awsRegion;
private String functionName;
private String payload;
private boolean synchronous;
private boolean successOnly;
private List<JsonParameter> jsonParameters;

public InvokeConfig(String awsAccessKeyId, String awsSecretKey, String awsRegion, String functionName, String payload, boolean synchronous, boolean successOnly, List<JsonParameter> jsonParameters) {
this.awsAccessKeyId = awsAccessKeyId;
this.awsSecretKey = awsSecretKey;
this.awsRegion = awsRegion;
public InvokeConfig(String functionName, String payload, boolean synchronous, List<JsonParameter> jsonParameters) {
this.functionName = functionName;
this.payload = payload;
this.synchronous = synchronous;
this.successOnly = successOnly;
this.jsonParameters = jsonParameters;
}

public String getAwsAccessKeyId() {
return awsAccessKeyId;
}

public void setAwsAccessKeyId(String awsAccessKeyId) {
this.awsAccessKeyId = awsAccessKeyId;
}

public String getAwsSecretKey() {
return awsSecretKey;
}

public void setAwsSecretKey(String awsSecretKey) {
this.awsSecretKey = awsSecretKey;
}

public String getAwsRegion() {
return awsRegion;
}

public void setAwsRegion(String awsRegion) {
this.awsRegion = awsRegion;
}

public String getFunctionName() {
return functionName;
}
Expand All @@ -71,14 +39,6 @@ public void setSynchronous(boolean synchronous) {
this.synchronous = synchronous;
}

public boolean isSuccessOnly() {
return successOnly;
}

public void setSuccessOnly(boolean successOnly) {
this.successOnly = successOnly;
}

public List<JsonParameter> getJsonParameters() {
return jsonParameters;
}
Expand Down
Expand Up @@ -26,6 +26,9 @@
* #L%
*/

import com.xti.jenkins.plugin.awslambda.service.JenkinsLogger;
import com.xti.jenkins.plugin.awslambda.service.LambdaInvokeService;
import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
Expand Down Expand Up @@ -68,11 +71,14 @@ public boolean perform(LambdaInvokeBuildStepVariables lambdaInvokeBuildStepVaria
try {
LambdaInvokeBuildStepVariables executionVariables = lambdaInvokeBuildStepVariables.getClone();
executionVariables.expandVariables(build.getEnvironment(listener));
JenkinsLogger logger = new JenkinsLogger(listener.getLogger());
LambdaClientConfig clientConfig = executionVariables.getLambdaClientConfig();
InvokeConfig invokeConfig = executionVariables.getInvokeConfig();
LambdaInvokeService service = new LambdaInvokeService(clientConfig.getClient(), logger);

LambdaInvoker lambdaInvoker = new LambdaInvoker(invokeConfig, build, listener);
LambdaInvoker lambdaInvoker = new LambdaInvoker(service, logger);

LambdaInvocationResult invocationResult = lambdaInvoker.invoke();
LambdaInvocationResult invocationResult = lambdaInvoker.invoke(invokeConfig);
if(!invocationResult.isSuccess()){
build.setResult(Result.FAILURE);
}
Expand Down
Expand Up @@ -26,6 +26,7 @@
* #L%
*/

import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
Expand Down Expand Up @@ -137,7 +138,11 @@ public InvokeConfig getInvokeConfig(){
for (JsonParameterVariables jsonParameterVariables : getJsonParameters()) {
jsonParameters.add(jsonParameterVariables.getJsonParameter());
}
return new InvokeConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion, functionName, payload, synchronous, false, jsonParameters);
return new InvokeConfig(functionName, payload, synchronous, jsonParameters);
}

public LambdaClientConfig getLambdaClientConfig(){
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
}

@Extension // This indicates to Jenkins that this is an implementation of an extension point.
Expand Down
Expand Up @@ -26,6 +26,9 @@
* #L%
*/

import com.xti.jenkins.plugin.awslambda.service.JenkinsLogger;
import com.xti.jenkins.plugin.awslambda.service.LambdaInvokeService;
import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
Expand Down Expand Up @@ -86,11 +89,14 @@ public boolean perform(LambdaInvokeVariables lambdaInvokeVariables,AbstractBuild
try {
LambdaInvokeVariables executionVariables = lambdaInvokeVariables.getClone();
executionVariables.expandVariables(build.getEnvironment(listener));
JenkinsLogger logger = new JenkinsLogger(listener.getLogger());
LambdaClientConfig clientConfig = executionVariables.getLambdaClientConfig();
InvokeConfig invokeConfig = executionVariables.getInvokeConfig();
LambdaInvokeService service = new LambdaInvokeService(clientConfig.getClient(), logger);

LambdaInvoker lambdaInvoker = new LambdaInvoker(invokeConfig, build, listener);
LambdaInvoker lambdaInvoker = new LambdaInvoker(service, logger);

LambdaInvocationResult invocationResult = lambdaInvoker.invoke();
LambdaInvocationResult invocationResult = lambdaInvoker.invoke(invokeConfig);

if(!invocationResult.isSuccess()){
build.setResult(Result.FAILURE);
Expand Down
Expand Up @@ -26,6 +26,7 @@
* #L%
*/

import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
Expand Down Expand Up @@ -147,7 +148,11 @@ public InvokeConfig getInvokeConfig(){
for (JsonParameterVariables jsonParameterVariables : getJsonParameters()) {
jsonParameters.add(jsonParameterVariables.getJsonParameter());
}
return new InvokeConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion, functionName, payload, synchronous, successOnly, jsonParameters);
return new InvokeConfig(functionName, payload, synchronous, jsonParameters);
}

public LambdaClientConfig getLambdaClientConfig(){
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
}

@Extension // This indicates to Jenkins that this is an implementation of an extension point.
Expand Down
Expand Up @@ -26,32 +26,27 @@
* #L%
*/

import com.xti.jenkins.plugin.awslambda.service.*;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import com.xti.jenkins.plugin.awslambda.service.JenkinsLogger;
import com.xti.jenkins.plugin.awslambda.service.JsonPathParser;
import com.xti.jenkins.plugin.awslambda.service.LambdaInvokeService;

import java.io.IOException;
import java.util.Map;

public class LambdaInvoker {
private JenkinsLogger logger;
private InvokeConfig config;

private LambdaInvokeService lambda;
private JsonPathParser jsonPathParser;

public LambdaInvoker(InvokeConfig config, AbstractBuild<?, ?> build, BuildListener listener) throws IOException, InterruptedException {
this.config = config;
logger = new JenkinsLogger(listener.getLogger());
LambdaClientConfig lambdaClientConfig = new LambdaClientConfig(config.getAwsAccessKeyId(), config.getAwsSecretKey(), config.getAwsRegion());
lambda = new LambdaInvokeService(lambdaClientConfig.getClient(), logger);
jsonPathParser = new JsonPathParser(config.getJsonParameters(), logger);
private JenkinsLogger logger;

public LambdaInvoker(LambdaInvokeService lambda, JenkinsLogger logger) {
this.lambda = lambda;
this.logger = logger;
}

public LambdaInvocationResult invoke() throws IOException, InterruptedException {
public LambdaInvocationResult invoke(InvokeConfig config) throws IOException, InterruptedException {
JsonPathParser jsonPathParser = new JsonPathParser(config.getJsonParameters(), logger);
logger.log("%nStarting lambda invocation.");

String output = lambda.invokeLambdaFunction(config);

Map<String, String> injectables = jsonPathParser.parse(output);

return new LambdaInvocationResult(true, injectables);
Expand Down
@@ -0,0 +1,47 @@
package com.xti.jenkins.plugin.awslambda.service;

import hudson.FilePath;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WorkSpaceZipper {
private FilePath workSpace;
private JenkinsLogger logger;

public WorkSpaceZipper(FilePath workSpace, JenkinsLogger logger) {
this.workSpace = workSpace;
this.logger = logger;
}

public File getZip(String artifactLocation) throws IOException, InterruptedException {
FilePath artifactFilePath = null;

if(StringUtils.isNotEmpty(artifactLocation)) {
artifactFilePath = new FilePath(workSpace, artifactLocation);
}
File zipFile = null;
if(artifactFilePath != null){
zipFile = getArtifactZip(artifactFilePath);
}

return zipFile;
}

private File getArtifactZip(FilePath artifactLocation) throws IOException, InterruptedException {
File resultFile = File.createTempFile("awslambda-", ".zip");

if (!artifactLocation.isDirectory()) {
logger.log("Copying zip file");
artifactLocation.copyTo(new FileOutputStream(resultFile));
} else {
logger.log("Zipping folder ..., copying zip file");
artifactLocation.zip(new FileOutputStream(resultFile));
}

logger.log("File Name: %s%nAbsolute Path: %s%nFile Size: %d", resultFile.getName(), resultFile.getAbsolutePath(), resultFile.length());
return resultFile;
}
}
@@ -1,9 +1,6 @@
package com.xti.jenkins.plugin.awslambda.upload;

public class DeployConfig {
private String awsAccessKeyId;
private String awsSecretKey;
private String awsRegion;
private String artifactLocation;
private String description;
private String functionName;
Expand All @@ -12,13 +9,9 @@ public class DeployConfig {
private String role;
private String runtime;
private Integer timeout;
private boolean successOnly;
private String updateMode;

public DeployConfig(String awsAccessKeyId, String awsSecretKey, String awsRegion, String artifactLocation, String description, String functionName, String handler, Integer memorySize, String role, String runtime, Integer timeout, boolean successOnly, String updateMode) {
this.awsAccessKeyId = awsAccessKeyId;
this.awsSecretKey = awsSecretKey;
this.awsRegion = awsRegion;
public DeployConfig(String artifactLocation, String description, String functionName, String handler, Integer memorySize, String role, String runtime, Integer timeout, String updateMode) {
this.artifactLocation = artifactLocation;
this.description = description;
this.functionName = functionName;
Expand All @@ -27,34 +20,9 @@ public DeployConfig(String awsAccessKeyId, String awsSecretKey, String awsRegion
this.role = role;
this.runtime = runtime;
this.timeout = timeout;
this.successOnly = successOnly;
this.updateMode = updateMode;
}

public String getAwsAccessKeyId() {
return awsAccessKeyId;
}

public void setAwsAccessKeyId(String awsAccessKeyId) {
this.awsAccessKeyId = awsAccessKeyId;
}

public String getAwsSecretKey() {
return awsSecretKey;
}

public void setAwsSecretKey(String awsSecretKey) {
this.awsSecretKey = awsSecretKey;
}

public String getAwsRegion() {
return awsRegion;
}

public void setAwsRegion(String awsRegion) {
this.awsRegion = awsRegion;
}

public String getArtifactLocation() {
return artifactLocation;
}
Expand Down Expand Up @@ -119,14 +87,6 @@ public void setTimeout(Integer timeout) {
this.timeout = timeout;
}

public boolean isSuccessOnly() {
return successOnly;
}

public void setSuccessOnly(boolean successOnly) {
this.successOnly = successOnly;
}

public String getUpdateMode() {
return updateMode;
}
Expand Down

0 comments on commit 1f80544

Please sign in to comment.