Beispiel #1
0
    def test_log_on_variable(self):
        item = Item('fireflash')
        item.set_variable('hello', 'world')
        db_session.add(item)
        db_session.commit()

        logs = Log.find_all()
        assert len(logs) == 2
        assert 'hello' in logs[0].message or 'hello' in logs[1].message
Beispiel #2
0
    def test_api_add_child(self):
        Item('home').save()
        Item('fireflash').save()

        with app.test_client() as c:
            rv = self.get_json(c, '/api/item/home/')
            data = json.loads(rv.data)
            data['children'].append({'name':'fireflash'})
            rv = self.put_json(c, '/api/item/home/', data)
            print rv
            assert rv.status_code == 200
        fireflash = Item.find('fireflash')
        home = Item.find('home')
        assert fireflash.parents[0].name == 'home'
        assert home.children[0].name == 'fireflash'
Beispiel #3
0
def show_item(name):
    item = Item.find(name)
    if not item:
        abort(404)

    if request.method in ["POST", "DELETE"]:
        if "tag-add" in request.form:
            tag_name = request.form['tag-name']
            tag = Tag.find(tag_name)
            item.add_to(tag)
            item.save()
        elif "tag-remove" in request.form:
            tag_name = request.form['tag-name']
            tag = Tag.find(tag_name)
            item.remove_from(tag)
            item.save()
        elif "child-add" in request.form:
            child_name = request.form['child-name']
            child_item = Item.find(child_name)
            item.add_child(child_item)
            item.save()
        elif "child-remove" in request.form:
            child_name = request.form['child-name']
            child_item = Item.find(child_name)
            item.remove_child(child_item)
            item.save()
        elif "item-remove" in request.form:
            item.remove()
            return redirect(url_for("list_tags"))
        else:
            update_variables(item, request)

    var_list = []
    for var in item.to_data()['vars']:
        v = var['value']
        if type(v) in [str, int]:
            var['type'] = 'Text'
        elif type(v) == list:
            var['type'] = 'List'
        elif type(v) == dict:
            var['type'] = 'Map'
        var_list.append(var)

    tag_names = [tag.name for tag in item.tags]
    available_tags = [tag.name for tag in Tag.find_all()
                      if tag.name not in tag_names]

    return render_template('item.html', item=item, var_list=var_list, available_tags=available_tags)
Beispiel #4
0
def show_item(name):
    item = Item.find(name)
    if not item:
        abort(404)

    if request.method == 'DELETE':
        item.remove()
        response = make_response('', 200)
        return response

    if request.method == 'PUT':
        if not request.json:
            abort(415)
        try:
            if request.json['name'] != item.name:
                abort(400)
            item.update_from(request.json)
            item.save()
        except MelangeException:
            abort(400)

    def item_url(item):
        return url_for('melange_api.show_item', name=item.name)
    def tag_url(tag):
        return url_for('melange_api.show_tag', name=tag.name)
    data = item.to_data(item_href=item_url, tag_href=tag_url)

    response = make_response( json.dumps(data), 200 )
    response.headers['Content-Type'] = 'application/json'
    return response
Beispiel #5
0
    def test_ansible_inventory(self):
        fireflash = Item('fireflash')
        linux = Tag('linux')
        linux.set_variable('test', 'one')
        fireflash.add_to(linux)
        linux.save()

        with app.test_client() as c:
            rv = self.get_json(c, '/api/ansible_inventory/')
            data = json.loads(rv.data)
            assert rv.status_code == 200
            print data
            assert data == {
                'linux': {'hosts': ['fireflash']},
                '_meta': {'hostvars': {'fireflash': {'test': 'one'}}}
            }
Beispiel #6
0
    def test_duplicate_tag_variable(self):
        ''' variable in longest tag wins '''
        item = Item('firefly')
        laptop = Tag('laptop')
        laptop.set_variable('hello', 'laptop')
        linux = Tag('linux')
        linux.set_variable('hello', 'linux')
        item.add_to(laptop)
        item.add_to(linux)
        item.save()

        assert 'hello' in item.get_all_variables()
        assert item.get_all_variables()['hello'] == 'laptop'
Beispiel #7
0
    def test_api_set_tag_variable(self):
        fireflash = Item('fireflash')
        laptop = Tag('laptop')
        laptop.set_variable('test', 'one')
        fireflash.add_to(laptop)
        laptop.save()

        with app.test_client() as c:
            rv = self.get_json(c, '/api/tag/laptop/')
            data = json.loads(rv.data)
            data['vars']['test']= 'two'
            rv = self.put_json(c, '/api/tag/laptop/', data)
            print rv
            assert rv.status_code == 200
        fireflash = Item.find('fireflash')
        laptop = Tag.find('laptop')
        assert laptop.variables['test'] == 'two'
        assert fireflash.get_all_variables()['test'] == 'two'
Beispiel #8
0
def ansible_inventory():
    data = {}
    for tag in Tag.find_all():
        data[tag.name] = {'hosts': [host.name for host in tag.items]}
    data['_meta'] = {'hostvars': {}}
    for item in Item.find_all():
        vars = {}
        for var in item.to_data()['vars']:
            vars[var['key']] = var['value']
        data['_meta']['hostvars'][item.name] = vars
    ansible_groups = ['linux', 'ansible-managed']
    return json_response(keep_only(ansible_groups, data))
Beispiel #9
0
    def test_api_set_variable(self):
        Item('fireflash').save()

        with app.test_client() as c:
            rv = self.get_json(c, '/api/item/fireflash/')
            data = json.loads(rv.data)
            data['vars'].append({'key':'hello', 'value':'world'})
            rv = self.put_json(c, '/api/item/fireflash/', data)
            print rv
            assert rv.status_code == 200
        fireflash = Item.find('fireflash')
        assert fireflash.variables['hello'] == 'world'
Beispiel #10
0
def show_tag(name):
    tag = Tag.find(name)
    if not tag:
        abort(404)

    if request.method == 'POST':
        if not request.json:
            abort(415)
        data = request.json
        if not 'name' in data:
            abort(400)
        response_code = 200
        item = Item.find(data['name'])
        if not item:
            item = Item(data['name'])
            response_code = 201
        try:
            item.update_from(data)
        except MelangeException:
            abort(400)
        item.add_to(tag)
        item.save()
        response = make_response('', response_code)
        response.headers['Content-Location'] = url_for('melange_api.show_item',name=item.name)
        return response

    if request.method == 'DELETE':
        tag.remove()
        response = make_response('', 200)
        return response

    if request.method == 'PUT':
        if not request.json:
            abort(415)
        try:
            if request.json['name'] != tag.name:
                abort(400)
            tag.update_from(request.json)
            tag.save()
        except MelangeException:
            abort(400)

    def item_url(item):
        return url_for('melange_api.show_item', name=item.name)
    data = tag.to_data(item_href=item_url)

    response = make_response( json.dumps(data), 200 )
    response.headers['Content-Type'] = 'application/json'
    return response
Beispiel #11
0
    def test_api_remove_variable(self):
        fireflash = Item('fireflash')
        fireflash.set_variable('test', 'one')
        fireflash.set_variable('hello', 'world')
        fireflash.save()

        with app.test_client() as c:
            rv = self.get_json(c, '/api/item/fireflash/')
            data = json.loads(rv.data)
            data['vars'] = [{'key':'test', 'value':'one'}]
            rv = self.put_json(c, '/api/item/fireflash/', data)
            print rv
            assert rv.status_code == 200
        fireflash = Item.find('fireflash')
        assert len(fireflash.variables) == 1
Beispiel #12
0
 def test_api_create_item(self):
     Tag('laptop').save()
     data = {
         'name': 'fireflash',
     }
     with app.test_client() as c:
         rv = self.post_json(c, '/api/tag/laptop/', data)
         print rv
         assert rv.status_code == 201
     fireflash = Item.find('fireflash')
     assert fireflash is not None
     assert fireflash.name == 'fireflash'
     assert len(fireflash.tags) == 1
     assert fireflash.tags[0].name == 'laptop'
Beispiel #13
0
def list_items():
    if request.method == "POST":
        item_name = request.form["item-name"]
        item = Item.find(item_name)
        if not item:
            item = Item(item_name)
        if 'tag-name' in request.form:
            tag = Tag.find(request.form['tag-name'])
            if tag:
                item.add_to(tag)
        item.save()
        return redirect(url_for('show_item', name=item.name))
    items = Item.find_all()
    return render_template('items.html', items=items)
Beispiel #14
0
    def test_ansible_inventory_default_tags(self):
        fireflash = Item('fireflash')
        mole = Item('mole')
        fab1 = Item('fab1')
        linux = Tag('linux')
        ansible = Tag('ansible-managed')
        windows = Tag('windows')
        multiple = Tag('multiple')
        fireflash.add_to(linux)
        mole.add_to(ansible)
        fab1.add_to(windows)
        fireflash.add_to(multiple)
        fab1.add_to(multiple)
        linux.save()
        ansible.save()
        windows.save()
        multiple.save()

        with app.test_client() as c:
            rv = self.get_json(c, '/api/ansible_inventory/')
            data = json.loads(rv.data)
            assert rv.status_code == 200
            print data
            assert data == {
                'linux': {'hosts': ['fireflash']},
                'ansible-managed': {'hosts': ['mole']},
                'windows': {'hosts': []},
                'multiple': {'hosts': ['fireflash']},
                '_meta': {'hostvars': {'fireflash': {}, 'mole': {}}}
            }