Esempio n. 1
0
def test_discard_all_pending_changes_resources_created():
    res1 = DBResource.from_dict('test1',
                                {'name': 'test1',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res1.inputs['a'] = '9'
    res1.save_lazy()

    res2 = DBResource.from_dict('test2',
                                {'name': 'test2',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res2.inputs['a'] = '0'
    res2.save_lazy()
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    assert len(staged_log) == 2

    change.discard_all()
    staged_log = change.stage_changes()
    assert len(staged_log) == 0
    assert resource.load_all() == []
Esempio n. 2
0
def tagged_resources():
    base_tags = ['n1=x', 'n2']
    tags = base_tags + ['node=t1']
    t1 = Resource.from_dict('t1', {
        'name': 't1',
        'tags': tags,
        'base_path': 'x'
    })
    t1.save_lazy()
    tags = base_tags + ['node=t2']
    t2 = Resource.from_dict('t2', {
        'name': 't2',
        'tags': tags,
        'base_path': 'x'
    })
    t2.save_lazy()
    tags = base_tags + ['node=t3']
    t3 = Resource.from_dict('t3', {
        'name': 't3',
        'tags': tags,
        'base_path': 'x'
    })
    t3.save_lazy()
    tags = ['node=t3']
    t4 = Resource.from_dict('t4', {
        'name': 't4',
        'tags': tags,
        'base_path': 'x'
    })
    t4.save_lazy()
    ModelMeta.save_all_lazy()
    return [t1, t2, t3]
Esempio n. 3
0
def test_discard_connection():
    res1 = DBResource.from_dict('test1',
                                {'name': 'test1',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res1.inputs['a'] = '9'
    res1.save_lazy()

    res2 = DBResource.from_dict('test2',
                                {'name': 'test2',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res2.inputs['a'] = '0'
    res2.save_lazy()
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    for item in staged_log:
        operations.move_to_commited(item.log_action)

    res1 = resource.load('test1')
    res2 = resource.load('test2')
    res1.connect(res2, {'a': 'a'})
    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    assert res2.args == {'a': '9'}
    change.discard_all()
    assert res2.args == {'a': '0'}
    assert len(change.stage_changes()) == 0
Esempio n. 4
0
def test_discard_connection():
    res1 = DBResource.from_dict('test1',
                                {'name': 'test1',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res1.inputs['a'] = '9'
    res1.save_lazy()

    res2 = DBResource.from_dict('test2',
                                {'name': 'test2',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res2.inputs['a'] = '0'
    res2.save_lazy()
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    for item in staged_log:
        operations.move_to_commited(item.log_action)

    res1 = resource.load('test1')
    res2 = resource.load('test2')
    res1.connect(res2, {'a': 'a'})
    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    assert res2.args == {'a': '9'}
    change.discard_all()
    assert res2.args == {'a': '0'}
    assert len(change.stage_changes()) == 0
Esempio n. 5
0
def test_discard_all_pending_changes_resources_created():
    res1 = DBResource.from_dict('test1',
                                {'name': 'test1',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res1.inputs['a'] = '9'
    res1.save_lazy()

    res2 = DBResource.from_dict('test2',
                                {'name': 'test2',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res2.inputs['a'] = '0'
    res2.save_lazy()
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    assert len(staged_log) == 2

    change.discard_all()
    staged_log = change.stage_changes()
    assert len(staged_log) == 0
    assert resource.load_all() == []
Esempio n. 6
0
def test_discard_all_pending_changes_resources_created():
    res1 = DBResource.from_dict(
        "test1",
        {
            "name": "test1",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res1.inputs["a"] = "9"
    res1.save_lazy()

    res2 = DBResource.from_dict(
        "test2",
        {
            "name": "test2",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res2.inputs["a"] = "0"
    res2.save_lazy()
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    assert len(staged_log) == 2

    change.discard_all()
    staged_log = change.stage_changes()
    assert len(staged_log) == 0
    assert resource.load_all() == []
Esempio n. 7
0
def tagged_resources():
    tags = ['n1', 'n2', 'n3']
    t1 = Resource.from_dict('t1',
                            {'name': 't1', 'tags': tags, 'base_path': 'x'})
    t1.save_lazy()
    t2 = Resource.from_dict('t2',
                            {'name': 't2', 'tags': tags, 'base_path': 'x'})
    t2.save_lazy()
    t3 = Resource.from_dict('t3',
                            {'name': 't3', 'tags': tags, 'base_path': 'x'})
    t3.save_lazy()
    ModelMeta.save_all_lazy()
    return [t1, t2, t3]
Esempio n. 8
0
def test_riak():

    events = {
        'riak_service1': [
            evapi.React('riak_service1', 'run', 'success', 'riak_service2',
                        'run'),
            evapi.React('riak_service1', 'run', 'success',
                        'riak_service3', 'run')
        ],
        'riak_service3': [
            evapi.React('riak_service3', 'join', 'success', 'riak_service1',
                        'commit'),
            evapi.React('riak_service3', 'run', 'success', 'riak_service3',
                        'join')
        ],
        'riak_service2': [
            evapi.React('riak_service2', 'run', 'success', 'riak_service2',
                        'join'),
            evapi.React('riak_service2', 'join', 'success', 'riak_service1',
                        'commit')
        ],
    }
    for name in events:
        res = Resource.from_dict({'key': name, 'name': name})
        res.save()
        res.inputs.add_new('location_id', '1')
        evapi.add_events(name, events[name])

    changes_graph = nx.MultiDiGraph()
    changes_graph.add_node('riak_service1.run')
    evapi.build_edges(changes_graph, events)
    assert set(changes_graph.predecessors('riak_service1.commit')) == {
        'riak_service2.join', 'riak_service3.join'
    }
Esempio n. 9
0
def test_revert_create():
    res = DBResource.from_dict(
        "test1",
        {
            "name": "test1",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res.inputs["a"] = "9"
    res.save_lazy()
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    logitem = staged_log[0]
    operations.move_to_commited(logitem.log_action)
    assert logitem.diff == [["add", "", [["a", "9"]]]]

    commited = CommitedResource.get("test1")
    assert commited.inputs == {"a": "9"}

    change.revert(logitem.uid)

    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    for item in staged_log:
        operations.move_to_commited(item.log_action)
    assert resource.load_all() == []
Esempio n. 10
0
def test_revert_create():
    res = DBResource.from_dict('test1',
                               {'name': 'test1',
                                'base_path': 'x',
                                'state': RESOURCE_STATE.created.name,
                                'meta_inputs': {'a': {'value': None,
                                                      'schema': 'str'}}})
    res.inputs['a'] = '9'
    res.save_lazy()
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    logitem = staged_log[0]
    operations.move_to_commited(logitem.log_action)
    assert logitem.diff == [['add', '', [['a', '9']]]]

    commited = CommitedResource.get('test1')
    assert commited.inputs == {'a': '9'}

    change.revert(logitem.uid)

    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    for item in staged_log:
        operations.move_to_commited(item.log_action)
    assert resource.load_all() == []
Esempio n. 11
0
def test_revert_update():
    commit = {'a': '10'}
    previous = {'a': '9'}
    res = DBResource.from_dict('test1',
                               {'name': 'test1',
                                'base_path': 'x',
                                'meta_inputs': {'a': {'value': None,
                                                      'schema': 'str'}}})
    res.save()
    action = 'update'
    res.inputs['a'] = '9'
    resource_obj = resource.load(res.name)

    assert resource_obj.args == previous

    log = data.SL()
    logitem = change.create_logitem(res.name,
                                    action,
                                    change.create_diff(commit, previous),
                                    [],
                                    base_path=res.base_path)
    log.append(logitem)
    resource_obj.update(commit)
    operations.move_to_commited(logitem.log_action)

    assert logitem.diff == [['change', 'a', ['9', '10']]]
    assert resource_obj.args == commit

    change.revert(logitem.uid)
    assert resource_obj.args == previous
Esempio n. 12
0
def test_revert_create():
    res = DBResource.from_dict('test1',
                               {'name': 'test1',
                                'base_path': 'x',
                                'state': RESOURCE_STATE.created.name,
                                'meta_inputs': {'a': {'value': None,
                                                      'schema': 'str'}}})
    res.inputs['a'] = '9'
    res.save_lazy()
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    logitem = staged_log[0]
    operations.move_to_commited(logitem.log_action)
    assert logitem.diff == [['add', '', [['a', '9']]]]

    commited = CommitedResource.get('test1')
    assert commited.inputs == {'a': '9'}

    change.revert(logitem.uid)

    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    for item in staged_log:
        operations.move_to_commited(item.log_action)

    assert resource.load_all() == []
Esempio n. 13
0
def test_discard_removed():
    res1 = DBResource.from_dict(
        "test1",
        {
            "name": "test1",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res1.inputs["a"] = "9"
    res1.save_lazy()
    ModelMeta.save_all_lazy()
    staged_log = change.stage_changes()
    for item in staged_log:
        operations.move_to_commited(item.log_action)
    res1 = resource.load("test1")
    res1.remove()
    assert len(change.stage_changes()) == 1
    assert res1.to_be_removed()

    change.discard_all()

    assert len(change.stage_changes()) == 0
    assert not resource.load("test1").to_be_removed()
Esempio n. 14
0
def test_revert_update():
    commit = {'a': '10'}
    previous = {'a': '9'}
    res = DBResource.from_dict('test1',
                               {'name': 'test1',
                                'base_path': 'x',
                                'meta_inputs': {'a': {'value': None,
                                                      'schema': 'str'}}})
    res.save()
    action = 'update'
    res.inputs['a'] = '9'
    resource_obj = resource.load(res.name)

    assert resource_obj.args == previous

    log = data.SL()
    logitem = change.create_logitem(res.name,
                                    action,
                                    change.create_diff(commit, previous),
                                    [],
                                    base_path=res.base_path)
    log.append(logitem)
    resource_obj.update(commit)
    operations.move_to_commited(logitem.log_action)

    assert logitem.diff == [('change', 'a', ('9', '10'))]
    assert resource_obj.args == commit

    change.revert(logitem.uid)
    assert resource_obj.args == previous
Esempio n. 15
0
def test_remove_events(events_example):
    r = Resource.from_dict(dict(key='e1', name='e1', base_path='x'))
    r.save()
    to_be_removed = events_example[2]
    evapi.add_events('e1', events_example)
    evapi.remove_event(to_be_removed)
    assert to_be_removed not in evapi.all_events('e1')
Esempio n. 16
0
def test_events(rk):
    k = next(rk)
    r1 = Resource.from_dict(k, {'events': ['event1', 'event2']})
    r1.save()
    assert r1.events == ['event1', 'event2']
    r1.events.pop()

    assert r1.events == ['event1']
Esempio n. 17
0
def test_events(rk):
    k = next(rk)
    r1 = Resource.from_dict(k, {'events': ['event1', 'event2']})
    r1.save()
    assert r1.events == ['event1', 'event2']
    r1.events.pop()

    assert r1.events == ['event1']
Esempio n. 18
0
def create_resource(name, tags=None):
    resource = DBResource.from_dict(
        name,
        {'name': name,
         'base_path': 'x',
         'state': '',
         'tags': tags or [],
         'meta_inputs': {'a': {'value': None,
                               'schema': 'str'}}})
    resource.save_lazy()
    return resource
Esempio n. 19
0
def tagged_resources():
    base_tags = ['n1=x', 'n2']
    tags = base_tags + ['node=t1']
    t1 = Resource.from_dict('t1',
                            {'name': 't1', 'tags': tags, 'base_path': 'x'})
    t1.save_lazy()
    tags = base_tags + ['node=t2']
    t2 = Resource.from_dict('t2',
                            {'name': 't2', 'tags': tags, 'base_path': 'x'})
    t2.save_lazy()
    tags = base_tags + ['node=t3']
    t3 = Resource.from_dict('t3',
                            {'name': 't3', 'tags': tags, 'base_path': 'x'})
    t3.save_lazy()
    tags = ['node=t3']
    t4 = Resource.from_dict('t4',
                            {'name': 't4', 'tags': tags, 'base_path': 'x'})
    t4.save_lazy()
    ModelMeta.save_all_lazy()
    return [t1, t2, t3]
Esempio n. 20
0
def test_discard_connection():
    res1 = DBResource.from_dict(
        "test1",
        {
            "name": "test1",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res1.inputs["a"] = "9"
    res1.save_lazy()

    res2 = DBResource.from_dict(
        "test2",
        {
            "name": "test2",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res2.inputs["a"] = "0"
    res2.save_lazy()
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    for item in staged_log:
        operations.move_to_commited(item.log_action)

    res1 = resource.load("test1")
    res2 = resource.load("test2")
    res1.connect(res2, {"a": "a"})
    staged_log = change.stage_changes()
    assert len(staged_log) == 1
    assert res2.args == {"a": "9"}
    change.discard_all()
    assert res2.args == {"a": "0"}
    assert len(change.stage_changes()) == 0
Esempio n. 21
0
def nova_deps():
    for name in ['nova', 'nova_api', 'nova_sch']:
        r = Resource.from_dict(dict(key=name, name=name))
        r.inputs.add_new('location_id', '1')
        r.save()
    nova = [
        evapi.Dep('nova', 'run', 'success', 'nova_sch', 'run'),
        evapi.React('nova', 'run', 'success', 'nova_api', 'update')]
    nova_api = [
        evapi.React('nova_api', 'update', 'success', 'nova', 'reboot')]
    evapi.add_events('nova', nova)
    evapi.add_events('nova_api', nova_api)
    return {'nova': nova}
Esempio n. 22
0
def test_mandatory_revisit():
    events = {
        'e1': [evapi.Dep('e1', 'run', 'success', 'e2', 'run'),
               evapi.React('e1', 'run', 'success', 'e2', 'start')],
        'e2': [evapi.React('e2', 'start', 'success', 'e2', 'run')]}
    for name in events:
        r = Resource.from_dict(dict(key=name, name=name))
        r.inputs.add_new('location_id', '1')
        r.save()
        evapi.add_events(name, events[name])
    changes_graph = nx.DiGraph()
    changes_graph.add_node('e1.run')
    evapi.build_edges(changes_graph, events)
    assert set(changes_graph.predecessors('e2.run')) == {'e1.run', 'e2.start'}
Esempio n. 23
0
    def create_from_spec(self, name, spec, args=None, tags=None):
        args = args or {}
        self.name = name
        if spec:
            if spec.startswith('/'):
                # it's full path, don't use repo
                self.base_path = spec
                metadata = read_meta(spec)
            else:
                repo, spec = Repository.parse(spec)
                metadata = repo.get_metadata(spec)
                self.base_path = repo.get_path(spec)
        else:
            metadata = deepcopy(self._metadata)
            self.base_path = spec  # TODO: remove this old method?

        if tags is None:
            tags = []
        m_tags = metadata.get('tags', [])
        tags.extend(m_tags)
        tags.append('resource={}'.format(name))

        inputs = metadata.get('input', {})

        self.auto_extend_inputs(inputs)
        self.db_obj = DBResource.from_dict(
            name,
            {
                'id': name,
                'name': name,
                'actions_path': metadata.get('actions_path', ''),
                'actions': metadata.get('actions', {}),
                'base_name': metadata.get('base_name', ''),
                'base_path': metadata.get('base_path', ''),
                'handler': metadata.get('handler', ''),
                'version': metadata.get('version', ''),
                'meta_inputs': inputs,
                'tags': tags,
                'state': RESOURCE_STATE.created.name,
                'managers': metadata.get('managers', [])
            })
        self.create_inputs(args)

        self.db_obj.save()
        LogItem.new({
            'resource': self.name,
            'action': 'run',
            'log': 'staged',
            'tags': self.tags}).save_lazy()
Esempio n. 24
0
    def create_from_spec(self, name, spec, args=None, tags=None):
        args = args or {}
        self.name = name
        if spec:
            if spec.startswith('/'):
                # it's full path, don't use repo
                self.base_path = spec
                metadata = read_meta(spec)
            else:
                repo, spec = Repository.parse(spec)
                metadata = repo.get_metadata(spec)
                self.base_path = repo.get_path(spec)
        else:
            metadata = deepcopy(self._metadata)
            self.base_path = spec  # TODO: remove this old method?

        if tags is None:
            tags = []
        m_tags = metadata.get('tags', [])
        tags.extend(m_tags)
        tags.append('resource={}'.format(name))

        inputs = metadata.get('input', {})

        self.auto_extend_inputs(inputs)
        self.db_obj = DBResource.from_dict(
            name, {
                'id': name,
                'name': name,
                'actions_path': metadata.get('actions_path', ''),
                'actions': metadata.get('actions', {}),
                'base_name': metadata.get('base_name', ''),
                'base_path': metadata.get('base_path', ''),
                'handler': metadata.get('handler', ''),
                'version': metadata.get('version', ''),
                'meta_inputs': inputs,
                'tags': tags,
                'state': RESOURCE_STATE.created.name,
                'managers': metadata.get('managers', [])
            })
        self.create_inputs(args)

        self.db_obj.save()
        LogItem.new({
            'resource': self.name,
            'action': 'run',
            'log': 'staged',
            'tags': self.tags
        }).save_lazy()
Esempio n. 25
0
def create_resource(key, data):
    mi = data.get('meta_inputs', {})
    for inp_name, inp_value in data.get('inputs', {}).items():
        if isinstance(inp_value, list):
            if len(inp_value) == 1 and isinstance(inp_value[0], dict):
                schema = [{}]
            else:
                schema = ['str!']
        elif isinstance(inp_value, dict):
            schema = {}
        else:
            schema = '%s!' % type(inp_value).__name__
        mi.setdefault(inp_name, {"schema": schema})
    data['meta_inputs'] = mi
    return Resource.from_dict(key, data)
Esempio n. 26
0
def create_resource(key, data):
    mi = data.get('meta_inputs', {})
    for inp_name, inp_value in data.get('inputs', {}).items():
        if isinstance(inp_value, list):
            if len(inp_value) == 1 and isinstance(inp_value[0], dict):
                schema = [{}]
            else:
                schema = ['str!']
        elif isinstance(inp_value, dict):
            schema = {}
        else:
            schema = '%s!' % type(inp_value).__name__
        mi.setdefault(inp_name, {"schema": schema})
    data['meta_inputs'] = mi
    return Resource.from_dict(key, data)
Esempio n. 27
0
def create_resource(name, tags=None):
    res = DBResource.from_dict(
        name, {
            'name': name,
            'base_path': 'x',
            'state': resource.RESOURCE_STATE.created.name,
            'tags': tags or [],
            'meta_inputs': {
                'a': {
                    'value': None,
                    'schema': 'str'
                }
            }
        })
    res.save_lazy()
    return res
Esempio n. 28
0
    def __init__(self,
                 name,
                 base_path,
                 args=None,
                 tags=None,
                 virtual_resource=None):
        args = args or {}
        self.name = name
        if base_path:
            metadata = read_meta(base_path)
        else:
            metadata = deepcopy(self._metadata)

        self.base_path = base_path

        if tags is None:
            tags = []
        m_tags = metadata.get('tags', [])
        tags.extend(m_tags)
        tags.append('resource={}'.format(metadata['id']))

        self.virtual_resource = virtual_resource

        inputs = metadata.get('input', {})

        self.auto_extend_inputs(inputs)
        self.db_obj = DBResource.from_dict(
            name, {
                'id': name,
                'name': name,
                'actions_path': metadata.get('actions_path', ''),
                'actions': metadata.get('actions', {}),
                'base_name': metadata.get('base_name', ''),
                'base_path': metadata.get('base_path', ''),
                'handler': metadata.get('handler', ''),
                'puppet_module': metadata.get('puppet_module', ''),
                'version': metadata.get('version', ''),
                'meta_inputs': inputs,
                'tags': tags,
                'state': RESOURCE_STATE.created.name,
                'managers': metadata.get('managers', [])
            })
        self.create_inputs(args)

        self.db_obj.save()
Esempio n. 29
0
def test_revert_removal():
    res = DBResource.from_dict('test1',
                               {'name': 'test1',
                                'base_path': 'x',
                                'state': RESOURCE_STATE.created.name,
                                'meta_inputs': {'a': {'value': None,
                                                      'schema': 'str'}}})
    res.inputs['a'] = '9'
    res.save_lazy()

    commited = CommitedResource.from_dict('test1', {'inputs': {'a': '9'},
                                                    'state': 'operational'})
    commited.save_lazy()

    resource_obj = resource.load(res.name)
    resource_obj.remove()
    ModelMeta.save_all_lazy()

    changes = change.stage_changes()
    assert len(changes) == 1
    assert changes[0].diff == [['remove', '', [['a', '9']]]]
    operations.move_to_commited(changes[0].log_action)

    clear_cache()
    assert DBResource._c.obj_cache == {}
    # assert DBResource.bucket.get('test1').siblings == []

    with mock.patch.object(repository.Repository, 'read_meta') as mread:
        mread.return_value = {
            'input': {'a': {'schema': 'str!'}},
            'id': 'mocked'
        }
        with mock.patch.object(repository.Repository, 'get_path') as mpath:
            mpath.return_value = 'x'

            change.revert(changes[0].uid)
    ModelMeta.save_all_lazy()
    # assert len(DBResource.bucket.get('test1').siblings) == 1

    resource_obj = resource.load('test1')
    assert resource_obj.args == {
        'a': '9',
        'location_id': '',
        'transports_id': ''
    }
Esempio n. 30
0
def create_resource(name, tags=None, inputs=None):
    if inputs is None:
        inputs = {'a': ''}
    meta_inputs = {}
    for key, value in inputs.items():
        if not isinstance(value, basestring):
            raise Exception('Only strings are allowed')
        meta_inputs[key] = {'value': value, 'schema': 'str'}
    res = DBResource.from_dict(
        name,
        {'name': name,
         'base_path': 'x',
         'state': resource.RESOURCE_STATE.created.name,
         'tags': tags or [],
         'meta_inputs': meta_inputs,
         'inputs': meta_inputs})
    res.save_lazy()
    return resource.Resource(res)
Esempio n. 31
0
    def __init__(self, name, base_path, args=None, tags=None,
                 virtual_resource=None):
        args = args or {}
        self.name = name
        if base_path:
            metadata = read_meta(base_path)
        else:
            metadata = deepcopy(self._metadata)

        self.base_path = base_path

        if tags is None:
            tags = []
        m_tags = metadata.get('tags', [])
        tags.extend(m_tags)
        tags.append('resource={}'.format(metadata['id']))

        self.virtual_resource = virtual_resource

        inputs = metadata.get('input', {})

        self.auto_extend_inputs(inputs)
        self.db_obj = DBResource.from_dict(
            name,
            {
                'id': name,
                'name': name,
                'actions_path': metadata.get('actions_path', ''),
                'actions': metadata.get('actions', {}),
                'base_name': metadata.get('base_name', ''),
                'base_path': metadata.get('base_path', ''),
                'handler': metadata.get('handler', ''),
                'puppet_module': metadata.get('puppet_module', ''),
                'version': metadata.get('version', ''),
                'meta_inputs': inputs,
                'tags': tags,
                'state': RESOURCE_STATE.created.name,
                'managers': metadata.get('managers', [])
            })
        self.create_inputs(args)

        self.db_obj.save()
Esempio n. 32
0
def test_revert_removal():
    res = DBResource.from_dict('test1',
                               {'name': 'test1',
                                'base_path': 'x',
                                'state': RESOURCE_STATE.created.name,
                                'meta_inputs': {'a': {'value': None,
                                                      'schema': 'str'}}})
    res.inputs['a'] = '9'
    res.save_lazy()

    commited = CommitedResource.from_dict('test1', {'inputs': {'a': '9'},
                                                    'state': 'operational'})
    commited.save_lazy()

    resource_obj = resource.load(res.name)
    resource_obj.remove()
    ModelMeta.save_all_lazy()

    changes = change.stage_changes()
    assert len(changes) == 1
    assert changes[0].diff == [['remove', '', [['a', '9']]]]
    operations.move_to_commited(changes[0].log_action)

    ModelMeta.session_start()
    assert DBResource._c.obj_cache == {}
    assert DBResource.bucket.get('test1').siblings == []

    with mock.patch.object(resource, 'read_meta') as mread:
        mread.return_value = {
            'input': {'a': {'schema': 'str!'}},
            'id': 'mocked'
        }
        change.revert(changes[0].uid)
    ModelMeta.save_all_lazy()
    assert len(DBResource.bucket.get('test1').siblings) == 1

    resource_obj = resource.load('test1')
    assert resource_obj.args == {
        'a': '9',
        'location_id': '',
        'transports_id': ''
    }
Esempio n. 33
0
def test_discard_removed():
    res1 = DBResource.from_dict('test1',
                                {'name': 'test1',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res1.inputs['a'] = '9'
    res1.save_lazy()
    ModelMeta.save_all_lazy()
    staged_log = change.stage_changes()
    for item in staged_log:
        operations.move_to_commited(item.log_action)
    res1 = resource.load('test1')
    res1.remove()
    assert len(change.stage_changes()) == 1
    assert res1.to_be_removed()

    change.discard_all()

    assert len(change.stage_changes()) == 0
    assert not resource.load('test1').to_be_removed()
Esempio n. 34
0
def test_discard_removed():
    res1 = DBResource.from_dict('test1',
                                {'name': 'test1',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res1.inputs['a'] = '9'
    res1.save_lazy()
    ModelMeta.save_all_lazy()
    staged_log = change.stage_changes()
    for item in staged_log:
        operations.move_to_commited(item.log_action)
    res1 = resource.load('test1')
    res1.remove()
    assert len(change.stage_changes()) == 1
    assert res1.to_be_removed()

    change.discard_all()

    assert len(change.stage_changes()) == 0
    assert not resource.load('test1').to_be_removed()
Esempio n. 35
0
def test_revert_removal():
    res = DBResource.from_dict(
        "test1",
        {
            "name": "test1",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res.inputs["a"] = "9"
    res.save_lazy()

    commited = CommitedResource.from_dict("test1", {"inputs": {"a": "9"}, "state": "operational"})
    commited.save_lazy()

    resource_obj = resource.load(res.name)
    resource_obj.remove()
    ModelMeta.save_all_lazy()

    changes = change.stage_changes()
    assert len(changes) == 1
    assert changes[0].diff == [["remove", "", [["a", "9"]]]]
    operations.move_to_commited(changes[0].log_action)

    ModelMeta.session_start()
    assert DBResource._c.obj_cache == {}
    assert DBResource.bucket.get("test1").siblings == []

    with mock.patch.object(resource, "read_meta") as mread:
        mread.return_value = {"input": {"a": {"schema": "str!"}}, "id": "mocked"}
        change.revert(changes[0].uid)
    ModelMeta.save_all_lazy()
    assert len(DBResource.bucket.get("test1").siblings) == 1

    resource_obj = resource.load("test1")
    assert resource_obj.args == {"a": "9", "location_id": "", "transports_id": ""}
Esempio n. 36
0
def test_revert_update():
    commit = {"a": "10"}
    previous = {"a": "9"}
    res = DBResource.from_dict(
        "test1", {"name": "test1", "base_path": "x", "meta_inputs": {"a": {"value": None, "schema": "str"}}}
    )
    res.save()
    action = "update"
    res.inputs["a"] = "9"
    resource_obj = resource.load(res.name)

    assert resource_obj.args == previous

    log = data.SL()
    logitem = change.create_logitem(res.name, action, change.create_diff(commit, previous), [], base_path=res.base_path)
    log.append(logitem)
    resource_obj.update(commit)
    operations.move_to_commited(logitem.log_action)

    assert logitem.diff == [("change", "a", ("9", "10"))]
    assert resource_obj.args == commit

    change.revert(logitem.uid)
    assert resource_obj.args == previous
Esempio n. 37
0
def test_revert_update_connected():
    res1 = DBResource.from_dict(
        "test1",
        {
            "name": "test1",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res1.inputs["a"] = "9"
    res1.save_lazy()

    res2 = DBResource.from_dict(
        "test2",
        {
            "name": "test2",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res2.inputs["a"] = ""
    res2.save_lazy()

    res3 = DBResource.from_dict(
        "test3",
        {
            "name": "test3",
            "base_path": "x",
            "state": RESOURCE_STATE.created.name,
            "meta_inputs": {"a": {"value": None, "schema": "str"}},
        },
    )
    res3.inputs["a"] = ""
    res3.save_lazy()

    res1 = resource.load("test1")
    res2 = resource.load("test2")
    res3 = resource.load("test3")
    res1.connect(res2)
    res2.connect(res3)
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    assert len(staged_log) == 3

    for item in staged_log:
        assert item.action == "run"
        operations.move_to_commited(item.log_action)
    assert len(change.stage_changes()) == 0

    res1.disconnect(res2)
    staged_log = change.stage_changes()
    assert len(staged_log) == 2
    to_revert = []

    for item in staged_log:
        assert item.action == "update"
        operations.move_to_commited(item.log_action)
        to_revert.append(item.uid)

    change.revert_uids(sorted(to_revert, reverse=True))
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()

    assert len(staged_log) == 2
    for item in staged_log:
        assert item.diff == [["change", "a", ["", "9"]]]
Esempio n. 38
0
def test_revert_update_connected():
    res1 = DBResource.from_dict('test1',
                                {'name': 'test1',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res1.inputs['a'] = '9'
    res1.save_lazy()

    res2 = DBResource.from_dict('test2',
                                {'name': 'test2',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res2.inputs['a'] = ''
    res2.save_lazy()

    res3 = DBResource.from_dict('test3',
                                {'name': 'test3',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res3.inputs['a'] = ''
    res3.save_lazy()

    res1 = resource.load('test1')
    res2 = resource.load('test2')
    res3 = resource.load('test3')
    res1.connect(res2)
    res2.connect(res3)
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    assert len(staged_log) == 3

    for item in staged_log:
        assert item.action == 'run'
        operations.move_to_commited(item.log_action)
    assert len(change.stage_changes()) == 0

    res1.disconnect(res2)
    staged_log = change.stage_changes()
    assert len(staged_log) == 2
    to_revert = []

    for item in staged_log:
        assert item.action == 'update'
        operations.move_to_commited(item.log_action)
        to_revert.append(item.uid)

    change.revert_uids(sorted(to_revert, reverse=True))
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()

    assert len(staged_log) == 2
    for item in staged_log:
        assert item.diff == [['change', 'a', ['', '9']]]
Esempio n. 39
0
def test_single_event(events_example):
    r = Resource.from_dict(dict(key='e1', name='e1', base_path='x'))
    r.save()
    evapi.add_events('e1', events_example[:2])
    evapi.add_event(events_example[2])
    assert set(evapi.all_events('e1')) == set(events_example)
Esempio n. 40
0
def test_revert_update_connected():
    res1 = DBResource.from_dict('test1',
                                {'name': 'test1',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res1.inputs['a'] = '9'
    res1.save_lazy()

    res2 = DBResource.from_dict('test2',
                                {'name': 'test2',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res2.inputs['a'] = ''
    res2.save_lazy()

    res3 = DBResource.from_dict('test3',
                                {'name': 'test3',
                                 'base_path': 'x',
                                 'state': RESOURCE_STATE.created.name,
                                 'meta_inputs': {'a': {'value': None,
                                                       'schema': 'str'}}})
    res3.inputs['a'] = ''
    res3.save_lazy()

    res1 = resource.load('test1')
    res2 = resource.load('test2')
    res3 = resource.load('test3')
    res1.connect(res2)
    res2.connect(res3)
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()
    assert len(staged_log) == 3

    for item in staged_log:
        assert item.action == 'run'
        operations.move_to_commited(item.log_action)
    assert len(change.stage_changes()) == 0

    res1.disconnect(res2)
    staged_log = change.stage_changes()
    assert len(staged_log) == 2
    to_revert = []

    for item in staged_log:
        assert item.action == 'update'
        operations.move_to_commited(item.log_action)
        to_revert.append(item.uid)

    change.revert_uids(sorted(to_revert, reverse=True))
    ModelMeta.save_all_lazy()

    staged_log = change.stage_changes()

    assert len(staged_log) == 2
    for item in staged_log:
        assert item.diff == [['change', 'a', ['', '9']]]