Esempio n. 1
0
def list_tags():
    if request.method == "POST":
        tag_name = request.form["tag-name"]
        tag = Tag.find(tag_name)
        if not tag:
            tag = Tag(tag_name)
            tag.save()
    tags = Tag.find_all()
    return render_template('tags.html', tags=tags)
Esempio n. 2
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)
Esempio n. 3
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'}}}
            }
Esempio n. 4
0
def tag_items():
    def item_url(item):
        return url_for('melange_api.show_item', name=item.name)
    tags = [tag.to_data(item_href=item_url) for tag in Tag.find_all()]
    for tag in tags:
        tag['href'] = url_for('melange_api.show_tag',name=tag['name'])

    response = make_response( json.dumps(tags), 200 )
    response.headers['Content-Type'] = 'application/json'
    return response
Esempio n. 5
0
 def test_api_create_tag(self):
     data = {
         'name': 'laptop'
     }
     with app.test_client() as c:
         rv = self.post_json(c, '/api/tag/', data)
         print rv
     laptop = Tag.find('laptop')
     assert laptop is not None
     assert laptop.name == 'laptop'
Esempio n. 6
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))
Esempio n. 7
0
    def test_api_add_tag(self):
        Item('fireflash').save()
        Tag('laptop').save()

        with app.test_client() as c:
            rv = self.get_json(c, '/api/item/fireflash/')
            data = json.loads(rv.data)
            data['tags'].append({'name':'laptop'})
            rv = self.put_json(c, '/api/item/fireflash/', data)
            print rv
            assert rv.status_code == 200
        laptop = Tag.find('laptop')
        assert laptop.items[0].name == 'fireflash'
Esempio n. 8
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)
Esempio n. 9
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
Esempio n. 10
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'
Esempio n. 11
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'
Esempio n. 12
0
def show_tag(name):
    tag = Tag.find(name)

    if request.method == "POST":
        if "tag-remove" in request.form:
            tag.remove()
            return redirect(url_for("list_tags"))
        else:
            update_variables(tag, request)

    var_list = []
    for k, v in tag.variables.items():
        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({
            'key': k,
            'value': v,
            'type': var_type,
        })
    return render_template("tag.html", tag=tag, var_list=var_list)
Esempio n. 13
0
def check_items():
    tag_names = [ tag_name.strip() for tag_name in request.args.get('check-tags','').split(',') ]
    variable_name = request.args.get('check-variable', '')
    condition = request.args.get('check-condition', '')

    items = {}
    for tag_name in tag_names:
        tag = Tag.find(tag_name)
        if not tag:
            continue
        for item in tag.items:
            if item.name not in items:
                items[item.name] = item

    results = {}
    for item in items.values():
        variables = item.get_all_variables()
        if variable_name and variable_name in variables:
            if condition:
                if not re.search(condition, variables[variable_name]):
                    continue
            results[item.name] = variables[variable_name]

    return render_template('check.html', results=results, tags=tag_names, variable_name=variable_name, condition=condition)
Esempio n. 14
0
def list_tags():
    if request.method == 'POST':
        if not request.json:
            abort(415)
        data = request.json
        if not 'name' in data:
            abort(400)
        if Tag.find(data['name']):
            abort(409)
        tag = Tag(data['name'])
        try:
            tag.update_from(data)
        except MelangeException:
            abort(400)
        tag.save()
        response = make_response('', 201)
        response.headers['Content-Location'] = url_for('melange_api.show_tag',name=tag.name)
        return response

    tags = [ {'name':tag.name, 'href':url_for('melange_api.show_tag', name=tag.name)} for tag in Tag.find_all() ]

    response = make_response( json.dumps(tags), 200 )
    response.headers['Content-Type'] = 'application/json'
    return response
Esempio n. 15
0
def show_frontpage():
    return render_template('front.html', tags=Tag.find_all())
Esempio n. 16
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': {}}}
            }