View Javadoc

1   package org.rundeck.api;
2   
3   import org.rundeck.api.util.AssertUtil;
4   
5   
6   /**
7    * Builder to create a {@link RundeckClient}, you must specify a url, and at least one of (login, password), token, or
8    * sessionId.
9    */
10  public class RundeckClientBuilder {
11      private String url;
12      private String login;
13      private String password;
14      private String token = null;
15      private String id = null;
16      private int version = -1;
17  
18      RundeckClientBuilder(){
19  
20      }
21      /**
22       * Specify the URL
23       */
24      public RundeckClientBuilder url(String url) {
25          this.url = url;
26          return this;
27      }
28  
29      public RundeckClientBuilder login(String login) {
30          this.login = login;
31          return this;
32      }
33  
34      public RundeckClientBuilder login(String login, String password) {
35          this.login = login;
36          this.password = password;
37          return this;
38      }
39  
40      public RundeckClientBuilder password(String password) {
41          this.password = password;
42          return this;
43      }
44  
45      /**
46       * Specify a Rundeck API Token string for authentication
47       */
48      public RundeckClientBuilder token(String token) {
49          this.token = token;
50          return this;
51      }
52  
53      /**
54       * Specify a web session ID string for authentication
55       */
56      public RundeckClientBuilder sessionId(String id) {
57          this.id = id;
58          return this;
59      }
60  
61      /**
62       * Specify another version number to use
63       */
64      public RundeckClientBuilder version(final RundeckClient.Version version) {
65          this.version = version.getVersionNumber();
66          return this;
67      }
68  
69      /**
70       * Specify another version number to use
71       */
72      public RundeckClientBuilder version(final int version) {
73          this.version = version;
74          return this;
75      }
76  
77      /**
78       * Create the RundeckClient instance
79       */
80      public RundeckClient build() {
81          if (null == url) {
82              AssertUtil.notBlank(url, "The Rundeck URL is required");
83          }
84          final RundeckClient client = new RundeckClient(url);
85          if (null != login && null != password) {
86              AssertUtil.notBlank(login, "login cannot be blank");
87              AssertUtil.notBlank(password, "password cannot be blank");
88              client.setLogin(login);
89              client.setPassword(password);
90          } else if (null != token) {
91              AssertUtil.notBlank(token, "token cannot be blank");
92              client.setToken(token);
93          } else if (null != id) {
94              AssertUtil.notBlank(token, "sessionId cannot be blank");
95              client.setSessionID(id);
96          } else {
97              throw new IllegalStateException("login/password, token, or sessionID must be specified");
98          }
99          if (version > 0) {
100             client.setApiVersion(version);
101         }
102         return client;
103     }
104 }