Skip to content

Commit

Permalink
force rename if needed - e.g resource object was changed (icf_name, n…
Browse files Browse the repository at this point in the history
…ameing attribute) and these changes were not synced to the midpoint..
  • Loading branch information
katkav committed Oct 10, 2014
1 parent 204bf42 commit ec5b05b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 76 deletions.
Expand Up @@ -293,9 +293,12 @@ public PrismObject<ShadowType> getShadow(String oid, PrismObject<ShadowType> rep
LOGGER.trace("Resource object fetched from resource:\n{}", resourceShadow.debugDump());
}

forceRenameIfNeeded(resourceShadow.asObjectable(), repositoryShadow.asObjectable(), objectClassDefinition, parentResult);
// Complete the shadow by adding attributes from the resource object
PrismObject<ShadowType> resultShadow = completeShadow(connector, resourceShadow, repositoryShadow, resource, objectClassDefinition, parentResult);



if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Shadow when assembled:\n{}", resultShadow.debugDump());
}
Expand Down Expand Up @@ -1267,75 +1270,49 @@ private void forceRenameIfNeeded(ShadowType currentShadowType, ShadowType oldSha
if (oldSecondaryIdentifiers.isEmpty()){
return;
}

// if (oldSecondaryIdentifiers.size() > 1){
// return;
// }
LOGGER.info("force rename if needed");
ResourceAttributeContainer newSecondaryIdentifiers = ShadowUtil.getAttributesContainer(currentShadowType);

//remember name before normalizing attributes
PolyString currentShadowName = ProvisioningUtil.determineShadowName(currentShadowType);
currentShadowType.setName(new PolyStringType(currentShadowName));

Iterator<ResourceAttribute<?>> oldSecondaryIterator = oldSecondaryIdentifiers.iterator();
Collection<PropertyDelta> renameDeltas = new ArrayList<PropertyDelta>();
while (oldSecondaryIterator.hasNext()){
ResourceAttribute<?> oldSecondaryIdentifier = oldSecondaryIterator.next();
LOGGER.info("old identifier: {}", oldSecondaryIdentifier);
ResourceAttribute newSecondaryIdentifier = newSecondaryIdentifiers.findAttribute(oldSecondaryIdentifier.getElementName());
LOGGER.info("new identifier: {}", newSecondaryIdentifier);
Collection newValue = newSecondaryIdentifier.getRealValues();

if (!shadowManager.compareAttribute(refinedObjectClassDefinition, newSecondaryIdentifier, oldSecondaryIdentifier)){
PropertyDelta<?> shadowNameDelta = PropertyDelta.createDelta(new ItemPath(ShadowType.F_ATTRIBUTES, oldSecondaryIdentifier.getElementName()), oldShadowType.asPrismObject().getDefinition());
shadowNameDelta.addValuesToDelete(PrismPropertyValue.cloneCollection((Collection)oldSecondaryIdentifier.getValues()));
shadowManager.normalizeAttributes(currentShadowType.asPrismObject(), refinedObjectClassDefinition);
shadowNameDelta.addValuesToAdd(PrismPropertyValue.cloneCollection((Collection)newSecondaryIdentifier.getValues()));
// PropertyDelta<?> shadowNameDelta = PropertyDelta.createModificationReplaceProperty(new ItemPath(ShadowType.F_ATTRIBUTES, oldSecondaryIdentifier.getElementName()), oldShadowType.asPrismObject().getDefinition(), newValue.toArray());
renameDeltas.add(shadowNameDelta);


}

}
LOGGER.info("rename delta: {}", renameDeltas);

if (!renameDeltas.isEmpty()){

PropertyDelta<?> shadowNameDelta = PropertyDelta.createModificationReplaceProperty(ShadowType.F_NAME,
oldShadowType.asPrismObject().getDefinition(),
ProvisioningUtil.determineShadowName(currentShadowType.asPrismObject()));
oldShadowType.asPrismObject().getDefinition(),currentShadowName);
renameDeltas.add(shadowNameDelta);
} else {

if (!oldShadowType.getName().getOrig().equals(currentShadowType.getName().getOrig())){
PropertyDelta<?> shadowNameDelta = PropertyDelta.createModificationReplaceProperty(ShadowType.F_NAME,
oldShadowType.asPrismObject().getDefinition(), currentShadowName);
renameDeltas.add(shadowNameDelta);

}
}
if (!renameDeltas.isEmpty()){
repositoryService.modifyObject(ShadowType.class, oldShadowType.getOid(), renameDeltas, parentResult);
oldShadowType.setName(new PolyStringType(currentShadowName));
}



// ResourceAttribute<?> oldSecondaryIdentifier = oldSecondaryIdentifiers.iterator().next();
// Object oldValue = oldSecondaryIdentifier.getRealValue();
//
// Collection<ResourceAttribute<?>> newSecondaryIdentifiers = ShadowUtil.getSecondaryIdentifiers(currentShadowType);
// if (newSecondaryIdentifiers.isEmpty()){
// return;
// }
//
// if (newSecondaryIdentifiers.size() > 1){
// return;
// }
//
// ResourceAttribute newSecondaryIdentifier = newSecondaryIdentifiers.iterator().next();
// Object newValue = newSecondaryIdentifier.getRealValue();
//
// if (!shadowManager.compareAttribute(refinedObjectClassDefinition, newSecondaryIdentifier, oldSecondaryIdentifier)){
// Collection<PropertyDelta> renameDeltas = new ArrayList<PropertyDelta>();
//
//
// PropertyDelta<?> shadowNameDelta = PropertyDelta.createModificationReplaceProperty(ShadowType.F_NAME,
// oldShadowType.asPrismObject().getDefinition(),
// ProvisioningUtil.determineShadowName(currentShadowType.asPrismObject()));
// renameDeltas.add(shadowNameDelta);
//
// shadowNameDelta = PropertyDelta.createModificationReplaceProperty(new ItemPath(ShadowType.F_ATTRIBUTES, ConnectorFactoryIcfImpl.ICFS_NAME), oldShadowType.asPrismObject().getDefinition(), newValue);
// renameDeltas.add(shadowNameDelta);
//
// repositoryService.modifyObject(ShadowType.class, oldShadowType.getOid(), renameDeltas, parentResult);
// }


}

public PrismProperty<?> fetchCurrentToken(ResourceType resourceType, OperationResult parentResult)
Expand Down
Expand Up @@ -458,30 +458,22 @@ private ObjectQuery createSearchShadowQuery(Collection<ResourceAttribute<?>> ide
private ObjectQuery createSearchShadowQuery(PrismObject<ShadowType> resourceShadow,
RefinedObjectClassDefinition rObjClassDef, ResourceType resource,
PrismContext prismContext, OperationResult parentResult) throws SchemaException {
// XPathHolder xpath = createXpathHolder();
ResourceAttributeContainer attributesContainer = ShadowUtil
.getAttributesContainer(resourceShadow);
PrismProperty identifier = attributesContainer.getIdentifier();

Collection<PrismPropertyValue<Object>> idValues = identifier.getValues();
// Only one value is supported for an identifier
if (idValues.size() > 1) {
// LOGGER.error("More than one identifier value is not supported");
// TODO: This should probably be switched to checked exception later
throw new IllegalArgumentException("More than one identifier value is not supported");
}
if (idValues.size() < 1) {
// LOGGER.error("The identifier has no value");
// TODO: This should probably be switched to checked exception later
throw new IllegalArgumentException("The identifier has no value");
}

// We have all the data, we can construct the filter now
// Document doc = DOMUtil.getDocument();
// Element filter;
// List<Element> identifierElements =
// prismContext.getPrismDomProcessor().serializeItemToDom(identifier,
// doc);
ObjectFilter filter = null;
try {
// TODO TODO TODO TODO: set matching rule instead of null
Expand All @@ -490,15 +482,11 @@ private ObjectQuery createSearchShadowQuery(PrismObject<ShadowType> resourceShad
RefFilter.createReferenceEqual(ShadowType.F_RESOURCE_REF, ShadowType.class, resource),
EqualFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES, def.getName()), def, getNormalizedValue(identifier, rObjClassDef)));
} catch (SchemaException e) {
// LOGGER.error("Schema error while creating search filter: {}",
// e.getMessage(), e);
throw new SchemaException("Schema error while creating search filter: " + e.getMessage(), e);
}

ObjectQuery query = ObjectQuery.createObjectQuery(filter);

// LOGGER.trace("created query " + DOMUtil.printDom(filter));

return query;
}

Expand Down
Expand Up @@ -532,10 +532,39 @@ public void test120AddObject() throws Exception {
PrismAsserts.assertEqualsPolyString("Name not equal.", "uid=will,ou=People,dc=example,dc=com", provisioningAccountType.getName());
}

@Test
public void test121renameAccountWillOnResource() throws Exception{
String TEST_NAME = "test500renameAccountOnResource";
TestUtil.displayTestTile(TEST_NAME);
Task task = taskManager.createTaskInstance(TEST_NAME);
OperationResult result = task.getResult();

openDJController.executeRenameChange(TEST_DIR_NAME + "/rename.ldif");

SearchResultEntry entry = openDJController.fetchEntry("uid=will123,ou=People,dc=example,dc=com");
assertNotNull("Entry with dn uid=will123,ou=People,dc=example,dc=com does not exist", entry);

ShadowType repoShadowType = repositoryService.getObject(ShadowType.class, ACCOUNT_NEW_OID,
null, result).asObjectable();
PrismAsserts.assertEqualsPolyString("Name not equal (repo)", "uid=will,ou=People,dc=example,dc=com", repoShadowType.getName());
assertAttribute(repoShadowType, ConnectorFactoryIcfImpl.ICFS_NAME, StringUtils.lowerCase(ACCOUNT_NEW_DN));

ShadowType provisioningAccountType = provisioningService.getObject(ShadowType.class, ACCOUNT_NEW_OID,
null, task, result).asObjectable();
PrismAsserts.assertEqualsPolyString("Name not equal.", "uid=will123,ou=People,dc=example,dc=com", provisioningAccountType.getName());
assertAttribute(provisioningAccountType, ConnectorFactoryIcfImpl.ICFS_NAME, "uid=will123,ou=people,dc=example,dc=com");

repoShadowType = repositoryService.getObject(ShadowType.class, ACCOUNT_NEW_OID,
null, result).asObjectable();
PrismAsserts.assertEqualsPolyString("Name not equal (repo after provisioning)", "uid=will123,ou=People,dc=example,dc=com", repoShadowType.getName());
assertAttribute(repoShadowType, ConnectorFactoryIcfImpl.ICFS_NAME, "uid=will123,ou=people,dc=example,dc=com");


}

@Test
public void test121AddObjectNull() throws Exception {
final String TEST_NAME = "test121AddObjectNull";
public void test125AddObjectNull() throws Exception {
final String TEST_NAME = "test125AddObjectNull";
TestUtil.displayTestTile(TEST_NAME);

OperationResult result = new OperationResult(TestOpenDJ.class.getName()
Expand Down Expand Up @@ -641,18 +670,6 @@ public void test140ModifyObject() throws Exception {
display("Object after change",accountType);


// String changedSn = null;
// String uid = null;
// for (Object e : accountType.getAttributes().getAny()) {
// if ("sn".equals(JAXBUtil.getElementQName(e).getLocalPart())) {
// changedSn = ((Element)e).getTextContent();
// }
// if (ConnectorFactoryIcfImpl.ICFS_UID.equals(JAXBUtil.getElementQName(e))) {
// uid = ((Element)e).getTextContent();
// }
//
// }

String uid = ShadowUtil.getSingleStringAttributeValue(accountType, ConnectorFactoryIcfImpl.ICFS_UID);
List<Object> snValues = ShadowUtil.getAttributeValues(accountType, new QName(RESOURCE_NS, "sn"));
assertNotNull(snValues);
Expand Down Expand Up @@ -1015,7 +1032,7 @@ public void test182GetLockedAccount() throws Exception {
Task task = taskManager.createTaskInstance();

openDJController.executeLdifChange(
"dn: uid=will,ou=People,dc=example,dc=com\n" +
"dn: uid=will123,ou=People,dc=example,dc=com\n" +
"changetype: modify\n" +
"replace: pager\n" +
"pager: 1"
Expand Down Expand Up @@ -1298,7 +1315,7 @@ public void test300AddObjectObjectAlreadyExistResource() throws Exception{
OperationResult result = new OperationResult(TestOpenDJ.class.getName()
+ ".test300AddObjectObjectAlreadyExist");

PrismObject<ShadowType> account = PrismTestUtil.parseObject(new File(ACCOUNT_NEW_FILENAME));
PrismObject<ShadowType> account = PrismTestUtil.parseObject(new File(ACCOUNT_SEARCH_FILENAME));
display("Account to add", account);

try {
Expand Down Expand Up @@ -1431,4 +1448,6 @@ public void test404ConfiguredCapabilityNoUpdate() throws Exception{
}
}



}
Expand Up @@ -21,12 +21,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:icfs="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3"
xmlns:ri="http://midpoint.evolveum.com/xml/ns/public/resource/instance/ef2bc95b-76e0-59e2-86d6-3d4f02d3ffff">
<c:name>uid=will123,ou=People,dc=example,dc=com</c:name>
<c:name>uid=will12345,ou=People,dc=example,dc=com</c:name>
<c:resourceRef oid="ef2bc95b-76e0-59e2-86d6-3d4f02d3ffff" />
<c:objectClass>ri:AccountObjectClass</c:objectClass>
<c:attributes>
<icfs:name>uid=will123,ou=People,dc=example,dc=com</icfs:name>
<ri:uid>will123</ri:uid>
<icfs:name>uid=will12345,ou=People,dc=example,dc=com</icfs:name>
<ri:uid>will12345</ri:uid>
<ri:cn>Will Turner</ri:cn>
<ri:givenName>Will</ri:givenName>
<ri:sn>Turner</ri:sn>
Expand Down
@@ -0,0 +1,4 @@
dn: uid=will,ou=People,dc=example,dc=com
changetype: modrdn
newrdn: uid=will123
deleteoldrdn: true

0 comments on commit ec5b05b

Please sign in to comment.