Ejemplo 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)
Ejemplo 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)

        try:
            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)

            code, msg_show, new_service = app_service.create_third_party_app(self.response_region, self.tenant,
                                                                             self.user, service_cname,
                                                                             endpoints, endpoints_type)
            if code != 200:
                return Response(general_message(code, "service create fail", msg_show), status=code)

            # 添加服务所在组
            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信息
                logger.debug('========dict=========>{0}'.format(type(endpoints)))
                if endpoints.has_key("username") and endpoints.has_key("password"):
                    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', request.get_host())
                api_url = "http://" + host + "/console/" + "third_party/{0}".format(new_service.service_id)
                bean["api_service_key"] = api_secret_key
                bean["url"] = api_url

            result = general_message(200, "success", "创建成功", bean=bean)
        except ResourceNotEnoughException as re:
            logger.exception(re)
            return Response(general_message(10406, "resource is not enough", re.message), status=412)
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])