Esempio n. 1
0
 def __init__(self,
              source_uid,
              destination_uid,
              status='Success',
              conditions=None,
              priority=999,
              uid=None):
     """Initializes a new Branch object.
     
     Args:
         source_uid (str): The UID of the source action that will be sending inputs to this Branch.
         destination_uid (str): The UID of the destination action that will be returned if the conditions for this
             Branch are met.
         status (str, optional): Optional field to keep track of the status of the Branch. Defaults to
             "Success".
         conditions (list[Condition], optional): A list of Condition objects for the Branch object.
             Defaults to None.
         priority (int, optional): Optional priority parameter to specify which Branch in the Workflow's
             list of Branches should be executed if multiple have conditions resulting to True.
             Defaults to 999 (lowest priority).
         uid (str, optional): A universally unique identifier for this object. Created from uuid.uuid4() in Python.
     """
     ExecutionElement.__init__(self, uid)
     self.source_uid = source_uid
     self.destination_uid = destination_uid
     self.status = status
     self.conditions = conditions if conditions is not None else []
     self.priority = priority
Esempio n. 2
0
 def __init__(self,
              app_name,
              action_name,
              arguments=None,
              transforms=None,
              uid=None):
     """Initializes a new Condition object.
     
     Args:
         app_name (str): The name of the app which contains this condition
         action_name (str): The action name for the Condition. Defaults to an empty string.
         arguments (list[Argument], optional): Dictionary of Argument keys to Argument values. This dictionary will be
             converted to a dictionary of str:Argument. Defaults to None.
         transforms(list[Transform], optional): A list of Transform objects for the Condition object. Defaults to None.
         uid (str, optional): A universally unique identifier for this object.
             Created from uuid.uuid4() in Python
     """
     ExecutionElement.__init__(self, uid)
     self.app_name = app_name
     self.action_name = action_name
     self._data_param_name, self._run, self._api = get_condition_api(
         self.app_name, self.action_name)
     self._condition_executable = get_condition(self.app_name, self._run)
     arguments = {arg.name: arg
                  for arg in arguments} if arguments is not None else {}
     tmp_api = split_api_params(self._api, self._data_param_name)
     validate_condition_parameters(tmp_api, arguments, self.action_name)
     self.arguments = arguments
     self.transforms = transforms if transforms is not None else []
Esempio n. 3
0
 def __init__(self, status='Success', name='', flags=None, uid=None):
     """Initializes a new NextStep object.
     
     Args:
         name (str, optional): The name of the NextStep object. Defaults to an empty string.
         flags (list[Flag], optional): A list of Flag objects for the NextStep object. Defaults to None.
         uid (str, optional): A universally unique identifier for this object.
         Created from uuid.uuid4().hex in Python
     """
     ExecutionElement.__init__(self, uid)
     self.name = name
     self.status = status
     self.flags = flags if flags is not None else []
Esempio n. 4
0
    def __init__(self, name, workflows=None, uid=None,
                 walkoff_version=core.config.config.walkoff_version):
        """Creates a Playbook object.

        Args:
            name (str): The name of the Playbook
            workflows (list[Workflow], optional): An optional list of Workflows associated with this Playbook
            uid (str, optional): An optional UID to specify for this Playbook
        """
        ExecutionElement.__init__(self, uid)
        self.name = name
        # TODO: When playbook endpoints use UIDs, this should store UIDS
        self.workflows = {workflow.name: workflow for workflow in workflows} if workflows is not None else {}
        self.walkoff_version = walkoff_version
Esempio n. 5
0
    def __init__(self, action, args=None, uid=None):
        """Initializes a new Filter object. A Filter is used to filter input into a workflow.
        
        Args:
            action (str, optional): The action name for the filter. Defaults to an empty string.
            args (dict[str:str], optional): Dictionary of Argument keys to Argument values. This dictionary will be
                converted to a dictionary of str:Argument. Defaults to None.
            uid (str, optional): A universally unique identifier for this object.
                Created from uuid.uuid4().hex in Python
        """
        ExecutionElement.__init__(self, uid)
        self.action = action
        self._args_api, self._data_in_api = get_filter_api(self.action)
        if isinstance(args, list):
            args = {arg['name']: arg['value'] for arg in args}
        elif isinstance(args, dict):
            args = args
        else:
            args = {}

        self.args = validate_filter_parameters(self._args_api, args,
                                               self.action)
Esempio n. 6
0
    def __init__(self, name='', uid=None, steps=None, start=None, accumulated_risk=0.0):
        """Initializes a Workflow object. A Workflow falls under a Playbook, and has many associated Steps
            within it that get executed.
            
        Args:
            name (str, optional): The name of the Workflow object. Defaults to an empty string.
            uid (str, optional): Optional UID to pass in for the workflow. Defaults to None.
            steps (dict, optional): Optional Step objects. Defaults to None.
            start (str, optional): Optional name of the starting Step. Defaults to None.
            accumulated_risk (float, optional): The amount of risk that the execution of this Workflow has
                accrued. Defaults to 0.0.
        """
        ExecutionElement.__init__(self, uid)
        self.name = name
        self.steps = {step.name: step for step in steps} if steps is not None else {}
        self.start = start if start is not None else 'start'
        self.accumulated_risk = accumulated_risk

        self._total_risk = float(sum([step.risk for step in self.steps.values() if step.risk > 0]))
        self._is_paused = False
        self._accumulator = {}
        self._execution_uid = 'default'
Esempio n. 7
0
 def __init__(self, app_name, action_name, arguments=None, uid=None):
     """Initializes a new Transform object. A Transform is used to transform input into a workflow.
     
     Args:
         app_name (str): The app name associated with this transform
         action_name (str): The action name for the transform.
         arguments (list[Argument], optional): Dictionary of Argument keys to Argument values. This dictionary will be
             converted to a dictionary of str:Argument. Defaults to None.
         uid (str, optional): A universally unique identifier for this object.
             Created from uuid.uuid4() in Python
     """
     ExecutionElement.__init__(self, uid)
     self.app_name = app_name
     self.action_name = action_name
     self._data_param_name, self._run, self._api = get_transform_api(
         self.app_name, self.action_name)
     self._transform_executable = get_transform(self.app_name, self._run)
     arguments = {arg.name: arg
                  for arg in arguments} if arguments is not None else {}
     tmp_api = split_api_params(self._api, self._data_param_name)
     validate_transform_parameters(tmp_api, arguments, self.action_name)
     self.arguments = arguments
Esempio n. 8
0
    def __init__(self,
                 name='',
                 uid=None,
                 actions=None,
                 branches=None,
                 start=None):
        """Initializes a Workflow object. A Workflow falls under a Playbook, and has many associated Actions
            within it that get executed.
            
        Args:
            name (str, optional): The name of the Workflow object. Defaults to an empty string.
            uid (str, optional): Optional UID to pass in for the workflow. Defaults to uuid.uuid4().
            actions (dict, optional): Optional Action objects. Defaults to None.
            branches (list[Branch], optional): A list of Branch objects for the Action object. Defaults to None.
            start (str, optional): Optional UID of the starting Action. Defaults to None.
        """
        ExecutionElement.__init__(self, uid)
        self.name = name
        self.actions = {action.uid: action
                        for action in actions} if actions is not None else {}

        self.branches = {}
        if actions:
            for action in actions:
                self.branches[action.uid] = []

        if branches:
            for branch in branches:
                if branch.source_uid in self.branches:
                    self.branches[branch.source_uid].append(branch)

        self.start = start if start is not None else 'start'

        self._is_paused = False
        self._resume = threading.Event()
        self._accumulator = {}
        self._execution_uid = 'default'
Esempio n. 9
0
    def __init__(self,
                 app,
                 action,
                 name='',
                 device='',
                 inputs=None,
                 triggers=None,
                 next_steps=None,
                 position=None,
                 widgets=None,
                 risk=0,
                 uid=None,
                 templated=False,
                 raw_representation=None):
        """Initializes a new Step object. A Workflow has many steps that it executes.

        Args:
            app (str): The name of the app associated with the Step
            action (str): The name of the action associated with a Step
            name (str, optional): The name of the Step object. Defaults to an empty string.
            device (str, optional): The name of the device associated with the app associated with the Step. Defaults
                to an empty string.
            inputs (dict, optional): A dictionary of Argument objects that are input to the step execution. Defaults
                to None.
            triggers (list[Flag], optional): A list of Flag objects for the Step. If a Step should wait for data
                before continuing, then include these Trigger objects in the Step init. Defaults to None.
            next_steps (list[NextStep], optional): A list of NextStep objects for the Step object. Defaults to None.
            position (dict, optional): A dictionary with the x and y coordinates of the Step object. This is used
                for UI display purposes. Defaults to None.
            widgets (list[tuple(str, str)], optional): A list of widget tuples, which holds the app and the
                corresponding widget. Defaults to None.
            risk (int, optional): The risk associated with the Step. Defaults to 0.
            uid (str, optional): A universally unique identifier for this object.
                Created from uuid.uuid4().hex in Python
            templated (bool, optional): Whether or not the Step is templated. Used for Jinja templating.
            raw_representation (dict, optional): JSON representation of this object. Used for Jinja templating.
        """
        ExecutionElement.__init__(self, uid)

        self.triggers = triggers if triggers is not None else []
        self._incoming_data = AsyncResult()

        self.name = name
        self.app = app
        self.action = action
        self._run, self._input_api = get_app_action_api(self.app, self.action)
        get_app_action(self.app, self._run)
        if isinstance(inputs, list):
            inputs = {arg['name']: arg['value'] for arg in inputs}
        elif isinstance(inputs, dict):
            inputs = inputs
        else:
            inputs = {}
        self.templated = templated
        if not self.templated:
            self.inputs = validate_app_action_parameters(
                self._input_api, inputs, self.app, self.action)
        else:
            self.inputs = inputs
        self.device = device if (device is not None
                                 and device != 'None') else ''
        self.risk = risk
        self.next_steps = next_steps if next_steps is not None else []
        self.position = position if position is not None else {}
        self.widgets = [
            widget if isinstance(widget, Widget) else Widget(**widget)
            for widget in widgets
        ] if widgets is not None else []

        self._output = None
        self._next_up = None
        self._raw_representation = raw_representation if raw_representation is not None else {}
        self._execution_uid = 'default'
Esempio n. 10
0
    def __init__(self,
                 app_name,
                 action_name,
                 name='',
                 device_id=None,
                 arguments=None,
                 triggers=None,
                 position=None,
                 uid=None,
                 templated=False,
                 raw_representation=None):
        """Initializes a new Action object. A Workflow has many actions that it executes.

        Args:
            app_name (str): The name of the app associated with the Action
            action_name (str): The name of the action associated with a Action
            name (str, optional): The name of the Action object. Defaults to an empty string.
            device_id (int, optional): The id of the device associated with the app associated with the Action. Defaults
                to None.
            arguments ([Argument], optional): A list of Argument objects that are parameters to the action.
                Defaults to None.
            triggers (list[Flag], optional): A list of Flag objects for the Action. If a Action should wait for data
                before continuing, then include these Trigger objects in the Action init. Defaults to None.
            position (dict, optional): A dictionary with the x and y coordinates of the Action object. This is used
                for UI display purposes. Defaults to None.
            uid (str, optional): A universally unique identifier for this object.
                Created from uuid.uuid4().hex in Python
            templated (bool, optional): Whether or not the Action is templated. Used for Jinja templating.
            raw_representation (dict, optional): JSON representation of this object. Used for Jinja templating.
        """
        ExecutionElement.__init__(self, uid)

        self.triggers = triggers if triggers is not None else []
        self._incoming_data = None
        self._event = threading.Event()

        self.name = name
        self.device_id = device_id
        self.app_name = app_name
        self.action_name = action_name
        self._run, self._arguments_api = get_app_action_api(
            self.app_name, self.action_name)

        if is_app_action_bound(self.app_name,
                               self._run) and not self.device_id:
            raise InvalidArgument(
                "Cannot initialize Action {}. App action is bound but no device ID was provided."
                .format(self.name))

        self._action_executable = get_app_action(self.app_name, self._run)

        arguments = {argument.name: argument
                     for argument in arguments
                     } if arguments is not None else {}

        self.templated = templated
        if not self.templated:
            validate_app_action_parameters(self._arguments_api, arguments,
                                           self.app_name, self.action_name)
        self.arguments = arguments
        self.position = position if position is not None else {}

        self._output = None
        self._raw_representation = raw_representation if raw_representation is not None else {}
        self._execution_uid = 'default'
Esempio n. 11
0
 def __init__(self):
     ExecutionElement.__init__(self)
     self.a = 42
     self.b = B('a')
     self.c = [B(i) for i in range(3)]
     self.d = {i: B(i) for i in range(3)}
Esempio n. 12
0
 def __init__(self, b):
     ExecutionElement.__init__(self)
     self.b = b
Esempio n. 13
0
 def __init__(self):
     ExecutionElement.__init__(self)
     self.a = 42
     self.d = [B(i) for i in range(3)]
Esempio n. 14
0
 def __init__(self):
     ExecutionElement.__init__(self)
     self.a = 42
     self.d = B('something')
Esempio n. 15
0
 def test_init_with_uid(self):
     uid = uuid.uuid4().hex
     elem = ExecutionElement(uid=uid)
     self.assertEqual(elem.uid, uid)
Esempio n. 16
0
 def test_init_default(self):
     elem = ExecutionElement()
     self.assertIsNotNone(elem.uid)