EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.aws.ec2.compute.strategy]

COVERAGE SUMMARY FOR SOURCE FILE [CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.java]

nameclass, %method, %block, %line, %
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.java100% (4/4)85%  (11/13)69%  (199/287)67%  (34/51)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions100% (1/1)71%  (5/7)65%  (162/250)65%  (31/48)
addSecurityGroups (String, String, Template, RunInstancesOptions): void 0%   (0/1)0%   (0/40)0%   (0/9)
execute (String, String, Template): AWSRunInstancesOptions 0%   (0/1)0%   (0/42)0%   (0/7)
createNewKeyPairUnlessUserSpecifiedOtherwise (String, String, TemplateOptions... 100% (1/1)91%  (61/67)91%  (10/11)
<static initializer> 100% (1/1)100% (13/13)100% (3/3)
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions (Function,... 100% (1/1)100% (19/19)100% (6/6)
createNewPlacementGroupUnlessUserSpecifiedOtherwise (String, String, Template... 100% (1/1)100% (51/51)100% (11/11)
userSpecifiedTheirOwnGroups (TemplateOptions): boolean 100% (1/1)100% (18/18)100% (1/1)
     
class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$1100% (1/1)100% (2/2)100% (10/10)100% (2/2)
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$1 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (TemplateOptions): boolean 100% (1/1)100% (7/7)100% (1/1)
     
class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$2100% (1/1)100% (2/2)100% (13/13)100% (2/2)
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$2 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (TemplateOptions): boolean 100% (1/1)100% (10/10)100% (1/1)
     
class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$3100% (1/1)100% (2/2)100% (14/14)100% (2/2)
CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions$3 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (TemplateOptions): boolean 100% (1/1)100% (11/11)100% (1/1)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.aws.ec2.compute.strategy;
20 
21import static com.google.common.base.Predicates.and;
22import static com.google.common.base.Predicates.or;
23 
24import java.util.concurrent.ConcurrentMap;
25 
26import javax.annotation.Resource;
27import javax.inject.Inject;
28import javax.inject.Named;
29import javax.inject.Provider;
30import javax.inject.Singleton;
31 
32import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions;
33import org.jclouds.aws.ec2.domain.RegionNameAndPublicKeyMaterial;
34import org.jclouds.aws.ec2.functions.CreatePlacementGroupIfNeeded;
35import org.jclouds.aws.ec2.options.AWSRunInstancesOptions;
36import org.jclouds.compute.domain.Template;
37import org.jclouds.compute.options.TemplateOptions;
38import org.jclouds.compute.reference.ComputeServiceConstants;
39import org.jclouds.ec2.compute.domain.RegionAndName;
40import org.jclouds.ec2.compute.options.EC2TemplateOptions;
41import org.jclouds.ec2.compute.strategy.CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions;
42import org.jclouds.ec2.domain.KeyPair;
43import org.jclouds.ec2.options.RunInstancesOptions;
44import org.jclouds.logging.Logger;
45 
46import com.google.common.annotations.VisibleForTesting;
47import com.google.common.base.Function;
48import com.google.common.base.Predicate;
49import com.google.common.cache.Cache;
50 
51/**
52 * 
53 * @author Adrian Cole
54 */
55@Singleton
56public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions extends
57         CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
58   @Resource
59   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
60   protected Logger logger = Logger.NULL;
61   @VisibleForTesting
62   final Cache<RegionAndName, String> placementGroupMap;
63   @VisibleForTesting
64   final CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded;
65   @VisibleForTesting
66   final Function<RegionNameAndPublicKeyMaterial, KeyPair> importExistingKeyPair;
67 
68   @Inject
69   public CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions(
70            Function<RegionAndName, KeyPair> makeKeyPair, ConcurrentMap<RegionAndName, KeyPair> credentialsMap,
71            @Named("SECURITY") Cache<RegionAndName, String> securityGroupMap,
72            Provider<RunInstancesOptions> optionsProvider,
73            @Named("PLACEMENT") Cache<RegionAndName, String> placementGroupMap,
74            CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded,
75            Function<RegionNameAndPublicKeyMaterial, KeyPair> importExistingKeyPair) {
76      super(makeKeyPair, credentialsMap, securityGroupMap, optionsProvider);
77      this.placementGroupMap = placementGroupMap;
78      this.createPlacementGroupIfNeeded = createPlacementGroupIfNeeded;
79      this.importExistingKeyPair = importExistingKeyPair;
80   }
81 
82   public AWSRunInstancesOptions execute(String region, String group, Template template) {
83      AWSRunInstancesOptions instanceOptions = AWSRunInstancesOptions.class
84               .cast(super.execute(region, group, template));
85 
86      String placementGroupName = template.getHardware().getId().startsWith("cc") ? createNewPlacementGroupUnlessUserSpecifiedOtherwise(
87               region, group, template.getOptions())
88               : null;
89 
90      if (placementGroupName != null)
91         instanceOptions.inPlacementGroup(placementGroupName);
92 
93      if (AWSEC2TemplateOptions.class.cast(template.getOptions()).isMonitoringEnabled())
94         instanceOptions.enableMonitoring();
95 
96      return instanceOptions;
97   }
98 
99   @VisibleForTesting
100   String createNewPlacementGroupUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) {
101      String placementGroupName = null;
102      boolean shouldAutomaticallyCreatePlacementGroup = true;
103      if (options instanceof EC2TemplateOptions) {
104         placementGroupName = AWSEC2TemplateOptions.class.cast(options).getPlacementGroup();
105         if (placementGroupName == null)
106            shouldAutomaticallyCreatePlacementGroup = AWSEC2TemplateOptions.class.cast(options)
107                     .shouldAutomaticallyCreatePlacementGroup();
108      }
109      if (placementGroupName == null && shouldAutomaticallyCreatePlacementGroup) {
110         // placementGroupName must be unique within an account per
111         // http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using_cluster_computing.html
112         placementGroupName = String.format("jclouds#%s#%s", group, region);
113         RegionAndName regionAndName = new RegionAndName(region, placementGroupName);
114         // make this entry as needed
115         placementGroupMap.getUnchecked(regionAndName);
116      }
117      return placementGroupName;
118   }
119 
120   @Override
121   public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) {
122      RegionAndName key = new RegionAndName(region, group);
123      KeyPair pair;
124      if (and(hasPublicKeyMaterial, or(doesntNeedSshAfterImportingPublicKey, hasLoginCredential)).apply(options)) {
125         pair = importExistingKeyPair.apply(new RegionNameAndPublicKeyMaterial(region, group, options.getPublicKey()));
126         options.dontAuthorizePublicKey();
127         if (hasLoginCredential.apply(options))
128            pair = pair.toBuilder().keyMaterial(options.getOverridingCredentials().credential).build();
129         credentialsMap.put(key, pair);
130      } else {
131         if (hasPublicKeyMaterial.apply(options)) {
132            logger
133                     .warn("to avoid creating temporary keys in aws-ec2, use templateOption overrideLoginCredentialWith(id_rsa)");
134         }
135         return super.createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, options);
136      }
137      return pair.getKeyName();
138   }
139 
140   public static final Predicate<TemplateOptions> hasPublicKeyMaterial = new Predicate<TemplateOptions>() {
141 
142      @Override
143      public boolean apply(TemplateOptions options) {
144         return options.getPublicKey() != null;
145      }
146 
147   };
148 
149   public static final Predicate<TemplateOptions> doesntNeedSshAfterImportingPublicKey = new Predicate<TemplateOptions>() {
150 
151      @Override
152      public boolean apply(TemplateOptions options) {
153         return (options.getRunScript() == null && options.getPrivateKey() == null);
154      }
155 
156   };
157 
158   public static final Predicate<TemplateOptions> hasLoginCredential = new Predicate<TemplateOptions>() {
159 
160      @Override
161      public boolean apply(TemplateOptions options) {
162         return options.getOverridingCredentials() != null && options.getOverridingCredentials().credential != null;
163      }
164 
165   };
166 
167   @Override
168   protected boolean userSpecifiedTheirOwnGroups(TemplateOptions options) {
169      return options instanceof AWSEC2TemplateOptions
170               && AWSEC2TemplateOptions.class.cast(options).getGroupIds().size() > 0
171               || super.userSpecifiedTheirOwnGroups(options);
172   }
173 
174   @Override
175   protected void addSecurityGroups(String region, String group, Template template, RunInstancesOptions instanceOptions) {
176      AWSEC2TemplateOptions awsTemplateOptions = AWSEC2TemplateOptions.class.cast(template.getOptions());
177      AWSRunInstancesOptions awsInstanceOptions = AWSRunInstancesOptions.class.cast(instanceOptions);
178      if (awsTemplateOptions.getGroupIds().size() > 0)
179         awsInstanceOptions.withSecurityGroupIds(awsTemplateOptions.getGroupIds());
180      String subnetId = awsTemplateOptions.getSubnetId();
181      if (subnetId != null) {
182         AWSRunInstancesOptions.class.cast(instanceOptions).withSubnetId(subnetId);
183      } else {
184         super.addSecurityGroups(region, group, template, instanceOptions);
185      }
186   }
187}

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