Beispiel #1
0
 def post(self, request, *args, **kwargs):
     result = {}
     plugin_id = request.POST.get("plugin_id", None)
     build_version = request.POST.get("build_version", None)
     if not plugin_id or not build_version:
         logger.error("plugin.relation", u'参数错误,plugin_id and version_id')
         data = {"success":False, "code":405, 'msg':u'参数错误'}
         result = oldResultSuitGeneralMessage(data, "params error", "")
         return JsonResponse(result, status=200)
     logger.debug("plugin.relation", "plugin_id is {0}, build_version is {1}".format(plugin_id, build_version))
     try:
         #1. 建立关联关系
         #2. 生成默认的配置发送给前端
         #3. 生成默认配置存储至console数据库
         #4. 生成默认配置发送给region
         # >> 进行关联
         body_relation = {}
         body_relation["plugin_id"] = plugin_id
         body_relation["switch"]  = True
         body_relation["version_id"] = build_version
         # 1)发送关联请求
         try:
             res, resultBody = region_api.pluginServiceRelation(
                     self.response_region, self.tenant.tenant_name, self.serviceAlias, body_relation)
             if res.status == 200:
                 plugin_svc.add_service_plugin_relation(
                     service_id=self.service.service_id, plugin_id=plugin_id,build_version=build_version)
             pass
         except region_api.CallApiError as e:
             if e.status == 400:
                 result["success"] = False
                 result["code"] = 400
                 result["msg"] = u"该类型插件已关联,请先卸载同类插件"
                 result = oldResultSuitGeneralMessage(result, "relation already exist", "")
                 return JsonResponse(result, status=200)
             else:
                 result["success"] = False
                 result["code"] = e.status
                 result['msg'] = u"安装插件失败"
                 result = oldResultSuitGeneralMessage(result, "add relation error", "")
                 return JsonResponse(result, status=200)
         # 2)发送配置请求
         result["config_group"] = self.useDefaultAttr(plugin_id, build_version, "post")
         complex, normal = plugin_svc.createAttrsJsonForRegion(self.service.service_id, self.serviceAlias, plugin_id)
         config_envs = {}
         config_envs["normal_envs"] = normal
         config_envs["complex_envs"] = complex
         body = {}
         body["tenant_id"] = self.tenant.tenant_id
         body["service_id"] = self.service.service_id
         body["config_envs"] = config_envs
         res, resultBody = region_api.postPluginAttr(self.response_region, self.tenant.tenant_name, self.serviceAlias, plugin_id, body)
         if res.status == 200:
             result["success"] = True
             result["code"] = 200
             result["msg"] = u"操作成功"
             result = oldResultSuitGeneralMessage(result, "success", result["config_group"])
             return JsonResponse(result, status=200)
         else:
             result["success"] = False
             result["code"] = res.status
             result['msg'] = resultBody
             result = oldResultSuitGeneralMessage(result, "add plugin attr error", "")
             return JsonResponse(result, status=200)
     except HasNoDownStreamService as e:
         try:
             plugin_svc.del_service_plugin_relation_and_attrs(self.service.service_id, plugin_id)
             region_api.delPluginServiceRelation(
                 self.response_region, self.tenant.tenant_name, plugin_id, self.serviceAlias)
         except Exception, e:
             pass
         result["success"] = False
         result['code']=400
         result['msg']=u'缺少依赖应用,不能使用该类型插件'
         result = oldResultSuitGeneralMessage(result, "havs no downstream services", "")
         logger.error("plugin.relation", u'缺少关联应用,不能使用该类型插件')
         logger.exception("plugin.relation", e)
         return JsonResponse(result, status=200)
Beispiel #2
0
    def post(self, request, plugin_id, *args, **kwargs):
        """
        应用安装插件
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: serviceAlias
              description: 服务别名
              required: true
              type: string
              paramType: path
            - name: plugin_id
              description: 插件ID
              required: true
              type: string
              paramType: path
            - name: build_version
              description: 插件版本
              required: true
              type: string
              paramType: form
        """
        result = {}
        build_version = request.data.get("build_version", None)

        try:
            if not plugin_id:
                return Response(general_message(400, "params error", "参数错误"),
                                status=400)
            if not build_version:
                plugin_version = plugin_version_service.get_newest_usable_plugin_version(
                    plugin_id)
                build_version = plugin_version.build_version

            # 1. 建立关联关系
            # 2. 生成默认的配置发送给前端
            # 3. 生成默认配置存储至console数据库
            # 4. 生成默认配置发送给region
            # >> 进行关联
            body_relation = {}
            body_relation["plugin_id"] = plugin_id
            body_relation["switch"] = True
            body_relation["version_id"] = build_version
            # 1)发送关联请求
            try:
                res, resultBody = region_api.pluginServiceRelation(
                    self.service.service_region, self.tenant.tenant_name,
                    self.service.service_alias, body_relation)
                if res.status == 200:
                    plugin_svc.add_service_plugin_relation(
                        service_id=self.service.service_id,
                        plugin_id=plugin_id,
                        build_version=build_version)
            except region_api.CallApiError as e:
                if e.status == 400:
                    result = general_message(400, "plugin already related",
                                             u"该类型插件已关联,请先卸载同类插件")
                    return Response(result, status=400)
                else:
                    result = general_message(int(e.status),
                                             "region install error", "安装插件失败")
                    return Response(result, status=result["code"])
            # 2)发送配置请求
            result["config_group"] = self.useDefaultAttr(
                plugin_id, build_version, "post")
            complex, normal = plugin_svc.createAttrsJsonForRegion(
                self.service.service_id, self.service.service_alias, plugin_id)
            config_envs = {}
            config_envs["normal_envs"] = normal
            config_envs["complex_envs"] = complex
            body = {}
            body["tenant_id"] = self.tenant.tenant_id
            body["service_id"] = self.service.service_id
            body["config_envs"] = config_envs
            res, resultBody = region_api.postPluginAttr(
                self.service.service_region, self.tenant.tenant_name,
                self.service.service_alias, plugin_id, body)
            if res.status == 200:
                result = general_message(200,
                                         "success",
                                         "操作成功",
                                         bean=result["config_group"])
                return Response(result, result["code"])
            else:
                result = general_message(int(res.status),
                                         "add plugin attr error", "操作失败")
                return Response(result, status=200)
        except HasNoDownStreamService as e:
            try:
                plugin_svc.del_service_plugin_relation_and_attrs(
                    self.service.service_id, plugin_id)
                region_api.delPluginServiceRelation(
                    self.service.service_region, self.tenant.tenant_name,
                    plugin_id, self.service.service_alias)
            except Exception, e:
                pass
            result = general_message(400, "havs no downstream services",
                                     u'缺少关联应用,不能使用该类型插件')
            logger.exception(e)
            return Response(result, status=400)