def __init_and_create_user_tenant(self, user, enterprise): # 创建租户信息 code, msg, team = team_services.create_team(user, enterprise) if code != 200: logger.debug("account.login", "create tenant error") return code, msg, team # 创建用户在团队的权限 perm_info = { "user_id": user.user_id, "tenant_id": team.ID, "identity": "owner", "enterprise_id": enterprise.pk } perm_services.add_user_tenant_perm(perm_info) # 创建用户在企业的权限 user_services.make_user_as_admin_for_enterprise( user.user_id, enterprise.enterprise_id) # 为团队开通默认数据中心并在数据中心创建租户 code, msg, tenant_region = region_services.create_tenant_on_region( team.tenant_name, team.region) if code != 200: logger.debug("account.login", "create teanant on region error") return code, msg, team # 如果没有领过资源包,为默认开通的数据中心领取免费资源包 result = region_services.get_enterprise_free_resource( tenant_region.tenant_id, enterprise.enterprise_id, tenant_region.region_name, user.nick_name) logger.debug( "account.login", "get free resource on [{}] to team {}: {}".format( tenant_region.region_name, team.tenant_name, result)) user.is_active = True user.save() return code, msg, team
def post(self, request): """ 初始化团队和数据中心信息 --- parameters: - name: team_alias description: 团队别名 required: true type: string paramType: form - name: region_name description: 数据中心名称 required: true type: string paramType: form """ try: team_alias = request.data.get("team_alias", None) region_name = request.data.get("region_name", None) if not team_alias: return Response(general_message(400, "team alias is null", "团队名称不能为空"), status=400) if not region_name: return Response(general_message(400, "region name is null", "请选择数据中心"), status=400) r = re.compile('^[a-zA-Z0-9_\\-\\u4e00-\\u9fa5]+$') if not r.match(team_alias): return Response(general_message(400, "team alias is not allow", "组名称只支持中英文下划线和中划线"), status=400) team = team_services.get_team_by_team_alias(team_alias) if team: return Response(general_message(409, "region alias is exist", "团队名称{0}已存在".format(team_alias)), status=409) region = region_repo.get_region_by_region_name(region_name) if not region: return Response(general_message(404, "region not exist", "需要开通的数据中心{0}不存在".format(region_name)), status=404) enterprise = console_enterprise_service.get_enterprise_by_enterprise_id(self.user.enterprise_id) if not enterprise: return Response(general_message(404, "user's enterprise is not found", "无法找到用户所在的数据中心")) team = team_services.create_team(self.user, enterprise, [region_name], team_alias) # 为团队开通默认数据中心并在数据中心创建租户 tenant_region = region_services.create_tenant_on_region(enterprise.enterprise_id, team.tenant_name, team.region) # 公有云,如果没有领过资源包,为开通的数据中心领取免费资源包 if settings.MODULES.get('SSO_LOGIN'): result = region_services.get_enterprise_free_resource(tenant_region.tenant_id, enterprise.enterprise_id, tenant_region.region_name, self.user.nick_name) logger.debug("get free resource on [{}] to team {}: {}".format(tenant_region.region_name, team.tenant_name, result)) self.user.is_active = True self.user.save() result = general_message(200, "success", "初始化成功") except Exception as e: logger.exception(e) result = error_message(e.message) return Response(result, status=result["code"])