Exemplo n.º 1
0
 def post(self, request, app_id, *args, **kwargs):
     ctcs = CreateThirdComponentSerializer(data=request.data)
     ctcs.is_valid(raise_exception=True)
     req_date = ctcs.data
     validate_endpoints_info(req_date["endpoints"])
     new_component = console_app_service.create_third_party_app(self.region_name, self.team, self.user,
                                                                req_date["component_name"], req_date["endpoints"],
                                                                req_date["endpoints_type"])
     # add component to app
     code, msg_show = group_service.add_service_to_group(self.team, self.region_name, app_id, new_component.service_id)
     if code != 200:
         raise ServiceHandleException(
             msg="add component to app failure", msg_show=msg_show, status_code=code, error_code=code)
     endpoints_type = req_date["endpoints_type"]
     bean = new_component.to_dict()
     if endpoints_type == "api":
         # 生成秘钥
         deploy = deploy_repo.get_deploy_relation_by_service_id(service_id=new_component.service_id)
         api_secret_key = pickle.loads(base64.b64decode(deploy)).get("secret_key")
         # 从环境变量中获取域名,没有在从请求中获取
         host = os.environ.get('DEFAULT_DOMAIN', "http://" + request.get_host())
         api_url = host + "/console/" + "third_party/{0}".format(new_component.service_id)
         bean["api_service_key"] = api_secret_key
         bean["url"] = api_url
     console_app_service.create_third_party_service(self.team, new_component, self.user.nick_name)
     return Response(bean, status=status.HTTP_200_OK)
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        """
        创建第三方组件

        """

        group_id = request.data.get("group_id", -1)
        service_cname = request.data.get("service_cname", None)
        endpoints = request.data.get("endpoints", None)
        endpoints_type = request.data.get("endpoints_type", None)

        if not service_cname:
            return Response(general_message(400, "service_cname is null",
                                            "组件名未指明"),
                            status=400)
        if not endpoints and endpoints_type != "api":
            return Response(general_message(400, "end_point is null",
                                            "end_point未指明"),
                            status=400)
        validate_endpoints_info(endpoints)

        new_service = app_service.create_third_party_app(
            self.response_region, self.tenant, self.user, service_cname,
            endpoints, endpoints_type)

        # 添加组件所在组
        code, msg_show = group_service.add_service_to_group(
            self.tenant, self.response_region, group_id,
            new_service.service_id)
        if code != 200:
            logger.debug("service.create", msg_show)

        if endpoints_type == "discovery":
            # 添加username,password信息
            if "username" in endpoints and "password" in endpoints:
                if endpoints["username"] or endpoints["password"]:
                    app_service.create_service_source_info(
                        self.tenant, new_service, endpoints["username"],
                        endpoints["password"])

        bean = new_service.to_dict()
        if endpoints_type == "api":
            # 生成秘钥
            deploy = deploy_repo.get_deploy_relation_by_service_id(
                service_id=new_service.service_id)
            api_secret_key = pickle.loads(
                base64.b64decode(deploy)).get("secret_key")
            # 从环境变量中获取域名,没有在从请求中获取
            host = os.environ.get('DEFAULT_DOMAIN',
                                  "http://" + request.get_host())
            api_url = host + "/console/" + "third_party/{0}".format(
                new_service.service_id)
            bean["api_service_key"] = api_secret_key
            bean["url"] = api_url
            service_endpoints_repo.create_api_endpoints(
                self.tenant, new_service)

        result = general_message(200, "success", "创建成功", bean=bean)
        return Response(result, status=result["code"])
 def check_domain_thirdpart(self, tenant, service):
     from console.utils.validation import validate_endpoints_info
     res, body = region_api.get_third_party_service_pods(
         service.service_region, tenant.tenant_name, service.service_alias)
     if res.status != 200:
         return "region error", "数据中心查询失败", 412
     endpoint_list = body["list"]
     endpoint_info = [endpoint.address for endpoint in endpoint_list]
     validate_endpoints_info(endpoint_info)
     return "", "", 200
Exemplo n.º 4
0
    def create_third_party_service(self, tenant, service, user_name):
        data = self.__init_third_party_data(tenant, service, user_name)
        # env var
        envs_info = env_var_repo.get_service_env(tenant.tenant_id,
                                                 service.service_id).values(
                                                     'container_port', 'name',
                                                     'attr_name', 'attr_value',
                                                     'is_change', 'scope')
        if envs_info:
            data["envs_info"] = list(envs_info)
        # 端口
        ports = port_repo.get_service_ports(tenant.tenant_id,
                                            service.service_id)
        ports_info = ports.values('container_port', 'mapping_port', 'protocol',
                                  'port_alias', 'is_inner_service',
                                  'is_outer_service', 'k8s_service_name')

        for port_info in ports_info:
            port_info["is_inner_service"] = False
            port_info["is_outer_service"] = False

        if ports_info:
            data["ports_info"] = list(ports_info)

        # endpoints
        endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(
            service.service_id).first()
        if endpoints:
            if endpoints.endpoints_type == "static":
                eps = json.loads(endpoints.endpoints_info)
                validate_endpoints_info(eps)
            endpoints_dict = dict()
            endpoints_dict[endpoints.endpoints_type] = endpoints.endpoints_info
            data["endpoints"] = endpoints_dict
        data["kind"] = service.service_source

        # etcd keys
        data["etcd_key"] = service.check_uuid
        # 数据中心创建
        logger.debug('-----------data-----------_>{0}'.format(data))
        app_id = service_group_relation_repo.get_group_id_by_service(service)
        region_app_id = region_app_repo.get_region_app_id(
            service.service_region, app_id)
        data["app_id"] = region_app_id
        region_api.create_service(service.service_region, tenant.tenant_name,
                                  data)
        # 将组件创建状态变更为创建完成
        service.create_status = "complete"
        self.__handle_service_ports(tenant, service, ports)
        service.save()
        return service
Exemplo n.º 5
0
    def post(self, request, *args, **kwargs):
        """
        添加endpoint实例
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        address = request.data.get("ip", None)
        if not address:
            return Response(general_message(400, "end_point is null",
                                            "end_point未指明"),
                            status=400)
        validate_endpoints_info([address])
        endpoint_service.add_endpoint(self.tenant, self.service, address)

        result = general_message(200, "success", "添加成功")
        return Response(result)
Exemplo n.º 6
0
    def post(self, request, *args, **kwargs):
        """
        创建第三方组件

        """

        group_id = request.data.get("group_id", -1)
        service_cname = request.data.get("service_cname", None)
        static = request.data.get("static", None)
        endpoints_type = request.data.get("endpoints_type", None)
        service_name = request.data.get("serviceName", "")

        if not service_cname:
            return Response(general_message(400, "service_cname is null",
                                            "组件名未指明"),
                            status=400)
        if endpoints_type == "static":
            validate_endpoints_info(static)
        source_config = {}
        if endpoints_type == "kubernetes":
            if not service_name:
                return Response(general_message(
                    400, "kubernetes service name is null",
                    "Kubernetes Service名称必须指定"),
                                status=400)
            source_config = {
                "service_name": service_name,
                "namespace": request.data.get("namespace", "")
            }
        new_service = app_service.create_third_party_app(
            self.response_region, self.tenant, self.user, service_cname,
            static, endpoints_type, source_config)
        # 添加组件所在组
        code, msg_show = group_service.add_service_to_group(
            self.tenant, self.response_region, group_id,
            new_service.service_id)
        if code != 200:
            new_service.delete()
            raise ServiceHandleException(msg="add component to app failure",
                                         msg_show=msg_show,
                                         status_code=code,
                                         error_code=code)
        bean = new_service.to_dict()
        if endpoints_type == "api":
            # 生成秘钥
            deploy = deploy_repo.get_deploy_relation_by_service_id(
                service_id=new_service.service_id)
            api_secret_key = pickle.loads(
                base64.b64decode(deploy)).get("secret_key")
            # 从环境变量中获取域名,没有在从请求中获取
            host = os.environ.get('DEFAULT_DOMAIN',
                                  "http://" + request.get_host())
            api_url = host + "/console/" + "third_party/{0}".format(
                new_service.service_id)
            bean["api_service_key"] = api_secret_key
            bean["url"] = api_url
            service_endpoints_repo.create_api_endpoints(
                self.tenant, new_service)

        result = general_message(200, "success", "创建成功", bean=bean)
        return Response(result, status=result["code"])