Skip to content

Commit 2c534e1

Browse files
committed
feat: #274 Open the createSecret() API for after container startup
Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com>
1 parent 1fe3c74 commit 2c534e1

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

src/main/java/io/github/microcks/testcontainers/MicrocksContainer.java

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,54 @@ public String getGrpcMockEndpoint() {
375375
return String.format("grpc://%s:%s", getHost(), getMappedPort(MICROCKS_GRPC_PORT));
376376
}
377377

378+
/**
379+
* Create a Secret mithin the MicrocksContainer.
380+
* @param secret The description of a secret to access remote Git repository, test endpoint or broker.
381+
* @throws SecretCreationException If secret cannot be correctly created in container.
382+
*/
383+
public void createSecret( Secret secret) throws SecretCreationException {
384+
createSecret(getHttpEndpoint(), secret);
385+
}
386+
387+
/**
388+
* Create a Secret mithin the MicrocksContainer.
389+
* @param microcksContainerHttpEndpoint The Http endpoint where to reach running MicrocksContainer
390+
* @param secret The description of a secret to access remote Git repository, test endpoint or broker.
391+
* @throws SecretCreationException If secret cannot be correctly created in container.
392+
*/
393+
public static void createSecret(String microcksContainerHttpEndpoint, Secret secret) throws SecretCreationException {
394+
try {
395+
URL url = new URL(microcksContainerHttpEndpoint + "/api/secrets");
396+
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
397+
httpConn.setRequestMethod("POST");
398+
httpConn.setRequestProperty(HTTP_CONTENT_TYPE, APPLICATION_JSON);
399+
httpConn.setRequestProperty(HTTP_ACCEPT, APPLICATION_JSON);
400+
httpConn.setDoOutput(true);
401+
402+
String requestBody = getMapper().writeValueAsString(secret);
403+
404+
try (OutputStream os = httpConn.getOutputStream()) {
405+
byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
406+
os.write(input, 0, input.length);
407+
os.flush();
408+
}
409+
410+
if (httpConn.getResponseCode() != 201) {
411+
// Read response content for diagnostic purpose and disconnect.
412+
StringBuilder responseContent = getResponseContent(httpConn);
413+
httpConn.disconnect();
414+
415+
log.error("Secret has not been correctly created: {}", responseContent);
416+
throw new MicrocksException("Secret has not been correctly created: " + responseContent);
417+
}
418+
// Disconnect Http connection.
419+
httpConn.disconnect();
420+
} catch (Exception e) {
421+
log.warn("Error while creating Secret: {}", secret.getName());
422+
throw new SecretCreationException("Error while creating Secret", e);
423+
}
424+
}
425+
378426
/**
379427
* Import an artifact as a primary or main one within the Microcks container.
380428
* @param artifact The file representing artifact (OpenAPI, Postman collection, Protobuf, GraphQL schema, ...)
@@ -917,39 +965,6 @@ private void importSnapshot(String snapshotPath) {
917965
}
918966
}
919967

920-
private void createSecret(Secret secret) {
921-
try {
922-
URL url = new URL(getHttpEndpoint() + "/api/secrets");
923-
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
924-
httpConn.setRequestMethod("POST");
925-
httpConn.setRequestProperty(HTTP_CONTENT_TYPE, APPLICATION_JSON);
926-
httpConn.setRequestProperty(HTTP_ACCEPT, APPLICATION_JSON);
927-
httpConn.setDoOutput(true);
928-
929-
String requestBody = getMapper().writeValueAsString(secret);
930-
931-
try (OutputStream os = httpConn.getOutputStream()) {
932-
byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
933-
os.write(input, 0, input.length);
934-
os.flush();
935-
}
936-
937-
if (httpConn.getResponseCode() != 201) {
938-
// Read response content for diagnostic purpose and disconnect.
939-
StringBuilder responseContent = getResponseContent(httpConn);
940-
httpConn.disconnect();
941-
942-
log.error("Secret has not been correctly created: {}", responseContent);
943-
throw new MicrocksException("Secret has not been correctly created: " + responseContent);
944-
}
945-
// Disconnect Http connection.
946-
httpConn.disconnect();
947-
} catch (Exception e) {
948-
log.warn("Error while creating Secret: {}", secret.getName());
949-
throw new SecretCreationException("Error while creating Secret", e);
950-
}
951-
}
952-
953968
private static ObjectMapper getMapper() {
954969
if (mapper == null) {
955970
mapper = new ObjectMapper();

0 commit comments

Comments
 (0)