View Javadoc
1   /*
2    * Copyright (c) 2016 ingenieux Labs
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package br.com.ingenieux.mojo.beanstalk.config;
18  
19  import com.amazonaws.services.elasticbeanstalk.model.CreateConfigurationTemplateRequest;
20  import com.amazonaws.services.elasticbeanstalk.model.CreateConfigurationTemplateResult;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  
28  import java.util.Arrays;
29  
30  import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
31  import br.com.ingenieux.mojo.beanstalk.ConfigurationTemplate;
32  
33  import static org.apache.commons.lang.StringUtils.isBlank;
34  
35  /**
36   * Describes Available Configuration Templates
37   *
38   * @author Aldrin Leal
39   * @since 0.2.5
40   */
41  @Mojo(name = "create-configuration-templates")
42  public class CreateConfigurationTemplateMojo extends AbstractBeanstalkMojo {
43  
44    /**
45     * Beanstalk Application Name
46     */
47    @Parameter(property = "beanstalk.applicationName", defaultValue = "${project.artifactId}", required = true)
48    String applicationName;
49  
50    /**
51     * Configuration Template Name (Optional)
52     */
53    @Parameter(property = "beanstalk.configurationTemplate")
54    String configurationTemplate;
55  
56    /**
57     * Configuration Templates
58     */
59    @Parameter ConfigurationTemplate[] configurationTemplates;
60  
61    @Override
62    protected Object executeInternal() throws MojoExecutionException, MojoFailureException {
63      boolean bConfigurationTemplateDefined = StringUtils.isNotBlank(configurationTemplate);
64  
65      if (bConfigurationTemplateDefined) {
66        return createConfiguration(configurationTemplate);
67      } else {
68        for (ConfigurationTemplate template : configurationTemplates) {
69          createConfiguration(template.getId());
70        }
71      }
72  
73      return null;
74    }
75  
76    CreateConfigurationTemplateResult createConfiguration(String templateName) throws MojoFailureException {
77      ConfigurationTemplate template = getConfigurationTemplate(templateName);
78  
79      if (null == template) {
80        throw new MojoFailureException(String.format("templateName ('%s') not found", templateName));
81      }
82  
83      if (isBlank(template.getSolutionStack())) {
84        throw new MojoFailureException(String.format("Please define solutionStack/ in template %s", templateName));
85      }
86  
87      CreateConfigurationTemplateRequest req = new CreateConfigurationTemplateRequest(applicationName, templateName);
88  
89      req.setSolutionStackName(template.getSolutionStack());
90      req.setOptionSettings(Arrays.asList(template.getOptionSettings()));
91  
92      return getService().createConfigurationTemplate(req);
93    }
94  
95    private ConfigurationTemplate getConfigurationTemplate(String id) {
96      for (ConfigurationTemplate template : configurationTemplates) {
97        if (id.equals(template.getId())) {
98          return template;
99        }
100     }
101 
102     return null;
103   }
104 }