| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.ec2.compute.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.getOnlyElement; |
| 23 | |
| 24 | import java.util.NoSuchElementException; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 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.strategy.GetNodeMetadataStrategy; |
| 32 | import org.jclouds.ec2.EC2Client; |
| 33 | import org.jclouds.ec2.domain.RunningInstance; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | import com.google.common.collect.Iterables; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class EC2GetNodeMetadataStrategy implements GetNodeMetadataStrategy { |
| 44 | |
| 45 | private final EC2Client client; |
| 46 | private final Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata; |
| 47 | |
| 48 | @Inject |
| 49 | protected EC2GetNodeMetadataStrategy(EC2Client client, |
| 50 | Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata) { |
| 51 | this.client = checkNotNull(client, "client"); |
| 52 | this.runningInstanceToNodeMetadata = checkNotNull(runningInstanceToNodeMetadata, "runningInstanceToNodeMetadata"); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public NodeMetadata getNode(String id) { |
| 57 | checkNotNull(id, "id"); |
| 58 | String[] parts = AWSUtils.parseHandle(id); |
| 59 | String region = parts[0]; |
| 60 | String instanceId = parts[1]; |
| 61 | try { |
| 62 | RunningInstance runningInstance = getRunningInstanceInRegion(region, instanceId); |
| 63 | return runningInstanceToNodeMetadata.apply(runningInstance); |
| 64 | } catch (NoSuchElementException e) { |
| 65 | return null; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public RunningInstance getRunningInstanceInRegion(String region, String id) { |
| 70 | return getOnlyElement(Iterables.concat(client.getInstanceServices().describeInstancesInRegion(region, id))); |
| 71 | } |
| 72 | |
| 73 | } |