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.ApplicationDescription;
20  import com.amazonaws.services.elasticbeanstalk.model.ConfigurationOptionSetting;
21  import com.amazonaws.services.elasticbeanstalk.model.ConfigurationSettingsDescription;
22  import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationsRequest;
23  import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationsResult;
24  import com.amazonaws.services.elasticbeanstalk.model.DescribeConfigurationSettingsRequest;
25  import com.amazonaws.services.elasticbeanstalk.model.DescribeConfigurationSettingsResult;
26  
27  import org.apache.commons.io.IOUtils;
28  import org.apache.commons.lang.StringUtils;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  
33  import java.io.File;
34  import java.io.FileWriter;
35  import java.io.IOException;
36  import java.io.StringReader;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
41  
42  /**
43   * Describes Available Configuration Templates
44   *
45   * @author Aldrin Leal
46   * @since 0.2.5
47   */
48  @Mojo(name = "describe-configuration-templates")
49  public class DescribeConfigurationTemplatesMojo extends AbstractBeanstalkMojo {
50  
51    private static final String ENDL = System.getProperty("line.separator");
52  
53    /**
54     * Beanstalk Application Name
55     */
56    @Parameter(property = "beanstalk.applicationName", defaultValue = "${project.artifactId}", required = true)
57    protected String applicationName;
58  
59    /**
60     * Configuration Template Name (Optional)
61     */
62    @Parameter(property = "beanstalk.configurationTemplate")
63    String configurationTemplate;
64  
65    /**
66     * Output file (Optional)
67     */
68    @Parameter(property = "beanstalk.outputFile")
69    File outputFile;
70  
71    @Override
72    protected Object executeInternal() throws Exception {
73      DescribeApplicationsRequest req = new DescribeApplicationsRequest().withApplicationNames(applicationName);
74      boolean bConfigurationTemplateDefined = StringUtils.isNotBlank(configurationTemplate);
75  
76      DescribeApplicationsResult apps = getService().describeApplications(req);
77  
78      List<ApplicationDescription> applications = apps.getApplications();
79  
80      if (applications.isEmpty()) {
81        String errorMessage = "Application ('" + applicationName + "') not found!";
82  
83        getLog().warn(errorMessage);
84  
85        throw new MojoFailureException(errorMessage);
86      }
87  
88      ApplicationDescription desc = applications.get(0);
89  
90      List<String> configTemplates = desc.getConfigurationTemplates();
91  
92      if (bConfigurationTemplateDefined) {
93        describeConfigurationTemplate(configurationTemplate);
94      } else {
95        for (String availConfigTemplate : configTemplates) {
96          describeConfigurationTemplate(availConfigTemplate);
97        }
98      }
99  
100     return null;
101   }
102 
103   void describeConfigurationTemplate(String configTemplateName) throws Exception {
104     DescribeConfigurationSettingsRequest req =
105         new DescribeConfigurationSettingsRequest().withApplicationName(applicationName).withTemplateName(configTemplateName);
106 
107     DescribeConfigurationSettingsResult configSettings = getService().describeConfigurationSettings(req);
108 
109     List<String> buf = new ArrayList<String>();
110 
111     buf.add("<optionSettings>");
112 
113     for (ConfigurationSettingsDescription configSetting : configSettings.getConfigurationSettings()) {
114       for (ConfigurationOptionSetting setting : configSetting.getOptionSettings()) {
115         if (harmfulOptionSettingP(null, setting)) {
116           continue;
117         }
118         buf.add("  <optionSetting>");
119         buf.add(String.format("    <%s>%s</%1$s>", "namespace", setting.getNamespace()));
120         buf.add(String.format("    <%s>%s</%1$s>", "optionName", setting.getOptionName()));
121         buf.add(String.format("    <%s>%s</%1$s>", "value", setting.getValue()));
122         buf.add("  </optionSetting>");
123       }
124     }
125 
126     buf.add("</optionSettings>");
127 
128     if (null != outputFile) {
129       getLog().info("Dumping results to file: " + outputFile.getName());
130 
131       String bufChars = StringUtils.join(buf.iterator(), ENDL);
132       FileWriter writer = null;
133 
134       try {
135         writer = new FileWriter(outputFile);
136 
137         IOUtils.copy(new StringReader(bufChars), writer);
138       } catch (IOException e) {
139         throw new RuntimeException("Failure when writing to file: " + outputFile.getName(), e);
140       } finally {
141         IOUtils.closeQuietly(writer);
142       }
143     } else {
144       getLog().info("Dumping results to stdout");
145 
146       for (String e : buf) {
147         getLog().info(e);
148       }
149     }
150   }
151 }