def drv_attr_make_diff(cls, context, dep_id, old_driver_attr_list, new_driver_attr_list): """Diff new dirver-side Attribute Object lists with the old one.""" LOG.info("Start differing attributes.") new_key_list = [ driver_attr_obj.key for driver_attr_obj in new_driver_attr_list ] old_key_list = [ driver_attr_obj.key for driver_attr_obj in old_driver_attr_list ] same = set(new_key_list) & set(old_key_list) # key is same, diff the value. for s in same: # value is not same, update new_driver_attr_obj = new_driver_attr_list[new_key_list.index(s)] old_driver_attr_obj = old_driver_attr_list[old_key_list.index(s)] if new_driver_attr_obj.value != old_driver_attr_obj.value: attr_obj = Attribute.get_by_dep_key(context, dep_id, s) attr_obj.value = new_driver_attr_obj.value attr_obj.save(context) # key is deleted. deleted = set(old_key_list) - same for d in deleted: old_driver_attr_obj = old_driver_attr_list[old_key_list.index(d)] old_driver_attr_obj.destroy(context, dep_id) # key is added. added = set(new_key_list) - same for a in added: new_driver_attr_obj = new_driver_attr_list[new_key_list.index(a)] new_driver_attr_obj.create(context, dep_id)
def save(self, context): """Update a Deployable record in the DB.""" updates = self.obj_get_changes() db_dep = self.dbapi.deployable_update(context, self.uuid, updates) self._from_db_object(self, db_dep) query = {"deployable_id": self.id} attr_get_list = Attribute.get_by_filter(context, query) self.attributes_list = attr_get_list
def get_by_host(cls, context, host): """Get a Deployable by host.""" db_deps = cls.dbapi.deployable_get_by_host(context, host) query = {"deployable_id": db_deps.id} attr_get_list = Attribute.get_by_filter(context, query) db_deps.attributes_list = attr_get_list return cls._from_db_object_list(db_deps, context)
def create(self, context, deployable_id): """Convert driver-side Attribute into Attribute Object so as to store in DB.""" attr_obj = Attribute() attr_obj.deployable_id = deployable_id attr_obj.set_key_value_pair(self.key, self.value) attr_obj.create(context)
def get_by_host(cls, context, host): """Get a Deployable by host.""" db_deps = cls.dbapi.deployable_get_by_host(context, host) obj_dpl_list = cls._from_db_object_list(db_deps, context) for obj_dpl in obj_dpl_list: query = {"deployable_id": obj_dpl.id} attr_get_list = Attribute.get_by_filter(context, query) obj_dpl.attributes_list = attr_get_list return obj_dpl_list
def get(cls, context, uuid): """Find a DB Deployable and return an Obj Deployable.""" db_dep = cls.dbapi.deployable_get(context, uuid) obj_dep = cls._from_db_object(cls(context), db_dep) # retrieve all the attrobutes for this deployable query = {"deployable_id": obj_dep.id} attr_get_list = Attribute.get_by_filter(context, query) obj_dep.attributes_list = attr_get_list return obj_dep
def add_attribute(self, context, key, value): """add a attribute object to the attribute_list. If the attribute already exists, it will update the value, otherwise, the vf will be appended to the list """ for exist_attr in self.attributes_list: if key == exist_attr.key: LOG.warning("The attribute already exists") if value != exist_attr.value: exist_attr.value = value exist_attr.save(context) return None # The attribute does not exist yet. Create it. attr_vals = {'deployable_id': self.id, 'key': key, 'value': value} attr = Attribute(context, **attr_vals) attr.create(context) self.attributes_list.append(attr)
def list(cls, context, deployable_id): """Form driver-side attribute list for one deployable.""" attr_obj_list = Attribute.get_by_deployable_id(context, deployable_id) driver_attr_obj_list = [] for attr_obj in attr_obj_list: driver_attr_obj = cls(context=context, key=attr_obj.key, value=attr_obj.value) driver_attr_obj_list.append(driver_attr_obj) return driver_attr_obj_list
def drv_attr_make_diff(self, context, dep_id, old_driver_attr_list, new_driver_attr_list): """Diff new driver-side Attribute Object lists with the old one.""" LOG.info("Start differing attributes.") dep_obj = Deployable.get_by_id(context, dep_id) driver_dep = DriverDeployable.get_by_name(context, dep_obj.name) rp_uuid = self.get_rp_uuid_from_obj(driver_dep) new_key_list = [ driver_attr_obj.key for driver_attr_obj in new_driver_attr_list ] old_key_list = [ driver_attr_obj.key for driver_attr_obj in old_driver_attr_list ] same = set(new_key_list) & set(old_key_list) # key is deleted. deleted = set(old_key_list) - same for d in deleted: old_driver_attr_obj = old_driver_attr_list[old_key_list.index(d)] self.placement_client.delete_trait_by_name( context, rp_uuid, old_driver_attr_obj.value) old_driver_attr_obj.delete_by_key(context, dep_id, d) # key is added. added = set(new_key_list) - same for a in added: new_driver_attr_obj = new_driver_attr_list[new_key_list.index(a)] new_driver_attr_obj.create(context, dep_id) self.placement_client.add_traits_to_rp(rp_uuid, [new_driver_attr_obj.value]) # key is same, diff the value. for s in same: # value is not same, update new_driver_attr_obj = new_driver_attr_list[new_key_list.index(s)] old_driver_attr_obj = old_driver_attr_list[old_key_list.index(s)] if new_driver_attr_obj.value != old_driver_attr_obj.value: attr_obj = Attribute.get_by_dep_key(context, dep_id, s) attr_obj.value = new_driver_attr_obj.value attr_obj.save(context) # Update traits here. if new_driver_attr_obj.key.startswith("trait"): self.placement_client.delete_trait_by_name( context, rp_uuid, old_driver_attr_obj.value) self.placement_client.add_traits_to_rp( rp_uuid, [new_driver_attr_obj.value]) # Update resource classes here. if new_driver_attr_obj.key.startswith("rc"): self.placement_client.ensure_resource_classes( context, [new_driver_attr_obj.value]) inv_data = _gen_resource_inventory( new_driver_attr_obj.value, dep_obj.num_accelerators) self.placement_client.update_inventory(rp_uuid, inv_data) self.placement_client.delete_rc_by_name( context, old_driver_attr_obj.value)
def get(cls, context, uuid, with_attribute_list=True): """Find a DB Deployable and return an Obj Deployable.""" db_dep = cls.dbapi.deployable_get(context, uuid) obj_dep = cls._from_db_object(cls(context), db_dep) # retrieve all the attributes for this deployable if with_attribute_list: query = {"deployable_id": obj_dep.id} attr_get_list = Attribute.get_by_filter(context, query) obj_dep.attributes_list = attr_get_list obj_dep.obj_reset_changes() return obj_dep
def get_by_filter(cls, context, filters): obj_dpl_list = [] db_dpl_list = cls.dbapi.deployable_get_by_filters_with_attributes( context, filters) if db_dpl_list: for db_dpl in db_dpl_list: obj_dpl = cls._from_db_object(cls(context), db_dpl) query = {"deployable_id": obj_dpl.id} attr_get_list = Attribute.get_by_filter(context, query) obj_dpl.attributes_list = attr_get_list obj_dpl_list.append(obj_dpl) return obj_dpl_list
def list(cls, context, filters={}): """Return a list of Deployable objects.""" if filters: sort_dir = filters.pop('sort_dir', 'desc') sort_key = filters.pop('sort_key', 'create_at') limit = filters.pop('limit', None) marker = filters.pop('marker_obj', None) db_deps = cls.dbapi.deployable_get_by_filters(context, filters, sort_dir=sort_dir, sort_key=sort_key, limit=limit, marker=marker) else: db_deps = cls.dbapi.deployable_list(context) obj_dpl_list = cls._from_db_object_list(db_deps, context) for obj_dpl in obj_dpl_list: query = {"deployable_id": obj_dpl.id} attr_get_list = Attribute.get_by_filter(context, query) obj_dpl.attributes_list = attr_get_list return obj_dpl_list
def delete_by_key(cls, context, deployable_id, key): """Delete driver-side attribute list from the DB.""" attr_obj_list = Attribute.get_by_deployable_id(context, deployable_id) for attr_obj in attr_obj_list: if key == attr_obj.key: attr_obj.destroy(context)