| 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 | */ |
| 19 | package org.jclouds.ec2.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.concurrent.ExecutionException; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Named; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.aws.util.AWSUtils; |
| 30 | import org.jclouds.compute.domain.NodeMetadata; |
| 31 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 32 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 33 | |
| 34 | import com.google.common.base.Function; |
| 35 | import com.google.common.base.Throwables; |
| 36 | import com.google.common.cache.CacheLoader; |
| 37 | import com.google.common.cache.LoadingCache; |
| 38 | import 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 |
| 47 | public 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 | } |