Exemple #1
0
def add_or_update_given_trace_db(trace_db, action_executions=None, rules=None,
                                 trigger_instances=None):
    """
    Will update an existing Trace.

    :param trace_db: The TraceDB to update.
    :type trace_db: ``TraceDB``

    :param action_executions: The action_execution to be added to the Trace. Should a list
                              of object_ids.
    :type action_executions: ``list``

    :param rules: The rules to be added to the Trace. Should a list of object_ids.
    :type rules: ``list``

    :param trigger_instances: The trigger_instances to be added to the Trace. Should a list
                              of object_ids.
    :type trigger_instances: ``list``

    :rtype: ``TraceDB``
    """
    if trace_db is None:
        raise ValueError('trace_db should be non-None.')

    if not action_executions:
        action_executions = []
    action_executions = [TraceComponentDB(object_id=action_execution)
                         for action_execution in action_executions]

    if not rules:
        rules = []
    rules = [TraceComponentDB(object_id=rule) for rule in rules]

    if not trigger_instances:
        trigger_instances = []
    trigger_instances = [TraceComponentDB(object_id=trigger_instance)
                         for trigger_instance in trigger_instances]

    # If an id exists then this is an update and we do not want to perform
    # an upsert so use push_components which will use the push operator.
    if trace_db.id:
        return Trace.push_components(trace_db,
                                     action_executions=action_executions,
                                     rules=rules,
                                     trigger_instances=trigger_instances)

    trace_db.action_executions = action_executions
    trace_db.rules = rules
    trace_db.trigger_instances = trigger_instances

    return Trace.add_or_update(trace_db)
Exemple #2
0
 def to_component_model(cls, component):
     values = {
         'object_id': component['object_id']
     }
     updated_at = component.get('updated_at', None)
     if updated_at:
         values['updated_at'] = isotime.parse(updated_at)
     return TraceComponentDB(**values)
Exemple #3
0
 def to_component_model(cls, component):
     values = {
         "object_id": component["object_id"],
         "ref": component["ref"],
         "caused_by": component.get("caused_by", {}),
     }
     updated_at = component.get("updated_at", None)
     if updated_at:
         values["updated_at"] = isotime.parse(updated_at)
     return TraceComponentDB(**values)
Exemple #4
0
    def test_update_via_list_push(self):
        no_action_executions = 4
        no_rules = 4
        no_trigger_instances = 5
        saved = TraceDBTest._create_save_trace(
            trace_tag='test_trace',
            action_executions=[str(bson.ObjectId()) for _ in range(no_action_executions)],
            rules=[str(bson.ObjectId()) for _ in range(no_rules)],
            trigger_instances=[str(bson.ObjectId()) for _ in range(no_trigger_instances)])

        # push updates
        Trace.push_action_execution(
            saved, action_execution=TraceComponentDB(object_id=str(bson.ObjectId())))
        Trace.push_rule(saved, rule=TraceComponentDB(object_id=str(bson.ObjectId())))
        Trace.push_trigger_instance(
            saved, trigger_instance=TraceComponentDB(object_id=str(bson.ObjectId())))

        retrieved = Trace.get(id=saved.id)
        self.assertEquals(retrieved.id, saved.id, 'Incorrect trace retrieved.')
        self.assertEquals(len(retrieved.action_executions), no_action_executions + 1)
        self.assertEquals(len(retrieved.rules), no_rules + 1)
        self.assertEquals(len(retrieved.trigger_instances), no_trigger_instances + 1)
Exemple #5
0
    def _create_save_trace(trace_tag, id_=None, action_executions=None, rules=None,
                           trigger_instances=None):

        if action_executions is None:
            action_executions = []
        action_executions = [TraceComponentDB(object_id=action_execution)
                             for action_execution in action_executions]

        if rules is None:
            rules = []
        rules = [TraceComponentDB(object_id=rule) for rule in rules]

        if trigger_instances is None:
            trigger_instances = []
        trigger_instances = [TraceComponentDB(object_id=trigger_instance)
                             for trigger_instance in trigger_instances]

        created = TraceDB(id=id_,
                          trace_tag=trace_tag,
                          trigger_instances=trigger_instances,
                          rules=rules,
                          action_executions=action_executions)
        return Trace.add_or_update(created)
Exemple #6
0
    def test_update_via_list_push_components(self):
        no_action_executions = 4
        no_rules = 4
        no_trigger_instances = 5
        saved = TraceDBTest._create_save_trace(
            trace_tag="test_trace",
            action_executions=[
                str(bson.ObjectId()) for _ in range(no_action_executions)
            ],
            rules=[str(bson.ObjectId()) for _ in range(no_rules)],
            trigger_instances=[
                str(bson.ObjectId()) for _ in range(no_trigger_instances)
            ],
        )

        retrieved = Trace.push_components(
            saved,
            action_executions=[
                TraceComponentDB(object_id=str(bson.ObjectId()))
                for _ in range(no_action_executions)
            ],
            rules=[
                TraceComponentDB(object_id=str(bson.ObjectId()))
                for _ in range(no_rules)
            ],
            trigger_instances=[
                TraceComponentDB(object_id=str(bson.ObjectId()))
                for _ in range(no_trigger_instances)
            ],
        )

        self.assertEqual(retrieved.id, saved.id, "Incorrect trace retrieved.")
        self.assertEqual(len(retrieved.action_executions),
                         no_action_executions * 2)
        self.assertEqual(len(retrieved.rules), no_rules * 2)
        self.assertEqual(len(retrieved.trigger_instances),
                         no_trigger_instances * 2)
Exemple #7
0
def _to_trace_component_db(component):
    """
    Take the component as string or a dict and will construct a TraceComponentDB.

    :param component: Should identify the component. If a string should be id of the
                      component. If a dict should contain id and the caused_by.
    :type component: ``bson.ObjectId`` or ``dict``

    :rtype: ``TraceComponentDB``
    """
    if not isinstance(component, (basestring, dict)):
        print type(component)
        raise ValueError('Expected component to be str or dict')

    object_id = component if isinstance(component, basestring) else component['id']
    ref = component.get('ref', '') if isinstance(component, dict) else ''
    caused_by = component.get('caused_by', {}) if isinstance(component, dict) else {}

    return TraceComponentDB(object_id=object_id, ref=ref, caused_by=caused_by)
Exemple #8
0
def _to_trace_component_db(component):
    """
    Take the component as string or a dict and will construct a TraceComponentDB.

    :param component: Should identify the component. If a string should be id of the
                      component. If a dict should contain id and the caused_by.
    :type component: ``bson.ObjectId`` or ``dict``

    :rtype: ``TraceComponentDB``
    """
    if not isinstance(component, (six.string_types, dict)):
        print(type(component))
        raise ValueError("Expected component to be str or dict")

    object_id = (component if isinstance(component, six.string_types) else
                 component["id"])
    ref = component.get("ref", "") if isinstance(component, dict) else ""
    caused_by = component.get("caused_by", {}) if isinstance(component,
                                                             dict) else {}

    return TraceComponentDB(object_id=object_id, ref=ref, caused_by=caused_by)