| 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.vcloud.terremark.binders; |
| 20 | |
| 21 | import static org.jclouds.Constants.PROPERTY_API_VERSION; |
| 22 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_DEFAULT_FENCEMODE; |
| 23 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; |
| 24 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA; |
| 25 | |
| 26 | import java.net.URI; |
| 27 | import java.util.Map; |
| 28 | import java.util.SortedMap; |
| 29 | import java.util.Map.Entry; |
| 30 | |
| 31 | import javax.annotation.Nullable; |
| 32 | import javax.inject.Inject; |
| 33 | import javax.inject.Named; |
| 34 | import javax.inject.Singleton; |
| 35 | |
| 36 | import org.jclouds.cim.ResourceAllocationSettingData.ResourceType; |
| 37 | import org.jclouds.rest.binders.BindToStringPayload; |
| 38 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 39 | import org.jclouds.vcloud.binders.BindInstantiateVCloudExpressVAppTemplateParamsToXmlPayload; |
| 40 | import org.jclouds.vcloud.endpoints.Network; |
| 41 | import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions; |
| 42 | import org.jclouds.vcloud.terremark.options.TerremarkInstantiateVAppTemplateOptions; |
| 43 | |
| 44 | import com.jamesmurty.utils.XMLBuilder; |
| 45 | |
| 46 | /** |
| 47 | * |
| 48 | * @author Adrian Cole |
| 49 | * |
| 50 | */ |
| 51 | @Singleton |
| 52 | public class TerremarkBindInstantiateVAppTemplateParamsToXmlPayload extends |
| 53 | BindInstantiateVCloudExpressVAppTemplateParamsToXmlPayload { |
| 54 | |
| 55 | @Inject |
| 56 | public TerremarkBindInstantiateVAppTemplateParamsToXmlPayload(BindToStringPayload stringBinder, |
| 57 | @Named(PROPERTY_API_VERSION) String apiVersion, @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, |
| 58 | @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema, @Nullable @Network URI network, |
| 59 | @Named(PROPERTY_VCLOUD_DEFAULT_FENCEMODE) String fenceMode) { |
| 60 | super(stringBinder, apiVersion, ns, schema, network, fenceMode); |
| 61 | } |
| 62 | |
| 63 | ThreadLocal<Map<String, String>> propLocal = new ThreadLocal<Map<String, String>>(); |
| 64 | |
| 65 | @Override |
| 66 | protected InstantiateVAppTemplateOptions findOptionsInArgsOrNull(GeneratedHttpRequest<?> gRequest) { |
| 67 | InstantiateVAppTemplateOptions options = super.findOptionsInArgsOrNull(gRequest); |
| 68 | if (options != null && options instanceof TerremarkInstantiateVAppTemplateOptions) |
| 69 | propLocal.set(TerremarkInstantiateVAppTemplateOptions.class.cast(options).getProperties()); |
| 70 | return options; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | protected void addVirtualQuantityIfPresent(XMLBuilder instantiationParamsBuilder, |
| 75 | SortedMap<ResourceType, String> virtualHardwareQuantity) { |
| 76 | XMLBuilder productSectionBuilder = instantiationParamsBuilder.e("ProductSection").a("xmlns:q1", ns).a( |
| 77 | "xmlns:ovf", "http://schemas.dmtf.org/ovf/envelope/1"); |
| 78 | if (propLocal.get() != null) { |
| 79 | for (Entry<String, String> entry : propLocal.get().entrySet()) { |
| 80 | productSectionBuilder.e("Property").a("xmlns", "http://schemas.dmtf.org/ovf/envelope/1").a("ovf:key", |
| 81 | entry.getKey()).a("ovf:value", entry.getValue()); |
| 82 | } |
| 83 | propLocal.set(null); |
| 84 | } |
| 85 | super.addVirtualQuantityIfPresent(instantiationParamsBuilder, virtualHardwareQuantity); |
| 86 | } |
| 87 | |
| 88 | } |