| 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.softlayer.binders; |
| 20 | |
| 21 | import com.google.common.base.Function; |
| 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.ImmutableMap; |
| 24 | import com.google.common.collect.Iterables; |
| 25 | import com.google.common.collect.Sets; |
| 26 | import org.jclouds.http.HttpRequest; |
| 27 | import org.jclouds.json.Json; |
| 28 | import org.jclouds.rest.Binder; |
| 29 | import org.jclouds.softlayer.domain.ProductItemPrice; |
| 30 | import org.jclouds.softlayer.domain.ProductOrder; |
| 31 | import org.jclouds.softlayer.domain.VirtualGuest; |
| 32 | |
| 33 | import javax.inject.Inject; |
| 34 | import java.util.Set; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | |
| 38 | /** |
| 39 | * Converts a ProductOrder into a json string valid for placing an order via the softlayer api The |
| 40 | * String is set into the payload of the HttpRequest |
| 41 | * |
| 42 | * @author Jason King |
| 43 | */ |
| 44 | public class ProductOrderToJson implements Binder { |
| 45 | |
| 46 | private Json json; |
| 47 | |
| 48 | @Inject |
| 49 | public ProductOrderToJson(Json json) { |
| 50 | this.json = json; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 55 | checkNotNull(input, "order"); |
| 56 | ProductOrder order = ProductOrder.class.cast(input); |
| 57 | request.setPayload(buildJson(order)); |
| 58 | return request; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Builds a Json string suitable for sending to the softlayer api |
| 63 | * |
| 64 | * @param order |
| 65 | * @return |
| 66 | */ |
| 67 | String buildJson(ProductOrder order) { |
| 68 | |
| 69 | Iterable<Price> prices = Iterables.transform(order.getPrices(), new Function<ProductItemPrice, Price>() { |
| 70 | @Override |
| 71 | public Price apply(ProductItemPrice productItemPrice) { |
| 72 | return new Price(productItemPrice.getId()); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | Iterable<HostnameAndDomain> hosts = Iterables.transform(order.getVirtualGuests(), |
| 77 | new Function<VirtualGuest, HostnameAndDomain>() { |
| 78 | @Override |
| 79 | public HostnameAndDomain apply(VirtualGuest virtualGuest) { |
| 80 | return new HostnameAndDomain(virtualGuest.getHostname(), virtualGuest.getDomain()); |
| 81 | } |
| 82 | }); |
| 83 | |
| 84 | OrderData data = new OrderData(order.getPackageId(), order.getLocation(), Sets.newLinkedHashSet(prices), Sets |
| 85 | .newLinkedHashSet(hosts), order.getQuantity(), order.getUseHourlyPricing()); |
| 86 | |
| 87 | return json.toJson(ImmutableMap.of("parameters", ImmutableList.<OrderData> of(data))); |
| 88 | } |
| 89 | |
| 90 | @SuppressWarnings("unused") |
| 91 | private static class OrderData { |
| 92 | private String complexType = "SoftLayer_Container_Product_Order_Virtual_Guest"; |
| 93 | private long packageId = -1; |
| 94 | private String location; |
| 95 | private Set<Price> prices; |
| 96 | private Set<HostnameAndDomain> virtualGuests; |
| 97 | private long quantity; |
| 98 | private boolean useHourlyPricing; |
| 99 | |
| 100 | public OrderData(long packageId, String location, Set<Price> prices, Set<HostnameAndDomain> virtualGuests, |
| 101 | long quantity, boolean useHourlyPricing) { |
| 102 | this.packageId = packageId; |
| 103 | this.location = location; |
| 104 | this.prices = prices; |
| 105 | this.virtualGuests = virtualGuests; |
| 106 | this.quantity = quantity; |
| 107 | this.useHourlyPricing = useHourlyPricing; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | @SuppressWarnings("unused") |
| 113 | private static class HostnameAndDomain { |
| 114 | private String hostname; |
| 115 | private String domain; |
| 116 | |
| 117 | public HostnameAndDomain(String hostname, String domain) { |
| 118 | this.hostname = hostname; |
| 119 | this.domain = domain; |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | @SuppressWarnings("unused") |
| 125 | private static class Price { |
| 126 | private long id; |
| 127 | |
| 128 | public Price(long id) { |
| 129 | this.id = id; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } |