1 /*
2 * Copyright 2011 Vincent Behar
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.rundeck.api;
17
18 import java.util.Properties;
19 import org.apache.commons.lang.StringUtils;
20
21 /**
22 * Builder for node filters
23 *
24 * @author Vincent Behar
25 */
26 public class NodeFiltersBuilder {
27
28 private final Properties filters;
29
30 /**
31 * Build a new instance. At the end, use {@link #toProperties()}.
32 */
33 public NodeFiltersBuilder() {
34 filters = new Properties();
35 }
36
37 /**
38 * Include nodes matching the given hostname
39 *
40 * @param hostname
41 * @return this, for method chaining
42 * @see #excludeHostname(String)
43 */
44 public NodeFiltersBuilder hostname(String hostname) {
45 if (StringUtils.isNotBlank(hostname)) {
46 filters.put("hostname", hostname);
47 }
48 return this;
49 }
50
51 /**
52 * Include nodes matching the given type
53 *
54 * @param type
55 * @return this, for method chaining
56 * @see #excludeType(String)
57 */
58 public NodeFiltersBuilder type(String type) {
59 if (StringUtils.isNotBlank(type)) {
60 filters.put("type", type);
61 }
62 return this;
63 }
64
65 /**
66 * Include nodes matching the given tags
67 *
68 * @param tags
69 * @return this, for method chaining
70 * @see #excludeTags(String)
71 */
72 public NodeFiltersBuilder tags(String tags) {
73 if (StringUtils.isNotBlank(tags)) {
74 filters.put("tags", tags);
75 }
76 return this;
77 }
78
79 /**
80 * Include nodes matching the given name
81 *
82 * @param name
83 * @return this, for method chaining
84 * @see #excludeName(String)
85 */
86 public NodeFiltersBuilder name(String name) {
87 if (StringUtils.isNotBlank(name)) {
88 filters.put("name", name);
89 }
90 return this;
91 }
92
93 /**
94 * Include nodes matching the given OS-name
95 *
96 * @param osName
97 * @return this, for method chaining
98 * @see #excludeOsName(String)
99 */
100 public NodeFiltersBuilder osName(String osName) {
101 if (StringUtils.isNotBlank(osName)) {
102 filters.put("os-name", osName);
103 }
104 return this;
105 }
106
107 /**
108 * Include nodes matching the given OS-family
109 *
110 * @param osFamily
111 * @return this, for method chaining
112 * @see #excludeOsFamily(String)
113 */
114 public NodeFiltersBuilder osFamily(String osFamily) {
115 if (StringUtils.isNotBlank(osFamily)) {
116 filters.put("os-family", osFamily);
117 }
118 return this;
119 }
120
121 /**
122 * Include nodes matching the given OS-arch
123 *
124 * @param osArch
125 * @return this, for method chaining
126 * @see #excludeOsArch(String)
127 */
128 public NodeFiltersBuilder osArch(String osArch) {
129 if (StringUtils.isNotBlank(osArch)) {
130 filters.put("os-arch", osArch);
131 }
132 return this;
133 }
134
135 /**
136 * Include nodes matching the given OS-version
137 *
138 * @param osVersion
139 * @return this, for method chaining
140 * @see #excludeOsVersion(String)
141 */
142 public NodeFiltersBuilder osVersion(String osVersion) {
143 if (StringUtils.isNotBlank(osVersion)) {
144 filters.put("os-version", osVersion);
145 }
146 return this;
147 }
148
149 /**
150 * Exclude nodes matching the given hostname
151 *
152 * @param hostname
153 * @return this, for method chaining
154 * @see #hostname(String)
155 * @see #excludePrecedence(boolean)
156 */
157 public NodeFiltersBuilder excludeHostname(String hostname) {
158 if (StringUtils.isNotBlank(hostname)) {
159 filters.put("exclude-hostname", hostname);
160 }
161 return this;
162 }
163
164 /**
165 * Exclude nodes matching the given type
166 *
167 * @param type
168 * @return this, for method chaining
169 * @see #type(String)
170 * @see #excludePrecedence(boolean)
171 */
172 public NodeFiltersBuilder excludeType(String type) {
173 if (StringUtils.isNotBlank(type)) {
174 filters.put("exclude-type", type);
175 }
176 return this;
177 }
178
179 /**
180 * Exclude nodes matching the given tags
181 *
182 * @param tags
183 * @return this, for method chaining
184 * @see #tags(String)
185 * @see #excludePrecedence(boolean)
186 */
187 public NodeFiltersBuilder excludeTags(String tags) {
188 if (StringUtils.isNotBlank(tags)) {
189 filters.put("exclude-tags", tags);
190 }
191 return this;
192 }
193
194 /**
195 * Exclude nodes matching the given name
196 *
197 * @param name
198 * @return this, for method chaining
199 * @see #name(String)
200 * @see #excludePrecedence(boolean)
201 */
202 public NodeFiltersBuilder excludeName(String name) {
203 if (StringUtils.isNotBlank(name)) {
204 filters.put("exclude-name", name);
205 }
206 return this;
207 }
208
209 /**
210 * Exclude nodes matching the given OS-name
211 *
212 * @param osName
213 * @return this, for method chaining
214 * @see #osName(String)
215 * @see #excludePrecedence(boolean)
216 */
217 public NodeFiltersBuilder excludeOsName(String osName) {
218 if (StringUtils.isNotBlank(osName)) {
219 filters.put("exclude-os-name", osName);
220 }
221 return this;
222 }
223
224 /**
225 * Exclude nodes matching the given OS-family
226 *
227 * @param osFamily
228 * @return this, for method chaining
229 * @see #osFamily(String)
230 * @see #excludePrecedence(boolean)
231 */
232 public NodeFiltersBuilder excludeOsFamily(String osFamily) {
233 if (StringUtils.isNotBlank(osFamily)) {
234 filters.put("exclude-os-family", osFamily);
235 }
236 return this;
237 }
238
239 /**
240 * Exclude nodes matching the given OS-arch
241 *
242 * @param osArch
243 * @return this, for method chaining
244 * @see #osArch(String)
245 * @see #excludePrecedence(boolean)
246 */
247 public NodeFiltersBuilder excludeOsArch(String osArch) {
248 if (StringUtils.isNotBlank(osArch)) {
249 filters.put("exclude-os-arch", osArch);
250 }
251 return this;
252 }
253
254 /**
255 * Exclude nodes matching the given OS-version
256 *
257 * @param osVersion
258 * @return this, for method chaining
259 * @see #osVersion(String)
260 * @see #excludePrecedence(boolean)
261 */
262 public NodeFiltersBuilder excludeOsVersion(String osVersion) {
263 if (StringUtils.isNotBlank(osVersion)) {
264 filters.put("exclude-os-version", osVersion);
265 }
266 return this;
267 }
268
269 /**
270 * Whether exclusion filters take precedence (default to yes).
271 *
272 * @param excludePrecedence
273 * @return this, for method chaining
274 */
275 public NodeFiltersBuilder excludePrecedence(boolean excludePrecedence) {
276 filters.put("exclude-precedence", Boolean.toString(excludePrecedence));
277 return this;
278 }
279
280 /**
281 * @return a new {@link Properties} instance
282 */
283 public Properties toProperties() {
284 Properties filters = new Properties();
285 filters.putAll(this.filters);
286 return filters;
287 }
288
289 }