| 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.elasticstack.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.List; |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.collect.FindResourceInSet; |
| 33 | import org.jclouds.collect.Memoized; |
| 34 | import org.jclouds.compute.domain.HardwareBuilder; |
| 35 | import org.jclouds.compute.domain.Image; |
| 36 | import org.jclouds.compute.domain.NodeMetadata; |
| 37 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 38 | import org.jclouds.compute.domain.NodeState; |
| 39 | import org.jclouds.compute.domain.Processor; |
| 40 | import org.jclouds.compute.domain.Volume; |
| 41 | import org.jclouds.compute.domain.VolumeBuilder; |
| 42 | import org.jclouds.domain.Credentials; |
| 43 | import org.jclouds.domain.Location; |
| 44 | import org.jclouds.elasticstack.domain.Device; |
| 45 | import org.jclouds.elasticstack.domain.DriveInfo; |
| 46 | import org.jclouds.elasticstack.domain.Server; |
| 47 | import org.jclouds.elasticstack.domain.ServerInfo; |
| 48 | import org.jclouds.elasticstack.domain.ServerStatus; |
| 49 | import org.jclouds.logging.Logger; |
| 50 | |
| 51 | import com.google.common.base.Function; |
| 52 | import com.google.common.base.Supplier; |
| 53 | import com.google.common.cache.Cache; |
| 54 | import com.google.common.collect.ImmutableList; |
| 55 | import com.google.common.collect.ImmutableMap; |
| 56 | import com.google.common.collect.ImmutableSet; |
| 57 | import com.google.common.collect.Iterables; |
| 58 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| 59 | |
| 60 | /** |
| 61 | * @author Adrian Cole |
| 62 | */ |
| 63 | @Singleton |
| 64 | public class ServerInfoToNodeMetadata implements Function<ServerInfo, NodeMetadata> { |
| 65 | public static final Map<ServerStatus, NodeState> serverStatusToNodeState = ImmutableMap |
| 66 | .<ServerStatus, NodeState> builder().put(ServerStatus.ACTIVE, NodeState.RUNNING)// |
| 67 | .put(ServerStatus.STOPPED, NodeState.SUSPENDED)// |
| 68 | .put(ServerStatus.PAUSED, NodeState.SUSPENDED)// |
| 69 | .put(ServerStatus.DUMPED, NodeState.PENDING)// |
| 70 | .put(ServerStatus.DEAD, NodeState.TERMINATED)// |
| 71 | .put(ServerStatus.UNRECOGNIZED, NodeState.UNRECOGNIZED)// |
| 72 | .build(); |
| 73 | |
| 74 | private final Function<Server, String> getImageIdFromServer; |
| 75 | private final Function<String, Image> findImageForId; |
| 76 | private final Map<String, Credentials> credentialStore; |
| 77 | private final Supplier<Location> locationSupplier; |
| 78 | private final Function<Device, Volume> deviceToVolume; |
| 79 | |
| 80 | @Inject |
| 81 | ServerInfoToNodeMetadata(Map<String, Credentials> credentialStore, Function<Server, String> getImageIdFromServer, |
| 82 | Function<String, Image> findImageForId, Function<Device, Volume> deviceToVolume, |
| 83 | Supplier<Location> locationSupplier) { |
| 84 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 85 | this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); |
| 86 | this.deviceToVolume = checkNotNull(deviceToVolume, "deviceToVolume"); |
| 87 | this.findImageForId = checkNotNull(findImageForId, "findImageForId"); |
| 88 | this.getImageIdFromServer = checkNotNull(getImageIdFromServer, "getImageIdFromServer"); |
| 89 | } |
| 90 | |
| 91 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 92 | @Override |
| 93 | public NodeMetadata apply(ServerInfo from) { |
| 94 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 95 | builder.ids(from.getUuid()); |
| 96 | builder.name(from.getName()); |
| 97 | builder.location(locationSupplier.get()); |
| 98 | builder.group(parseGroupFromName(from.getName())); |
| 99 | |
| 100 | String imageId = getImageIdFromServer.apply(from); |
| 101 | if (imageId != null) { |
| 102 | Image image = findImageForId.apply(imageId); |
| 103 | if (image != null) { |
| 104 | builder.operatingSystem(image.getOperatingSystem()); |
| 105 | } |
| 106 | } |
| 107 | builder.hardware(new HardwareBuilder().ids(from.getUuid()) |
| 108 | .processors(ImmutableList.of(new Processor(1, from.getCpu()))).ram(from.getMem()) |
| 109 | .volumes((List) ImmutableList.of(Iterables.transform(from.getDevices().values(), deviceToVolume))).build()); |
| 110 | builder.state(serverStatusToNodeState.get(from.getStatus())); |
| 111 | builder.publicAddresses(ImmutableSet.<String> of(from.getNics().get(0).getDhcp())); |
| 112 | builder.privateAddresses(ImmutableSet.<String> of()); |
| 113 | builder.credentials(credentialStore.get("node#" + from.getUuid())); |
| 114 | return builder.build(); |
| 115 | } |
| 116 | |
| 117 | @Singleton |
| 118 | public static final class DeviceToVolume implements Function<Device, Volume> { |
| 119 | @Resource |
| 120 | protected Logger logger = Logger.NULL; |
| 121 | |
| 122 | private final Cache<String, DriveInfo> cache; |
| 123 | |
| 124 | @Inject |
| 125 | public DeviceToVolume(Cache<String, DriveInfo> cache) { |
| 126 | this.cache = checkNotNull(cache, "cache"); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Volume apply(Device input) { |
| 131 | VolumeBuilder builder = new VolumeBuilder(); |
| 132 | builder.id(input.getId()); |
| 133 | try { |
| 134 | DriveInfo drive = cache.getUnchecked(input.getDriveUuid()); |
| 135 | builder.size(drive.getSize() / 1024 / 1024f); |
| 136 | } catch (NullPointerException e) { |
| 137 | logger.debug("drive %s not found", input.getDriveUuid()); |
| 138 | } catch (UncheckedExecutionException e) { |
| 139 | logger.warn(e, "error finding drive %s: %s", input.getDriveUuid(), e.getMessage()); |
| 140 | } |
| 141 | return new VolumeBuilder().durable(true).type(Volume.Type.NAS).build(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * When we create the boot drive of the server, by convention we set the name |
| 147 | * to the image it came from. |
| 148 | * |
| 149 | * @author Adrian Cole |
| 150 | * |
| 151 | */ |
| 152 | @Singleton |
| 153 | public static class GetImageIdFromServer implements Function<Server, String> { |
| 154 | @Resource |
| 155 | protected Logger logger = Logger.NULL; |
| 156 | |
| 157 | private final Cache<String, DriveInfo> cache; |
| 158 | |
| 159 | @Inject |
| 160 | public GetImageIdFromServer(Cache<String, DriveInfo> cache) { |
| 161 | this.cache = cache; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public String apply(Server from) { |
| 166 | String imageId = null; |
| 167 | String bootDeviceId = Iterables.get(from.getBootDeviceIds(), 0); |
| 168 | Device bootDevice = from.getDevices().get(bootDeviceId); |
| 169 | if (bootDevice != null) { |
| 170 | try { |
| 171 | DriveInfo drive = cache.getUnchecked(bootDevice.getDriveUuid()); |
| 172 | imageId = drive.getName(); |
| 173 | } catch (NullPointerException e) { |
| 174 | logger.debug("drive %s not found", bootDevice.getDriveUuid()); |
| 175 | } catch (UncheckedExecutionException e) { |
| 176 | logger.warn(e, "error finding drive %s: %s", bootDevice.getDriveUuid(), e.getMessage()); |
| 177 | } |
| 178 | } |
| 179 | return imageId; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | @Singleton |
| 184 | public static class FindImageForId extends FindResourceInSet<String, Image> { |
| 185 | |
| 186 | @Inject |
| 187 | public FindImageForId(@Memoized Supplier<Set<? extends Image>> images) { |
| 188 | super(images); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public boolean matches(String from, Image input) { |
| 193 | return input.getProviderId().equals(from); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | } |