EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.byon.functions]

COVERAGE SUMMARY FOR SOURCE FILE [NodeToNodeMetadata.java]

nameclass, %method, %block, %line, %
NodeToNodeMetadata.java100% (2/2)100% (5/5)88%  (212/240)90%  (38/42)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class NodeToNodeMetadata100% (1/1)100% (3/3)88%  (197/225)90%  (37/41)
apply (Node): NodeMetadata 100% (1/1)82%  (124/152)85%  (23/27)
NodeToNodeMetadata (Supplier, Supplier, Function, Map): void 100% (1/1)100% (30/30)100% (7/7)
findLocationWithId (String): Location 100% (1/1)100% (43/43)100% (7/7)
     
class NodeToNodeMetadata$1100% (1/1)100% (2/2)100% (15/15)100% (2/2)
NodeToNodeMetadata$1 (NodeToNodeMetadata, String): void 100% (1/1)100% (9/9)100% (1/1)
apply (Location): boolean 100% (1/1)100% (6/6)100% (1/1)

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 */
19package org.jclouds.byon.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.io.IOException;
24import java.io.InputStream;
25import java.net.URI;
26import java.util.Map;
27import java.util.NoSuchElementException;
28import java.util.Set;
29 
30import javax.annotation.Resource;
31import javax.inject.Inject;
32import javax.inject.Named;
33import javax.inject.Singleton;
34 
35import org.jclouds.byon.Node;
36import org.jclouds.collect.Memoized;
37import org.jclouds.compute.domain.NodeMetadata;
38import org.jclouds.compute.domain.NodeMetadataBuilder;
39import org.jclouds.compute.domain.NodeState;
40import org.jclouds.compute.domain.OperatingSystem;
41import org.jclouds.compute.domain.OsFamily;
42import org.jclouds.compute.reference.ComputeServiceConstants;
43import org.jclouds.domain.Credentials;
44import org.jclouds.domain.Location;
45import org.jclouds.logging.Logger;
46import org.jclouds.util.Strings2;
47 
48import com.google.common.base.Function;
49import com.google.common.base.Predicate;
50import com.google.common.base.Supplier;
51import com.google.common.collect.ImmutableSet;
52import com.google.common.collect.Iterables;
53 
54/**
55 * @author Adrian Cole
56 */
57@Singleton
58public 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}

[all classes][org.jclouds.byon.functions]
EMMA 2.0.5312 (C) Vladimir Roubtsov