| 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.suppliers; |
| 20 | |
| 21 | import static com.google.common.collect.Iterables.filter; |
| 22 | import static com.google.common.collect.Iterables.transform; |
| 23 | import static org.jclouds.aws.ec2.options.AWSDescribeImagesOptions.Builder.filters; |
| 24 | |
| 25 | import java.util.Map.Entry; |
| 26 | import java.util.concurrent.Callable; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Named; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.compute.domain.Image; |
| 34 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 35 | import org.jclouds.ec2.compute.functions.EC2ImageParser; |
| 36 | import org.jclouds.ec2.compute.strategy.DescribeImagesParallel; |
| 37 | import org.jclouds.ec2.options.DescribeImagesOptions; |
| 38 | import org.jclouds.logging.Logger; |
| 39 | |
| 40 | import com.google.common.base.Predicates; |
| 41 | import com.google.common.collect.ImmutableMap; |
| 42 | import com.google.common.collect.Iterables; |
| 43 | import com.google.common.collect.Multimap; |
| 44 | import com.google.common.collect.ImmutableMap.Builder; |
| 45 | import com.google.inject.assistedinject.Assisted; |
| 46 | |
| 47 | /** |
| 48 | * |
| 49 | * @author Adrian Cole |
| 50 | */ |
| 51 | @Singleton |
| 52 | public class CallForImages implements Callable<Iterable<Image>> { |
| 53 | public interface Factory { |
| 54 | CallForImages parseImagesFromRegionsUsingFilter(Iterable<String> regions, Multimap<String, String> filter); |
| 55 | } |
| 56 | |
| 57 | @Resource |
| 58 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 59 | protected Logger logger = Logger.NULL; |
| 60 | |
| 61 | private final Iterable<String> regions; |
| 62 | private final DescribeImagesParallel describer; |
| 63 | private final EC2ImageParser parser; |
| 64 | private final Multimap<String, String> filter; |
| 65 | |
| 66 | @Inject |
| 67 | protected CallForImages(DescribeImagesParallel describer, EC2ImageParser parser, @Assisted Iterable<String> regions, |
| 68 | @Assisted Multimap<String, String> filter) { |
| 69 | this.regions = regions; |
| 70 | this.describer = describer; |
| 71 | this.filter = filter; |
| 72 | this.parser = parser; |
| 73 | } |
| 74 | |
| 75 | public Iterable<Image> call() { |
| 76 | |
| 77 | logger.debug(">> providing images"); |
| 78 | |
| 79 | Builder<String, DescribeImagesOptions> builder = ImmutableMap.<String, DescribeImagesOptions> builder(); |
| 80 | for (String region : regions) |
| 81 | builder.put(region, filters(filter)); |
| 82 | |
| 83 | Iterable<Entry<String, DescribeImagesOptions>> queries = builder.build().entrySet(); |
| 84 | |
| 85 | Iterable<Image> returnVal = filter(transform(describer.apply(queries), parser), Predicates.notNull()); |
| 86 | if (logger.isDebugEnabled()) |
| 87 | logger.debug("<< images(%s)", Iterables.size(returnVal)); |
| 88 | return returnVal; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public String toString() { |
| 93 | return String.format("desribingImages(filter=%s,regions=%s)", filter, regions); |
| 94 | } |
| 95 | |
| 96 | } |