示例#1
0
    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)
示例#2
0
    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
示例#3
0
    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)
示例#4
0
    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)
示例#5
0
文件: ci.py 项目: xuweiwei2011/cmdb-1
 def get(self, ci_id=None):
     from api.tasks.cmdb import ci_cache
     from api.lib.cmdb.const import CMDB_QUEUE
     if ci_id is not None:
         ci_cache.apply_async([ci_id], queue=CMDB_QUEUE)
     else:
         cis = CI.get_by(to_dict=False)
         for ci in cis:
             ci_cache.apply_async([ci.id], queue=CMDB_QUEUE)
     return self.jsonify(code=200)
示例#6
0
    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)