def delete(cls, type_id, attr_ids=None): """ delete attributes from CIType :param type_id: :param attr_ids: list :return: """ from api.tasks.cmdb import ci_cache cls._check(type_id, attr_ids) for attr_id in attr_ids: existed = CITypeAttribute.get_by(type_id=type_id, attr_id=attr_id, first=True, to_dict=False) if existed is not None: existed.soft_delete() for ci in CI.get_by(type_id=type_id, to_dict=False): AttributeValueManager.delete_attr_value(attr_id, ci.id) ci_cache.apply_async([ci.id], queue=CMDB_QUEUE) CITypeAttributeCache.clean(type_id, attr_id) CITypeAttributesCache.clean(type_id)
def add(cls, ci_type_name, exist_policy=ExistPolicy.REPLACE, _no_attribute_policy=ExistPolicy.IGNORE, **ci_dict): """ :param ci_type_name: :param exist_policy: replace or reject or need :param _no_attribute_policy: ignore or reject :param ci_dict: :return: """ ci_type = CITypeManager.check_is_existed(ci_type_name) unique_key = AttributeCache.get(ci_type.unique_id) or abort( 400, 'illegality unique attribute') unique_value = ci_dict.get(unique_key.name) unique_value = unique_value or ci_dict.get(unique_key.alias) unique_value = unique_value or ci_dict.get(unique_key.id) unique_value = unique_value or abort( 400, '{0} missing'.format(unique_key.name)) existed = cls.ci_is_exist(unique_key, unique_value) if existed is not None: if exist_policy == ExistPolicy.REJECT: return abort(400, 'CI is already existed') if existed.type_id != ci_type.id: existed.update(type_id=ci_type.id) ci = existed else: if exist_policy == ExistPolicy.NEED: return abort(404, 'CI <{0}> does not exist'.format(unique_value)) ci = CI.create(type_id=ci_type.id) ci_type_attrs_name = [ attr["name"] for attr in CITypeAttributeManager().get_attributes_by_type_id(ci_type.id) ] value_manager = AttributeValueManager() for p, v in ci_dict.items(): if p not in ci_type_attrs_name: current_app.logger.warning( 'ci_type: {0} not has attribute {1}, please check!'.format( ci_type_name, p)) continue try: value_manager.create_or_update_attr_value( p, v, ci, _no_attribute_policy) except BadRequest as e: if existed is None: cls.delete(ci.id) raise e ci_cache.apply_async([ci.id], queue=CMDB_QUEUE) return ci.id
def update(self, ci_id, **ci_dict): self.confirm_ci_existed(ci_id) value_manager = AttributeValueManager() for p, v in ci_dict.items(): try: value_manager.create_or_update_attr_value(p, v, ci_id) except BadRequest as e: raise e ci_cache.apply_async([ci_id], queue=CMDB_QUEUE)
def get_ci_by_id_from_db(ci_id, ret_key=RetKey.NAME, fields=None, need_children=True, use_master=False): """ :param ci_id: :param ret_key: name, id or alias :param fields: list :param need_children: :param use_master: whether to use master db :return: """ ci = CI.get_by_id(ci_id) or abort(404, "CI <{0}> is not existed".format(ci_id)) res = dict() if need_children: children = CIRelationManager.get_children(ci_id, ret_key=ret_key) # one floor res.update(children) ci_type = CITypeCache.get(ci.type_id) res["ci_type"] = ci_type.name fields = CITypeAttributeManager.get_attr_names_by_type_id(ci.type_id) if not fields else fields unique_key = AttributeCache.get(ci_type.unique_id) _res = AttributeValueManager().get_attr_values(fields, ci_id, ret_key=ret_key, unique_key=unique_key, use_master=use_master) res.update(_res) res['type_id'] = ci_type.id res['ci_id'] = ci_id return res
def update_unique_value(ci_id, unique_name, unique_value): CI.get_by_id(ci_id) or abort(404, "CI <{0}> is not found".format(ci_id)) AttributeValueManager().create_or_update_attr_value( unique_name, unique_value, ci_id) ci_cache.apply_async([ci_id], queue=CMDB_QUEUE)
def update(self, ci_id, **ci_dict): ci = self.confirm_ci_existed(ci_id) ci_type_attrs_name = [ attr["name"] for attr in CITypeAttributeManager().get_attributes_by_type_id(ci.type_id) ] value_manager = AttributeValueManager() for p, v in ci_dict.items(): if p not in ci_type_attrs_name: current_app.logger.warning( 'ci_type: {0} not has attribute {1}, please check!'.format( ci.type_id, p)) continue try: value_manager.create_or_update_attr_value(p, v, ci) except BadRequest as e: raise e ci_cache.apply_async([ci_id], queue=CMDB_QUEUE)