1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
37
38
39
40
41
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
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 }