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