EMMA Coverage Report (generated Fri Apr 27 15:03:37 EDT 2012)
[all classes][org.jclouds.ec2.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [AddElasticIpsToNodemetadata.java]

nameclass, %method, %block, %line, %
AddElasticIpsToNodemetadata.java100% (1/1)100% (2/2)91%  (43/47)83%  (10/12)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AddElasticIpsToNodemetadata100% (1/1)100% (2/2)91%  (43/47)83%  (10/12)
apply (NodeMetadata): NodeMetadata 100% (1/1)89%  (34/38)78%  (7/9)
AddElasticIpsToNodemetadata (LoadingCache): void 100% (1/1)100% (9/9)100% (3/3)

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.ec2.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.util.concurrent.ExecutionException;
24 
25import javax.inject.Inject;
26import javax.inject.Named;
27import javax.inject.Singleton;
28 
29import org.jclouds.aws.util.AWSUtils;
30import org.jclouds.compute.domain.NodeMetadata;
31import org.jclouds.compute.domain.NodeMetadataBuilder;
32import org.jclouds.ec2.compute.domain.RegionAndName;
33 
34import com.google.common.base.Function;
35import com.google.common.base.Throwables;
36import com.google.common.cache.CacheLoader;
37import com.google.common.cache.LoadingCache;
38import com.google.common.collect.ImmutableSet;
39 
40/**
41 * This class searches for elastic ip addresses that are associated with the node, and adds them to
42 * the publicIpAddress collection if present.
43 * 
44 * @author Adrian Cole
45 */
46@Singleton
47public class AddElasticIpsToNodemetadata implements Function<NodeMetadata, NodeMetadata> {
48 
49   private final LoadingCache<RegionAndName, String> cache;
50 
51   @Inject
52   protected AddElasticIpsToNodemetadata(@Named("ELASTICIP") LoadingCache<RegionAndName, String> cache) {
53      this.cache = checkNotNull(cache, "cache");
54   }
55 
56   // Note: Instances only have one Internet routable IP address. When an Elastic IP is associated to an
57   // instance, the instance's existing Public IP address mapping is removed and is no longer valid for this instance
58   // http://aws.amazon.com/articles/1346
59 
60   // TODO can there be multiple elastic ips on one instance?
61   @Override
62   public NodeMetadata apply(NodeMetadata arg0) {
63      String[] parts = AWSUtils.parseHandle(arg0.getId());
64      String region = parts[0];
65      String instanceId = parts[1];
66      try {
67         String publicIp = cache.get(new RegionAndName(region, instanceId));
68         // Replace existing public addresses with elastic IP (see note above)
69         return NodeMetadataBuilder.fromNodeMetadata(arg0)
70                 .publicAddresses(ImmutableSet.<String> builder().add(publicIp).build()).build();
71      } catch (CacheLoader.InvalidCacheLoadException e) {
72         // no ip was found
73         return arg0;
74      } catch (ExecutionException e) {
75         throw Throwables.propagate(e);
76      }
77   }
78 
79}

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