Skip to content

Commit ca00f25

Browse files
committed
Added installModule
1 parent 92ad1e4 commit ca00f25

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

subprojects/admin/admin.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dependencies {
2+
implementation 'commons-io:commons-io:2.11.0'
23
implementation 'org.slf4j:slf4j-api'
34
implementation 'com.fasterxml.jackson.core:jackson-databind'
45
implementation 'org.apache.httpcomponents.client5:httpclient5'

subprojects/admin/src/main/java/com/redis/enterprise/Admin.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.redis.enterprise;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.IOException;
5+
import java.io.InputStream;
46
import java.net.URI;
57
import java.net.URISyntaxException;
68
import java.security.GeneralSecurityException;
@@ -9,6 +11,7 @@
911

1012
import javax.net.ssl.SSLContext;
1113

14+
import org.apache.commons.io.IOUtils;
1215
import org.apache.hc.client5.http.HttpResponseException;
1316
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
1417
import org.apache.hc.client5.http.classic.methods.HttpDelete;
@@ -134,15 +137,20 @@ private <T> T get(String path, Class<T> type) throws ParseException, IOException
134137
}
135138

136139
private <T> T get(String path, JavaType type) throws ParseException, IOException {
137-
return read(new HttpGet(uri(path)), type, HttpStatus.SC_OK);
140+
return read(header(new HttpGet(uri(path))), type, HttpStatus.SC_OK);
138141
}
139142

140143
private <T> T delete(String path, Class<T> type) throws ParseException, IOException {
141144
return delete(path, SimpleType.constructUnsafe(type));
142145
}
143146

144147
private <T> T delete(String path, JavaType type) throws ParseException, IOException {
145-
return read(new HttpDelete(uri(path)), type, HttpStatus.SC_OK);
148+
return read(header(new HttpDelete(uri(path))), type, HttpStatus.SC_OK);
149+
}
150+
151+
private ClassicHttpRequest header(ClassicHttpRequest request) {
152+
request.setHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE_JSON);
153+
return request;
146154
}
147155

148156
private <T> T post(String path, Object request, Class<T> responseType) throws ParseException, IOException {
@@ -153,15 +161,10 @@ private <T> T post(String path, Object request, JavaType responseType) throws Pa
153161
HttpPost post = new HttpPost(uri(path));
154162
String json = objectMapper.writeValueAsString(request);
155163
post.setEntity(new StringEntity(json));
156-
return read(post, responseType, HttpStatus.SC_OK);
157-
}
158-
159-
private <T> T read(ClassicHttpRequest request, Class<T> type, int successCode) throws ParseException, IOException {
160-
return read(request, SimpleType.constructUnsafe(type), successCode);
164+
return read(header(post), responseType, HttpStatus.SC_OK);
161165
}
162166

163167
private <T> T read(ClassicHttpRequest request, JavaType type, int successCode) throws IOException, ParseException {
164-
request.setHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE_JSON);
165168
HttpHost target = new HttpHost(protocol, host, port);
166169
HttpClientContext localContext = HttpClientContext.create();
167170
if (credentials != null) {
@@ -170,9 +173,11 @@ private <T> T read(ClassicHttpRequest request, JavaType type, int successCode) t
170173
localContext.resetAuthExchange(target, basicAuth);
171174
}
172175
CloseableHttpResponse response = client.execute(request, localContext);
176+
String json = EntityUtils.toString(response.getEntity());
173177
if (response.getCode() == successCode) {
174-
return objectMapper.readValue(EntityUtils.toString(response.getEntity()), type);
178+
return objectMapper.readValue(json, type);
175179
}
180+
log.error("Error: {}", json);
176181
throw new HttpResponseException(response.getCode(), response.getReasonPhrase());
177182
}
178183

@@ -217,13 +222,18 @@ public void deleteDatabase(long uid) {
217222
});
218223
}
219224

220-
public ModuleInstallResponse installModule(String filename, byte[] bytes) throws ParseException, IOException {
225+
public ModuleInstallResponse installModule(String filename, InputStream inputStream)
226+
throws ParseException, IOException {
221227
HttpPost post = new HttpPost(uri(v2(MODULES)));
222228
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
223229
builder.setMode(HttpMultipartMode.STRICT);
224-
builder.addPart("module", new ByteArrayBody(bytes, ContentType.MULTIPART_FORM_DATA, filename));
230+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
231+
IOUtils.copy(inputStream, baos);
232+
builder.addPart("module", new ByteArrayBody(baos.toByteArray(), ContentType.MULTIPART_FORM_DATA, filename));
225233
post.setEntity(builder.build());
226-
ModuleInstallResponse response = read(post, ModuleInstallResponse.class, HttpStatus.SC_ACCEPTED);
234+
ModuleInstallResponse response = read(post, SimpleType.constructUnsafe(ModuleInstallResponse.class),
235+
HttpStatus.SC_ACCEPTED);
236+
baos.close();
227237
Awaitility.await().timeout(Duration.ofSeconds(30)).pollInterval(Duration.ofSeconds(3)).until(() -> {
228238
log.info("Checking status of action {}", response.getActionUid());
229239
Action status = getAction(response.getActionUid());

subprojects/admin/src/test/java/com/redis/enterprise/AdminTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.redis.enterprise;
22

3-
import java.io.ByteArrayOutputStream;
43
import java.io.IOException;
54
import java.io.InputStream;
65
import java.security.GeneralSecurityException;
@@ -21,9 +20,9 @@
2120
import org.slf4j.LoggerFactory;
2221
import org.testcontainers.junit.jupiter.Container;
2322
import org.testcontainers.junit.jupiter.Testcontainers;
24-
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;
2523

2624
import com.redis.enterprise.rest.Database;
25+
import com.redis.enterprise.rest.ModuleInstallResponse;
2726
import com.redis.testcontainers.RedisEnterpriseContainer;
2827
import com.redis.testcontainers.RedisEnterpriseContainer.RedisModule;
2928

@@ -85,10 +84,9 @@ void deleteDatabase() throws ParseException, GeneralSecurityException, IOExcepti
8584
void installModule() throws IOException, ParseException {
8685
String gearsModuleFile = "redisgears.linux-bionic-x64.1.0.6.zip";
8786
try (InputStream zipInputStream = getClass().getClassLoader().getResourceAsStream(gearsModuleFile)) {
88-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
89-
IOUtils.copy(zipInputStream, baos);
9087
log.info("Installing module {}", gearsModuleFile);
91-
admin.installModule(gearsModuleFile, baos.toByteArray()).getActionUid();
88+
ModuleInstallResponse response = admin.installModule(gearsModuleFile, zipInputStream);
89+
log.info("Installed module {}, action ID: {}", gearsModuleFile, response.getActionUid());
9290
Assertions.assertTrue(
9391
admin.getModules().stream().anyMatch(m -> m.getName().equals(RedisModule.GEARS.getName())));
9492
}

0 commit comments

Comments
 (0)