| 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.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.collect.FindResourceInSet; |
| 31 | import org.jclouds.collect.Memoized; |
| 32 | import org.jclouds.compute.domain.Hardware; |
| 33 | import org.jclouds.compute.domain.Image; |
| 34 | import org.jclouds.compute.domain.NodeMetadata; |
| 35 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 36 | import org.jclouds.compute.domain.NodeState; |
| 37 | import org.jclouds.domain.Credentials; |
| 38 | import org.jclouds.domain.Location; |
| 39 | import org.jclouds.softlayer.SoftLayerClient; |
| 40 | import org.jclouds.softlayer.domain.Datacenter; |
| 41 | import org.jclouds.softlayer.domain.ProductItem; |
| 42 | import org.jclouds.softlayer.domain.ProductOrder; |
| 43 | import org.jclouds.softlayer.domain.VirtualGuest; |
| 44 | import org.jclouds.softlayer.predicates.ProductItemPredicates; |
| 45 | |
| 46 | import com.google.common.base.Function; |
| 47 | import com.google.common.base.Supplier; |
| 48 | import com.google.common.collect.ImmutableMap; |
| 49 | import com.google.common.collect.ImmutableSet; |
| 50 | import com.google.common.collect.Iterables; |
| 51 | |
| 52 | /** |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | @Singleton |
| 56 | public class VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMetadata> { |
| 57 | |
| 58 | public static final Map<VirtualGuest.State, NodeState> serverStateToNodeState = ImmutableMap |
| 59 | .<VirtualGuest.State, NodeState> builder().put(VirtualGuest.State.HALTED, NodeState.PENDING).put( |
| 60 | VirtualGuest.State.PAUSED, NodeState.SUSPENDED).put(VirtualGuest.State.RUNNING, NodeState.RUNNING) |
| 61 | .put(VirtualGuest.State.UNRECOGNIZED, NodeState.UNRECOGNIZED).build(); |
| 62 | |
| 63 | private final Map<String, Credentials> credentialStore; |
| 64 | private final FindLocationForVirtualGuest findLocationForVirtualGuest; |
| 65 | private final GetHardwareForVirtualGuest getHardwareForVirtualGuest; |
| 66 | private final GetImageForVirtualGuest getImageForVirtualGuest; |
| 67 | |
| 68 | @Inject |
| 69 | VirtualGuestToNodeMetadata(Map<String, Credentials> credentialStore, |
| 70 | FindLocationForVirtualGuest findLocationForVirtualGuest, |
| 71 | GetHardwareForVirtualGuest getHardwareForVirtualGuest, GetImageForVirtualGuest getImageForVirtualGuest) { |
| 72 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 73 | this.findLocationForVirtualGuest = checkNotNull(findLocationForVirtualGuest, "findLocationForVirtualGuest"); |
| 74 | this.getHardwareForVirtualGuest = checkNotNull(getHardwareForVirtualGuest, "getHardwareForVirtualGuest"); |
| 75 | this.getImageForVirtualGuest = checkNotNull(getImageForVirtualGuest, "getImageForVirtualGuest"); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public NodeMetadata apply(VirtualGuest from) { |
| 80 | // convert the result object to a jclouds NodeMetadata |
| 81 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 82 | builder.ids(from.getId() + ""); |
| 83 | builder.name(from.getHostname()); |
| 84 | builder.hostname(from.getHostname()); |
| 85 | builder.location(findLocationForVirtualGuest.apply(from)); |
| 86 | builder.group(parseGroupFromName(from.getHostname())); |
| 87 | |
| 88 | Image image = getImageForVirtualGuest.getImage(from); |
| 89 | if (image != null) { |
| 90 | builder.imageId(image.getId()); |
| 91 | builder.operatingSystem(image.getOperatingSystem()); |
| 92 | } |
| 93 | |
| 94 | Hardware hardware = getHardwareForVirtualGuest.getHardware(from); |
| 95 | if (hardware != null) |
| 96 | builder.hardware(hardware); |
| 97 | |
| 98 | builder.state(serverStateToNodeState.get(from.getPowerState().getKeyName())); |
| 99 | |
| 100 | // These are null for 'bad' guest orders in the HALTED state. |
| 101 | if (from.getPrimaryIpAddress() != null) |
| 102 | builder.publicAddresses(ImmutableSet.<String> of(from.getPrimaryIpAddress())); |
| 103 | if (from.getPrimaryBackendIpAddress() != null) |
| 104 | builder.privateAddresses(ImmutableSet.<String> of(from.getPrimaryBackendIpAddress())); |
| 105 | |
| 106 | builder.credentials(credentialStore.get("node#" + from.getId())); |
| 107 | return builder.build(); |
| 108 | } |
| 109 | |
| 110 | @Singleton |
| 111 | public static class FindLocationForVirtualGuest extends FindResourceInSet<VirtualGuest, Location> { |
| 112 | |
| 113 | @Inject |
| 114 | public FindLocationForVirtualGuest(@Memoized Supplier<Set<? extends Location>> location) { |
| 115 | super(location); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean matches(VirtualGuest from, Location input) { |
| 120 | Datacenter dc = from.getDatacenter(); |
| 121 | if (dc == null) |
| 122 | return false; |
| 123 | return input.getId().equals(Integer.toString(dc.getId())); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Singleton |
| 128 | public static class GetHardwareForVirtualGuest { |
| 129 | |
| 130 | private final SoftLayerClient client; |
| 131 | private final Function<Iterable<ProductItem>, Hardware> productItemsToHardware; |
| 132 | |
| 133 | @Inject |
| 134 | public GetHardwareForVirtualGuest(SoftLayerClient client, |
| 135 | Function<Iterable<ProductItem>, Hardware> productItemsToHardware) { |
| 136 | this.client = checkNotNull(client, "client"); |
| 137 | this.productItemsToHardware = checkNotNull(productItemsToHardware, "productItemsToHardware"); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | public Hardware getHardware(VirtualGuest guest) { |
| 142 | // 'bad' orders have no start cpu's and cause the order lookup to fail. |
| 143 | if (guest.getStartCpus() < 1) |
| 144 | return null; |
| 145 | ProductOrder order = client.getVirtualGuestClient().getOrderTemplate(guest.getId()); |
| 146 | if (order == null) |
| 147 | return null; |
| 148 | Iterable<ProductItem> items = Iterables.transform(order.getPrices(), ProductItems.item()); |
| 149 | return productItemsToHardware.apply(items); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | @Singleton |
| 154 | public static class GetImageForVirtualGuest { |
| 155 | |
| 156 | private SoftLayerClient client; |
| 157 | |
| 158 | @Inject |
| 159 | public GetImageForVirtualGuest(SoftLayerClient client) { |
| 160 | this.client = client; |
| 161 | } |
| 162 | |
| 163 | public Image getImage(VirtualGuest guest) { |
| 164 | // 'bad' orders have no start cpu's and cause the order lookup to fail. |
| 165 | if (guest.getStartCpus() < 1) |
| 166 | return null; |
| 167 | ProductOrder order = client.getVirtualGuestClient().getOrderTemplate(guest.getId()); |
| 168 | if (order == null) |
| 169 | return null; |
| 170 | Iterable<ProductItem> items = Iterables.transform(order.getPrices(), ProductItems.item()); |
| 171 | ProductItem os = Iterables.find(items, ProductItemPredicates.categoryCode("os")); |
| 172 | return new ProductItemToImage().apply(os); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | } |