Add id to partial import results.

This commit is contained in:
Stan Silvert 2015-12-09 12:38:14 -05:00
parent f6a02bd408
commit dd038ddbd5
8 changed files with 73 additions and 25 deletions

View file

@ -92,6 +92,7 @@
<th>Action</th> <th>Action</th>
<th>Type</th> <th>Type</th>
<th>Name</th> <th>Name</th>
<th>Id</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -99,6 +100,7 @@
<td>{{result.action}}</td> <td>{{result.action}}</td>
<td>{{result.resourceType}}</td> <td>{{result.resourceType}}</td>
<td>{{result.resourceName}}</td> <td>{{result.resourceName}}</td>
<td>{{result.id}}</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View file

@ -34,6 +34,7 @@ public abstract class AbstractPartialImport<T> implements PartialImport {
public abstract List<T> getRepList(PartialImportRepresentation partialImportRep); public abstract List<T> getRepList(PartialImportRepresentation partialImportRep);
public abstract String getName(T resourceRep); public abstract String getName(T resourceRep);
public abstract String getModelId(RealmModel realm, KeycloakSession session, T resourceRep);
public abstract boolean exists(RealmModel realm, KeycloakSession session, T resourceRep); public abstract boolean exists(RealmModel realm, KeycloakSession session, T resourceRep);
public abstract String existsMessage(T resourceRep); public abstract String existsMessage(T resourceRep);
public abstract ResourceType getResourceType(); public abstract ResourceType getResourceType();
@ -61,16 +62,16 @@ public abstract class AbstractPartialImport<T> implements PartialImport {
return new ErrorResponseException(error); return new ErrorResponseException(error);
} }
protected PartialImportResult overwritten(T resourceRep){ protected PartialImportResult overwritten(String modelId, T resourceRep){
return PartialImportResult.overwritten(getResourceType(), getName(resourceRep), resourceRep); return PartialImportResult.overwritten(getResourceType(), getName(resourceRep), modelId, resourceRep);
} }
protected PartialImportResult skipped(T resourceRep) { protected PartialImportResult skipped(String modelId, T resourceRep) {
return PartialImportResult.skipped(getResourceType(), getName(resourceRep), resourceRep); return PartialImportResult.skipped(getResourceType(), getName(resourceRep), modelId, resourceRep);
} }
protected PartialImportResult added(T resourceRep) { protected PartialImportResult added(String modelId, T resourceRep) {
return PartialImportResult.added(getResourceType(), getName(resourceRep), resourceRep); return PartialImportResult.added(getResourceType(), getName(resourceRep), modelId, resourceRep);
} }
@Override @Override
@ -91,12 +92,14 @@ public abstract class AbstractPartialImport<T> implements PartialImport {
throw new ErrorResponseException(ErrorResponse.error(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR)); throw new ErrorResponseException(ErrorResponse.error(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR));
} }
results.addResult(overwritten(resourceRep)); String modelId = getModelId(realm, session, resourceRep);
results.addResult(overwritten(modelId, resourceRep));
} }
for (T resourceRep : toSkip) { for (T resourceRep : toSkip) {
System.out.println("skipping " + getResourceType() + " " + getName(resourceRep)); System.out.println("skipping " + getResourceType() + " " + getName(resourceRep));
results.addResult(skipped(resourceRep)); String modelId = getModelId(realm, session, resourceRep);
results.addResult(skipped(modelId, resourceRep));
} }
for (T resourceRep : repList) { for (T resourceRep : repList) {
@ -106,7 +109,8 @@ public abstract class AbstractPartialImport<T> implements PartialImport {
try { try {
System.out.println("adding " + getResourceType() + " " + getName(resourceRep)); System.out.println("adding " + getResourceType() + " " + getName(resourceRep));
create(realm, session, resourceRep); create(realm, session, resourceRep);
results.addResult(added(resourceRep)); String modelId = getModelId(realm, session, resourceRep);
results.addResult(added(modelId, resourceRep));
} catch (Exception e) { } catch (Exception e) {
//e.printStackTrace(); //e.printStackTrace();
throw new ErrorResponseException(ErrorResponse.error(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR)); throw new ErrorResponseException(ErrorResponse.error(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR));

View file

@ -135,16 +135,16 @@ public class ClientRolesPartialImport implements PartialImport {
return new ErrorResponseException(error); return new ErrorResponseException(error);
} }
protected PartialImportResult overwritten(String clientId, RoleRepresentation roleRep) { protected PartialImportResult overwritten(String clientId, String modelId, RoleRepresentation roleRep) {
return PartialImportResult.overwritten(getResourceType(), getCombinedName(clientId, roleRep), roleRep); return PartialImportResult.overwritten(getResourceType(), getCombinedName(clientId, roleRep), modelId, roleRep);
} }
protected PartialImportResult skipped(String clientId, RoleRepresentation roleRep) { protected PartialImportResult skipped(String clientId, String modelId, RoleRepresentation roleRep) {
return PartialImportResult.skipped(getResourceType(), getCombinedName(clientId, roleRep), roleRep); return PartialImportResult.skipped(getResourceType(), getCombinedName(clientId, roleRep), modelId, roleRep);
} }
protected PartialImportResult added(String clientId, RoleRepresentation roleRep) { protected PartialImportResult added(String clientId, String modelId, RoleRepresentation roleRep) {
return PartialImportResult.added(getResourceType(), getCombinedName(clientId, roleRep), roleRep); return PartialImportResult.added(getResourceType(), getCombinedName(clientId, roleRep), modelId, roleRep);
} }
@Override @Override
@ -166,14 +166,16 @@ public class ClientRolesPartialImport implements PartialImport {
throw new ErrorResponseException(ErrorResponse.error(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR)); throw new ErrorResponseException(ErrorResponse.error(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR));
} }
results.addResult(overwritten(clientId, roleRep)); String modelId = getModelId(realm, clientId);
results.addResult(overwritten(clientId, modelId, roleRep));
} }
} }
for (String clientId : toSkip.keySet()) { for (String clientId : toSkip.keySet()) {
for (RoleRepresentation roleRep : toSkip.get(clientId)) { for (RoleRepresentation roleRep : toSkip.get(clientId)) {
System.out.println("skipping " + getResourceType() + " " + getCombinedName(clientId, roleRep)); System.out.println("skipping " + getResourceType() + " " + getCombinedName(clientId, roleRep));
results.addResult(skipped(clientId, roleRep)); String modelId = getModelId(realm, clientId);
results.addResult(skipped(clientId, modelId, roleRep));
} }
} }
@ -185,7 +187,8 @@ public class ClientRolesPartialImport implements PartialImport {
try { try {
System.out.println("adding " + getResourceType() + " " + getCombinedName(clientId, roleRep)); System.out.println("adding " + getResourceType() + " " + getCombinedName(clientId, roleRep));
create(realm, session, clientId, roleRep); create(realm, session, clientId, roleRep);
results.addResult(added(clientId, roleRep)); String modelId = getModelId(realm, clientId);
results.addResult(added(clientId, modelId, roleRep));
} catch (Exception e) { } catch (Exception e) {
//e.printStackTrace(); //e.printStackTrace();
throw new ErrorResponseException(ErrorResponse.error(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR)); throw new ErrorResponseException(ErrorResponse.error(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR));
@ -196,4 +199,7 @@ public class ClientRolesPartialImport implements PartialImport {
return results; return results;
} }
private String getModelId(RealmModel realm, String clientId) {
return realm.getClientByClientId(clientId).getId();
}
} }

View file

@ -42,6 +42,11 @@ public class ClientsPartialImport extends AbstractPartialImport<ClientRepresenta
return clientRep.getClientId(); return clientRep.getClientId();
} }
@Override
public String getModelId(RealmModel realm, KeycloakSession session, ClientRepresentation clientRep) {
return realm.getClientByClientId(getName(clientRep)).getId();
}
@Override @Override
public boolean exists(RealmModel realm, KeycloakSession session, ClientRepresentation clientRep) { public boolean exists(RealmModel realm, KeycloakSession session, ClientRepresentation clientRep) {
return realm.getClientByClientId(getName(clientRep)) != null; return realm.getClientByClientId(getName(clientRep)) != null;

View file

@ -42,6 +42,11 @@ public class IdentityProvidersPartialImport extends AbstractPartialImport<Identi
return idpRep.getAlias(); return idpRep.getAlias();
} }
@Override
public String getModelId(RealmModel realm, KeycloakSession session, IdentityProviderRepresentation idpRep) {
return realm.getIdentityProviderByAlias(getName(idpRep)).getInternalId();
}
@Override @Override
public boolean exists(RealmModel realm, KeycloakSession session, IdentityProviderRepresentation idpRep) { public boolean exists(RealmModel realm, KeycloakSession session, IdentityProviderRepresentation idpRep) {
return realm.getIdentityProviderByAlias(getName(idpRep)) != null; return realm.getIdentityProviderByAlias(getName(idpRep)) != null;

View file

@ -28,25 +28,27 @@ public class PartialImportResult {
private final Action action; private final Action action;
private final String resourceType; private final String resourceType;
private final String resourceName; private final String resourceName;
private final String id;
private final Object representation; private final Object representation;
private PartialImportResult(Action action, ResourceType resourceType, String resourceName, Object representation) { private PartialImportResult(Action action, ResourceType resourceType, String resourceName, String id, Object representation) {
this.action = action; this.action = action;
this.resourceType = resourceType.toString(); this.resourceType = resourceType.toString();
this.resourceName = resourceName; this.resourceName = resourceName;
this.id = id;
this.representation = representation; this.representation = representation;
}; };
public static PartialImportResult skipped(ResourceType resourceType, String resourceName, Object representation) { public static PartialImportResult skipped(ResourceType resourceType, String resourceName, String id, Object representation) {
return new PartialImportResult(Action.SKIPPED, resourceType, resourceName, representation); return new PartialImportResult(Action.SKIPPED, resourceType, resourceName, id, representation);
} }
public static PartialImportResult added(ResourceType resourceType, String resourceName, Object representation) { public static PartialImportResult added(ResourceType resourceType, String resourceName, String id, Object representation) {
return new PartialImportResult(Action.ADDED, resourceType, resourceName, representation); return new PartialImportResult(Action.ADDED, resourceType, resourceName, id, representation);
} }
public static PartialImportResult overwritten(ResourceType resourceType, String resourceName, Object representation) { public static PartialImportResult overwritten(ResourceType resourceType, String resourceName, String id, Object representation) {
return new PartialImportResult(Action.OVERWRITTEN, resourceType, resourceName, representation); return new PartialImportResult(Action.OVERWRITTEN, resourceType, resourceName, id, representation);
} }
public Action getAction() { public Action getAction() {
@ -61,6 +63,10 @@ public class PartialImportResult {
return resourceName; return resourceName;
} }
public String getId() {
return id;
}
@JsonIgnore @JsonIgnore
public Object getRepresentation() { public Object getRepresentation() {
return representation; return representation;

View file

@ -43,6 +43,15 @@ public class RealmRolesPartialImport extends AbstractPartialImport<RoleRepresent
return roleRep.getName(); return roleRep.getName();
} }
@Override
public String getModelId(RealmModel realm, KeycloakSession session, RoleRepresentation roleRep) {
for (RoleModel role : realm.getRoles()) {
if (getName(roleRep).equals(role.getName())) return role.getId();
}
return null;
}
@Override @Override
public boolean exists(RealmModel realm, KeycloakSession session, RoleRepresentation roleRep) { public boolean exists(RealmModel realm, KeycloakSession session, RoleRepresentation roleRep) {
for (RoleModel role : realm.getRoles()) { for (RoleModel role : realm.getRoles()) {

View file

@ -44,6 +44,17 @@ public class UsersPartialImport extends AbstractPartialImport<UserRepresentation
return user.getUsername(); return user.getUsername();
} }
@Override
public String getModelId(RealmModel realm, KeycloakSession session, UserRepresentation user) {
String userName = user.getUsername();
if (userName != null) {
return session.users().getUserByUsername(userName, realm).getId();
} else {
String email = user.getEmail();
return session.users().getUserByEmail(email, realm).getId();
}
}
@Override @Override
public boolean exists(RealmModel realm, KeycloakSession session, UserRepresentation user) { public boolean exists(RealmModel realm, KeycloakSession session, UserRepresentation user) {
return userNameExists(realm, session, user) || userEmailExists(realm, session, user); return userNameExists(realm, session, user) || userEmailExists(realm, session, user);