Example #1
0
 def delete(self, request, app_id, rule_id, *args, **kwargs):
     rule = domain_service.get_http_rule_by_id(self.team.tenant_id, rule_id)
     if not rule:
         raise ErrNotFoundDomain
     domain_service.unbind_httpdomain(self.team, self.region_name, rule_id)
     re = HTTPGatewayRuleSerializer(data=model_to_dict(rule))
     re.is_valid()
     return Response(re.data, status=status.HTTP_200_OK)
    def post(self, request, app_id, *args, **kwargs):
        ads = PostHTTPGatewayRuleSerializer(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
        # Compatible history code
        httpdomain["domain_heander"] = httpdomain.get("domain_header", None)
        httpdomain["domain_type"] = DomainType.WWW
        protocol = "http"
        if httpdomain.get("certificate_id", None):
            protocol = "https"
        httpdomain["protocol"] = protocol
        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)
        if domain_service.check_domain_exist(
                httpdomain["service_id"], httpdomain["container_port"],
                httpdomain["domain_name"], protocol,
                httpdomain.get("domain_path"),
                httpdomain.get("rule_extensions")):
            rst = {"msg": "策略已存在"}
            return Response(rst, status=status.HTTP_400_BAD_REQUEST)

        if service.service_source == "third_party":
            msg, msg_show, code = port_service.check_domain_thirdpart(
                self.team, service)
            if code != 200:
                logger.exception(msg, msg_show)
                return Response({
                    "msg": msg,
                    "msg_show": msg_show
                },
                                status=code)
        if httpdomain.get("whether_open", True):
            tenant_service_port = port_service.get_service_port_by_port(
                service, httpdomain["container_port"])
            # 仅开启对外端口
            code, msg, data = port_service.manage_port(
                self.team, service, service.service_region,
                int(tenant_service_port.container_port), "only_open_outer",
                tenant_service_port.protocol, tenant_service_port.port_alias)
            if code != 200:
                return Response({"msg": "change port fail"}, status=code)
        tenant_service_port = port_service.get_service_port_by_port(
            service, httpdomain["container_port"])
        if not tenant_service_port.is_outer_service:
            return Response({"msg": "没有开启对外端口"},
                            status=status.HTTP_400_BAD_REQUEST)
        data = domain_service.bind_httpdomain(self.team, self.request.user,
                                              service, httpdomain, True)
        serializer = HTTPGatewayRuleSerializer(data=data.to_dict())
        serializer.is_valid()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
Example #3
0
class UpdateAppGatewayHTTPRuleView(TeamAppAPIView):
    @swagger_auto_schema(
        operation_description="获取应用http访问策略详情",
        manual_parameters=[
            openapi.Parameter("app_id", openapi.IN_PATH, description="应用ID", type=openapi.TYPE_INTEGER),
            openapi.Parameter("rule_id", openapi.IN_PATH, description="网关策略id", type=openapi.TYPE_STRING),
        ],
        responses={200: HTTPGatewayRuleSerializer()},
        tags=['openapi-gateway'],
    )
    def get(self, req, app_id, rule_id, *args, **kwargs):
        rule = domain_service.get_http_rules_by_app_id(self.app.ID).filter(http_rule_id=rule_id).first()
        re = HTTPGatewayRuleSerializer(rule)
        return Response(re.data, status=status.HTTP_200_OK)

    @swagger_auto_schema(
        operation_description="更新HTTP访问策略",
        manual_parameters=[
            openapi.Parameter("app_id", openapi.IN_PATH, description="应用ID", type=openapi.TYPE_INTEGER),
            openapi.Parameter("rule_id", openapi.IN_PATH, description="网关策略id", type=openapi.TYPE_STRING),
        ],
        request_body=UpdatePostHTTPGatewayRuleSerializer(),
        responses={200: HTTPGatewayRuleSerializer()},
        tags=['openapi-gateway'],
    )
    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)

    @swagger_auto_schema(
        operation_description="删除HTTP访问策略",
        manual_parameters=[],
        responses={200: HTTPGatewayRuleSerializer()},
        tags=['openapi-gateway'],
    )
    def delete(self, request, app_id, rule_id, *args, **kwargs):
        rule = domain_service.get_http_rule_by_id(self.team.tenant_id, rule_id)
        if not rule:
            raise ErrNotFoundDomain
        domain_service.unbind_httpdomain(self.team, self.region_name, rule_id)
        re = HTTPGatewayRuleSerializer(data=model_to_dict(rule))
        re.is_valid()
        return Response(re.data, status=status.HTTP_200_OK)
Example #4
0
 def get(self, req, app_id, *args, **kwargs):
     app = group_service.get_app_by_id(app_id)
     if not app:
         return Response({"msg": "app is not exist"}, status=status.HTTP_404_NOT_FOUND)
     rules = domain_service.get_http_rules_by_app_id(app_id)
     re = HTTPGatewayRuleSerializer(rules, many=True)
     return Response(re.data, status=status.HTTP_200_OK)
Example #5
0
 def get(self, req, app_id, *args, **kwargs):
     app = group_service.get_app_by_id(self.team, self.region_name, app_id)
     if not app:
         raise ErrAppNotFound
     rules = domain_service.get_http_rules_by_app_id(app_id)
     re = HTTPGatewayRuleSerializer(rules, many=True)
     return Response(re.data, status=status.HTTP_200_OK)
Example #6
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)
Example #7
0
class AddGatewayHTTPRuleView(ListAPIView):
    @swagger_auto_schema(
        operation_description="创建HTTP网关策略",
        request_body=PostHTTPGatewayRuleSerializer(),
        responses={200: HTTPGatewayRuleSerializer()},
        tags=['openapi-apps'],
    )
    def post(self, request, *args, **kwargs):
        serializer = PostHTTPGatewayRuleSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        return Response(None, status=status.HTTP_201_CREATED)
Example #8
0
class ListAppGatewayHTTPRuleView(ListAPIView):
    @swagger_auto_schema(
        operation_description="获取应用http访问策略列表",
        manual_parameters=[],
        responses={200: HTTPGatewayRuleSerializer(many=True)},
        tags=['openapi-gateway'],
    )
    def get(self, req, app_id, *args, **kwargs):
        app = group_service.get_app_by_id(app_id)
        if not app:
            return Response({"msg": "app is not exist"}, status=status.HTTP_404_NOT_FOUND)
        rules = domain_service.get_http_rules_by_app_id(app_id)
        re = HTTPGatewayRuleSerializer(rules, many=True)
        return Response(re.data, status=status.HTTP_200_OK)
Example #9
0
class ListAppGatewayHTTPRuleView(TeamAppAPIView):
    @swagger_auto_schema(
        operation_description="获取应用http访问策略列表",
        manual_parameters=[
            openapi.Parameter("app_id", openapi.IN_PATH, description="应用ID", type=openapi.TYPE_INTEGER),
        ],
        responses={200: HTTPGatewayRuleSerializer(many=True)},
        tags=['openapi-gateway'],
    )
    def get(self, req, app_id, *args, **kwargs):
        app = group_service.get_app_by_id(self.team, self.region_name, app_id)
        if not app:
            raise ErrAppNotFound
        rules = domain_service.get_http_rules_by_app_id(app_id)
        re = HTTPGatewayRuleSerializer(rules, many=True)
        return Response(re.data, status=status.HTTP_200_OK)

    @swagger_auto_schema(
        operation_description="创建HTTP网关策略",
        manual_parameters=[
            openapi.Parameter("app_id", openapi.IN_PATH, description="应用ID", type=openapi.TYPE_INTEGER),
        ],
        request_body=PostHTTPGatewayRuleSerializer(),
        responses={200: HTTPGatewayRuleSerializer()},
        tags=['openapi-gateway'],
    )
    def post(self, request, app_id, *args, **kwargs):
        ads = PostHTTPGatewayRuleSerializer(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
        # Compatible history code
        httpdomain["domain_heander"] = httpdomain.get("domain_header", None)
        httpdomain["domain_type"] = DomainType.WWW
        protocol = "http"
        if httpdomain.get("certificate_id", None):
            protocol = "https"
        httpdomain["protocol"] = protocol
        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)
        if domain_service.check_domain_exist(httpdomain["service_id"], httpdomain["container_port"], httpdomain["domain_name"],
                                             protocol, httpdomain.get("domain_path"), httpdomain.get("rule_extensions")):
            rst = {"msg": "策略已存在"}
            return Response(rst, status=status.HTTP_400_BAD_REQUEST)

        if service.service_source == "third_party":
            msg, msg_show, code = port_service.check_domain_thirdpart(self.team, service)
            if code != 200:
                logger.exception(msg, msg_show)
                return Response({"msg": msg, "msg_show": msg_show}, status=code)
        if httpdomain.get("whether_open", True):
            tenant_service_port = port_service.get_service_port_by_port(service, httpdomain["container_port"])
            # 仅开启对外端口
            code, msg, data = port_service.manage_port(self.team, service, service.service_region,
                                                       int(tenant_service_port.container_port), "only_open_outer",
                                                       tenant_service_port.protocol, tenant_service_port.port_alias)
            if code != 200:
                return Response({"msg": "change port fail"}, status=code)
        tenant_service_port = port_service.get_service_port_by_port(service, httpdomain["container_port"])
        if not tenant_service_port.is_outer_service:
            return Response({"msg": "没有开启对外端口"}, status=status.HTTP_400_BAD_REQUEST)
        data = domain_service.bind_httpdomain(self.team, self.request.user, service, httpdomain, True)
        configuration = httpdomain.get("configuration", None)
        if configuration:
            domain_service.update_http_rule_config(self.team, self.region_name, data.http_rule_id, configuration)
        serializer = HTTPGatewayRuleSerializer(data=data.to_dict())
        serializer.is_valid()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
Example #10
0
 def get(self, req, app_id, rule_id, *args, **kwargs):
     rule = domain_service.get_http_rules_by_app_id(self.app.ID).filter(http_rule_id=rule_id).first()
     re = HTTPGatewayRuleSerializer(rule)
     return Response(re.data, status=status.HTTP_200_OK)