EMMA Coverage Report (generated Fri Apr 27 15:03:37 EDT 2012)
[all classes][org.jclouds.ec2.compute.strategy]

COVERAGE SUMMARY FOR SOURCE FILE [EC2PopulateDefaultLoginCredentialsForImageStrategy.java]

nameclass, %method, %block, %line, %
EC2PopulateDefaultLoginCredentialsForImageStrategy.java100% (1/1)100% (3/3)72%  (54/75)78%  (14.9/19)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EC2PopulateDefaultLoginCredentialsForImageStrategy100% (1/1)100% (3/3)72%  (54/75)78%  (14.9/19)
apply (Object): LoginCredentials 100% (1/1)67%  (42/63)73%  (10.9/15)
EC2PopulateDefaultLoginCredentialsForImageStrategy (): void 100% (1/1)100% (6/6)100% (2/2)
EC2PopulateDefaultLoginCredentialsForImageStrategy (LoginCredentials, Map, Ma... 100% (1/1)100% (6/6)100% (2/2)

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.ec2.compute.strategy;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22 
23import java.util.Map;
24 
25import javax.inject.Inject;
26import javax.inject.Named;
27import javax.inject.Singleton;
28 
29import org.jclouds.compute.domain.OsFamily;
30import org.jclouds.compute.strategy.impl.ReturnCredentialsBoundToImage;
31import org.jclouds.domain.Credentials;
32import org.jclouds.domain.LoginCredentials;
33import org.jclouds.domain.LoginCredentials.Builder;
34import org.jclouds.ec2.domain.Image;
35import org.jclouds.javax.annotation.Nullable;
36 
37import com.google.common.collect.ImmutableMap;
38 
39/**
40 * @author Oleksiy Yarmula
41 */
42@Singleton
43public class EC2PopulateDefaultLoginCredentialsForImageStrategy extends ReturnCredentialsBoundToImage {
44   public EC2PopulateDefaultLoginCredentialsForImageStrategy() {
45      this(null, ImmutableMap.<String, Credentials> of(), ImmutableMap.<OsFamily, LoginCredentials>of());
46   }
47 
48   @Inject
49   public EC2PopulateDefaultLoginCredentialsForImageStrategy(@Nullable @Named("image") LoginCredentials creds,
50            Map<String, Credentials> credentialStore, Map<OsFamily, LoginCredentials> osFamilyToCredentials) {
51      super(creds, credentialStore, osFamilyToCredentials);
52   }
53 
54   @Override
55   public LoginCredentials apply(Object resourceToAuthenticate) {
56      if (creds != null)
57         return creds;
58      Builder credentials = LoginCredentials.builder().user("root");
59      if (resourceToAuthenticate != null) {
60         String owner = null;
61         if (resourceToAuthenticate instanceof Image) {
62            owner = Image.class.cast(resourceToAuthenticate).getImageOwnerId();
63         } else if (resourceToAuthenticate instanceof org.jclouds.compute.domain.Image) {
64            owner = org.jclouds.compute.domain.Image.class.cast(resourceToAuthenticate).getUserMetadata().get("owner");
65         }
66         checkArgument(owner != null, "Resource must be an image (for EC2)");
67         // canonical/alestic images use the ubuntu user to login
68         if (owner.matches("063491364108|099720109477")) {
69            credentials.user("ubuntu");
70            // http://typepad.com/2010/09/introducing-amazon-linux-ami.html
71         } else if (owner.equals("137112412989")) {
72            credentials.user("ec2-user");
73         }
74      }
75      return credentials.build();
76   }
77}

[all classes][org.jclouds.ec2.compute.strategy]
EMMA 2.0.5312 (C) Vladimir Roubtsov