| 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.rimuhosting.miro.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.NoSuchElementException; |
| 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.Memoized; |
| 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.compute.domain.OperatingSystem; |
| 38 | import org.jclouds.domain.Credentials; |
| 39 | import org.jclouds.domain.Location; |
| 40 | import org.jclouds.logging.Logger; |
| 41 | import org.jclouds.rimuhosting.miro.domain.Server; |
| 42 | import org.jclouds.rimuhosting.miro.domain.internal.RunningState; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.base.Predicate; |
| 46 | import com.google.common.base.Supplier; |
| 47 | import com.google.common.collect.Iterables; |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * @author Adrian Cole |
| 52 | */ |
| 53 | @Singleton |
| 54 | public class ServerToNodeMetadata implements Function<Server, NodeMetadata> { |
| 55 | |
| 56 | @Resource |
| 57 | protected Logger logger = Logger.NULL; |
| 58 | protected final Map<String, Credentials> credentialStore; |
| 59 | protected final Supplier<Set<? extends Location>> locations; |
| 60 | protected final Function<Server, Iterable<String>> getPublicAddresses; |
| 61 | protected final Map<RunningState, NodeState> runningStateToNodeState; |
| 62 | protected final Supplier<Set<? extends Image>> images; |
| 63 | |
| 64 | private static class FindImageForServer implements Predicate<Image> { |
| 65 | private final Location location; |
| 66 | private final Server instance; |
| 67 | |
| 68 | private FindImageForServer(Location location, Server instance) { |
| 69 | this.location = location; |
| 70 | this.instance = instance; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean apply(Image input) { |
| 75 | return input.getProviderId().equals(instance.getImageId()) |
| 76 | && (input.getLocation() == null || input.getLocation().equals(location) || input.getLocation() |
| 77 | .equals(location.getParent())); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @Inject |
| 82 | ServerToNodeMetadata(Function<Server, Iterable<String>> getPublicAddresses, |
| 83 | @Memoized Supplier<Set<? extends Location>> locations, Map<String, Credentials> credentialStore, |
| 84 | Map<RunningState, NodeState> runningStateToNodeState, @Memoized Supplier<Set<? extends Image>> images) { |
| 85 | this.getPublicAddresses = checkNotNull(getPublicAddresses, "serverStateToNodeState"); |
| 86 | this.locations = checkNotNull(locations, "locations"); |
| 87 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 88 | this.runningStateToNodeState = checkNotNull(runningStateToNodeState, "serverStateToNodeState"); |
| 89 | this.images = checkNotNull(images, "images"); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public NodeMetadata apply(Server from) { |
| 94 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 95 | builder.ids(from.getId() + ""); |
| 96 | builder.name(from.getName()); |
| 97 | builder.hostname(from.getName()); |
| 98 | Location location = findLocationWithId(from.getLocation().getId()); |
| 99 | builder.location(location); |
| 100 | builder.group(parseGroupFromName(from.getName())); |
| 101 | builder.imageId(from.getImageId() + ""); |
| 102 | builder.operatingSystem(parseOperatingSystem(from, location)); |
| 103 | builder.hardware(null);// TODO |
| 104 | if (from.getBillingData() != null && from.getBillingData().getDateCancelled() != null |
| 105 | && RunningState.NOTRUNNING == from.getState()) |
| 106 | builder.state(NodeState.TERMINATED); |
| 107 | else |
| 108 | builder.state(runningStateToNodeState.get(from.getState())); |
| 109 | builder.publicAddresses(getPublicAddresses.apply(from)); |
| 110 | builder.credentials(credentialStore.get("node#" + from.getId())); |
| 111 | return builder.build(); |
| 112 | } |
| 113 | |
| 114 | private Location findLocationWithId(final String locationId) { |
| 115 | try { |
| 116 | Location location = Iterables.find(locations.get(), new Predicate<Location>() { |
| 117 | |
| 118 | @Override |
| 119 | public boolean apply(Location input) { |
| 120 | return input.getId().equals(locationId); |
| 121 | } |
| 122 | |
| 123 | }); |
| 124 | return location; |
| 125 | |
| 126 | } catch (NoSuchElementException e) { |
| 127 | logger.debug("couldn't match instance location %s in: %s", locationId, locations.get()); |
| 128 | return null; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | protected OperatingSystem parseOperatingSystem(Server from, Location location) { |
| 133 | try { |
| 134 | return Iterables.find(images.get(), new FindImageForServer(location, from)).getOperatingSystem(); |
| 135 | } catch (NoSuchElementException e) { |
| 136 | logger.warn("could not find a matching image for server %s in location %s", from, location); |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | } |