コード例 #1
0
ファイル: api.py プロジェクト: hiroakis/host-management-app
def update_host(old_host_name):
    if not is_json_request(request):
        return abort(400)

    req = request.json
    if not is_valid_keys(req, ["host_name", "ip", "role"]):
        abort(400)

    try:
        role_maps = RoleMap.get_by_host_name(db.session.query, old_host_name)
        for r in role_maps:
            rm = RoleMap.get_by_host_name_and_role_name(db.session.query, old_host_name, r.role_name)
            db.session.delete(rm)
            db.session.flush()

        host = Host.get_one_by_host_name(db.session.query, old_host_name)
        host.host_name = req["host_name"]
        old_ip = host.ip
        host.ip = req["ip"]
        host.role = [RoleMap(role_name=role_name) for role_name in req["role"]]
        db.session.add(host)

        if req["ip"] != old_ip:
            ip = IP.get_one(db.session.query, req["ip"])
            ip.is_used = 1
            ip = IP.get_one(db.session.query, old_ip)
            ip.is_used = 0

        db.session.commit()
    except Exception as e:
        abort(500)
    return jsonify(result=[dict(message="OK", request="update host", payload=str(request.json))])
コード例 #2
0
ファイル: api.py プロジェクト: hiroakis/host-management-app
def list_ip_by_role(role_name):
    fmt = request.args.get("format")

    role_maps = RoleMap.get_by_role_name(db.session.query, role_name)
    host_names = [role_map.host_name for role_map in role_maps]
    hosts = Host.get_in_host_names(db.session.query, host_names)
    result = [r.ip for r in hosts]
    return formatter(fmt, result)
コード例 #3
0
 def test_get_one_by_host_name(self):
     expected = ('web01', '192.168.1.101')
     self.create_test_role_data()
     self.create_test_ip_data()
     self.create_test_host_data()
     self.create_test_role_map_data()
     host = Host.get_one_by_host_name(db.session.query, 'web01')
     actual = (host.host_name, host.ip)
     self.assertEqual(expected, actual)
コード例 #4
0
ファイル: api.py プロジェクト: hiroakis/host-management-app
def output_hosts(role_name):
    role_maps = RoleMap.get_by_role_name(db.session.query, role_name)
    host_names = [role_map.host_name for role_map in role_maps]

    hosts = Host.get_in_host_names(db.session.query, host_names)
    if len(hosts) == 0:
        abort(404)

    result = [dict(host_name=host.host_name, ip=host.ip) for host in hosts]
    return formatter("hosts", result)
コード例 #5
0
ファイル: api.py プロジェクト: hiroakis/host-management-app
def search_by_host(host_name):
    host = Host.get_one_by_host_name(db.session.query, host_name)
    if not host:
        abort(404)

    role_maps = RoleMap.get_by_host_name(db.session.query, host.host_name)
    role_names = [r.role_name for r in role_maps]

    result = [dict(host_name=host.host_name, ip=host.ip, role=role_names)]
    return jsonify(result=result)
コード例 #6
0
ファイル: api.py プロジェクト: hiroakis/host-management-app
def delete_host(host_name):
    try:
        host = Host.get_one_by_host_name(db.session.query, host_name)
        if host:
            db.session.delete(host)
            ip = IP.get_one(db.session.query, host.ip)
            ip.is_used = 0
            db.session.commit()
    except Exception as e:
        abort(500)
    return jsonify(result=dict(message="OK", request="delete host", payload=str(request.json)))
コード例 #7
0
ファイル: api.py プロジェクト: hiroakis/host-management-app
def register_new_host(host_name, ipaddr, role_names):
    try:
        # is exist unused ip
        ip = IP.get_one(db.session.query, ipaddr)
        if not ip or ip.is_used == 1:
            raise Exception("ip does not exist or used")
        ip.is_used = 1
        db.session.flush()

        # is exist role in db
        roles = Role.get_in_role_names(db.session.query, role_names)
        if len(roles) != len(role_names):
            raise Exception("role not found")

        # insert host
        host = Host(host_name=host_name, ip=ipaddr)
        host.role = [RoleMap(role_name=role_name) for role_name in role_names]
        db.session.add(host)

    except Exception as e:
        raise e
コード例 #8
0
 def test_get_all(self):
     expected = [('web01', '192.168.1.101'),\
             ('web02', '192.168.1.102'),\
             ('mem01', '192.168.1.121'),\
             ('vip01', '192.168.1.100')]
     self.create_test_role_data()
     self.create_test_ip_data()
     self.create_test_host_data()
     self.create_test_role_map_data()
     hosts = Host.get_in_host_names(db.session.query, ['web01', 'web02', 'mem01', 'vip01'])
     actual = [(h.host_name, h.ip) for h in hosts]
     self.assertListEqual(sorted(expected), sorted(actual))
コード例 #9
0
ファイル: api.py プロジェクト: hiroakis/host-management-app
def all_host():
    hosts = Host.get_all(db.session.query)
    if len(hosts) == 0:
        abort(404)

    result = []
    for host in hosts:
        d = {}
        d["host_name"] = host.host_name
        d["ip"] = host.ip
        d["role"] = [r.role_name for r in host.role]
        result.append(d)
    return jsonify(result=result)
コード例 #10
0
ファイル: api.py プロジェクト: hiroakis/host-management-app
def search_by_role(role_name):
    role_maps = RoleMap.get_by_role_name(db.session.query, role_name)
    host_names = [r.host_name for r in role_maps]

    hosts = Host.get_in_host_names(db.session.query, host_names)
    if len(hosts) == 0:
        abort(404)

    result = []
    for host in hosts:
        d = {}
        d["host_name"] = host.host_name
        d["ip"] = host.ip
        d["role"] = [r.role_name for r in host.role]
        result.append(d)
    return jsonify(result=result)
コード例 #11
0
ファイル: api.py プロジェクト: hiroakis/host-management-app
def list_host():
    fmt = request.args.get("format")

    hosts = Host.get_all(db.session.query)
    result = [r.host_name for r in hosts]
    return formatter(fmt, result)