| 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.byon.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | import java.net.URI; |
| 26 | import java.util.Map; |
| 27 | import java.util.NoSuchElementException; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import javax.annotation.Resource; |
| 31 | import javax.inject.Inject; |
| 32 | import javax.inject.Named; |
| 33 | import javax.inject.Singleton; |
| 34 | |
| 35 | import org.jclouds.byon.Node; |
| 36 | import org.jclouds.collect.Memoized; |
| 37 | import org.jclouds.compute.domain.NodeMetadata; |
| 38 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 39 | import org.jclouds.compute.domain.NodeState; |
| 40 | import org.jclouds.compute.domain.OperatingSystem; |
| 41 | import org.jclouds.compute.domain.OsFamily; |
| 42 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 43 | import org.jclouds.domain.Credentials; |
| 44 | import org.jclouds.domain.Location; |
| 45 | import org.jclouds.logging.Logger; |
| 46 | import org.jclouds.util.Strings2; |
| 47 | |
| 48 | import com.google.common.base.Function; |
| 49 | import com.google.common.base.Predicate; |
| 50 | import com.google.common.base.Supplier; |
| 51 | import com.google.common.collect.ImmutableSet; |
| 52 | import com.google.common.collect.Iterables; |
| 53 | |
| 54 | /** |
| 55 | * @author Adrian Cole |
| 56 | */ |
| 57 | @Singleton |
| 58 | public class NodeToNodeMetadata implements Function<Node, NodeMetadata> { |
| 59 | @Resource |
| 60 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 61 | protected Logger logger = Logger.NULL; |
| 62 | |
| 63 | private final Supplier<Location> location; |
| 64 | private final Supplier<Set<? extends Location>> locations; |
| 65 | private final Map<String, Credentials> credentialStore; |
| 66 | private final Function<URI, InputStream> slurp; |
| 67 | |
| 68 | @Inject |
| 69 | NodeToNodeMetadata(Supplier<Location> location, @Memoized Supplier<Set<? extends Location>> locations, |
| 70 | Function<URI, InputStream> slurp, Map<String, Credentials> credentialStore) { |
| 71 | this.location = checkNotNull(location, "location"); |
| 72 | this.locations = checkNotNull(locations, "locations"); |
| 73 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 74 | this.slurp = checkNotNull(slurp, "slurp"); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public NodeMetadata apply(Node from) { |
| 79 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 80 | builder.ids(from.getId()); |
| 81 | builder.name(from.getName()); |
| 82 | builder.loginPort(from.getLoginPort()); |
| 83 | builder.hostname(from.getHostname()); |
| 84 | builder.location(findLocationWithId(from.getLocationId())); |
| 85 | builder.group(from.getGroup()); |
| 86 | builder.tags(from.getTags()); |
| 87 | builder.userMetadata(from.getMetadata()); |
| 88 | builder.operatingSystem(OperatingSystem.builder().arch(from.getOsArch()).family( |
| 89 | OsFamily.fromValue(from.getOsFamily())).description(from.getOsDescription()) |
| 90 | .version(from.getOsVersion()).build()); |
| 91 | builder.state(NodeState.RUNNING); |
| 92 | builder.publicAddresses(ImmutableSet.<String> of(from.getHostname())); |
| 93 | |
| 94 | if (from.getUsername() != null) { |
| 95 | Credentials creds = null; |
| 96 | if (from.getCredentialUrl() != null) { |
| 97 | try { |
| 98 | creds = new Credentials(from.getUsername(), Strings2.toStringAndClose(slurp.apply(from |
| 99 | .getCredentialUrl()))); |
| 100 | } catch (IOException e) { |
| 101 | logger.error(e, "URI could not be read: %s", from.getCredentialUrl()); |
| 102 | } |
| 103 | } else if (from.getCredential() != null) { |
| 104 | creds = new Credentials(from.getUsername(), from.getCredential()); |
| 105 | } |
| 106 | if (creds != null) |
| 107 | builder.credentials(creds); |
| 108 | credentialStore.put("node#" + from.getId(), creds); |
| 109 | } |
| 110 | |
| 111 | if (from.getSudoPassword() != null) |
| 112 | builder.adminPassword(from.getSudoPassword()); |
| 113 | return builder.build(); |
| 114 | } |
| 115 | |
| 116 | private Location findLocationWithId(final String locationId) { |
| 117 | if (locationId == null) |
| 118 | return location.get(); |
| 119 | try { |
| 120 | Location location = Iterables.find(locations.get(), new Predicate<Location>() { |
| 121 | |
| 122 | @Override |
| 123 | public boolean apply(Location input) { |
| 124 | return input.getId().equals(locationId); |
| 125 | } |
| 126 | |
| 127 | }); |
| 128 | return location; |
| 129 | |
| 130 | } catch (NoSuchElementException e) { |
| 131 | logger.debug("couldn't match instance location %s in: %s", locationId, locations.get()); |
| 132 | return location.get(); |
| 133 | } |
| 134 | } |
| 135 | } |