| 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.aws.ec2.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Predicates.*; |
| 22 | import static com.google.common.collect.Maps.*; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.aws.ec2.domain.AWSRunningInstance; |
| 31 | import org.jclouds.collect.Memoized; |
| 32 | import org.jclouds.compute.domain.Hardware; |
| 33 | import org.jclouds.compute.domain.Image; |
| 34 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 35 | import org.jclouds.compute.domain.NodeState; |
| 36 | import org.jclouds.domain.Credentials; |
| 37 | import org.jclouds.domain.Location; |
| 38 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 39 | import org.jclouds.ec2.compute.functions.RunningInstanceToNodeMetadata; |
| 40 | import org.jclouds.ec2.domain.InstanceState; |
| 41 | import org.jclouds.ec2.domain.RunningInstance; |
| 42 | |
| 43 | import com.google.common.base.Supplier; |
| 44 | import com.google.common.cache.Cache; |
| 45 | |
| 46 | /** |
| 47 | * @author Adrian Cole |
| 48 | */ |
| 49 | @Singleton |
| 50 | public class AWSRunningInstanceToNodeMetadata extends RunningInstanceToNodeMetadata { |
| 51 | |
| 52 | @Inject |
| 53 | protected AWSRunningInstanceToNodeMetadata(Map<InstanceState, NodeState> instanceToNodeState, |
| 54 | Map<String, Credentials> credentialStore, Supplier<Cache<RegionAndName, ? extends Image>> imageMap, |
| 55 | @Memoized Supplier<Set<? extends Location>> locations, @Memoized Supplier<Set<? extends Hardware>> hardware) { |
| 56 | super(instanceToNodeState, credentialStore, imageMap, locations, hardware); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) { |
| 61 | Credentials creds = credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId()); |
| 62 | String spotRequestId = AWSRunningInstance.class.cast(instance).getSpotInstanceRequestId(); |
| 63 | if (creds == null && spotRequestId != null) { |
| 64 | creds = credentialStore.get("node#" + instance.getRegion() + "/" + spotRequestId); |
| 65 | if (creds != null) |
| 66 | credentialStore.put("node#" + instance.getRegion() + "/" + instance.getId(), creds); |
| 67 | } |
| 68 | if (creds != null) |
| 69 | builder.credentials(creds); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected NodeMetadataBuilder buildInstance(RunningInstance instance, NodeMetadataBuilder builder) { |
| 74 | Map<String, String> tags = AWSRunningInstance.class.cast(instance).getTags(); |
| 75 | return super.buildInstance(instance, builder).name(tags.get("Name")).tags( |
| 76 | filterValues(tags, equalTo("")).keySet()).userMetadata(filterValues(tags, not(equalTo("")))); |
| 77 | } |
| 78 | } |