| 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.servermanager.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.servermanager.Server; |
| 40 | |
| 41 | import com.google.common.base.Function; |
| 42 | import com.google.common.base.Supplier; |
| 43 | import com.google.common.collect.ImmutableMap; |
| 44 | import com.google.common.collect.ImmutableSet; |
| 45 | |
| 46 | /** |
| 47 | * @author Adrian Cole |
| 48 | */ |
| 49 | @Singleton |
| 50 | public class ServerToNodeMetadata implements Function<Server, NodeMetadata> { |
| 51 | |
| 52 | public static final Map<Server.Status, NodeState> serverStatusToNodeState = ImmutableMap |
| 53 | .<Server.Status, NodeState> builder().put(Server.Status.ACTIVE, NodeState.RUNNING)// |
| 54 | .put(Server.Status.BUILD, NodeState.PENDING)// |
| 55 | .put(Server.Status.TERMINATED, NodeState.TERMINATED)// |
| 56 | .put(Server.Status.UNRECOGNIZED, NodeState.UNRECOGNIZED)// |
| 57 | .build(); |
| 58 | |
| 59 | private final FindHardwareForServer findHardwareForServer; |
| 60 | private final FindLocationForServer findLocationForServer; |
| 61 | private final FindImageForServer findImageForServer; |
| 62 | private final Map<String, Credentials> credentialStore; |
| 63 | |
| 64 | @Inject |
| 65 | ServerToNodeMetadata(Map<String, Credentials> credentialStore, FindHardwareForServer findHardwareForServer, |
| 66 | FindLocationForServer findLocationForServer, FindImageForServer findImageForServer) { |
| 67 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 68 | this.findHardwareForServer = checkNotNull(findHardwareForServer, "findHardwareForServer"); |
| 69 | this.findLocationForServer = checkNotNull(findLocationForServer, "findLocationForServer"); |
| 70 | this.findImageForServer = checkNotNull(findImageForServer, "findImageForServer"); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public NodeMetadata apply(Server from) { |
| 75 | // convert the result object to a jclouds NodeMetadata |
| 76 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 77 | builder.ids(from.id + ""); |
| 78 | builder.name(from.name); |
| 79 | builder.location(findLocationForServer.apply(from)); |
| 80 | builder.group(parseGroupFromName(from.name)); |
| 81 | builder.imageId(from.imageId + ""); |
| 82 | Image image = findImageForServer.apply(from); |
| 83 | if (image != null) |
| 84 | builder.operatingSystem(image.getOperatingSystem()); |
| 85 | builder.hardware(findHardwareForServer.apply(from)); |
| 86 | builder.state(serverStatusToNodeState.get(from.status)); |
| 87 | builder.publicAddresses(ImmutableSet.<String> of(from.publicAddress)); |
| 88 | builder.privateAddresses(ImmutableSet.<String> of(from.privateAddress)); |
| 89 | builder.credentials(credentialStore.get(from.id + "")); |
| 90 | return builder.build(); |
| 91 | } |
| 92 | |
| 93 | @Singleton |
| 94 | public static class FindHardwareForServer extends FindResourceInSet<Server, Hardware> { |
| 95 | |
| 96 | @Inject |
| 97 | public FindHardwareForServer(@Memoized Supplier<Set<? extends Hardware>> hardware) { |
| 98 | super(hardware); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean matches(Server from, Hardware input) { |
| 103 | return input.getProviderId().equals(from.hardwareId + ""); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Singleton |
| 108 | public static class FindImageForServer extends FindResourceInSet<Server, Image> { |
| 109 | |
| 110 | @Inject |
| 111 | public FindImageForServer(@Memoized Supplier<Set<? extends Image>> hardware) { |
| 112 | super(hardware); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean matches(Server from, Image input) { |
| 117 | return input.getProviderId().equals(from.imageId + ""); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Singleton |
| 122 | public static class FindLocationForServer extends FindResourceInSet<Server, Location> { |
| 123 | |
| 124 | @Inject |
| 125 | public FindLocationForServer(@Memoized Supplier<Set<? extends Location>> hardware) { |
| 126 | super(hardware); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public boolean matches(Server from, Location input) { |
| 131 | return input.getId().equals(from.datacenter + ""); |
| 132 | } |
| 133 | } |
| 134 | } |