| 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.vcloud.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import javax.annotation.Resource; |
| 24 | import javax.inject.Inject; |
| 25 | |
| 26 | import org.jclouds.compute.domain.Hardware; |
| 27 | import org.jclouds.compute.domain.HardwareBuilder; |
| 28 | import org.jclouds.compute.predicates.ImagePredicates; |
| 29 | import org.jclouds.logging.Logger; |
| 30 | import org.jclouds.ovf.Envelope; |
| 31 | import org.jclouds.ovf.VirtualHardwareSection; |
| 32 | import org.jclouds.vcloud.VCloudClient; |
| 33 | import org.jclouds.vcloud.domain.ReferenceType; |
| 34 | import org.jclouds.vcloud.domain.VAppTemplate; |
| 35 | |
| 36 | import com.google.common.base.Function; |
| 37 | import com.google.common.collect.Iterables; |
| 38 | |
| 39 | /** |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | public class HardwareForVAppTemplate implements Function<VAppTemplate, Hardware> { |
| 43 | |
| 44 | @Resource |
| 45 | protected Logger logger = Logger.NULL; |
| 46 | |
| 47 | private final VCloudClient client; |
| 48 | private final FindLocationForResource findLocationForResource; |
| 49 | private final VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder; |
| 50 | |
| 51 | private ReferenceType parent; |
| 52 | |
| 53 | @Inject |
| 54 | protected HardwareForVAppTemplate(VCloudClient client, FindLocationForResource findLocationForResource, |
| 55 | VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder) { |
| 56 | this.client = checkNotNull(client, "client"); |
| 57 | this.findLocationForResource = checkNotNull(findLocationForResource, "findLocationForResource"); |
| 58 | this.rasdToHardwareBuilder = checkNotNull(rasdToHardwareBuilder, "rasdToHardwareBuilder"); |
| 59 | } |
| 60 | |
| 61 | public HardwareForVAppTemplate withParent(ReferenceType parent) { |
| 62 | this.parent = parent; |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public Hardware apply(VAppTemplate from) { |
| 68 | checkNotNull(from, "VAppTemplate"); |
| 69 | |
| 70 | if (!from.isOvfDescriptorUploaded()) { |
| 71 | logger.warn("cannot parse hardware as ovf descriptor for %s is not uploaded", from); |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | Envelope ovf = client.getVAppTemplateClient().getOvfEnvelopeForVAppTemplate(from.getHref()); |
| 76 | if (ovf == null) { |
| 77 | logger.warn("cannot parse hardware as no ovf envelope found for %s", from); |
| 78 | return null; |
| 79 | } |
| 80 | if (ovf.getVirtualSystem().getVirtualHardwareSections().size() == 0) { |
| 81 | logger.warn("cannot parse hardware for %s as no hardware sections exist in ovf %s", ovf); |
| 82 | return null; |
| 83 | } |
| 84 | if (ovf.getVirtualSystem().getVirtualHardwareSections().size() > 1) { |
| 85 | logger.warn("multiple hardware choices found. using first", ovf); |
| 86 | } |
| 87 | VirtualHardwareSection hardware = Iterables.get(ovf.getVirtualSystem().getVirtualHardwareSections(), 0); |
| 88 | HardwareBuilder builder = rasdToHardwareBuilder.apply(hardware.getItems()); |
| 89 | builder.location(findLocationForResource.apply(checkNotNull(parent, "parent"))); |
| 90 | builder.ids(from.getHref().toASCIIString()).name(from.getName()).supportsImage( |
| 91 | ImagePredicates.idEquals(from.getHref().toASCIIString())); |
| 92 | return builder.build(); |
| 93 | } |
| 94 | |
| 95 | protected String getName(String name) { |
| 96 | return name; |
| 97 | } |
| 98 | } |