コード例 #1
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': {}}}
            }
コード例 #2
0
ファイル: TestModels.py プロジェクト: jhoekx/melange
    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'
コード例 #3
0
ファイル: views.py プロジェクト: jhoekx/melange
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)
コード例 #4
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
コード例 #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'}}}
            }
コード例 #6
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'