| 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.trmk.vcloud_0_8.compute.suppliers; |
| 20 | |
| 21 | import static com.google.common.collect.Iterables.concat; |
| 22 | import static com.google.common.collect.Iterables.transform; |
| 23 | import static com.google.common.collect.Sets.newLinkedHashSet; |
| 24 | |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Named; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.collect.Memoized; |
| 33 | import org.jclouds.compute.domain.Image; |
| 34 | import org.jclouds.compute.domain.ImageBuilder; |
| 35 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 36 | import org.jclouds.domain.Location; |
| 37 | import org.jclouds.logging.Logger; |
| 38 | import org.jclouds.trmk.vcloud_0_8.compute.functions.ImagesInVCloudExpressOrg; |
| 39 | import org.jclouds.trmk.vcloud_0_8.domain.Org; |
| 40 | |
| 41 | import com.google.common.base.Function; |
| 42 | import com.google.common.base.Supplier; |
| 43 | |
| 44 | /** |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | @Singleton |
| 48 | public class VAppTemplatesInOrgs implements Supplier<Set<? extends Image>> { |
| 49 | |
| 50 | @Resource |
| 51 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 52 | public Logger logger = Logger.NULL; |
| 53 | |
| 54 | private final Supplier<Set<? extends Location>> locations; |
| 55 | private final Function<Iterable<? extends Location>, Iterable<? extends Org>> organizatonsForLocations; |
| 56 | private final ImagesInVCloudExpressOrg imagesInOrg; |
| 57 | |
| 58 | @Inject |
| 59 | VAppTemplatesInOrgs(@Memoized Supplier<Set<? extends Location>> locations, |
| 60 | Function<Iterable<? extends Location>, Iterable<? extends Org>> organizatonsForLocations, |
| 61 | ImagesInVCloudExpressOrg imagesInOrg) { |
| 62 | this.locations = locations; |
| 63 | this.organizatonsForLocations = organizatonsForLocations; |
| 64 | this.imagesInOrg = imagesInOrg; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Terremark does not provide vApp templates in the vDC resourceEntity list. Rather, you must |
| 69 | * query the catalog. |
| 70 | */ |
| 71 | @Override |
| 72 | public Set<? extends Image> get() { |
| 73 | logger.debug(">> providing vAppTemplates"); |
| 74 | return newLinkedHashSet(transform( |
| 75 | concat(transform(organizatonsForLocations.apply(locations.get()), imagesInOrg)), |
| 76 | new Function<Image, Image>() { |
| 77 | |
| 78 | @Override |
| 79 | public Image apply(Image from) { |
| 80 | ImageBuilder builder = ImageBuilder.fromImage(from); |
| 81 | // the password in the image is the sudo password |
| 82 | // TODO refactor authenticate image logic so that it can populate the |
| 83 | // adminPassword |
| 84 | // value |
| 85 | // independently |
| 86 | if (from.getDefaultCredentials() != null) |
| 87 | builder.adminPassword(from.getDefaultCredentials().credential); |
| 88 | return builder.build(); |
| 89 | } |
| 90 | |
| 91 | })); |
| 92 | } |
| 93 | } |