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 [AWSEC2CreateNodesInGroupThenAddToSet.java]

nameclass, %method, %block, %line, %
AWSEC2CreateNodesInGroupThenAddToSet.java0%   (0/2)0%   (0/7)0%   (0/219)0%   (0/22)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AWSEC2CreateNodesInGroupThenAddToSet0%   (0/1)0%   (0/5)0%   (0/210)0%   (0/21)
AWSEC2CreateNodesInGroupThenAddToSet (AWSEC2Client, AWSEC2AsyncClient, boolea... 0%   (0/1)0%   (0/35)0%   (0/7)
addTagsToInstancesInRegion (Map, Iterable, String, String): Iterable 0%   (0/1)0%   (0/37)0%   (0/4)
createNodesInRegionAndZone (String, String, String, int, Template, RunInstanc... 0%   (0/1)0%   (0/100)0%   (0/8)
getSpotPriceOrNull (TemplateOptions): Float 0%   (0/1)0%   (0/11)0%   (0/1)
metadataForId (String, String, Map): Map 0%   (0/1)0%   (0/27)0%   (0/1)
     
class AWSEC2CreateNodesInGroupThenAddToSet$10%   (0/1)0%   (0/2)0%   (0/9)0%   (0/2)
AWSEC2CreateNodesInGroupThenAddToSet$1 (AWSEC2CreateNodesInGroupThenAddToSet)... 0%   (0/1)0%   (0/6)0%   (0/1)
apply (RunningInstance): String 0%   (0/1)0%   (0/3)0%   (0/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.Preconditions.checkNotNull;
22import static com.google.common.collect.Iterables.transform;
23import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_GENERATE_INSTANCE_NAMES;
24 
25import java.util.Map;
26 
27import javax.annotation.Resource;
28import javax.inject.Inject;
29import javax.inject.Named;
30import javax.inject.Provider;
31import javax.inject.Singleton;
32 
33import org.jclouds.aws.ec2.AWSEC2AsyncClient;
34import org.jclouds.aws.ec2.AWSEC2Client;
35import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions;
36import org.jclouds.aws.ec2.compute.predicates.AWSEC2InstancePresent;
37import org.jclouds.aws.ec2.domain.LaunchSpecification;
38import org.jclouds.aws.ec2.functions.SpotInstanceRequestToAWSRunningInstance;
39import org.jclouds.aws.ec2.options.AWSRunInstancesOptions;
40import org.jclouds.aws.ec2.options.RequestSpotInstancesOptions;
41import org.jclouds.compute.domain.NodeMetadata;
42import org.jclouds.compute.domain.Template;
43import org.jclouds.compute.domain.TemplateBuilder;
44import org.jclouds.compute.options.TemplateOptions;
45import org.jclouds.compute.reference.ComputeServiceConstants;
46import org.jclouds.compute.util.ComputeUtils;
47import org.jclouds.domain.Credentials;
48import org.jclouds.ec2.compute.strategy.EC2CreateNodesInGroupThenAddToSet;
49import org.jclouds.ec2.domain.RunningInstance;
50import org.jclouds.ec2.options.RunInstancesOptions;
51import org.jclouds.logging.Logger;
52 
53import com.google.common.annotations.VisibleForTesting;
54import com.google.common.base.Function;
55import com.google.common.cache.Cache;
56import com.google.common.collect.ImmutableMap;
57import com.google.common.collect.ImmutableSet;
58 
59/**
60 * 
61 * @author Adrian Cole
62 */
63@Singleton
64public class AWSEC2CreateNodesInGroupThenAddToSet extends EC2CreateNodesInGroupThenAddToSet {
65 
66   @Resource
67   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
68   protected Logger logger = Logger.NULL;
69 
70   @VisibleForTesting
71   final AWSEC2Client client;
72   final SpotInstanceRequestToAWSRunningInstance spotConverter;
73   final AWSEC2AsyncClient aclient;
74   final boolean generateInstanceNames;
75 
76   @Inject
77   protected AWSEC2CreateNodesInGroupThenAddToSet(
78            AWSEC2Client client,
79            AWSEC2AsyncClient aclient,
80            @Named(PROPERTY_EC2_GENERATE_INSTANCE_NAMES) boolean generateInstanceNames,
81            Provider<TemplateBuilder> templateBuilderProvider,
82            CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions createKeyPairAndSecurityGroupsAsNeededAndReturncustomize,
83            AWSEC2InstancePresent instancePresent,
84            Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata,
85            Cache<RunningInstance, Credentials> instanceToCredentials, Map<String, Credentials> credentialStore,
86            ComputeUtils utils, SpotInstanceRequestToAWSRunningInstance spotConverter) {
87      super(client, templateBuilderProvider, createKeyPairAndSecurityGroupsAsNeededAndReturncustomize, instancePresent,
88               runningInstanceToNodeMetadata, instanceToCredentials, credentialStore, utils);
89      this.client = checkNotNull(client, "client");
90      this.aclient = checkNotNull(aclient, "aclient");
91      this.spotConverter = checkNotNull(spotConverter, "spotConverter");
92      this.generateInstanceNames = generateInstanceNames;
93   }
94 
95   @Override
96   protected Iterable<? extends RunningInstance> createNodesInRegionAndZone(String region, String zone, String group,
97            int count, Template template, RunInstancesOptions instanceOptions) {
98      Float spotPrice = getSpotPriceOrNull(template.getOptions());
99      if (spotPrice != null) {
100         LaunchSpecification spec = AWSRunInstancesOptions.class.cast(instanceOptions).getLaunchSpecificationBuilder()
101                  .imageId(template.getImage().getProviderId()).availabilityZone(zone).build();
102         RequestSpotInstancesOptions options = AWSEC2TemplateOptions.class.cast(template.getOptions()).getSpotOptions();
103         if (logger.isDebugEnabled())
104            logger.debug(">> requesting %d spot instances region(%s) price(%f) spec(%s) options(%s)", count, region,
105                     spotPrice, spec, options);
106 
107         return addTagsToInstancesInRegion(template.getOptions().getUserMetadata(), transform(client
108                  .getSpotInstanceServices().requestSpotInstancesInRegion(region, spotPrice, count, spec, options),
109                  spotConverter), region, group);
110      } else {
111         return addTagsToInstancesInRegion(template.getOptions().getUserMetadata(), super.createNodesInRegionAndZone(
112                  region, zone, group, count, template, instanceOptions), region, group);
113      }
114 
115   }
116 
117   public Iterable<? extends RunningInstance> addTagsToInstancesInRegion(Map<String, String> metadata,
118            Iterable<? extends RunningInstance> iterable, String region, String group) {
119      if (metadata.size() > 0 || generateInstanceNames) {
120         for (String id : transform(iterable, new Function<RunningInstance, String>() {
121 
122            @Override
123            public String apply(RunningInstance arg0) {
124               return arg0.getId();
125            }
126 
127         }))
128            aclient.getTagServices()
129                     .createTagsInRegion(region, ImmutableSet.of(id), metadataForId(id, group, metadata));
130      }
131      return iterable;
132   }
133 
134   private Map<String, String> metadataForId(String id, String group, Map<String, String> metadata) {
135      return generateInstanceNames && !metadata.containsKey("Name") ? ImmutableMap.<String, String> builder().putAll(
136               metadata).put("Name", id.replaceAll(".*-", group + "-")).build() : metadata;
137   }
138 
139   private Float getSpotPriceOrNull(TemplateOptions options) {
140      return options instanceof AWSEC2TemplateOptions ? AWSEC2TemplateOptions.class.cast(options).getSpotPrice() : null;
141   }
142 
143}

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