diff --git a/src/main/java/org/tkit/onecx/iam/kc/client/operator/KeycloakClientController.java b/src/main/java/org/tkit/onecx/iam/kc/client/operator/KeycloakClientController.java index 433133f..39002f2 100644 --- a/src/main/java/org/tkit/onecx/iam/kc/client/operator/KeycloakClientController.java +++ b/src/main/java/org/tkit/onecx/iam/kc/client/operator/KeycloakClientController.java @@ -87,23 +87,24 @@ public UpdateControl reconcile(KeycloakClient keycloakClient, Co keycloakClient.getSpec().getKcConfig().setPassword(new String(password)); } - int responseCode = service.createClient(keycloakClient); + var response = service.createClient(keycloakClient); - updateStatusPojo(keycloakClient, responseCode); + updateStatusPojo(keycloakClient, response); log.info("Resource '{}' reconciled - updating status", keycloakClient.getMetadata().getName()); return UpdateControl.updateStatus(keycloakClient); } - private void updateStatusPojo(KeycloakClient keycloakClient, int responseCode) { + private void updateStatusPojo(KeycloakClient keycloakClient, KeycloakAdminService.CreateClientResponse response) { KeycloakClientStatus result = new KeycloakClientStatus(); KeycloakClientSpec spec = keycloakClient.getSpec(); result.setClientId(spec.getKcConfig().getClientId()); - result.setResponseCode(responseCode); + result.setResponseCode(response.getStatusCode()); + result.setMessage(response.getMessage()); var status = KeycloakClientStatus.Status.UNDEFINED; - if (responseCode == 200) { + if (response.getStatusCode() == 200) { status = KeycloakClientStatus.Status.UPDATED; } - if (responseCode == 201) { + if (response.getStatusCode() == 201) { status = KeycloakClientStatus.Status.CREATED; } diff --git a/src/main/java/org/tkit/onecx/iam/kc/client/operator/service/KeycloakAdminService.java b/src/main/java/org/tkit/onecx/iam/kc/client/operator/service/KeycloakAdminService.java index f686553..34629bd 100644 --- a/src/main/java/org/tkit/onecx/iam/kc/client/operator/service/KeycloakAdminService.java +++ b/src/main/java/org/tkit/onecx/iam/kc/client/operator/service/KeycloakAdminService.java @@ -6,6 +6,7 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.control.ActivateRequestContext; import jakarta.inject.Inject; +import jakarta.ws.rs.core.Response; import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.resource.ClientResource; @@ -35,7 +36,7 @@ public class KeycloakAdminService { KCClientConfig kcClientConfig; @ActivateRequestContext - public int createClient(KeycloakClient keycloakClient) { + public CreateClientResponse createClient(KeycloakClient keycloakClient) { var spec = keycloakClient.getSpec(); var clientId = spec.getKcConfig().getClientId(); var realm = spec.getRealm() != null ? spec.getRealm() : kcClientConfig.realm(); @@ -72,7 +73,7 @@ public int createClient(KeycloakClient keycloakClient) { if (clients.isEmpty()) { // do create try (var resp = keycloak.realm(realm).clients().create(client)) { - return resp.getStatus(); + return CreateClientResponse.of(resp.getStatus(), resp.readEntity(String.class)); } } else { // do update @@ -101,7 +102,7 @@ public int createClient(KeycloakClient keycloakClient) { toRemoveOpt.forEach(scope -> removeOptClientScope(clientToUpdate, scope)); toAddOpt.forEach(scope -> addOptClientScope(clientToUpdate, scope)); - return 200; + return CreateClientResponse.of(Response.Status.OK.getStatusCode()); } } @@ -258,4 +259,30 @@ private String resolveValue(String value, String defaultValue) { return defaultValue; } + public static class CreateClientResponse { + private final int statusCode; + private final String message; + + public static CreateClientResponse of(int statusCode) { + return of(statusCode, null); + } + + public static CreateClientResponse of(int statusCode, String message) { + return new CreateClientResponse(statusCode, message); + } + + private CreateClientResponse(int statusCode, String message) { + this.message = message; + this.statusCode = statusCode; + } + + public int getStatusCode() { + return statusCode; + } + + public String getMessage() { + return message; + } + } + }