| 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.aws.ec2.compute.suppliers; |
| 20 | |
| 21 | /** |
| 22 | * |
| 23 | * |
| 24 | * ==================================================================== |
| 25 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 26 | * you may not use this file except in compliance with the License. |
| 27 | * You may obtain a copy of the License at |
| 28 | * |
| 29 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 30 | * |
| 31 | * Unless required by applicable law or agreed to in writing, software |
| 32 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 34 | * See the License for the specific language governing permissions and |
| 35 | * limitations under the License. |
| 36 | * ==================================================================== |
| 37 | */ |
| 38 | |
| 39 | import static com.google.common.collect.Iterables.concat; |
| 40 | import static com.google.common.collect.Maps.newLinkedHashMap; |
| 41 | import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_AMIs; |
| 42 | import static org.jclouds.ec2.options.DescribeImagesOptions.Builder.imageIds; |
| 43 | import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS; |
| 44 | |
| 45 | import java.util.Map; |
| 46 | import java.util.Set; |
| 47 | import java.util.Map.Entry; |
| 48 | |
| 49 | import javax.inject.Inject; |
| 50 | import javax.inject.Named; |
| 51 | import javax.inject.Singleton; |
| 52 | |
| 53 | import org.jclouds.compute.domain.Image; |
| 54 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 55 | import org.jclouds.ec2.compute.functions.EC2ImageParser; |
| 56 | import org.jclouds.ec2.compute.strategy.DescribeImagesParallel; |
| 57 | import org.jclouds.ec2.compute.suppliers.RegionAndNameToImageSupplier; |
| 58 | import org.jclouds.ec2.options.DescribeImagesOptions; |
| 59 | import org.jclouds.location.Region; |
| 60 | |
| 61 | /** |
| 62 | * |
| 63 | * @author Adrian Cole |
| 64 | */ |
| 65 | @Singleton |
| 66 | public class AWSRegionAndNameToImageSupplier extends RegionAndNameToImageSupplier { |
| 67 | |
| 68 | private final String[] ccAmis; |
| 69 | |
| 70 | @Inject |
| 71 | AWSRegionAndNameToImageSupplier(@Region Set<String> regions, DescribeImagesParallel describer, |
| 72 | @Named(PROPERTY_EC2_CC_AMIs) String[] ccAmis, @Named(PROPERTY_EC2_AMI_OWNERS) String[] amiOwners, |
| 73 | EC2ImageParser parser, Map<RegionAndName, Image> images) { |
| 74 | super(regions, describer, amiOwners, parser, images); |
| 75 | this.ccAmis = ccAmis; |
| 76 | } |
| 77 | |
| 78 | public Iterable<Entry<String, DescribeImagesOptions>> getDescribeQueriesForOwnersInRegions(Set<String> regions, |
| 79 | String[] amiOwners) { |
| 80 | return concat(super.getDescribeQueriesForOwnersInRegions(regions, amiOwners), ccAmisToDescribeQueries(ccAmis) |
| 81 | .entrySet()); |
| 82 | } |
| 83 | |
| 84 | static Map<String, DescribeImagesOptions> ccAmisToDescribeQueries(String[] ccAmis) { |
| 85 | Map<String, DescribeImagesOptions> queries = newLinkedHashMap(); |
| 86 | for (String from : ccAmis) { |
| 87 | queries.put(from.split("/")[0], imageIds(from.split("/")[1])); |
| 88 | } |
| 89 | return queries; |
| 90 | } |
| 91 | |
| 92 | } |