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))])
def test_get_used(self): expected = ['192.168.1.101','192.168.1.102','192.168.1.103',\ '192.168.1.111','192.168.1.112','192.168.1.113',\ '192.168.1.121','192.168.1.100'] self.create_test_ip_data() ips = IP.get_used(db.session.query) actual = [r.ip for r in ips] self.assertListEqual(sorted(expected), sorted(actual))
def delete_unused_ip(ipaddr): ip = IP.get_one(db.session.query, ipaddr) if ip: if ip.is_used == 1: raise try: db.session.delete(ip) except Exception as e: raise
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)))
def update_ip(old_ipaddr): if not is_json_request(request) or not is_valid_keys(request.json, ["ip"]): abort(400) new_ipaddr = request.json["ip"] if not is_valid_ip(new_ipaddr): abort(400) try: check_registerable_ip(new_ipaddr) ip = IP.get_one(db.session.query, old_ipaddr) if ip.is_used == 1: ip.is_used = 1 ip.ip = new_ipaddr db.session.commit() except Exception as e: abort(500) return jsonify(result=[dict(message="OK", request="update ip", payload=str(request.json))])
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
def all_ip(): result = [] ips = IP.get_all(db.session.query) for ip in ips: result.append(dict(ip=ip.ip, is_used=ip.is_used)) return jsonify(result=result)
def check_registerable_ip(ipaddr): try: ip = IP.get_one(db.session.query, ipaddr) except Exception as e: raise
def list_ip_unused(): fmt = request.args.get("format") ips = IP.get_unused(db.session.query) result = [r.ip for r in ips] return formatter(fmt, result)
def test_get_one_miss(self): expected = None self.create_test_ip_data() ip = IP.get_one(db.session.query, '192.168.1.10') actual = ip self.assertEqual(expected, actual)
def list_ip(): fmt = request.args.get('format') ips = IP.get_all(db.session.query) result = [r.ip for r in ips] return formatter(fmt, result)