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.env;
18  
19  import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsRequest;
20  import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsResult;
21  import com.fasterxml.jackson.databind.ObjectMapper;
22  import com.fasterxml.jackson.databind.ObjectWriter;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  
29  import java.io.File;
30  
31  import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
32  
33  /**
34   * Describe running environments
35   *
36   * See the docs for the <a href= "http://docs.amazonwebservices.com/elasticbeanstalk/latest/api/API_DescribeEnvironments.html"
37   * >DescribeEnvironments API</a> call.
38   *
39   * @author Aldrin Leal
40   * @since 0.1.0
41   */
42  @Mojo(name = "describe-environments", requiresDirectInvocation = true)
43  public class DescribeEnvironmentsMojo extends AbstractBeanstalkMojo {
44  
45    /**
46     * Beanstalk Application Name
47     */
48    @Parameter(property = "beanstalk.applicationName", defaultValue = "${project.artifactId}", required = true)
49    protected String applicationName;
50  
51    /**
52     * Include Deleted?
53     */
54    @Parameter(property = "beanstalk.includeDeleted")
55    boolean includeDeleted;
56  
57    /**
58     * Output file (Optional)
59     */
60    @Parameter(property = "beanstalk.outputFile")
61    File outputFile;
62  
63    @Override
64    protected Object executeInternal() throws MojoExecutionException, MojoFailureException {
65      DescribeEnvironmentsRequest req = new DescribeEnvironmentsRequest();
66  
67      req.setApplicationName(applicationName);
68      req.setIncludeDeleted(includeDeleted);
69  
70      // TODO add environmentNames / environmentIds / includeDeletedBackTo
71  
72      DescribeEnvironmentsResult result = getService().describeEnvironments(req);
73  
74      if (null != outputFile) {
75        getLog().info("Writing results into " + outputFile.getName());
76  
77        try {
78          ObjectMapper objectMapper = new ObjectMapper();
79  
80          ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();
81  
82          writer.writeValue(outputFile, result.getEnvironments());
83        } catch (Exception e) {
84          throw new RuntimeException(e);
85        }
86  
87        return null;
88      }
89  
90      return result;
91    }
92  }