def asset_create(rack, **kwargs): with Session() as session: asset = models.Asset() asset.update(kwargs) asset.rack_id = rack.id asset.save(session) return asset
def server_add_interface(server, **kwargs): with Session() as session: iface = models.ServerInterface() for k, v in kwargs.items(): setattr(iface, k, v) iface.server_id = server.id iface.save(session)
def rack_update(rack): """ :type rack: models.Rack :rtype: models.Rack """ with Session() as session: rack.save(session) return rack
def update(obj, log=False): with Session() as session: if log: log_obj = models.ChangeLog() log_obj.type = obj.__tablename__ log_obj.object_id = obj.id log_obj.new, log_obj.old = obj.get_changes() log_obj.save() obj.save(session) return obj
def server_get_by(cls, **kwargs): """ :param kwargs: filters joined by AND logic :rtype: models.Server """ join = [models.Asset, models.Rack] filters = {'asset.location': CONF.common.location} filters.update(kwargs) with Session() as session: return cls._server_base(join, session=session, **filters).one()
def rack_get(cls, **kwargs): """ :param kwargs: args to be used as a filter to get rack. Are joined using 'and' logic :rtype: models.Rack """ with Session() as session: obj_cls = models.Rack return cls._object_get_by(obj_cls, [], ['_network_map', '_worker'], session=session, **kwargs).one()
def pxe_boot_create(cls, serial, lock_id): """ :param kwargs: filters joined by AND logic :rtype: models.PxEBoot """ pxe_boot = models.PxEBoot() pxe_boot.serial = serial pxe_boot.lock_id = lock_id with Session() as session: pxe_boot.save(session) return pxe_boot
def rack_get_all(cls, **kwargs): """ :type kwargs: dict :rtype: list of models.Rack """ with Session() as session: joins = [] if [k for k in kwargs.keys() if 'network_map.' in k]: joins.append(models.NetworkMap) if [k for k in kwargs.keys() if 'worker.' in k]: joins.append(models.Worker) return cls._object_get_by(models.Rack, joins, ['_worker', '_network_map'], session=session, **kwargs).all()
def object_get(object_type, key, key_value): """ Object get by key :param object_type: name of the DB object to get :type object_type: str :param key: name of the field used as a query key :type key: str :param key_value: value to be used for a query :type key_value: str :rtype: models.DaoBase """ with Session() as session: obj_cls = getattr(models, object_type) key_field = getattr(obj_cls, key) return (model_query( obj_cls, session=session).filter(key_field == key_value).one())
def worker_register(name, worker_url, location): """ Ensure worker record exists. Update worker_url field. :rtype: models.Worker """ with Session() as session: try: worker = model_query(models.Worker, session=session).filter_by( name=name, location=location).one() worker.worker_url = worker_url worker.save(session) except exceptions.DAONotFound: worker = models.Worker() worker.worker_url = worker_url worker.name = name worker.location = location worker.save(session) return worker
def server_create(self, cluster, asset, **kwargs): """ :type server: models.Asset :return: """ try: server = models.Server() server.update(kwargs) server.asset_id = asset.id server.cluster_id = cluster.id self.server_get_by(**{'asset.serial': asset.serial}) raise exceptions.DAOConflict('Server %r exists' % server) except exceptions.DAONotFound: with Session() as session: server.save(session) return server
def sku_create(self, location, name, cpu, ram, storage, description=''): """ Create new SKU object :rtype: models.Sku """ with Session() as session: try: self.sku_get(name) raise exceptions.DAOConflict( 'SKU <{0}> already exists'.format(name)) except exceptions.DAONotFound: sku = models.Sku() sku.name = name sku.location = location or CONF.common.location sku.cpu = cpu sku.ram = ram sku.storage = storage sku.description = description sku.save(session) return sku
def port_create(rack_name, device_id, vlan_tag, mac, ip, subnet_id): """ Create new port record :type rack_name: str :type device_id: str :type vlan_tag: int :type mac: str :type ip: str :type subnet_id: int :rtype: dao.control.db.model.Port """ with Session() as session: ports = model_query(models.Port, session=session).\ filter_by(ip=ip).all() if ports: raise exceptions.DAOConflict('Port for ip %r exists' % ip) p = models.Port() p.device_id = device_id p.rack_name = rack_name p.vlan_tag = vlan_tag p.ip = ip p.mac = mac p.subnet_id = subnet_id p.save(session=session) return p
def rack_get_by_subnet_ip(cls, ip): """ :type ip: str :rtype: models.Rack """ with Session() as session: nds = cls._object_get_by( models.NetworkDevice, [models.SwitchInterface], # Join on interfaces ['asset.rack._network_map', '_interfaces'], session=session, **{ '_interfaces.net_ip': ip }).all() racks = set(nd.asset.rack.id for nd in nds) if racks: if len(racks) == 1: return nds[0].asset.rack else: raise exceptions.DAOManyFound( 'More than one rack for {0}'.format(ip)) else: raise exceptions.DAONotFound( 'No rack found for {0}'.format(ip))
def servers_get_by_worker(self, worker, **kwargs): with Session() as session: kwargs['asset.rack.worker_id'] = worker.id return self.servers_get_by(session=session, **kwargs)
def asset_get_by(self, **kwargs): with Session() as session: r = self._object_get_by(models.Asset, [models.Rack], ['rack'], session=session, **kwargs) return r.one()