| 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.suppliers; |
| 20 | |
| 21 | import static com.google.common.collect.Iterables.filter; |
| 22 | import static com.google.common.collect.Iterables.transform; |
| 23 | import static com.google.common.collect.Maps.uniqueIndex; |
| 24 | import static org.jclouds.ec2.options.DescribeImagesOptions.Builder.ownedBy; |
| 25 | import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS; |
| 26 | |
| 27 | import java.util.Set; |
| 28 | import java.util.Map.Entry; |
| 29 | |
| 30 | import javax.annotation.Resource; |
| 31 | import javax.inject.Inject; |
| 32 | import javax.inject.Named; |
| 33 | import javax.inject.Singleton; |
| 34 | |
| 35 | import org.jclouds.compute.domain.Image; |
| 36 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 37 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 38 | import org.jclouds.ec2.compute.functions.EC2ImageParser; |
| 39 | import org.jclouds.ec2.compute.strategy.DescribeImagesParallel; |
| 40 | import org.jclouds.ec2.options.DescribeImagesOptions; |
| 41 | import org.jclouds.location.Region; |
| 42 | import org.jclouds.logging.Logger; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.base.Predicates; |
| 46 | import com.google.common.base.Supplier; |
| 47 | import com.google.common.cache.Cache; |
| 48 | import com.google.common.cache.CacheBuilder; |
| 49 | import com.google.common.cache.CacheLoader; |
| 50 | import com.google.common.collect.ImmutableMap; |
| 51 | import com.google.common.collect.ImmutableSet; |
| 52 | import com.google.common.collect.ImmutableMap.Builder; |
| 53 | |
| 54 | /** |
| 55 | * |
| 56 | * @author Adrian Cole |
| 57 | */ |
| 58 | @Singleton |
| 59 | public class RegionAndNameToImageSupplier implements Supplier<Cache<RegionAndName, ? extends Image>> { |
| 60 | @Resource |
| 61 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 62 | protected Logger logger = Logger.NULL; |
| 63 | |
| 64 | private final Set<String> regions; |
| 65 | private final DescribeImagesParallel describer; |
| 66 | private final String[] amiOwners; |
| 67 | private final EC2ImageParser parser; |
| 68 | private final CacheLoader<RegionAndName, Image> regionAndIdToImage; |
| 69 | |
| 70 | @Inject |
| 71 | protected RegionAndNameToImageSupplier(@Region Set<String> regions, DescribeImagesParallel describer, |
| 72 | @Named(PROPERTY_EC2_AMI_OWNERS) String[] amiOwners, EC2ImageParser parser, CacheLoader<RegionAndName, Image> regionAndIdToImage) { |
| 73 | this.regions = regions; |
| 74 | this.describer = describer; |
| 75 | this.amiOwners = amiOwners; |
| 76 | this.parser = parser; |
| 77 | this.regionAndIdToImage = regionAndIdToImage; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Cache<RegionAndName, ? extends Image> get() { |
| 82 | Cache<RegionAndName, Image> cache = CacheBuilder.newBuilder().build(regionAndIdToImage); |
| 83 | |
| 84 | if (amiOwners.length == 0) { |
| 85 | logger.debug(">> no owners specified, skipping image parsing"); |
| 86 | } else { |
| 87 | logger.debug(">> providing images"); |
| 88 | |
| 89 | Iterable<Entry<String, DescribeImagesOptions>> queries = getDescribeQueriesForOwnersInRegions(regions, |
| 90 | amiOwners); |
| 91 | |
| 92 | Iterable<? extends Image> parsedImages = ImmutableSet.copyOf(filter(transform(describer.apply(queries), parser), Predicates |
| 93 | .notNull())); |
| 94 | |
| 95 | cache.asMap().putAll(uniqueIndex(parsedImages, new Function<Image, RegionAndName>() { |
| 96 | |
| 97 | @Override |
| 98 | public RegionAndName apply(Image from) { |
| 99 | return new RegionAndName(from.getLocation().getId(), from.getProviderId()); |
| 100 | } |
| 101 | |
| 102 | })); |
| 103 | logger.debug("<< images(%d)", cache.asMap().size()); |
| 104 | } |
| 105 | return cache; |
| 106 | } |
| 107 | |
| 108 | public Iterable<Entry<String, DescribeImagesOptions>> getDescribeQueriesForOwnersInRegions(Set<String> regions, |
| 109 | String[] amiOwners) { |
| 110 | DescribeImagesOptions options = getOptionsForOwners(amiOwners); |
| 111 | Builder<String, DescribeImagesOptions> builder = ImmutableMap.<String, DescribeImagesOptions> builder(); |
| 112 | for (String region : regions) |
| 113 | builder.put(region, options); |
| 114 | return builder.build().entrySet(); |
| 115 | } |
| 116 | |
| 117 | public DescribeImagesOptions getOptionsForOwners(String... amiOwners) { |
| 118 | DescribeImagesOptions options; |
| 119 | if (amiOwners.length == 1 && amiOwners[0].equals("*")) |
| 120 | options = new DescribeImagesOptions(); |
| 121 | else |
| 122 | options = ownedBy(amiOwners); |
| 123 | return options; |
| 124 | } |
| 125 | } |