Skip to content

Commit

Permalink
feat: extend error message for create client request (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras authored Aug 22, 2024
1 parent d4a0730 commit f5a22ca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,24 @@ public UpdateControl<KeycloakClient> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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;
}
}

}

0 comments on commit f5a22ca

Please sign in to comment.