Esempio n. 1
0
File: st2kv.py Progetto: zwunix/st2
def st2kv_(context, key, **kwargs):
    if not isinstance(key, six.string_types):
        raise TypeError('Given key is not typeof string.')

    decrypt = kwargs.get('decrypt', False)

    if not isinstance(decrypt, bool):
        raise TypeError('Decrypt parameter is not typeof bool.')

    try:
        username = context['__vars']['st2']['user']
    except KeyError:
        raise KeyError('Could not get user from context.')

    try:
        user_db = auth_db_access.User.get(username)
    except Exception as e:
        raise Exception('Failed to retrieve User object for user "%s" % (username)' %
                        (six.text_type(e)))

    has_default = 'default' in kwargs
    default_value = kwargs.get('default')

    try:
        return kvp_util.get_key(key=key, user_db=user_db, decrypt=decrypt)
    except db_exc.StackStormDBObjectNotFoundError as e:
        if not has_default:
            raise exc.ExpressionEvaluationException(str(e))
        else:
            return default_value
    except Exception as e:
        raise exc.ExpressionEvaluationException(str(e))
Esempio n. 2
0
def _get_current_task(context):
    if not context:
        raise exc.ExpressionEvaluationException('The context is not set.')

    current_task = context['__current_task'] or {}

    if not current_task:
        raise exc.ExpressionEvaluationException(
            'The current task is not set in the context.')

    return current_task
Esempio n. 3
0
def _get_current_task(context):
    if not context:
        raise exc.ExpressionEvaluationException("The context is not set.")

    try:
        current_task = context["__current_task"] or {}
    except KeyError:
        current_task = {}

    if not current_task:
        raise exc.ExpressionEvaluationException(
            "The current task is not set in the context.")

    return current_task
Esempio n. 4
0
def st2kv_(context, key, decrypt=False):
    if not isinstance(key, six.string_types):
        raise TypeError('Given key is not typeof string.')

    if not isinstance(decrypt, bool):
        raise TypeError('Decrypt parameter is not typeof bool.')

    try:
        username = context['__vars']['st2']['user']
    except KeyError:
        raise KeyError('Could not get user from context.')

    try:
        user_db = auth_db_access.User.get(username)
    except Exception as e:
        raise Exception(
            'Failed to retrieve User object for user "%s" % (username)' %
            (str(e)))

    kvp = kvp_util.get_key(key=key, user_db=user_db, decrypt=decrypt)

    if not kvp:
        raise exc.ExpressionEvaluationException(
            'Key %s does not exist in StackStorm datastore.' % key)

    return kvp
Esempio n. 5
0
def item_(context, key=None):
    if not context:
        raise exc.ExpressionEvaluationException('The context is not set.')

    current_item = context['__current_item']

    if not key:
        return current_item

    if not isinstance(current_item, dict):
        raise exc.ExpressionEvaluationException('Item is not type of dict.')

    if key not in current_item:
        raise exc.ExpressionEvaluationException(
            'Item does not have key "%s".' % key)

    return current_item[key]
Esempio n. 6
0
def task(context, task_id=None, route=None):
    instances = None

    try:
        current_task = workflow_functions._get_current_task(context)
    except:
        current_task = {}

    if task_id is None:
        task_id = current_task['id']

    if route is None:
        route = current_task.get('route', 0)

    try:
        workflow_state = context['__state'] or {}
    except KeyError:
        workflow_state = {}

    task_state_pointers = workflow_state.get('tasks') or {}
    task_state_entry_uid = constants.TASK_STATE_ROUTE_FORMAT % (task_id,
                                                                str(route))
    task_state_entry_idx = task_state_pointers.get(task_state_entry_uid)

    # If unable to identify the task flow entry and if there are other routes, then
    # use an earlier route before the split to find the specific task.
    if task_state_entry_idx is None:
        if route > 0:
            current_route_details = workflow_state['routes'][route]
            # Reverse the list because we want to start with the next longest route.
            for idx, prev_route_details in enumerate(
                    reversed(workflow_state['routes'][:route])):
                if len(set(prev_route_details) -
                       set(current_route_details)) == 0:
                    # The index is from a reversed list so need to calculate
                    # the index of the item in the list before the reverse.
                    prev_route = route - idx - 1
                    return task(context, task_id=task_id, route=prev_route)
    else:
        # Otherwise, get the task flow entry and use the
        # task id and route to query the database.
        task_state_seqs = workflow_state.get('sequence') or []
        task_state_entry = task_state_seqs[task_state_entry_idx]
        route = task_state_entry['route']
        st2_ctx = context['__vars']['st2']
        workflow_execution_id = st2_ctx['workflow_execution_id']

        # Query the database by the workflow execution ID, task ID, and task route.
        instances = wf_db_access.TaskExecution.query(
            workflow_execution=workflow_execution_id,
            task_id=task_id,
            task_route=route)

    if not instances:
        message = 'Unable to find task execution for "%s".' % task_id
        raise exc.ExpressionEvaluationException(message)

    return format_task_result(instances)
Esempio n. 7
0
def item_(context, key=None):
    if not context:
        raise exc.ExpressionEvaluationException("The context is not set.")

    current_item = context["__current_item"]

    if not key:
        return current_item

    if not isinstance(current_item, collections.Mapping):
        raise exc.ExpressionEvaluationException(
            "Item is not type of collections.Mapping.")

    if key not in current_item:
        raise exc.ExpressionEvaluationException(
            'Item does not have key "%s".' % key)

    return current_item[key]