in pai/toolkit/config.py [0:0]
def prompt_for_workspace(user_profile: UserProfile):
"""Ask for workspace configurations."""
workspace_api = user_profile.get_workspace_api()
row_format = "{:<50}{}"
def workspace_choice_name(workspace: Dict[str, Any]):
if workspace.get("IsDefault"):
return row_format.format(
workspace["WorkspaceName"],
str(workspace["WorkspaceId"]) + " (*default)",
)
return row_format.format(
workspace["WorkspaceName"],
str(workspace["WorkspaceId"]),
)
items = sorted(
[
(item, workspace_choice_name(workspace=item))
for item in make_list_resource_iterator(
workspace_api.list,
page_number=1,
page_size=50,
)
],
key=lambda x: (x[0].get("IsDefault", False), x[0]["WorkspaceName"]),
reverse=True,
)
if not items:
if user_profile.is_account:
raise RuntimeError(
localized_text(
f"You do not have any available Workspace in the selected region ("
f"{user_profile.region_id}). Please create one in the region."
f" through the PAI console (https://pai.console.aliyun.com/).",
f"您在选择的Region ({user_profile.region_id})下没有可用的工作空间,"
f"请通过PAI的控制台( https://pai.console.aliyun.com/ )创建一个工作空间。",
)
)
else:
raise RuntimeError(
localized_text(
f"You do not have any available Workspace in the selected region ("
f"{user_profile.region_id}). Please create a new workspace or "
f"contact an administrator to be added to an existing workspace.",
f"您在选择的Region ({user_profile.region_id}) 下没有可用的工作空间,"
f"请创建一个新的工作空间,或是联系管理员将您添加到已有的工作空间。",
)
)
else:
header = row_format.format("WorkspaceName", "WorkspaceId")
workspace_item = radio_list_prompt(
localized_text(
f"Please select the workspace you need to access:\n {header}",
f"请选择您需要访问的工作空间\n {header})",
),
values=items,
erase_when_done=True,
)
workspace_id = workspace_item["WorkspaceId"]
roles = user_profile.get_roles_in_workspace(workspace_id)
role_info = ", ".join(roles)
if not any(r for r in roles if r in WorkspaceRoles.recommend_roles()):
print_warning(
localized_text(
f"We recommend using a workspace with at least developer permission. "
f"Role in the workspace: {role_info}",
f"建议使用至少有开发者权限的工作空间,当前工作空间角色:{role_info}",
)
)
print_highlight(
localized_text(
"Current workspace configuration selected:",
"当前选择的工作空间信息:",
)
)
print_highlight(
"WorkspaceName: {}\nWorkspaceId: {}\nRoles: {}".format(
workspace_item["WorkspaceName"],
workspace_item["WorkspaceId"],
role_info,
)
)
return workspace_id