| 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.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Predicates.notNull; |
| 23 | import static com.google.common.collect.Iterables.concat; |
| 24 | import static com.google.common.collect.Iterables.filter; |
| 25 | import static com.google.common.collect.Iterables.transform; |
| 26 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
| 27 | |
| 28 | import java.util.Set; |
| 29 | import java.util.concurrent.ExecutorService; |
| 30 | import java.util.concurrent.Future; |
| 31 | |
| 32 | import javax.inject.Inject; |
| 33 | import javax.inject.Named; |
| 34 | import javax.inject.Singleton; |
| 35 | |
| 36 | import org.jclouds.Constants; |
| 37 | import org.jclouds.aws.ec2.AWSEC2AsyncClient; |
| 38 | import org.jclouds.aws.ec2.domain.AWSRunningInstance; |
| 39 | import org.jclouds.aws.ec2.domain.SpotInstanceRequest; |
| 40 | import org.jclouds.aws.ec2.functions.SpotInstanceRequestToAWSRunningInstance; |
| 41 | import org.jclouds.compute.domain.NodeMetadata; |
| 42 | import org.jclouds.ec2.compute.strategy.EC2ListNodesStrategy; |
| 43 | import org.jclouds.ec2.domain.RunningInstance; |
| 44 | import org.jclouds.location.Region; |
| 45 | |
| 46 | import com.google.common.base.Function; |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | @Singleton |
| 53 | public class AWSEC2ListNodesStrategy extends EC2ListNodesStrategy { |
| 54 | |
| 55 | protected final AWSEC2AsyncClient client; |
| 56 | protected final SpotInstanceRequestToAWSRunningInstance spotConverter; |
| 57 | |
| 58 | @Inject |
| 59 | protected AWSEC2ListNodesStrategy(AWSEC2AsyncClient client, @Region Set<String> regions, |
| 60 | Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata, |
| 61 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor, |
| 62 | SpotInstanceRequestToAWSRunningInstance spotConverter) { |
| 63 | super(client, regions, runningInstanceToNodeMetadata, executor); |
| 64 | this.client = checkNotNull(client, "client"); |
| 65 | this.spotConverter = checkNotNull(spotConverter, "spotConverter"); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected Iterable<? extends RunningInstance> pollRunningInstances() { |
| 70 | Iterable<? extends AWSRunningInstance> spots = filter(transform(concat(transformParallel(regions, |
| 71 | new Function<String, Future<Set<SpotInstanceRequest>>>() { |
| 72 | |
| 73 | @SuppressWarnings("unchecked") |
| 74 | @Override |
| 75 | public Future<Set<SpotInstanceRequest>> apply(String from) { |
| 76 | return (Future<Set<SpotInstanceRequest>>) client.getSpotInstanceServices() |
| 77 | .describeSpotInstanceRequestsInRegion(from); |
| 78 | } |
| 79 | |
| 80 | }, executor, null, logger, "reservations")), spotConverter), notNull()); |
| 81 | |
| 82 | return concat(super.pollRunningInstances(), spots); |
| 83 | } |
| 84 | } |