Exemplo n.º 1
0
    def put(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: body
              description: 配置内容
              required: true
              type: string
              paramType: body

        """
        try:
            config = json.loads(request.body)
            config_group = config
            if not config_group:
                return Response(general_message(400, "params error",
                                                "参数配置不可为空"),
                                status=400)

            plugin_svc.del_service_plugin_attrs(self.service.service_id,
                                                plugin_id)
            plugin_svc.updateALLTenantServicePluginAttr(
                config_group, plugin_id, self.service.service_id)
            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
            region_api.putPluginAttr(self.service.service_region,
                                     self.tenant.tenant_name,
                                     self.service.service_alias, plugin_id,
                                     body)
            result = general_message(200, "config success", "配置成功")
            return Response(result, result["code"])
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
            return Response(result, result["code"])
Exemplo n.º 2
0
    def put(self, request, *args, **kwargs):
        result = {}
        config = json.loads(request.body)
        logger.debug("plugin.relation", "in PluginServiceComplexAttr put method params is {}".format(config))
        plugin_id = config.get("plugin_id", None)
        service_id = config.get("service_id", None)
        try:
            config_group = config.get("config_group", [])
            if len(config_group) == 0:
                result["success"] = False
                result["code"] = 400
                result['msg'] = "参数配置不可为空"
                result = oldResultSuitGeneralMessage(result, "params canot be null", "")
                return JsonResponse(result, status=200)
            plugin_svc.del_service_plugin_attrs(self.service.service_id, plugin_id)
            plugin_svc.updateALLTenantServicePluginAttr(config_group, plugin_id, self.service.service_id)
            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
            logger.debug("plugin.relation", "--< config_envs is {}".format(config_envs))
            body = {}
            body["tenant_id"] = self.tenant.tenant_id
            body["service_id"] = self.service.service_id
            body["config_envs"] = config_envs
            res, resultBody = region_api.putPluginAttr(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", "")
                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 Exception, e:
            logger.error("plugin.relation", e)
            result["success"] = False
            result['code']=500
            result['msg']=u'更新插件配置失败'
            logger.error("plugin.relation", u'更新插件配置失败')
            logger.exception("plugin.relation", e)
            result = oldResultSuitGeneralMessage(result, "update plugin attr error", "")
            return JsonResponse(result, status=200)
Exemplo n.º 3
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)
Exemplo n.º 4
0
 def put(self, request, *args, **kwargs):
     result = {}
     config = QueryDict(request.body)
     logger.debug("plugin.relation", "in PluginServiceRelation put method params is {}".format(config))
     plugin_id = config.get("plugin_id", None)
     build_version = config.get("build_version", "newest")
     switch = config.get("switch", True)
     switch = plugin_svc.chargeSwtich(switch)
     if not plugin_id or not build_version:
         logger.error("plugin.relation", u'参数错误,plugin_id and version_id')
         data = {"success":False, "code":400, 'msg':u'参数错误'}
         result = oldResultSuitGeneralMessage(data, "params error", "")
         return JsonResponse(result, status=200)
     try:
         body_relation = {}
         body_relation["plugin_id"] = plugin_id
         body_relation["switch"] = switch
         versionList = plugin_svc.get_tenant_plugin_newest_versions(self.response_region, self.tenant, plugin_id)
         if versionList > 0:
             body_relation["version_id"] = versionList[0].build_version
             build_version = versionList[0].build_version
             logger.debug("plugin.relation", "body_relation is {0}, build_version is {1}".format(body_relation, build_version))
         else:
             result["success"] = False
             result["code"] = 404
             result["msg"] = u"未找到插件的最新版本"
             result = oldResultSuitGeneralMessage(result, "cant find newest plugin", "")
             return JsonResponse(result, status=200)
         res, resultBody = region_api.delPluginServiceRelation(
                 self.response_region, self.tenant.tenant_name, plugin_id, self.serviceAlias)
         if res.status == 200:
             pass
         else:
             result["success"] = False
             result["code"] = res.status
             result['msg'] = resultBody
             result = oldResultSuitGeneralMessage(result, "update plugin relation error", "")
             return JsonResponse(result, status=res.status)
         res, resultBody = region_api.pluginServiceRelation(
                 self.response_region, self.tenant.tenant_name, self.serviceAlias, body_relation)
         if res.status == 200:
             plugin_svc.update_service_plugin_relation(self.service.service_id, plugin_id, build_version, switch)
             pass
         else:
             result["success"] = False
             result["code"] = res.status
             result['msg'] = resultBody
             result = oldResultSuitGeneralMessage(result, "update plugin relation error", "")
             return JsonResponse(result, status=res.status)
         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["config_group"] = self.useDefaultAttr(plugin_id, build_version, "post")
             result["success"] = True
             result["code"] = 200
             result["msg"] = u"操作成功"
             result["bean"] = {"build_version": model_to_dict(versionList[0])}
             result = general_message(200, "success", u"操作成功", result["bean"], result["config_group"])
             return JsonResponse(result, status=200)
         else:
             result["success"] = False
             result["code"] = res.status
             result['msg'] = resultBody
             result = oldResultSuitGeneralMessage(result, "update plugin relation error",  "")
             return JsonResponse(result, status=200)
     except Exception, e:
         result["success"] = False
         result['code']=500
         result['msg']=u'更新插件关联失败'
         logger.error("plugin.relation", u'更新插件关联失败')
         logger.exception("plugin.relation", e)
         result = oldResultSuitGeneralMessage(result, "update plugin relation error", "")
         return JsonResponse(result, status=200)
Exemplo n.º 5
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)