| 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.binders; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 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.util.Properties; |
| 27 | |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.cim.ResourceAllocationSettingData.ResourceType; |
| 32 | import org.jclouds.http.HttpRequest; |
| 33 | import org.jclouds.rest.binders.BindToStringPayload; |
| 34 | |
| 35 | import com.google.common.base.Throwables; |
| 36 | import com.google.inject.Inject; |
| 37 | import com.jamesmurty.utils.XMLBuilder; |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @author Adrian Cole |
| 42 | * |
| 43 | */ |
| 44 | @Singleton |
| 45 | public class BindMemoryToXmlPayload extends BindToStringPayload { |
| 46 | protected final String ns; |
| 47 | protected final String schema; |
| 48 | |
| 49 | @Inject |
| 50 | public BindMemoryToXmlPayload(BindToStringPayload stringBinder, @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, |
| 51 | @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema) { |
| 52 | this.ns = ns; |
| 53 | this.schema = schema; |
| 54 | } |
| 55 | |
| 56 | private static final String RESOURCE_ALLOCATION_NS = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"; |
| 57 | |
| 58 | @Override |
| 59 | public <R extends HttpRequest> R bindToRequest(R request, Object payload) { |
| 60 | checkArgument(checkNotNull(payload, "memoryInMB") instanceof Integer, "this binder is only valid for Integers!"); |
| 61 | Integer memoryInMB = Integer.class.cast(payload); |
| 62 | XMLBuilder cpuItem; |
| 63 | try { |
| 64 | cpuItem = XMLBuilder.create("Item").a("xmlns", ns).a("xmlns:rasd", RESOURCE_ALLOCATION_NS); |
| 65 | cpuItem.e("rasd:AllocationUnits").t("byte * 2^20"); |
| 66 | cpuItem.e("rasd:Description").t("Memory Size"); |
| 67 | cpuItem.e("rasd:ElementName").t(memoryInMB.toString() + " MB of memory"); |
| 68 | cpuItem.e("rasd:InstanceID").t("5"); |
| 69 | cpuItem.e("rasd:Reservation").t("0"); |
| 70 | cpuItem.e("rasd:ResourceType").t(ResourceType.MEMORY.value()); |
| 71 | cpuItem.e("rasd:VirtualQuantity").t(memoryInMB.toString()); |
| 72 | cpuItem.e("rasd:Weight").t("0"); |
| 73 | Properties outputProperties = new Properties(); |
| 74 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 75 | request = super.bindToRequest(request, cpuItem.asString(outputProperties)); |
| 76 | } catch (Exception e) { |
| 77 | Throwables.propagate(e); |
| 78 | } |
| 79 | return request; |
| 80 | } |
| 81 | |
| 82 | } |