| 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.ec2.compute.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.NoSuchElementException; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Provider; |
| 30 | |
| 31 | import org.jclouds.collect.Memoized; |
| 32 | import org.jclouds.compute.domain.Hardware; |
| 33 | import org.jclouds.compute.domain.Image; |
| 34 | import org.jclouds.compute.domain.TemplateBuilder; |
| 35 | import org.jclouds.compute.domain.internal.TemplateBuilderImpl; |
| 36 | import org.jclouds.compute.options.TemplateOptions; |
| 37 | import org.jclouds.domain.Location; |
| 38 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 39 | |
| 40 | import com.google.common.base.Supplier; |
| 41 | import com.google.common.collect.ComputationException; |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | public class EC2TemplateBuilderImpl extends TemplateBuilderImpl { |
| 48 | |
| 49 | private final Map<RegionAndName, Image> imageMap; |
| 50 | |
| 51 | @Inject |
| 52 | protected EC2TemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations, |
| 53 | @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes, |
| 54 | Supplier<Location> defaultLocation, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider, |
| 55 | @Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, Map<RegionAndName, Image> imageMap) { |
| 56 | super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider); |
| 57 | this.imageMap = imageMap; |
| 58 | } |
| 59 | |
| 60 | final Provider<Image> lazyImageProvider = new Provider<Image>() { |
| 61 | |
| 62 | @Override |
| 63 | public Image get() { |
| 64 | if (imageId != null) { |
| 65 | String[] regionName = imageId.split("/"); |
| 66 | checkArgument(regionName.length == 2, |
| 67 | "amazon image ids must include the region ( ex. us-east-1/ami-7ea24a17 ) you specified: " + imageId); |
| 68 | RegionAndName key = new RegionAndName(regionName[0], regionName[1]); |
| 69 | try { |
| 70 | return imageMap.get(key); |
| 71 | } catch (NullPointerException nex) { |
| 72 | throw new NoSuchElementException(String.format("imageId(%s/%s) not found", key.getRegion(), key.getName())); |
| 73 | } catch (ComputationException nex) { |
| 74 | throw new NoSuchElementException(String.format("imageId(%s/%s) not found", key.getRegion(), key.getName())); |
| 75 | } |
| 76 | } |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * @throws NoSuchElementException |
| 84 | * if the image is not found |
| 85 | */ |
| 86 | @Override |
| 87 | protected Image resolveImage(Hardware size, Iterable<? extends Image> supportedImages) { |
| 88 | try { |
| 89 | return super.resolveImage(size, supportedImages); |
| 90 | } catch (NoSuchElementException e) { |
| 91 | Image returnVal = lazyImageProvider.get(); |
| 92 | if (returnVal != null) |
| 93 | return returnVal; |
| 94 | throw e; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @SuppressWarnings("unchecked") |
| 99 | @Override |
| 100 | protected Set<? extends Image> getImages() { |
| 101 | Set<Image> images = (Set<Image>) this.images.get(); |
| 102 | if (images.size() == 0) { |
| 103 | Image toReturn = lazyImageProvider.get(); |
| 104 | if (toReturn != null) { |
| 105 | images.add(toReturn); |
| 106 | } |
| 107 | } |
| 108 | return images; |
| 109 | } |
| 110 | |
| 111 | } |