Search in sources :

Example 11 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class AddExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    boolean raw = getParamRaw(expression, input, context, globalResult);
    boolean dryRun = getParamDryRun(expression, input, context, globalResult);
    for (PipelineItem item : input.getData()) {
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        PrismValue value = item.getValue();
        if (value instanceof PrismObjectValue) {
            @SuppressWarnings({ "unchecked", "raw" }) PrismObject<? extends ObjectType> prismObject = ((PrismObjectValue) value).asPrismObject();
            ObjectType objectType = prismObject.asObjectable();
            long started = operationsHelper.recordStart(context, objectType);
            Throwable exception = null;
            try {
                operationsHelper.applyDelta(createAddDelta(objectType), operationsHelper.createExecutionOptions(raw), dryRun, context, result);
                operationsHelper.recordEnd(context, objectType, started, null);
            } catch (Throwable ex) {
                operationsHelper.recordEnd(context, objectType, started, ex);
                exception = processActionException(ex, NAME, value, context);
            }
            context.println((exception != null ? "Attempted to add " : "Added ") + prismObject.toString() + rawDrySuffix(raw, dryRun) + exceptionSuffix(exception));
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return input;
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) PrismObjectValue(com.evolveum.midpoint.prism.PrismObjectValue) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PrismValue(com.evolveum.midpoint.prism.PrismValue)

Example 12 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class DiscoverConnectorsExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    boolean rebind = expressionHelper.getArgumentAsBoolean(expression.getParameter(), PARAM_REBIND_RESOURCES, input, context, false, PARAM_REBIND_RESOURCES, globalResult);
    PipelineData output = PipelineData.createEmpty();
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof ConnectorHostType) {
            PrismObject<ConnectorHostType> connectorHostTypePrismObject = ((PrismObjectValue) value).asPrismObject();
            Set<ConnectorType> newConnectors;
            long started = operationsHelper.recordStart(context, connectorHostTypePrismObject.asObjectable());
            Throwable exception = null;
            try {
                newConnectors = modelService.discoverConnectors(connectorHostTypePrismObject.asObjectable(), context.getTask(), result);
                operationsHelper.recordEnd(context, connectorHostTypePrismObject.asObjectable(), started, null);
            } catch (CommunicationException | SecurityViolationException | SchemaException | ConfigurationException | ObjectNotFoundException | RuntimeException e) {
                operationsHelper.recordEnd(context, connectorHostTypePrismObject.asObjectable(), started, e);
                exception = processActionException(e, NAME, value, context);
                newConnectors = Collections.emptySet();
            }
            context.println((exception != null ? "Attempted to discover " : "Discovered " + newConnectors.size()) + " new connector(s) from " + connectorHostTypePrismObject + exceptionSuffix(exception));
            for (ConnectorType connectorType : newConnectors) {
                output.addValue(connectorType.asPrismObject().getValue(), item.getResult());
            }
            try {
                if (rebind) {
                    rebindConnectors(newConnectors, context, result);
                }
            } catch (ScriptExecutionException e) {
                //noinspection ThrowableNotThrown
                // TODO better message
                processActionException(e, NAME, value, context);
            }
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Input item is not a PrismObject<ConnectorHost>"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    // TODO configurable output (either connector hosts or discovered connectors)
    return output;
}
Also used : ConnectorHostType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorHostType) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ConnectorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 13 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class GenerateValueExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    PolicyItemsDefinitionType itemsDefinition = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAMETER_ITEMS, false, false, PARAMETER_ITEMS, input, context, PolicyItemsDefinitionType.class, globalResult);
    if (itemsDefinition == null) {
        itemsDefinition = new PolicyItemsDefinitionType().policyItemDefinition(new PolicyItemDefinitionType().target(new PolicyItemTargetType().path(new ItemPathType(new ItemPath(UserType.F_CREDENTIALS, CredentialsType.F_PASSWORD, PasswordType.F_VALUE)))).execute(false));
    }
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismObjectValue) {
            PrismObject<? extends ObjectType> object = ((PrismObjectValue) value).asPrismObject();
            ObjectType objectBean = object.asObjectable();
            long started = operationsHelper.recordStart(context, objectBean);
            Throwable exception = null;
            try {
                LOGGER.trace("Generating value(s) for {}", objectBean);
                modelInteraction.generateValue(object, itemsDefinition, context.getTask(), result);
                operationsHelper.recordEnd(context, objectBean, started, null);
            } catch (Throwable e) {
                operationsHelper.recordEnd(context, objectBean, started, e);
                exception = processActionException(e, NAME, value, context);
            }
            context.println((exception != null ? "Attempted to generate value(s) for " : "Generated value(s) for ") + objectBean.toString() + exceptionSuffix(exception));
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return input;
}
Also used : PolicyItemsDefinitionType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.PolicyItemsDefinitionType) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) PolicyItemDefinitionType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.PolicyItemDefinitionType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PrismValue(com.evolveum.midpoint.prism.PrismValue) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) PrismObjectValue(com.evolveum.midpoint.prism.PrismObjectValue) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) PolicyItemTargetType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.PolicyItemTargetType) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 14 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class NotifyExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    String subtype = expressionHelper.getArgumentAsString(expression.getParameter(), PARAM_SUBTYPE, input, context, null, PARAM_SUBTYPE, globalResult);
    EventHandlerType handler = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_HANDLER, false, false, PARAM_HANDLER, input, context, EventHandlerType.class, globalResult);
    EventStatusType status = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_STATUS, false, false, PARAM_STATUS, input, context, EventStatusType.class, globalResult);
    EventOperationType operation = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_OPERATION, false, false, PARAM_OPERATION, input, context, EventOperationType.class, globalResult);
    boolean forWholeInput = expressionHelper.getArgumentAsBoolean(expression.getParameter(), PARAM_FOR_WHOLE_INPUT, input, context, false, PARAM_SUBTYPE, globalResult);
    if (handler != null) {
        // TODO explain that the reason is that handler is not null
        checkRootAuthorization(globalResult, NAME);
    }
    if (status == null) {
        status = EventStatusType.SUCCESS;
    }
    if (operation == null) {
        operation = EventOperationType.ADD;
    }
    if (notificationManager == null) {
        throw new IllegalStateException("Notification manager is unavailable");
    }
    int eventCount = 0;
    if (forWholeInput) {
        Event event = new CustomEvent(lightweightIdentifierGenerator, subtype, handler, input.getData(), operation, status, context.getChannel());
        notificationManager.processEvent(event, context.getTask(), globalResult);
        eventCount++;
    } else {
        for (PipelineItem item : input.getData()) {
            PrismValue value = item.getValue();
            OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
            context.checkTaskStop();
            Event event = new CustomEvent(lightweightIdentifierGenerator, subtype, handler, value, operation, status, context.getChannel());
            notificationManager.processEvent(event, context.getTask(), result);
            eventCount++;
            operationsHelper.trimAndCloneResult(result, globalResult, context);
        }
    }
    context.println("Produced " + eventCount + " event(s)");
    return input;
}
Also used : CustomEvent(com.evolveum.midpoint.notifications.api.events.CustomEvent) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) EventHandlerType(com.evolveum.midpoint.xml.ns._public.common.common_3.EventHandlerType) EventStatusType(com.evolveum.midpoint.xml.ns._public.common.common_3.EventStatusType) Event(com.evolveum.midpoint.notifications.api.events.Event) CustomEvent(com.evolveum.midpoint.notifications.api.events.CustomEvent) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) EventOperationType(com.evolveum.midpoint.xml.ns._public.common.common_3.EventOperationType) PrismValue(com.evolveum.midpoint.prism.PrismValue)

Example 15 with ActionExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType in project midpoint by Evolveum.

the class TestResourceExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    PipelineData output = PipelineData.createEmpty();
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof ResourceType) {
            PrismObject<ResourceType> resourceTypePrismObject = ((PrismObjectValue) value).asPrismObject();
            ResourceType resourceType = resourceTypePrismObject.asObjectable();
            long started = operationsHelper.recordStart(context, resourceType);
            Throwable exception = null;
            OperationResult testResult;
            try {
                testResult = modelService.testResource(resourceTypePrismObject.getOid(), context.getTask());
                operationsHelper.recordEnd(context, resourceType, started, null);
            } catch (ObjectNotFoundException | RuntimeException e) {
                operationsHelper.recordEnd(context, resourceType, started, e);
                exception = processActionException(e, NAME, value, context);
                testResult = new OperationResult(TestResourceExecutor.class.getName() + ".testResource");
                testResult.recordFatalError(e);
            }
            result.addSubresult(testResult);
            context.println("Tested " + resourceTypePrismObject + ": " + testResult.getStatus() + exceptionSuffix(exception));
            try {
                PrismObjectValue<ResourceType> resourceValue = operationsHelper.getObject(ResourceType.class, resourceTypePrismObject.getOid(), false, context, result).getValue();
                output.add(new PipelineItem(resourceValue, item.getResult()));
            } catch (ExpressionEvaluationException e) {
                throw new ScriptExecutionException("Error getting resource " + resourceTypePrismObject.getOid() + ": " + e.getMessage(), e);
            }
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject<ResourceType>"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return output;
}
Also used : ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Aggregations

PipelineItem (com.evolveum.midpoint.model.api.PipelineItem)13 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)13 ScriptExecutionException (com.evolveum.midpoint.model.api.ScriptExecutionException)12 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)7 PrismValue (com.evolveum.midpoint.prism.PrismValue)6 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)6 PrismObjectValue (com.evolveum.midpoint.prism.PrismObjectValue)5 FocusType (com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType)3 ResourceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)2 ActionExpressionType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType)2 ActionParameterValueType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType)2 ExpressionPipelineType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionPipelineType)2 ExpressionSequenceType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionSequenceType)2 SearchExpressionType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType)2 JAXBElement (javax.xml.bind.JAXBElement)2 QName (javax.xml.namespace.QName)2 ValidationResult (com.evolveum.midpoint.model.api.validator.ValidationResult)1 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)1 CustomEvent (com.evolveum.midpoint.notifications.api.events.CustomEvent)1