コード例 #1
0
    def put(self, request, *args, **kwargs):
        """
        编辑http策略
        """
        try:
            container_port = request.data.get("container_port", None)
            domain_name = request.data.get("domain_name", None)
            certificate_id = request.data.get("certificate_id", None)
            service_id = request.data.get("service_id", None)
            domain_path = request.data.get("domain_path", None)
            domain_cookie = request.data.get("domain_cookie", None)
            domain_heander = request.data.get("domain_heander", None)
            rule_extensions = request.data.get("rule_extensions", None)
            http_rule_id = request.data.get("http_rule_id", None)
            the_weight = request.data.get("the_weight", 100)

            # 判断参数
            if not service_id or not container_port or not domain_name or not http_rule_id:
                return Response(general_message(400, "parameters are missing", "参数缺失"), status=400)

            service = service_repo.get_service_by_service_id(service_id)
            if not service:
                return Response(general_message(400, "not service", "服务不存在"), status=400)

            # 判断域名格式(如果用户添加的域名与默认域名后缀一致,那么他后缀必须是 "租户别名.默认后缀"
            #
            # 比如默认域名后缀是:37e53f.grapps.cn  这个值来自于region_info  http_domain
            # 那么如果它绑定 xxx.37e53f.grapps.cn是不允许的,只能绑定:
            # xxx.yaufe6r5.37e53f.grapps.cn
            #
            # 此限制是防止租户之间盗用域名。)
            region = region_repo.get_region_by_region_name(service.service_region)
            if domain_name.endswith(region.httpdomain):
                domain_name_spt = domain_name.split(region.httpdomain)
                if self.tenant.tenant_name != domain_name_spt[0].split('.')[len(domain_name_spt[0].split('.'))-2]:
                    return Response(general_message(400, "the domain name format is incorrect", "域名格式不正确"), status=400)

            # 编辑域名
            code, msg, data = domain_service.update_httpdomain(self.tenant, self.user, service, domain_name, container_port,
                                                   certificate_id, DomainType.WWW, domain_path,
                                                   domain_cookie, domain_heander, http_rule_id, the_weight, rule_extensions)

            if code != 200:
                return Response(general_message(code, "bind domain error", msg), status=code)

            result = general_message(200, "success", "策略编辑成功")
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])
コード例 #2
0
    def put(self, request, app_id, rule_id, *args, **kwargs):
        ads = UpdatePostHTTPGatewayRuleSerializer(data=request.data)
        ads.is_valid(raise_exception=True)
        app = group_service.get_app_by_id(self.team, self.region_name, app_id)
        if not app:
            raise ErrAppNotFound
        httpdomain = ads.data
        service = service_repo.get_service_by_tenant_and_id(self.team.tenant_id, httpdomain["service_id"])
        if not service:
            rst = {"msg": "组件不存在"}
            return Response(rst, status=status.HTTP_400_BAD_REQUEST)
        data = domain_service.update_httpdomain(self.team, service, rule_id, ads.data, True)

        re = HTTPGatewayRuleSerializer(data=model_to_dict(data))
        re.is_valid()
        return Response(re.data, status=status.HTTP_200_OK)