예제 #1
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
예제 #2
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)
예제 #3
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)