Exemplo n.º 1
0
    def post(self, request, *args, **kwargs):

        try:
            data = json.loads(request.POST.get('data', '{}'))
            # 从参数中获取需要修改的规则uuid
            rule_uuid = data.get('rule_uuid', None)
            # 从参数中获取需要修改的策略组的下标
            strategy_index = data.get('strategy_index', None)
            # 从参数中获取前端传参过来的策略列表
            new_strategy_confs = data.get('strategy_list', None)
            assert strategy_index is not None
            assert isinstance(strategy_index, int) and strategy_index >= 0
            assert all((rule_uuid, new_strategy_confs))
        except (AssertionError, TypeError, ValueError):
            return self.render_json_response({'state': False, 'msg': '参数校验失败'})

        client = get_redis_client()

        try:
            # 从redis中获取需要修改的规则所有数据
            rule_conf = client.hgetall("rule:{}".format(rule_uuid))
            # 从规则数据中获取策略组数据列表
            strategy_confs = json.loads(rule_conf.get('strategys'))
            # 从策略组列表中找出需要编辑的策略组
            strategy_conf = strategy_confs[strategy_index]
        except (TypeError, ValueError, IndexError, RedisError):
            return self.render_json_response({'state': False, 'msg': '配置读取失败'})

        strategys_obj = Strategys()
        strategy_conf['strategy_list'] = [[
            x['strategy_uuid'], x['threshold_list'],
            strategys_obj.build_strategy_name_from_thresholds(
                x['strategy_uuid'], x['threshold_list'])
        ] for x in new_strategy_confs]  # 构造编辑后的策略组

        strategy_confs[strategy_index] = strategy_conf  # 回写策略组
        rule_conf["strategys"] = json.dumps(strategy_confs)
        rule_conf['user'] = request.user.username
        rule_conf['update_time'] = str(int(time.time()))
        try:
            client.hmset("rule:{}".format(rule_uuid), rule_conf)  # 回写规则数据
        except RedisError:
            return self.render_json_response({'state': False, 'msg': '配置回写失败'})

        return self.render_json_response({'state': True})
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):

        try:
            data = json.loads(request.POST.get('data', '{}'))
            # Get the rule uuid from the parameters that need to be modified
            rule_uuid = data.get('rule_uuid', None)
            # Get the subscript of the policy group that needs to be modified from the parameters
            strategy_index = data.get('strategy_index', None)
            # Get a list of policies from the parameters that the front end passes through
            new_strategy_confs = data.get('strategy_list', None)

            assert strategy_index is not None
            assert isinstance(strategy_index, int) and strategy_index >= 0
            assert all((rule_uuid, new_strategy_confs))

        except (AssertionError, TypeError, ValueError):
            return self.render_json_response({
                'state': False,
                'msg': 'Argument check failed'
            })

        client = get_redis_client()

        try:
            # Get all the data from redis rules that need to be modified
            rule_conf = client.hgetall("rule:{}".format(rule_uuid))
            # Get a list of policy group data from rule data
            strategy_confs = json.loads(rule_conf.get('strategys'))
            # Find the policy group that needs to be edited from the Strategy Group list
            strategy_conf = strategy_confs[strategy_index]
        except (TypeError, ValueError, IndexError, RedisError):
            return self.render_json_response({
                'state':
                False,
                'msg':
                _('Configuration read failed')
            })

        strategys_obj = Strategys()
        strategy_conf['strategy_list'] = [
            [
                x['strategy_uuid'], x['threshold_list'],
                strategys_obj.build_strategy_name_from_thresholds(
                    x['strategy_uuid'], x['threshold_list'])
            ] for x in new_strategy_confs
        ]  # Constructing the edited strategy group

        strategy_confs[
            strategy_index] = strategy_conf  # Write Back Strategy Group
        rule_conf["strategys"] = json.dumps(strategy_confs)
        rule_conf["user"] = request.user.username
        rule_conf["update_time"] = str(int(time.time()))
        try:
            client.hmset("rule:{}".format(rule_uuid),
                         rule_conf)  # Write back rule data
        except RedisError:
            return self.render_json_response({
                'state':
                False,
                'msg':
                _('Configuration write-back failed')
            })

        return self.render_json_response({'state': True})