def del_app_maker(self, biz_cc_id, app_id, fake=False): """ @param app_id: @param biz_cc_id: @param fake: @return: """ try: app_maker_obj = AppMaker.objects.get(id=app_id, business__cc_id=biz_cc_id, is_deleted=False) except AppMaker.DoesNotExist: return False, _(u"当前操作的轻应用不存在或已删除!") del_name = time_now_str() if not fake: # rename before delete to avoid name conflict when create a new app app_edit_result = edit_maker_app( app_maker_obj.creator, app_maker_obj.code, del_name[:20], ) if not app_edit_result['result']: return False, _(u"删除失败:%s") % app_edit_result['message'] # delete app on blueking desk app_del_result = del_maker_app(app_maker_obj.creator, app_maker_obj.code) if not app_del_result['result']: return False, _(u"删除失败:%s") % app_del_result['message'] app_maker_obj.is_deleted = True app_maker_obj.name = del_name[:20] app_maker_obj.save() return True, app_maker_obj.code
def save_app_maker(self, biz_cc_id, app_params, fake=False): """ @summary: @param biz_cc_id: 业务ID @param app_params: App maker参数 @param fake: 为True则不会真正调用API创建轻应用 @return: """ logger.info('save_app_maker params: %s' % app_params) app_id = app_params['id'] if app_id == '0' or not app_id: app_id = None template_id = app_params['template_id'] app_params['name'] = name_handler(app_params['name'], 20) app_params['desc'] = name_handler(app_params.get('desc', ''), 30) biz = Business.objects.get(cc_id=biz_cc_id) try: task_template = TaskTemplate.objects.get(pk=template_id, business__cc_id=biz_cc_id, is_deleted=False) except TaskTemplate.DoesNotExist: return False, _(u"保存失败,引用的流程模板不存在!") # create appmaker if not app_id: fields = { 'business': biz, 'name': app_params['name'], 'code': '', 'desc': app_params['desc'], 'logo_url': '', 'link': app_params['link_prefix'], 'creator': app_params['username'], 'editor': app_params['username'], 'task_template': task_template, # 生成一个删除状态的对象,以便拼接轻应用访问链接 'is_deleted': True, } if app_params.get('template_scheme_id'): fields['template_scheme_id'] = app_params['template_scheme_id'] app_maker_obj = AppMaker.objects.create(**fields) # update app link app_id = app_maker_obj.id app_link = '{appmaker_prefix}{app_id}/newtask/{biz_cc_id}/selectnode/?template_id={template_id}'.format( appmaker_prefix=app_params['link_prefix'], app_id=app_id, biz_cc_id=biz_cc_id, template_id=template_id) app_maker_obj.link = app_link if fake: app_maker_obj.code = '%s%s' % (settings.APP_CODE, time_now_str()) app_maker_obj.is_deleted = False app_maker_obj.save() return True, app_maker_obj # create app on blueking desk app_create_result = create_maker_app( app_params['username'], app_params['name'], app_link, app_params['username'], task_template.category, app_params['desc'], ) if not app_create_result['result']: return False, _(u"创建轻应用失败:%s") % app_create_result['message'] app_code = app_create_result['data']['bk_light_app_code'] app_maker_obj.code = app_code app_maker_obj.is_deleted = False # edit appmaker else: try: app_maker_obj = AppMaker.objects.get( id=app_id, business__cc_id=biz_cc_id, task_template__id=template_id, is_deleted=False) except AppMaker.DoesNotExist: return False, _(u"保存失败,当前操作的轻应用不存在或已删除!") app_code = app_maker_obj.code creator = app_maker_obj.creator link = app_maker_obj.link if not fake: # edit app on blueking app_edit_result = edit_maker_app( creator, app_code, app_params['name'], link, creator, task_template.category, app_params['desc'], ) if not app_edit_result['result']: return False, _(u"编辑轻应用失败:%s") % app_edit_result['message'] app_maker_obj.name = app_params['name'] app_maker_obj.desc = app_params['desc'] app_maker_obj.editor = app_params['username'] if 'template_scheme_id' in app_params: app_maker_obj.template_scheme_id = app_params[ 'template_scheme_id'] # upload app logo if not fake and app_params['logo_content']: logo = base64.b64encode(app_params['logo_content']) app_logo_result = modify_app_logo(app_maker_obj.creator, app_code, logo) if not app_logo_result['result']: logger.warning(u"AppMaker[id=%s] upload logo failed: %s" % (app_maker_obj.id, app_logo_result['message'])) # update app maker info app_maker_obj.logo_url = get_app_logo_url(app_code=app_code) app_maker_obj.save() return True, app_maker_obj
def save_app_maker(self, project_id, app_params, fake=False): """ @summary: @param project_id: 项目 ID @param app_params: App maker参数 @param fake: 为True则不会真正调用API创建轻应用 @return: """ logger.info('save_app_maker params: %s' % app_params) app_id = app_params['id'] if app_id == '0' or not app_id: app_id = None template_id = app_params['template_id'] app_params['name'] = name_handler(app_params['name'], 20) app_params['desc'] = name_handler(app_params.get('desc', ''), 30) proj = Project.objects.get(id=project_id) try: task_template = TaskTemplate.objects.get(pk=template_id, project_id=project_id, is_deleted=False) except TaskTemplate.DoesNotExist: return False, _(u"保存失败,引用的流程模板不存在!") # create appmaker from gcloud.contrib.appmaker.permissions import mini_app_resource if not app_id: verify_or_raise_auth_failed( principal_type='user', principal_id=app_params['username'], resource=task_template_resource, action_ids=[task_template_resource.actions.create_mini_app.id], instance=task_template) fields = { 'project': proj, 'name': app_params['name'], 'code': '', 'desc': app_params['desc'], 'logo_url': '', 'link': app_params['link_prefix'], 'creator': app_params['username'], 'editor': app_params['username'], 'task_template': task_template, # 生成一个删除状态的对象,以便拼接轻应用访问链接 'is_deleted': True, } if app_params.get('template_scheme_id'): fields['template_scheme_id'] = app_params['template_scheme_id'] app_maker_obj = AppMaker.objects.create(**fields) # update app link app_id = app_maker_obj.id app_link = '{appmaker_prefix}{app_id}/newtask/{project_id}/selectnode/?template_id={template_id}'.format( appmaker_prefix=app_params['link_prefix'], app_id=app_id, project_id=project_id, template_id=template_id) app_maker_obj.link = app_link if fake: app_maker_obj.code = '%s%s' % (settings.APP_CODE, time_now_str()) app_maker_obj.is_deleted = False app_maker_obj.save() return True, app_maker_obj # create app on blueking desk app_create_result = create_maker_app( app_params['username'], app_params['name'], app_link, app_params['username'], task_template.category, app_params['desc'], ) if not app_create_result['result']: return False, _(u"创建轻应用失败:%s") % app_create_result['message'] app_code = app_create_result['data']['bk_light_app_code'] app_maker_obj.code = app_code app_maker_obj.is_deleted = False # edit appmaker else: try: app_maker_obj = AppMaker.objects.get( id=app_id, project_id=project_id, task_template__id=template_id, is_deleted=False) except AppMaker.DoesNotExist: return False, _(u"保存失败,当前操作的轻应用不存在或已删除!") verify_or_raise_auth_failed( principal_type='user', principal_id=app_params['username'], resource=mini_app_resource, action_ids=[mini_app_resource.actions.edit.id], instance=app_maker_obj) app_code = app_maker_obj.code creator = app_maker_obj.creator link = app_maker_obj.link if not fake: # edit app on blueking app_edit_result = edit_maker_app( creator, app_code, app_params['name'], link, creator, task_template.category, app_params['desc'], ) if not app_edit_result['result']: return False, _(u"编辑轻应用失败:%s") % app_edit_result['message'] app_maker_obj.name = app_params['name'] app_maker_obj.desc = app_params['desc'] app_maker_obj.editor = app_params['username'] if 'template_scheme_id' in app_params: app_maker_obj.template_scheme_id = app_params[ 'template_scheme_id'] # upload app logo if not fake and app_params['logo_content']: logo = base64.b64encode(app_params['logo_content']) app_logo_result = modify_app_logo(app_maker_obj.creator, app_code, logo) if not app_logo_result['result']: logger.warning(u"AppMaker[id=%s] upload logo failed: %s" % (app_maker_obj.id, app_logo_result['message'])) # update app maker info app_maker_obj.logo_url = get_app_logo_url(app_code=app_code) app_maker_obj.save() return True, app_maker_obj
def save_app_maker(self, project_id, app_params, fake=False): """ @summary: @param project_id: 项目 ID @param app_params: App maker参数 @param fake: 为True则不会真正调用API创建轻应用 @return: """ from gcloud.iam_auth import res_factory logger.info("save_app_maker params: %s" % app_params) app_id = app_params["id"] if app_id == "0" or not app_id: app_id = None template_id = app_params["template_id"] app_params["name"] = standardize_name(app_params["name"], 20) app_params["desc"] = standardize_name(app_params.get("desc", ""), 30) proj = Project.objects.get(id=project_id) try: task_template = TaskTemplate.objects.get(pk=template_id, project_id=project_id, is_deleted=False) except TaskTemplate.DoesNotExist: return False, _("保存失败,引用的流程模板不存在!") if not app_id: subject = Subject("user", app_params["username"]) action = Action(IAMMeta.FLOW_CREATE_MINI_APP_ACTION) resources = res_factory.resources_for_flow_obj(task_template) allow_or_raise_auth_failed(iam=iam, system=IAMMeta.SYSTEM_ID, subject=subject, action=action, resources=resources) fields = { "project": proj, "name": app_params["name"], "code": "", "desc": app_params["desc"], "logo_url": "", "link": app_params["link_prefix"], "creator": app_params["username"], "editor": app_params["username"], "task_template": task_template, # 生成一个删除状态的对象,以便拼接轻应用访问链接 "is_deleted": True, } if app_params.get("category"): fields["category"] = app_params["category"] if app_params.get("template_scheme_id"): fields["template_scheme_id"] = app_params["template_scheme_id"] app_maker_obj = AppMaker.objects.create(**fields) # update app link app_id = app_maker_obj.id app_link = "{appmaker_prefix}{app_id}/newtask/{project_id}/selectnode/?template_id={template_id}".format( appmaker_prefix=app_params["link_prefix"], app_id=app_id, project_id=project_id, template_id=template_id) app_maker_obj.link = app_link if fake: app_maker_obj.code = "%s%s" % (settings.APP_CODE, time_now_str()) app_maker_obj.is_deleted = False app_maker_obj.save() return True, app_maker_obj # create app on blueking desk app_create_result = create_maker_app( app_params["username"], app_params["name"], app_link, app_params["username"], app_params.get("category") or task_template.category, app_params["desc"], ) if not app_create_result["result"]: return False, _("创建轻应用失败:%s") % app_create_result["message"] app_code = app_create_result["data"]["bk_light_app_code"] app_maker_obj.code = app_code app_maker_obj.is_deleted = False # edit appmaker else: try: app_maker_obj = AppMaker.objects.get( id=app_id, project_id=project_id, task_template__id=template_id, is_deleted=False) except AppMaker.DoesNotExist: return False, _("保存失败,当前操作的轻应用不存在或已删除!") subject = Subject("user", app_params["username"]) action = Action(IAMMeta.MINI_APP_EDIT_ACTION) resources = res_factory.resources_for_mini_app_obj(app_maker_obj) allow_or_raise_auth_failed(iam=iam, system=IAMMeta.SYSTEM_ID, subject=subject, action=action, resources=resources) app_code = app_maker_obj.code creator = app_maker_obj.creator # app_params["link_prefix"]: current_host.com/appmaker, link_prefix: current_host.com/ link_prefix = app_params["link_prefix"][:app_params["link_prefix"]. rfind("appmaker")] # app_maker_obj.link: old_host.com/appmaker/xxx, link_suffix: appmaker/xxx link_suffix = app_maker_obj.link[app_maker_obj.link. rfind("appmaker"):] # 只保留app_maker_obj.link的后缀,使用环境当前域名作为前缀 app_link = f"{link_prefix}{link_suffix}" app_maker_obj.link = app_link if not fake: # edit app on blueking app_edit_result = edit_maker_app( creator, app_code, app_params["name"], app_link, creator, app_params.get("category") or task_template.category, app_params["desc"], ) if not app_edit_result["result"]: return False, _("编辑轻应用失败:%s") % app_edit_result["message"] app_maker_obj.name = app_params["name"] app_maker_obj.desc = app_params["desc"] app_maker_obj.editor = app_params["username"] if "category" in app_params: app_maker_obj.category = app_params["category"] if "template_scheme_id" in app_params: app_maker_obj.template_scheme_id = app_params[ "template_scheme_id"] # upload app logo if not fake and app_params["logo_content"]: logo = base64.b64encode(app_params["logo_content"]) app_logo_result = modify_app_logo(app_maker_obj.creator, app_code, logo) if not app_logo_result["result"]: logger.error("AppMaker[id=%s] upload logo failed: %s" % (app_maker_obj.id, app_logo_result["message"])) # update app maker info app_maker_obj.logo_url = get_app_logo_url(app_code=app_code) app_maker_obj.save() return True, app_maker_obj