EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.ec2.compute.strategy]

COVERAGE SUMMARY FOR SOURCE FILE [CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.java]

nameclass, %method, %block, %line, %
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.java100% (1/1)88%  (7/8)93%  (228/244)94%  (48/51)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions100% (1/1)88%  (7/8)93%  (228/244)94%  (48/51)
getOptionsProvider (): Provider 0%   (0/1)0%   (0/3)0%   (0/1)
execute (String, String, Template): RunInstancesOptions 100% (1/1)81%  (57/70)86%  (12/14)
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions (Map, Map, Function... 100% (1/1)100% (18/18)100% (7/7)
addSecurityGroups (String, String, Template, RunInstancesOptions): void 100% (1/1)100% (12/12)100% (3/3)
createNewKeyPairUnlessUserSpecifiedOtherwise (String, String, TemplateOptions... 100% (1/1)100% (33/33)100% (9/9)
createOrImportKeyPair (String, String, TemplateOptions): String 100% (1/1)100% (5/5)100% (1/1)
createUniqueKeyPairAndPutIntoMap (String, String): String 100% (1/1)100% (28/28)100% (5/5)
getSecurityGroupsForTagAndOptions (String, String, TemplateOptions): Set 100% (1/1)100% (75/75)100% (11/11)

1/**
2 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 */
19package org.jclouds.ec2.compute.strategy;
20 
21import static com.google.common.base.Preconditions.checkState;
22 
23import java.util.Map;
24import java.util.Set;
25 
26import javax.annotation.Nullable;
27import javax.inject.Inject;
28import javax.inject.Named;
29import javax.inject.Provider;
30import javax.inject.Singleton;
31 
32import org.jclouds.compute.domain.Template;
33import org.jclouds.compute.options.TemplateOptions;
34import org.jclouds.ec2.compute.domain.RegionAndName;
35import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules;
36import org.jclouds.ec2.compute.options.EC2TemplateOptions;
37import org.jclouds.ec2.domain.BlockDeviceMapping;
38import org.jclouds.ec2.domain.KeyPair;
39import org.jclouds.ec2.options.RunInstancesOptions;
40 
41import com.google.common.annotations.VisibleForTesting;
42import com.google.common.base.Function;
43import com.google.common.collect.ImmutableSet;
44import com.google.common.collect.ImmutableSet.Builder;
45 
46/**
47 * 
48 * @author Adrian Cole
49 */
50@Singleton
51public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
52 
53   @VisibleForTesting
54   public final Map<RegionAndName, KeyPair> credentialsMap;
55   @VisibleForTesting
56   public final Map<RegionAndName, String> securityGroupMap;
57   @VisibleForTesting
58   public final Function<RegionAndName, KeyPair> createUniqueKeyPair;
59   @VisibleForTesting
60   public final Function<RegionNameAndIngressRules, String> createSecurityGroupIfNeeded;
61   protected final Provider<RunInstancesOptions> optionsProvider;
62 
63   @Inject
64   public CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions(Map<RegionAndName, KeyPair> credentialsMap,
65         @Named("SECURITY") Map<RegionAndName, String> securityGroupMap, Function<RegionAndName, KeyPair> createUniqueKeyPair,
66         Function<RegionNameAndIngressRules, String> createSecurityGroupIfNeeded,
67         Provider<RunInstancesOptions> optionsProvider) {
68      this.credentialsMap = credentialsMap;
69      this.securityGroupMap = securityGroupMap;
70      this.createUniqueKeyPair = createUniqueKeyPair;
71      this.createSecurityGroupIfNeeded = createSecurityGroupIfNeeded;
72      this.optionsProvider = optionsProvider;
73   }
74 
75   public RunInstancesOptions execute(String region, String group, Template template) {
76 
77      RunInstancesOptions instanceOptions = getOptionsProvider().get().asType(template.getHardware().getId());
78 
79      String keyPairName = createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, template.getOptions());
80 
81      addSecurityGroups(region, group, template, instanceOptions);
82      if (template.getOptions() instanceof EC2TemplateOptions) {
83 
84         if (keyPairName != null)
85            instanceOptions.withKeyName(keyPairName);
86 
87         byte[] userData = EC2TemplateOptions.class.cast(template.getOptions()).getUserData();
88 
89         if (userData != null)
90            instanceOptions.withUserData(userData);
91 
92         Set<BlockDeviceMapping> blockDeviceMappings = EC2TemplateOptions.class.cast(template.getOptions())
93               .getBlockDeviceMappings();
94         if (blockDeviceMappings.size() > 0) {
95            checkState("ebs".equals(template.getImage().getUserMetadata().get("rootDeviceType")),
96                  "BlockDeviceMapping only available on ebs boot");
97            instanceOptions.withBlockDeviceMappings(blockDeviceMappings);
98         }
99      }
100      return instanceOptions;
101   }
102 
103   protected void addSecurityGroups(String region, String group, Template template, RunInstancesOptions instanceOptions) {
104      Set<String> groups = getSecurityGroupsForTagAndOptions(region, group, template.getOptions());
105      instanceOptions.withSecurityGroups(groups);
106   }
107 
108   @VisibleForTesting
109   public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) {
110      String keyPairName = null;
111      boolean shouldAutomaticallyCreateKeyPair = true;
112      if (options instanceof EC2TemplateOptions) {
113         keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair();
114         if (keyPairName == null)
115            shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options)
116                  .shouldAutomaticallyCreateKeyPair();
117      }
118      if (keyPairName == null && shouldAutomaticallyCreateKeyPair) {
119         keyPairName = createOrImportKeyPair(region, group, options);
120      }
121      return keyPairName;
122   }
123 
124   // base EC2 driver currently does not support key import
125   protected String createOrImportKeyPair(String region, String group, TemplateOptions options) {
126      return createUniqueKeyPairAndPutIntoMap(region, group);
127   }
128 
129   protected String createUniqueKeyPairAndPutIntoMap(String region, String group) {
130      String keyPairName;
131      RegionAndName regionAndName = new RegionAndName(region, group);
132      KeyPair keyPair = createUniqueKeyPair.apply(regionAndName);
133      keyPairName = keyPair.getKeyName();
134      // get or create incidental resources
135      // TODO race condition. we were using MapMaker, but it doesn't seem to
136      // refresh properly
137      // when
138      // another thread
139      // deletes a key
140      credentialsMap.put(new RegionAndName(regionAndName.getRegion(), keyPairName), keyPair);
141      return keyPairName;
142   }
143 
144   @VisibleForTesting
145   public Set<String> getSecurityGroupsForTagAndOptions(String region, @Nullable String group, TemplateOptions options) {
146      Builder<String> groups = ImmutableSet.<String> builder();
147 
148      if (group != null) {
149         String markerGroup = String.format("jclouds#%s#%s", group, region);
150         groups.add(markerGroup);
151 
152         RegionNameAndIngressRules regionNameAndIngessRulesForMarkerGroup;
153 
154         if (options instanceof EC2TemplateOptions && EC2TemplateOptions.class.cast(options).getGroupIds().size() > 0) {
155            regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region, markerGroup, new int[] {},
156                  false);
157            groups.addAll(EC2TemplateOptions.class.cast(options).getGroupIds());
158 
159         } else {
160            regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region, markerGroup,
161                  options.getInboundPorts(), true);
162         }
163 
164         if (!securityGroupMap.containsKey(regionNameAndIngessRulesForMarkerGroup)) {
165            securityGroupMap.put(regionNameAndIngessRulesForMarkerGroup,
166                  createSecurityGroupIfNeeded.apply(regionNameAndIngessRulesForMarkerGroup));
167         }
168      }
169      return groups.build();
170   }
171 
172   // allows us to mock this method
173   @VisibleForTesting
174   public javax.inject.Provider<RunInstancesOptions> getOptionsProvider() {
175      return optionsProvider;
176   }
177}

[all classes][org.jclouds.ec2.compute.strategy]
EMMA 2.0.5312 (C) Vladimir Roubtsov