package br.com.ingenieux.mojo.beanstalk.config;

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Arrays;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
import br.com.ingenieux.mojo.beanstalk.ConfigurationTemplate;

import com.amazonaws.services.elasticbeanstalk.model.CreateConfigurationTemplateRequest;
import com.amazonaws.services.elasticbeanstalk.model.CreateConfigurationTemplateResult;

/**
 * Describes Available Configuration Templates
 * 
 * @author Aldrin Leal
 * @since 0.2.5
 */
@Mojo(name="create-configuration-templates")
public class CreateConfigurationTemplateMojo extends AbstractBeanstalkMojo {
	/**
	 * Beanstalk Application Name
	 */
	@Parameter(property="beanstalk.applicationName", defaultValue="${project.artifactId}", required=true)
	String applicationName;

	/**
	 * Configuration Template Name (Optional)
	 */
	@Parameter(property="beanstalk.configurationTemplate")
	String configurationTemplate;
	
	/**
	 * Configuration Templates
	 */
	@Parameter
	ConfigurationTemplate[] configurationTemplates;
	
	@Override
	protected Object executeInternal() throws MojoExecutionException,
	    MojoFailureException {
//		DescribeApplicationsRequest req = new DescribeApplicationsRequest()
//		    .withApplicationNames(applicationName);
		boolean bConfigurationTemplateDefined = StringUtils
		    .isNotBlank(configurationTemplate);
		
		if (bConfigurationTemplateDefined) {
			return createConfiguration(configurationTemplate);
		} else {
			for (ConfigurationTemplate template : configurationTemplates)
				createConfiguration(template.getId());
		}
		
		return null;
	}

	CreateConfigurationTemplateResult createConfiguration(String templateName) {
		ConfigurationTemplate template = getConfigurationTemplate(templateName);
		
		CreateConfigurationTemplateRequest req = new CreateConfigurationTemplateRequest(applicationName, templateName);
		
		req.setSolutionStackName(template.getSolutionStack());
		req.setOptionSettings(Arrays.asList(template.getOptionSettings()));
		
		return getService().createConfigurationTemplate(req);
  }

	private ConfigurationTemplate getConfigurationTemplate(String id) {
		for (ConfigurationTemplate template : configurationTemplates)
			if (id.equals(template.getId()))
				return template;
		
	  return null;
  }
}

@@ -1995,130 +1995,8 @@
  %7B%0D%0A
-//%09%09DescribeApplicationsRequest req = new DescribeApplicationsRequest()%0D%0A//%09%09    .withApplicationNames(applicationName);%0D%0A
 %09%09bo
@@ -2416,16 +2416,44 @@
 ateName)
+ throws MojoFailureException
  %7B%0D%0A%09%09Co
@@ -2520,26 +2520,337 @@
 lateName);%0D%0A
-%09%09
+%0D%0A        if (null == template)%0D%0A            throw new MojoFailureException(String.format(%22templateName ('%25s') not found%22, templateName));%0D%0A%0D%0A        if (null == template.getSolutionStack())%0D%0A            throw new MojoFailureException(String.format(%22Please define solutionStack/ in template %25s%22, templateName));%0D%0A
 %0D%0A%09%09CreateCo

Address a special request from our users