示例#1
0
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 test_get_in_role_names_miss(self):
     expected = []
     self.create_test_role_data()
     roles = Role.get_in_role_names(db.session.query, ['xx', 'yy', 'zz'])
     actual = [r.role_name for r in roles]
     self.assertListEqual(sorted(expected), sorted(actual))
 def test_get_in_role_names(self):
     expected = ['db', 'cache', 'vip']
     self.create_test_role_data()
     roles = Role.get_in_role_names(db.session.query, ['db', 'cache', 'vip'])
     actual = [r.role_name for r in roles]
     self.assertListEqual(sorted(expected), sorted(actual))