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

COVERAGE SUMMARY FOR SOURCE FILE [UserAdd.java]

nameclass, %method, %block, %line, %
UserAdd.java100% (2/2)72%  (13/18)82%  (352/428)72%  (63/87)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class UserAdd100% (1/1)57%  (4/7)81%  (272/334)68%  (43/63)
equals (Object): boolean 0%   (0/1)0%   (0/37)0%   (0/13)
functionDependencies (OsFamily): Iterable 0%   (0/1)0%   (0/2)0%   (0/1)
hashCode (): int 0%   (0/1)0%   (0/19)0%   (0/4)
render (OsFamily): String 100% (1/1)98%  (219/223)94%  (30/32)
UserAdd (String, List, String, String, List, String, String): void 100% (1/1)100% (44/44)100% (10/10)
builder (): UserAdd$Builder 100% (1/1)100% (4/4)100% (1/1)
cryptFunction (Function): UserAdd 100% (1/1)100% (5/5)100% (2/2)
     
class UserAdd$Builder100% (1/1)82%  (9/11)85%  (80/94)83%  (20/24)
authorizeRSAPublicKeys (Iterable): UserAdd$Builder 0%   (0/1)0%   (0/9)0%   (0/2)
shell (String): UserAdd$Builder 0%   (0/1)0%   (0/5)0%   (0/2)
UserAdd$Builder (): void 100% (1/1)100% (15/15)100% (5/5)
authorizeRSAPublicKey (String): UserAdd$Builder 100% (1/1)100% (9/9)100% (2/2)
build (): UserAdd 100% (1/1)100% (18/18)100% (1/1)
defaultHome (String): UserAdd$Builder 100% (1/1)100% (5/5)100% (2/2)
group (String): UserAdd$Builder 100% (1/1)100% (9/9)100% (2/2)
groups (Iterable): UserAdd$Builder 100% (1/1)100% (9/9)100% (2/2)
installRSAPrivateKey (String): UserAdd$Builder 100% (1/1)100% (5/5)100% (2/2)
login (String): UserAdd$Builder 100% (1/1)100% (5/5)100% (2/2)
password (String): UserAdd$Builder 100% (1/1)100% (5/5)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.scriptbuilder.statements.login;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.util.List;
24 
25import org.jclouds.javax.annotation.Nullable;
26import javax.inject.Named;
27 
28import org.jclouds.crypto.Sha512Crypt;
29import org.jclouds.scriptbuilder.domain.OsFamily;
30import org.jclouds.scriptbuilder.domain.Statement;
31import org.jclouds.scriptbuilder.domain.StatementList;
32import org.jclouds.scriptbuilder.domain.Statements;
33import org.jclouds.scriptbuilder.statements.ssh.AuthorizeRSAPublicKeys;
34import org.jclouds.scriptbuilder.statements.ssh.InstallRSAPrivateKey;
35 
36import com.google.common.annotations.VisibleForTesting;
37import com.google.common.base.Function;
38import com.google.common.base.Joiner;
39import com.google.common.base.Throwables;
40import com.google.common.collect.ImmutableList;
41import com.google.common.collect.ImmutableMap;
42import com.google.common.collect.Lists;
43import com.google.inject.Inject;
44 
45/**
46 * 
47 * @author Adrian Cole
48 */
49public class UserAdd implements Statement {
50   public static UserAdd.Builder builder() {
51      return new Builder();
52   }
53 
54   public static class Builder {
55      private String defaultHome = "/home/users";
56      private String login;
57      private String password;
58      private String RSAPrivateKey;
59      private List<String> groups = Lists.newArrayList();
60      private List<String> authorizeRSAPublicKeys = Lists.newArrayList();
61      private String shell = "/bin/bash";
62 
63      public UserAdd.Builder defaultHome(String defaultHome) {
64         this.defaultHome = defaultHome;
65         return this;
66      }
67 
68      public UserAdd.Builder login(String login) {
69         this.login = login;
70         return this;
71      }
72 
73      public UserAdd.Builder password(String password) {
74         this.password = password;
75         return this;
76      }
77 
78      public UserAdd.Builder group(String group) {
79         this.groups.add(checkNotNull(group, "group"));
80         return this;
81      }
82 
83      public UserAdd.Builder groups(Iterable<String> groups) {
84         this.groups = ImmutableList.<String> copyOf(checkNotNull(groups, "groups"));
85         return this;
86      }
87 
88      public UserAdd.Builder installRSAPrivateKey(String RSAPrivateKey) {
89         this.RSAPrivateKey = RSAPrivateKey;
90         return this;
91      }
92 
93      public UserAdd.Builder authorizeRSAPublicKey(String RSAPublicKey) {
94         this.authorizeRSAPublicKeys.add(checkNotNull(RSAPublicKey, "RSAPublicKey"));
95         return this;
96      }
97 
98      public UserAdd.Builder authorizeRSAPublicKeys(Iterable<String> RSAPublicKeys) {
99         this.authorizeRSAPublicKeys = ImmutableList.<String> copyOf(checkNotNull(RSAPublicKeys, "RSAPublicKeys"));
100         return this;
101      }
102 
103      public UserAdd.Builder shell(String shell) {
104         this.shell = shell;
105         return this;
106      }
107 
108      public UserAdd build() {
109         return new UserAdd(login, groups, password, RSAPrivateKey, authorizeRSAPublicKeys, defaultHome, shell);
110      }
111   }
112 
113   public UserAdd(String login, List<String> groups, @Nullable String password, @Nullable String installRSAPrivateKey,
114         List<String> authorizeRSAPublicKeys, String defaultHome, String shell) {
115      this.login = checkNotNull(login, "login");
116      this.password = password;
117      this.groups = ImmutableList.copyOf(checkNotNull(groups, "groups"));
118      this.installRSAPrivateKey = installRSAPrivateKey;
119      this.authorizeRSAPublicKeys = ImmutableList
120            .copyOf(checkNotNull(authorizeRSAPublicKeys, "authorizeRSAPublicKeys"));
121      this.defaultHome = checkNotNull(defaultHome, "defaultHome");
122      this.shell = checkNotNull(shell, "shell");
123   }
124 
125   private final String defaultHome;
126   private final String login;
127   private final List<String> groups;
128   private final String password;
129   private final String installRSAPrivateKey;
130   private final List<String> authorizeRSAPublicKeys;
131   private final String shell;
132 
133   private Function<String, String> cryptFunction = Sha512Crypt.function();
134 
135   @Inject(optional = true)
136   @Named("CRYPT")
137   @VisibleForTesting
138   UserAdd cryptFunction(Function<String, String> cryptFunction) {
139      this.cryptFunction = cryptFunction;
140      return this;
141   }
142 
143   @Override
144   public Iterable<String> functionDependencies(OsFamily family) {
145      return ImmutableList.of();
146   }
147 
148   @Override
149   public String render(OsFamily family) {
150      checkNotNull(family, "family");
151      if (family == OsFamily.WINDOWS)
152         throw new UnsupportedOperationException("windows not yet implemented");
153      String homeDir = defaultHome + "{fs}" + login;
154      ImmutableList.Builder<Statement> statements = ImmutableList.<Statement> builder();
155      statements.add(Statements.exec("{md} " + homeDir));
156 
157      ImmutableMap.Builder<String, String> userAddOptions = ImmutableMap.<String, String> builder();
158      userAddOptions.put("-s", shell);
159      if (groups.size() > 0) {
160         for (String group : groups)
161            statements.add(Statements.exec("groupadd -f " + group));
162 
163         List<String> groups = Lists.newArrayList(this.groups);
164         String primaryGroup = groups.remove(0);
165         userAddOptions.put("-g", primaryGroup);
166         if (groups.size() > 0)
167            userAddOptions.put("-G", Joiner.on(',').join(groups));
168 
169      }
170      userAddOptions.put("-d", homeDir);
171      if (password != null) {
172         try {
173            userAddOptions.put("-p", "'" + cryptFunction.apply(password) + "'");
174         } catch (Exception e) {
175            Throwables.propagate(e);
176         }
177      }
178 
179      String options = Joiner.on(' ').withKeyValueSeparator(" ").join(userAddOptions.build());
180 
181      statements.add(Statements.exec(String.format("useradd %s %s", options, login)));
182 
183      if (authorizeRSAPublicKeys.size() > 0 || installRSAPrivateKey != null) {
184         String sshDir = homeDir + "{fs}.ssh";
185         if (authorizeRSAPublicKeys.size() > 0)
186            statements.add(new AuthorizeRSAPublicKeys(sshDir, authorizeRSAPublicKeys));
187         if (installRSAPrivateKey != null)
188            statements.add(new InstallRSAPrivateKey(sshDir, installRSAPrivateKey));
189      }
190      statements.add(Statements.exec(String.format("chown -R %s %s", login, homeDir)));
191      return new StatementList(statements.build()).render(family);
192   }
193 
194   @Override
195   public int hashCode() {
196      final int prime = 31;
197      int result = 1;
198      result = prime * result + ((login == null) ? 0 : login.hashCode());
199      return result;
200   }
201 
202   @Override
203   public boolean equals(Object obj) {
204      if (this == obj)
205         return true;
206      if (obj == null)
207         return false;
208      if (getClass() != obj.getClass())
209         return false;
210      UserAdd other = (UserAdd) obj;
211      if (login == null) {
212         if (other.login != null)
213            return false;
214      } else if (!login.equals(other.login))
215         return false;
216      return true;
217   }
218}

[all classes][org.jclouds.scriptbuilder.statements.login]
EMMA 2.0.5312 (C) Vladimir Roubtsov