Skip to content
Snippets Groups Projects
Commit 293e19c3 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-5400

parent fc5ac56d
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ import java.util.Objects;
import java.util.Optional;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
......@@ -42,4 +43,7 @@ public class KeycloakGenericRemoteService {
.stream().filter(role -> Objects.equals(roleName, role.getName()))
.findFirst();
}
public Optional<RealmResource> getRealm(String realm) {
return Optional.ofNullable(keycloak.realm(realm)).findFirst();
}
}
......@@ -51,6 +51,7 @@ public class KeycloakRealmReconciler implements Reconciler<OzgCloudKeycloakRealm
var realmName = resource.getMetadata().getNamespace();
service.createRealm(resource.getSpec(), realmName);
service.createOrUpdateClient(resource.getSpec(), resource.getMetadata().getNamespace());
resource.setStatus(OzgCloudKeycloakRealmStatus.builder().status(OzgCloudCustomResourceStatus.OK).message(null).build());
return UpdateControl.updateStatus(resource);
......
......@@ -27,6 +27,7 @@ import java.util.Objects;
import java.util.Optional;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.springframework.stereotype.Component;
......@@ -47,4 +48,6 @@ class KeycloakRealmRemoteService {
public void deleteRealm(String realmName) {
keycloak.realm(realmName).remove();
}
}
......@@ -49,11 +49,36 @@ class KeycloakRealmService {
.ifPresent(remoteService::createRealm);
}
void createOrUpdateRealm(OzgCloudKeycloakRealmSpec spec, String realmName) {
keycloakGenericRemoteService.getRealm(realmName)
.ifPresentOrElse(existingRealm -> updateRealm(existingRealm, spec, namespace),
() -> createRealm(spec, namespace));
}
void createOrUpdateClient(OzgCloudKeycloakClientSpec spec, String namespace) {
genericRemoteService.getByClientId(spec.getClientId(), namespace)
.ifPresentOrElse(existingClient -> updateClient(existingClient, spec, namespace),
() -> createClient(spec, namespace));
}
void updateClient(ClientRepresentation existingClient, OzgCloudKeycloakClientSpec spec, String realm) {
var clientRepresentation = mapper.update(existingClient, spec);
setProtocolMapper(clientRepresentation);
remoteService.updateClient(clientRepresentation, realm);
addOrUpdateClientRoles(spec, realm, existingClient.getId());
}
RealmRepresentation addRealmName(RealmRepresentation realm, String realmName) {
realm.setRealm(realmName);
return realm;
}
String getRealmName(RealmRepresentation realm) {
return realm.getRealm();
}
public void deleteRealm(String realmName) {
remoteService.deleteRealm(realmName);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment