def set_rows(db_session, key, values, sg=None, sg_name=None): """ Set a whole key type to a list of values This is always called from command line or wsgi configuration code. """ if sg_name: sg = find_sg_byname(db_session, sg_name) if not sg: raise NoSgFound(sg_name) sg_id = sg.id_ if sg else None try: # Easiest to delete and recreate stuff = db_session.query(ZoneCfg).filter(and_(ZoneCfg.key == key, ZoneCfg.sg_id == sg_id)).all() for zone_cfg in stuff: db_session.delete(zone_cfg) except NoResultFound: pass for value in values: zone_cfg = ZoneCfg(key, value, sg_id) db_session.add(zone_cfg) # Flush should reconstruct sg zone_cfg_entries lists? # Any how, this function is called in configuration code, and this is # the end of the query group for that. ie - data will commited on function # call return db_session.flush()
def set_rows(db_session, key, values, sg=None, sg_name=None): """ Set a whole key type to a list of values This is always called from command line or wsgi configuration code. """ if sg_name: sg = find_sg_byname(db_session, sg_name) if not sg: raise NoSgFound(sg_name) sg_id = sg.id_ if sg else None try: # Easiest to delete and recreate stuff = db_session.query(ZoneCfg)\ .filter(and_(ZoneCfg.key == key, ZoneCfg.sg_id == sg_id)).all() for zone_cfg in stuff: db_session.delete(zone_cfg) except NoResultFound: pass for value in values: zone_cfg = ZoneCfg(key, value, sg_id) db_session.add(zone_cfg) # Flush should reconstruct sg zone_cfg_entries lists? # Any how, this function is called in configuration code, and this is # the end of the query group for that. ie - data will commited on function # call return db_session.flush()
def set_row(db_session, key, value, sg=None, sg_name=None): """ Set one row to a given value This is always called from command line or wsgi configuration code. """ if sg_name: sg = find_sg_byname(db_session, sg_name) if not sg: raise NoSgFound(sg_name) if sg: stuff = [x for x in sg.zone_cfg_entries if x.key == key] if stuff: zone_cfg = stuff[0] zone_cfg.value = value db_session.flush() return zone_cfg = ZoneCfg(key, value) db_session.add(zone_cfg) sg.zone_cfg_entries.append(zone_cfg) db_session.flush() return # We have reached the part which processes the case of no SG being given # to function try: stuff = db_session.query(ZoneCfg).filter(and_(ZoneCfg.key == key, ZoneCfg.sg_id == None)).one() zone_cfg = stuff zone_cfg.value = value except NoResultFound: zone_cfg = ZoneCfg(key, value) db_session.add(zone_cfg) finally: db_session.flush()
def show_apex_ns(self, sg_name=None): """ Display the apex NS server settings """ # Deal with SA auto-BEGIN - want fresh transaction to see fresh data self._begin_op() sg = find_sg_byname(self.db_session, sg_name) if not sg: raise NoSgFound(sg_name) result = zone_cfg.get_rows_exc(self.db_session, settings['apex_ns_key'], sg_name=sg_name) return result
def move_server_sg(db_session, server_name, sg_name=None): """ Move a server between SGs """ server_sm = find_server_byname(db_session, server_name) if not server_sm.is_disabled(): raise ServerNotDisabled(server_sm.name) if not sg_name: sg_name = zone_cfg.get_row_exc(db_session, 'default_sg') if not sg_name in list_all_sgs(db_session): raise NoSgFound(sg_name) sg = find_sg_byname(db_session, sg_name, raise_exc=True) server_sm.set_sg(sg)
def get_rows(db_session, key, sg=None, sg_name=None, raise_exc=False): """ Return all the values for a key as a list """ result = [] if sg_name: sg = find_sg_byname(db_session, sg_name) if sg: stuff = [x for x in sg.zone_cfg_entries if x.key == key] result = [x.value for x in stuff] if result: return result try: stuff = db_session.query(ZoneCfg).filter(and_(ZoneCfg.key == key, ZoneCfg.sg_id == None)).all() result = [x.value for x in stuff] except NoResultFound: result = [] if raise_exc and not result: raise ZoneCfgItemNotFound(key) return result
def get_rows(db_session, key, sg=None, sg_name=None, raise_exc=False): """ Return all the values for a key as a list """ result = [] if sg_name: sg = find_sg_byname(db_session, sg_name) if sg: stuff = [x for x in sg.zone_cfg_entries if x.key == key] result = [x.value for x in stuff] if result: return result try: stuff = db_session.query(ZoneCfg)\ .filter(and_(ZoneCfg.key == key, ZoneCfg.sg_id == None)).all() result = [x.value for x in stuff] except NoResultFound: result = [] if raise_exc and not result: raise ZoneCfgItemNotFound(key) return result
def get_row(db_session, key, sg=None, sg_name=None, raise_exc=False): """ Return the first value found for a key if key not found in sg, return value where sg_id is None """ result = None if sg_name: sg = find_sg_byname(db_session, sg_name) if sg: stuff = [x for x in sg.zone_cfg_entries if x.key == key] if stuff: result = stuff[0].value return result try: stuff = db_session.query(ZoneCfg).filter(and_(ZoneCfg.key == key, ZoneCfg.sg_id == None)).all() result = stuff[0].value except IndexError: result = None if raise_exc and not result: raise ZoneCfgItemNotFound(key) return result
def new_server(db_session, server_name, address, sg_name, server_type=None, ssh_address=None): """ Create a new server """ server_name = server_name.lower() if server_name.endswith('.'): server_name = server_name[:-1] if not sg_name: sg_name = zone_cfg.get_row_exc(db_session, 'default_sg') if not sg_name in list_all_sgs(db_session): raise NoSgFound(sg_name) try: server_list = db_session.query(ServerSM)\ .filter(ServerSM.name == server_name).all() if len(server_list): raise ServerExists(server_name) except NoResultFound: pass if not server_type: server_type = zone_cfg.get_row(db_session, 'default_stype', raise_exc=True) server_sm = ServerSM(server_name, address, sg_name, server_type, ssh_address) try: db_session.add(server_sm) db_session.flush() except IntegrityError as exc: raise ServerAddressExists(address) sg = find_sg_byname(db_session, sg_name, raise_exc=True) server_sm.set_sg(sg) db_session.flush() return server_sm
def get_row(db_session, key, sg=None, sg_name=None, raise_exc=False): """ Return the first value found for a key if key not found in sg, return value where sg_id is None """ result = None if sg_name: sg = find_sg_byname(db_session, sg_name) if sg: stuff = [x for x in sg.zone_cfg_entries if x.key == key] if stuff: result = stuff[0].value return result try: stuff = db_session.query(ZoneCfg)\ .filter(and_(ZoneCfg.key == key, ZoneCfg.sg_id == None)).all() result = stuff[0].value except IndexError: result = None if raise_exc and not result: raise ZoneCfgItemNotFound(key) return result
def set_row(db_session, key, value, sg=None, sg_name=None): """ Set one row to a given value This is always called from command line or wsgi configuration code. """ if sg_name: sg = find_sg_byname(db_session, sg_name) if not sg: raise NoSgFound(sg_name) if sg: stuff = [x for x in sg.zone_cfg_entries if x.key == key] if stuff: zone_cfg = stuff[0] zone_cfg.value = value db_session.flush() return zone_cfg = ZoneCfg(key, value) db_session.add(zone_cfg) sg.zone_cfg_entries.append(zone_cfg) db_session.flush() return # We have reached the part which processes the case of no SG being given # to function try: stuff = db_session.query(ZoneCfg)\ .filter(and_(ZoneCfg.key == key, ZoneCfg.sg_id == None)).one() zone_cfg = stuff zone_cfg.value = value except NoResultFound: zone_cfg = ZoneCfg(key, value) db_session.add(zone_cfg) finally: db_session.flush()