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.CreateConfigurationTemplateRequest;
20  import com.amazonaws.services.elasticbeanstalk.model.CreateConfigurationTemplateResult;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.maven.plugin.AbstractMojoExecutionException;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.Parameter;
26  
27  import java.text.DateFormat;
28  import java.text.SimpleDateFormat;
29  import java.util.Date;
30  import java.util.Set;
31  import java.util.TreeSet;
32  
33  import br.com.ingenieux.mojo.beanstalk.AbstractNeedsEnvironmentMojo;
34  
35  /**
36   * <p>Tags an Environment</p>
37   *
38   * <p>Defaults to environmentRef-yyyyMMdd-nn, where 'nn' is incremented according to
39   * availability.</p>
40   *
41   * @since 1.1.0
42   */
43  @Mojo(name = "tag-environment", requiresDirectInvocation = true)
44  public class TagEnvironmentMojo extends AbstractNeedsEnvironmentMojo {
45  
46    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
47  
48    /**
49     * Template Name to use
50     */
51    @Parameter(property = "beanstalk.templateName")
52    String templateName;
53  
54    @Override
55    protected Object executeInternal() throws AbstractMojoExecutionException {
56      Set<String> configTemplates = new TreeSet<String>(super.getConfigurationTemplates(applicationName));
57      String today = DATE_FORMAT.format(new Date());
58  
59      if (StringUtils.isBlank(templateName)) {
60        int i = 1;
61  
62        do {
63          templateName = String.format("%s-%s-%02d", curEnv.getEnvironmentName(), today, i++);
64        } while (configTemplates.contains(templateName));
65      }
66  
67      CreateConfigurationTemplateResult result =
68          getService()
69              .createConfigurationTemplate(
70                  new CreateConfigurationTemplateRequest()
71                      .withEnvironmentId(curEnv.getEnvironmentId())
72                      .withTemplateName(templateName)
73                      .withApplicationName(curEnv.getApplicationName()));
74  
75      getLog().info("Created config template " + templateName + " for environment " + curEnv.getEnvironmentId());
76  
77      return result;
78    }
79  }