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.cmd.env.create;
18  
19  import com.amazonaws.services.elasticbeanstalk.model.ConfigurationOptionSetting;
20  import com.amazonaws.services.elasticbeanstalk.model.CreateEnvironmentRequest;
21  import com.amazonaws.services.elasticbeanstalk.model.CreateEnvironmentResult;
22  import com.amazonaws.services.elasticbeanstalk.model.EnvironmentTier;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.maven.plugin.AbstractMojoExecutionException;
26  
27  import java.util.Arrays;
28  
29  import br.com.ingenieux.mojo.aws.util.CredentialsUtil;
30  import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
31  import br.com.ingenieux.mojo.beanstalk.cmd.BaseCommand;
32  
33  public class CreateEnvironmentCommand extends BaseCommand<CreateEnvironmentContext, CreateEnvironmentResult> {
34  
35    /**
36     * Constructor
37     *
38     * @param parentMojo parent mojo
39     */
40    public CreateEnvironmentCommand(AbstractBeanstalkMojo parentMojo) throws AbstractMojoExecutionException {
41      super(parentMojo);
42    }
43  
44    @Override
45    protected CreateEnvironmentResult executeInternal(CreateEnvironmentContext context) throws Exception {
46      CreateEnvironmentRequest request = new CreateEnvironmentRequest();
47  
48      request.setApplicationName(context.getApplicationName());
49      request.setCNAMEPrefix(parentMojo.ensureSuffixStripped(context.getCnamePrefix()));
50      request.setDescription(context.getApplicationDescription());
51      request.setEnvironmentName(context.getEnvironmentName());
52      request.setTags(context.getTags());
53  
54      request.setOptionSettings(Arrays.asList(context.getOptionSettings()));
55      request.setOptionsToRemove(Arrays.asList(context.getOptionsToRemove()));
56  
57      if ("Worker".equals(context.getEnvironmentTierName())) {
58        if (contextDoesNotContainsEC2Role(context)) {
59          parentMojo
60              .getLog()
61              .warn("It is meaningless to launch a worker without an IAM Role. If you set in templateName, thats fine, but here's a warning for you");
62        }
63        ;
64        context.setEnvironmentTierType(StringUtils.defaultString(context.getEnvironmentTierType(), "SQS/HTTP"));
65        request.setCNAMEPrefix(null);
66        request.setTier(
67            new EnvironmentTier()
68                .withName(context.getEnvironmentTierName())
69                .withType(context.getEnvironmentTierType())
70                .withVersion(context.getEnvironmentTierVersion()));
71      }
72  
73      if (StringUtils.isNotBlank(context.getTemplateName())) {
74        request.setTemplateName(parentMojo.lookupTemplateName(context.getApplicationName(), context.getTemplateName()));
75      } else if (StringUtils.isNotBlank(context.getSolutionStack())) {
76        request.setSolutionStackName(context.getSolutionStack());
77      }
78  
79      request.setVersionLabel(context.getVersionLabel());
80  
81      if (parentMojo.isVerbose()) {
82        parentMojo.getLog().info("Requesting createEnvironment w/ request: " + CredentialsUtil.redact("" + request));
83      }
84  
85      return service.createEnvironment(request);
86    }
87  
88    protected boolean contextDoesNotContainsEC2Role(CreateEnvironmentContext context) {
89      boolean found = false;
90  
91      for (ConfigurationOptionSetting opt : context.getOptionSettings()) {
92        found = opt.getOptionName().equals("IamInstanceProfile") && opt.getNamespace().equals("aws:autoscaling:launchconfiguration");
93  
94        if (found) {
95          break;
96        }
97      }
98  
99      return !found;
100   }
101 }