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.DescribeApplicationsRequest;
21  import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationsResult;
22  
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.List;
29  
30  import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
31  
32  import static java.lang.String.format;
33  
34  /**
35   * Describes Available Configuration Templates
36   *
37   * @author Aldrin Leal
38   * @since 1.0-SNAPSHOT
39   */
40  @Mojo(name = "list-configuration-templates", requiresDirectInvocation = true)
41  public class ListConfigurationTemplatesMojo extends AbstractBeanstalkMojo {
42  
43    @Parameter(property = "beanstalk.applicationName", defaultValue = "${project.artifactId}", required = true)
44    protected String applicationName;
45  
46    @Override
47    protected Object executeInternal() throws MojoExecutionException, MojoFailureException {
48      DescribeApplicationsRequest req = new DescribeApplicationsRequest().withApplicationNames(applicationName);
49  
50      DescribeApplicationsResult apps = getService().describeApplications(req);
51  
52      List<ApplicationDescription> applications = apps.getApplications();
53  
54      if (applications.isEmpty()) {
55        String errorMessage = "Application ('" + applicationName + "') not found!";
56  
57        getLog().warn(errorMessage);
58  
59        throw new MojoFailureException(errorMessage);
60      }
61  
62      ApplicationDescription desc = applications.get(0);
63  
64      List<String> configTemplates = desc.getConfigurationTemplates();
65  
66      getLog().info(format("There are %d config templates", configTemplates.size()));
67  
68      return configTemplates;
69    }
70  }