EMMA Coverage Report (generated Wed Jun 22 19:47:49 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)78%  (175/225)82%  (32/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class NodeToNodeMetadata100% (1/1)100% (3/3)76%  (160/210)82%  (31/38)
findLocationWithId (String): Location 100% (1/1)49%  (21/43)57%  (4/7)
apply (Node): NodeMetadata 100% (1/1)80%  (109/137)83%  (20/24)
NodeToNodeMetadata (Supplier, Supplier, Function, Map): void 100% (1/1)100% (30/30)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 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
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.location(findLocationWithId(from.getLocationId()));
83      builder.group(from.getGroup());
84      builder.tags(from.getTags());
85      builder.operatingSystem(OperatingSystem.builder().arch(from.getOsArch()).family(
86               OsFamily.fromValue(from.getOsFamily())).description(from.getOsDescription())
87               .version(from.getOsVersion()).build());
88      builder.state(NodeState.RUNNING);
89      builder.publicAddresses(ImmutableSet.<String> of(from.getHostname()));
90 
91      if (from.getUsername() != null) {
92         Credentials creds = null;
93         if (from.getCredentialUrl() != null) {
94            try {
95               creds = new Credentials(from.getUsername(), Strings2.toStringAndClose(slurp.apply(from
96                        .getCredentialUrl())));
97            } catch (IOException e) {
98               logger.error(e, "URI could not be read: %s", from.getCredentialUrl());
99            }
100         } else if (from.getCredential() != null) {
101            creds = new Credentials(from.getUsername(), from.getCredential());
102         }
103         if (creds != null)
104            builder.credentials(creds);
105         credentialStore.put("node#" + from.getId(), creds);
106      }
107 
108      if (from.getSudoPassword() != null)
109         builder.adminPassword(from.getSudoPassword());
110      return builder.build();
111   }
112 
113   private Location findLocationWithId(final String locationId) {
114      if (locationId == null)
115         return location.get();
116      try {
117         Location location = Iterables.find(locations.get(), new Predicate<Location>() {
118 
119            @Override
120            public boolean apply(Location input) {
121               return input.getId().equals(locationId);
122            }
123 
124         });
125         return location;
126 
127      } catch (NoSuchElementException e) {
128         logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
129         return location.get();
130      }
131   }
132}

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