EMMA Coverage Report (generated Fri Aug 26 14:14:05 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%  (180/230)82%  (33/40)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class NodeToNodeMetadata100% (1/1)100% (3/3)77%  (165/215)82%  (32/39)
findLocationWithId (String): Location 100% (1/1)49%  (21/43)57%  (4/7)
apply (Node): NodeMetadata 100% (1/1)80%  (114/142)84%  (21/25)
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.hostname(from.getHostname());
83      builder.location(findLocationWithId(from.getLocationId()));
84      builder.group(from.getGroup());
85      builder.tags(from.getTags());
86      builder.operatingSystem(OperatingSystem.builder().arch(from.getOsArch()).family(
87               OsFamily.fromValue(from.getOsFamily())).description(from.getOsDescription())
88               .version(from.getOsVersion()).build());
89      builder.state(NodeState.RUNNING);
90      builder.publicAddresses(ImmutableSet.<String> of(from.getHostname()));
91 
92      if (from.getUsername() != null) {
93         Credentials creds = null;
94         if (from.getCredentialUrl() != null) {
95            try {
96               creds = new Credentials(from.getUsername(), Strings2.toStringAndClose(slurp.apply(from
97                        .getCredentialUrl())));
98            } catch (IOException e) {
99               logger.error(e, "URI could not be read: %s", from.getCredentialUrl());
100            }
101         } else if (from.getCredential() != null) {
102            creds = new Credentials(from.getUsername(), from.getCredential());
103         }
104         if (creds != null)
105            builder.credentials(creds);
106         credentialStore.put("node#" + from.getId(), creds);
107      }
108 
109      if (from.getSudoPassword() != null)
110         builder.adminPassword(from.getSudoPassword());
111      return builder.build();
112   }
113 
114   private Location findLocationWithId(final String locationId) {
115      if (locationId == null)
116         return location.get();
117      try {
118         Location location = Iterables.find(locations.get(), new Predicate<Location>() {
119 
120            @Override
121            public boolean apply(Location input) {
122               return input.getId().equals(locationId);
123            }
124 
125         });
126         return location;
127 
128      } catch (NoSuchElementException e) {
129         logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
130         return location.get();
131      }
132   }
133}

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