// Path: src/main/java/com/xti/jenkins/plugin/awslambda/callable/EventSourceCallable.java
// public class EventSourceCallable implements Callable<Boolean, RuntimeException> {
// 
//     private TaskListener listener;
//     private EventSourceConfig eventSourceConfig;
//     private LambdaClientConfig clientConfig;
// 
//     public EventSourceCallable(TaskListener listener, EventSourceConfig eventSourceConfig, LambdaClientConfig clientConfig) {
//         this.listener = listener;
//         this.eventSourceConfig = eventSourceConfig;
//         this.clientConfig = clientConfig;
//     }
// 
//     @Override
//     public Boolean call() throws RuntimeException {
//         JenkinsLogger logger = new JenkinsLogger(listener.getLogger());
//         LambdaDeployService service = new LambdaDeployService(clientConfig.getClient(), logger);
//         EventSourceBuilder builder = new EventSourceBuilder(service, logger);
// 
//         return builder.createEventSource(eventSourceConfig);
//     }
// 
//     @Override
//     public void checkRoles(RoleChecker roleChecker) throws SecurityException {
//         //ignore for now
//     }
// }
// 
// Path: src/main/java/com/xti/jenkins/plugin/awslambda/util/LambdaClientConfig.java
// public class LambdaClientConfig implements Serializable {
//     private String region;
//     private String accessKeyId;
//     private String secretKey;
//     private Boolean useDefaultAWSCredentials;
//     private ProxyConfiguration proxyConfiguration;
// 
//     public LambdaClientConfig(String region, ProxyConfiguration proxyConfiguration){
//         this.region = region;
//         this.useDefaultAWSCredentials = true;
//         this.proxyConfiguration = proxyConfiguration;
//     }
// 
//     public LambdaClientConfig(String accessKeyId, String secretKey, String region, ProxyConfiguration proxyConfiguration) {
//         this.region = region;
//         this.accessKeyId = accessKeyId;
//         this.secretKey = secretKey;
//         this.useDefaultAWSCredentials = false;
//         this.proxyConfiguration = proxyConfiguration;
//     }
// 
//     public AWSLambdaClient getClient() {
//         if(useDefaultAWSCredentials){
//             return new AWSLambdaClient(new DefaultAWSCredentialsProviderChain(), getClientConfiguration())
//                     .withRegion(Region.getRegion(Regions.fromName(region)));
//         } else {
//             return new AWSLambdaClient(new BasicAWSCredentials(accessKeyId, secretKey), getClientConfiguration())
//                     .withRegion(Region.getRegion(Regions.fromName(region)));
//         }
//     }
// 
//     private ClientConfiguration getClientConfiguration() {
//         /*
//         Jenkins instance = Jenkins.getInstance();
// 
//         if (instance != null) {
//             ProxyConfiguration proxy = instance.proxy;
//         */
//         ClientConfiguration config = new ClientConfiguration();
// 
//         if (proxyConfiguration != null) {
//             config.setProxyHost(proxyConfiguration.name);
//             config.setProxyPort(proxyConfiguration.port);
//             if (proxyConfiguration.getUserName() != null) {
//                 config.setProxyUsername(proxyConfiguration.getUserName());
//                 config.setProxyPassword(proxyConfiguration.getPassword());
//             }
//             if (proxyConfiguration.noProxyHost != null) {
//                 config.setNonProxyHosts(proxyConfiguration.noProxyHost);
//             }
//         }
// 
//         return config;
// 
//     }
// }


import com.xti.jenkins.plugin.awslambda.callable.EventSourceCallable;
import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Builder;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import javax.annotation.Nonnull;
import java.io.IOException;
package com.xti.jenkins.plugin.awslambda.eventsource;



/**
 * Created by anthonyikeda on 25/11/2015.
 *
 */
public class LambdaEventSourceBuildStep extends Builder implements SimpleBuildStep {

    private LambdaEventSourceBuildStepVariables lambdaEventSourceBuildStepVariables;

    @DataBoundConstructor
    public LambdaEventSourceBuildStep(LambdaEventSourceBuildStepVariables lambdaEventSourceBuildStepVariables) {
        this.lambdaEventSourceBuildStepVariables = lambdaEventSourceBuildStepVariables;
    }

    public LambdaEventSourceBuildStepVariables getLambdaEventSourceBuildStepVariables() {
        return this.lambdaEventSourceBuildStepVariables;
    }

    @Override
    public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
        perform(lambdaEventSourceBuildStepVariables, run, launcher, listener);
    }

    public void perform(LambdaEventSourceBuildStepVariables lambdaEventSourceBuildStepVariables, Run<?, ?> run, Launcher launcher, TaskListener listener) {
        try {
            LambdaEventSourceBuildStepVariables executionVariables = lambdaEventSourceBuildStepVariables.getClone();
            executionVariables.expandVariables(run.getEnvironment(listener));
            EventSourceConfig eventSourceConfig = executionVariables.getEventSourceConfig();
            LambdaClientConfig clientConfig = executionVariables.getLambdaClientConfig();

// Path: src/main/java/com/xti/jenkins/plugin/awslambda/callable/EventSourceCallable.java
// public class EventSourceCallable implements Callable<Boolean, RuntimeException> {
// 
//     private TaskListener listener;
//     private EventSourceConfig eventSourceConfig;
//     private LambdaClientConfig clientConfig;
// 
//     public EventSourceCallable(TaskListener listener, EventSourceConfig eventSourceConfig, LambdaClientConfig clientConfig) {
//         this.listener = listener;
//         this.eventSourceConfig = eventSourceConfig;
//         this.clientConfig = clientConfig;
//     }
// 
//     @Override
//     public Boolean call() throws RuntimeException {
//         JenkinsLogger logger = new JenkinsLogger(listener.getLogger());
//         LambdaDeployService service = new LambdaDeployService(clientConfig.getClient(), logger);
//         EventSourceBuilder builder = new EventSourceBuilder(service, logger);
// 
//         return builder.createEventSource(eventSourceConfig);
//     }
// 
//     @Override
//     public void checkRoles(RoleChecker roleChecker) throws SecurityException {
//         //ignore for now
//     }
// }
// 
// Path: src/main/java/com/xti/jenkins/plugin/awslambda/util/LambdaClientConfig.java
// public class LambdaClientConfig implements Serializable {
//     private String region;
//     private String accessKeyId;
//     private String secretKey;
//     private Boolean useDefaultAWSCredentials;
//     private ProxyConfiguration proxyConfiguration;
// 
//     public LambdaClientConfig(String region, ProxyConfiguration proxyConfiguration){
//         this.region = region;
//         this.useDefaultAWSCredentials = true;
//         this.proxyConfiguration = proxyConfiguration;
//     }
// 
//     public LambdaClientConfig(String accessKeyId, String secretKey, String region, ProxyConfiguration proxyConfiguration) {
//         this.region = region;
//         this.accessKeyId = accessKeyId;
//         this.secretKey = secretKey;
//         this.useDefaultAWSCredentials = false;
//         this.proxyConfiguration = proxyConfiguration;
//     }
// 
//     public AWSLambdaClient getClient() {
//         if(useDefaultAWSCredentials){
//             return new AWSLambdaClient(new DefaultAWSCredentialsProviderChain(), getClientConfiguration())
//                     .withRegion(Region.getRegion(Regions.fromName(region)));
//         } else {
//             return new AWSLambdaClient(new BasicAWSCredentials(accessKeyId, secretKey), getClientConfiguration())
//                     .withRegion(Region.getRegion(Regions.fromName(region)));
//         }
//     }
// 
//     private ClientConfiguration getClientConfiguration() {
//         /*
//         Jenkins instance = Jenkins.getInstance();
// 
//         if (instance != null) {
//             ProxyConfiguration proxy = instance.proxy;
//         */
//         ClientConfiguration config = new ClientConfiguration();
// 
//         if (proxyConfiguration != null) {
//             config.setProxyHost(proxyConfiguration.name);
//             config.setProxyPort(proxyConfiguration.port);
//             if (proxyConfiguration.getUserName() != null) {
//                 config.setProxyUsername(proxyConfiguration.getUserName());
//                 config.setProxyPassword(proxyConfiguration.getPassword());
//             }
//             if (proxyConfiguration.noProxyHost != null) {
//                 config.setNonProxyHosts(proxyConfiguration.noProxyHost);
//             }
//         }
// 
//         return config;
// 
//     }
// }


// Path: src/main/java/com/xti/jenkins/plugin/awslambda/eventsource/LambdaEventSourceBuildStep.java
import com.xti.jenkins.plugin.awslambda.callable.EventSourceCallable;
import com.xti.jenkins.plugin.awslambda.util.LambdaClientConfig;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Builder;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import javax.annotation.Nonnull;
import java.io.IOException;

package com.xti.jenkins.plugin.awslambda.eventsource;



/**
 * Created by anthonyikeda on 25/11/2015.
 *
 */
public class LambdaEventSourceBuildStep extends Builder implements SimpleBuildStep {

    private LambdaEventSourceBuildStepVariables lambdaEventSourceBuildStepVariables;

    @DataBoundConstructor
    public LambdaEventSourceBuildStep(LambdaEventSourceBuildStepVariables lambdaEventSourceBuildStepVariables) {
        this.lambdaEventSourceBuildStepVariables = lambdaEventSourceBuildStepVariables;
    }

    public LambdaEventSourceBuildStepVariables getLambdaEventSourceBuildStepVariables() {
        return this.lambdaEventSourceBuildStepVariables;
    }

    @Override
    public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
        perform(lambdaEventSourceBuildStepVariables, run, launcher, listener);
    }

    public void perform(LambdaEventSourceBuildStepVariables lambdaEventSourceBuildStepVariables, Run<?, ?> run, Launcher launcher, TaskListener listener) {
        try {
            LambdaEventSourceBuildStepVariables executionVariables = lambdaEventSourceBuildStepVariables.getClone();
            executionVariables.expandVariables(run.getEnvironment(listener));
            EventSourceConfig eventSourceConfig = executionVariables.getEventSourceConfig();
            LambdaClientConfig clientConfig = executionVariables.getLambdaClientConfig();

            EventSourceCallable eventSourceCallable = new EventSourceCallable(listener, eventSourceConfig, clientConfig);