Skip to content

Commit 32e50fc

Browse files
committed
refactor!: Builders for Admin and Database
1 parent 474c314 commit 32e50fc

File tree

3 files changed

+182
-132
lines changed

3 files changed

+182
-132
lines changed

core/redis-enterprise-admin/src/main/java/com/redis/enterprise/Admin.java

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.net.URI;
55
import java.net.URISyntaxException;
66
import java.security.GeneralSecurityException;
7+
import java.security.KeyManagementException;
8+
import java.security.KeyStoreException;
9+
import java.security.NoSuchAlgorithmException;
710
import java.time.Duration;
811
import java.util.HashMap;
912
import java.util.List;
@@ -53,54 +56,59 @@ public class Admin implements AutoCloseable {
5356

5457
private static final Logger log = LoggerFactory.getLogger(Admin.class);
5558

56-
public static final String CONTENT_TYPE_JSON = "application/json";
57-
public static final String V1 = "/v1/";
58-
public static final String V2 = "/v2/";
59+
public static final String DEFAULT_USER_NAME = "admin@redis.com";
60+
public static final String DEFAULT_PASSWORD = "redis123";
5961
public static final String DEFAULT_PROTOCOL = "https";
6062
public static final String DEFAULT_HOST = "localhost";
6163
public static final int DEFAULT_PORT = 9443;
62-
public static final String BOOTSTRAP = "bootstrap";
63-
public static final String ACTIONS = "actions";
64-
public static final String MODULES = "modules";
65-
public static final String BDBS = "bdbs";
66-
public static final String COMMAND = "command";
64+
65+
private static final String BOOTSTRAP = "bootstrap";
66+
private static final String MODULES = "modules";
67+
private static final String BDBS = "bdbs";
68+
private static final String COMMAND = "command";
69+
private static final String CONTENT_TYPE_JSON = "application/json";
70+
private static final String V1 = "/v1/";
6771
private static final CharSequence PATH_SEPARATOR = "/";
6872

6973
private final ObjectMapper objectMapper = new ObjectMapper()
7074
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
71-
private final UsernamePasswordCredentials credentials;
72-
private final CloseableHttpClient client;
75+
private String userName = DEFAULT_USER_NAME;
76+
private String password = DEFAULT_PASSWORD;
77+
private CloseableHttpClient client;
7378
private String protocol = DEFAULT_PROTOCOL;
7479
private String host = DEFAULT_HOST;
7580
private int port = DEFAULT_PORT;
7681

77-
public Admin(String userName, final char[] password) throws GeneralSecurityException {
78-
this.credentials = new UsernamePasswordCredentials(userName, password);
79-
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy()).build();
80-
SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
81-
.setSslContext(sslcontext).setHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
82-
HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
83-
.setSSLSocketFactory(sslSocketFactory).build();
84-
HttpClientBuilder clientBuilder = HttpClients.custom();
85-
clientBuilder.setConnectionManager(cm);
86-
this.client = clientBuilder.build();
87-
}
88-
89-
@Override
90-
public void close() throws Exception {
91-
client.close();
82+
public void close() throws IOException {
83+
if (client != null) {
84+
client.close();
85+
client = null;
86+
}
9287
}
9388

94-
public void setHost(String host) {
89+
public Admin withHost(String host) {
9590
this.host = host;
91+
return this;
9692
}
9793

98-
public void setProtocol(String protocol) {
94+
public Admin withProtocol(String protocol) {
9995
this.protocol = protocol;
96+
return this;
10097
}
10198

102-
public void setPort(int port) {
99+
public Admin withPort(int port) {
103100
this.port = port;
101+
return this;
102+
}
103+
104+
public Admin withUserName(String userName) {
105+
this.userName = userName;
106+
return this;
107+
}
108+
109+
public Admin withPassword(String password) {
110+
this.password = password;
111+
return this;
104112
}
105113

106114
private static String v1(String... segments) {
@@ -119,19 +127,19 @@ private URI uri(String path) {
119127
}
120128
}
121129

122-
private <T> T get(String path, Class<T> type) throws IOException {
130+
private <T> T get(String path, Class<T> type) throws IOException, GeneralSecurityException {
123131
return get(path, SimpleType.constructUnsafe(type));
124132
}
125133

126-
private <T> T get(String path, JavaType type) throws IOException {
134+
private <T> T get(String path, JavaType type) throws IOException, GeneralSecurityException {
127135
return read(header(new HttpGet(uri(path))), type, HttpStatus.SC_OK);
128136
}
129137

130-
private <T> T delete(String path, Class<T> type) throws IOException {
138+
private <T> T delete(String path, Class<T> type) throws IOException, GeneralSecurityException {
131139
return delete(path, SimpleType.constructUnsafe(type));
132140
}
133141

134-
private <T> T delete(String path, JavaType type) throws IOException {
142+
private <T> T delete(String path, JavaType type) throws IOException, GeneralSecurityException {
135143
return read(header(new HttpDelete(uri(path))), type, HttpStatus.SC_OK);
136144
}
137145

@@ -140,25 +148,28 @@ private ClassicHttpRequest header(ClassicHttpRequest request) {
140148
return request;
141149
}
142150

143-
private <T> T post(String path, Object request, Class<T> responseType) throws IOException {
151+
private <T> T post(String path, Object request, Class<T> responseType)
152+
throws IOException, GeneralSecurityException {
144153
return post(path, request, SimpleType.constructUnsafe(responseType));
145154
}
146155

147-
private <T> T post(String path, Object request, JavaType responseType) throws IOException {
156+
private <T> T post(String path, Object request, JavaType responseType)
157+
throws IOException, GeneralSecurityException {
148158
HttpPost post = new HttpPost(uri(path));
149159
String json = objectMapper.writeValueAsString(request);
150160
log.debug("POST {}", json);
151161
post.setEntity(new StringEntity(json));
152162
return read(header(post), responseType, HttpStatus.SC_OK);
153163
}
154164

155-
private <T> T read(ClassicHttpRequest request, JavaType type, int successCode) throws IOException {
165+
private <T> T read(ClassicHttpRequest request, JavaType type, int successCode)
166+
throws IOException, GeneralSecurityException {
156167
HttpHost target = new HttpHost(protocol, host, port);
157168
HttpClientContext localContext = HttpClientContext.create();
158169
BasicScheme basicAuth = new BasicScheme();
159-
basicAuth.initPreemptive(credentials);
170+
basicAuth.initPreemptive(new UsernamePasswordCredentials(userName, password.toCharArray()));
160171
localContext.resetAuthExchange(target, basicAuth);
161-
CloseableHttpResponse response = client.execute(request, localContext);
172+
CloseableHttpResponse response = client().execute(request, localContext);
162173
String json;
163174
try {
164175
json = EntityUtils.toString(response.getEntity());
@@ -171,6 +182,20 @@ private <T> T read(ClassicHttpRequest request, JavaType type, int successCode) t
171182
throw new HttpResponseException(response.getCode(), response.getReasonPhrase() + " " + json);
172183
}
173184

185+
private CloseableHttpClient client() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
186+
if (client == null) {
187+
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy()).build();
188+
SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
189+
.setSslContext(sslcontext).setHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
190+
HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
191+
.setSSLSocketFactory(sslSocketFactory).build();
192+
HttpClientBuilder clientBuilder = HttpClients.custom();
193+
clientBuilder.setConnectionManager(cm);
194+
client = clientBuilder.build();
195+
}
196+
return client;
197+
}
198+
174199
private static class HttpResponseParsingException extends IOException {
175200

176201
private static final long serialVersionUID = 1L;
@@ -180,12 +205,12 @@ public HttpResponseParsingException(String message, Throwable cause) {
180205
}
181206
}
182207

183-
public List<InstalledModule> getModules() throws IOException {
208+
public List<InstalledModule> getModules() throws IOException, GeneralSecurityException {
184209
return get(v1(MODULES),
185210
objectMapper.getTypeFactory().constructCollectionType(List.class, InstalledModule.class));
186211
}
187212

188-
public Database createDatabase(Database database) throws IOException {
213+
public Database createDatabase(Database database) throws IOException, GeneralSecurityException {
189214
Map<String, InstalledModule> installedModules = new HashMap<>();
190215
for (InstalledModule module : getModules()) {
191216
installedModules.put(module.getName(), module);
@@ -203,7 +228,7 @@ public Database createDatabase(Database database) throws IOException {
203228
return response;
204229
}
205230

206-
public List<Database> getDatabases() throws IOException {
231+
public List<Database> getDatabases() throws IOException, GeneralSecurityException {
207232
return get(v1(BDBS), objectMapper.getTypeFactory().constructCollectionType(List.class, Database.class));
208233
}
209234

@@ -228,12 +253,25 @@ public void waitForBoostrap() {
228253

229254
}
230255

231-
private Bootstrap getBootstrap() throws IOException {
256+
private Bootstrap getBootstrap() throws IOException, GeneralSecurityException {
232257
return get(v1(BOOTSTRAP), Bootstrap.class);
233258
}
234259

235-
public CommandResponse executeCommand(long bdb, Command command) throws IOException {
260+
public CommandResponse executeCommand(long bdb, Command command) throws IOException, GeneralSecurityException {
236261
return post(v1(BDBS, String.valueOf(bdb), COMMAND), command, CommandResponse.class);
237262
}
238263

264+
public static Admin create(String host) {
265+
Admin admin = new Admin();
266+
admin.withHost(host);
267+
return admin;
268+
}
269+
270+
public static Admin create(String host, int port) {
271+
Admin admin = new Admin();
272+
admin.withHost(host);
273+
admin.withPort(port);
274+
return admin;
275+
}
276+
239277
}

core/redis-enterprise-admin/src/main/java/com/redis/enterprise/Database.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@
1717
@JsonIgnoreProperties(ignoreUnknown = true)
1818
public class Database {
1919

20+
public static final String DEFAULT_NAME = "redis-enterprise-admin-db";
21+
22+
public static final long DEFAULT_MEMORY_MB = 100;
23+
public static final DataSize DEFAULT_MEMORY = DataSize.ofMegabytes(DEFAULT_MEMORY_MB);
24+
public static final int DEFAULT_CLUSTER_SHARD_COUNT = 3;
25+
26+
public static List<String> defaultShardKeyRegexes() {
27+
return Arrays.asList(".*\\{(?<tag>.*)\\}.*", "(?<tag>.*)");
28+
}
29+
2030
private Long uid;
21-
private String name;
31+
private String name = DEFAULT_NAME;
2232
private boolean replication;
2333
private boolean sharding;
24-
private long memory;
34+
private long memory = DEFAULT_MEMORY.toBytes();
2535
private Integer port;
2636
private String type;
2737
private boolean ossCluster;
@@ -32,7 +42,7 @@ public class Database {
3242
private ShardPlacement shardPlacement;
3343
private List<ModuleConfig> modules;
3444

35-
private Database() {
45+
public Database() {
3646
}
3747

3848
private Database(Builder builder) {
@@ -97,7 +107,11 @@ public Integer getPort() {
97107
return port;
98108
}
99109

100-
public void setPort(int port) {
110+
/**
111+
*
112+
* @param port the database port. Use null for auto-assigned by Redis Enterprise
113+
*/
114+
public void setPort(Integer port) {
101115
this.port = port;
102116
}
103117

@@ -280,21 +294,17 @@ public void setRegex(String regex) {
280294

281295
}
282296

283-
public static Builder name(String name) {
284-
return new Builder().name(name);
297+
public static Builder builder() {
298+
return new Builder();
285299
}
286300

287301
public static final class Builder {
288302

289-
public static final long DEFAULT_MEMORY_MB = 100;
290-
public static final int DEFAULT_CLUSTER_SHARD_COUNT = 3;
291-
public static final String[] DEFAULT_SHARD_KEY_REGEXES = new String[] { ".*\\{(?<tag>.*)\\}.*", "(?<tag>.*)" };
292-
293303
private Long uid;
294-
private String name;
304+
private String name = DEFAULT_NAME;
295305
private boolean replication;
296306
private boolean sharding;
297-
private DataSize memory = DataSize.ofMegabytes(DEFAULT_MEMORY_MB);
307+
private DataSize memory = DEFAULT_MEMORY;
298308
private Integer port;
299309
private String type;
300310
private boolean ossCluster;
@@ -333,7 +343,7 @@ public Builder memory(DataSize memory) {
333343
return this;
334344
}
335345

336-
public Builder port(int port) {
346+
public Builder port(Integer port) {
337347
this.port = port;
338348
return this;
339349
}
@@ -382,7 +392,7 @@ public Builder shardCount(int shardCount) {
382392
this.shardCount = shardCount;
383393
if (shardCount > 1) {
384394
sharding(true);
385-
shardKeyRegexes(DEFAULT_SHARD_KEY_REGEXES);
395+
shardKeyRegexes(defaultShardKeyRegexes().toArray(new String[0]));
386396
}
387397
return this;
388398
}

0 commit comments

Comments
 (0)