重要
Azure Lab Services 將於 2027 年 6 月 28 日淘汰。 如需詳細資訊,請參閱淘汰指南。 為了簡化遷移流程,Microsoft 發布了自動化腳本幫助你清理實驗室服務資源,這些腳本可在 Azure Lab Services 退休腳本的 GitHub 倉庫中取得。
在本文中,您將了解如何使用 Python 和 Azure Python SDK 來建立實驗室計劃。 為 Azure 實驗室服務建立實驗室時,會使用實驗室計畫。 您也會新增角色指派,讓授課者可以根據實驗室計劃建立實驗室。 如需 Azure Lab Services 的概觀,請參閱 Azure Lab Services 簡介 (部分機器翻譯)。
必要條件
- 具有有效訂用帳戶的 Azure 帳戶。 如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶。
- 具有管理實驗室權限的 Azure 帳戶,例如實驗室建立者、擁有者、參與者或 實驗室服務參與者等 Azure RBAC 角色。 深入了解 Azure Lab Services 內建角色和指派範圍 (部分機器翻譯)。
建立實驗室計畫
下列步驟將示範如何建立實驗室計劃。 實驗室計劃中設定的任何屬性都會用於使用此計劃建立的實驗室。
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
import time
from datetime import timedelta
from azure.identity import DefaultAzureCredential
from azure.mgmt.labservices import LabServicesClient
from azure.mgmt.resource import ResourceManagementClient
def main():
SUBSCRIPTION_ID = "<Subscription ID>"
TIME = str(time.time()).replace('.','')
GROUP_NAME = "BellowsCollege_rg"
LABPLAN = "BellowsCollege_labplan"
LAB = "BellowsCollege_lab"
LOCATION = 'southcentralus'
# Create clients
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
resource_client = ResourceManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
labservices_client = LabServicesClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
# Create resource group
resource_client.resource_groups.create_or_update(
GROUP_NAME,
{"location": LOCATION}
)
# Create lab services lab plan
LABPLANBODY = {
"location" : LOCATION,
"properties" : {
"defaultConnectionProfile" : {
"webSshAccess" : "None",
"webRdpAccess" : "None",
"clientSshAccess" : "None",
"clientRdpAccess" : "Public"
},
"defaultAutoShutdownProfile" : {
"shutdownOnDisconnect" : "Disabled",
"shutdownWhenNotConnected" : "Disabled",
"shutdownOnIdle" : "None"
},
"allowedRegions" : [LOCATION],
"supportInfo" : {
"email" : "user@bellowscollege.com",
"phone" : "123-123-1234",
"instructions" : "Bellows College support."
}
}
}
#Create Lab Plan
poller = labservices_client.lab_plans.begin_create_or_update(
GROUP_NAME,
LABPLAN,
LABPLANBODY
)
# Poll for long running execution.
labplan_result = poller.result()
print(f"Created Lab Plan: {labplan_result.name}")
# Get LabServices Lab Plans by resource group
labservices_client.lab_plans.list_by_resource_group(
GROUP_NAME
)
#Get single LabServices Lab Plan
labservices_labplan = labservices_client.lab_plans.get(GROUP_NAME, LABPLAN)
if __name__ == "__main__":
main()
清除資源
如果您不打算繼續使用此應用程式,請使用下列步驟來刪除實驗室:
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from datetime import timedelta
import time
from azure.identity import DefaultAzureCredential
from azure.mgmt.labservices import LabServicesClient
from azure.mgmt.resource import ResourceManagementClient
# - other dependence -
# - end -
#
def main():
SUBSCRIPTION_ID = "<Subscription ID>"
TIME = str(time.time()).replace('.','')
GROUP_NAME = "BellowsCollege_rg" + TIME
LABPLAN = "BellowsCollege_labplan" + TIME
LAB = "BellowsCollege_lab" + TIME
LOCATION = 'southcentralus'
# Create clients
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
resource_client = ResourceManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
labservices_client = LabServicesClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
# Delete Lab
labservices_client.labs.begin_delete(
GROUP_NAME,
LAB
).result()
print("Deleted lab.\n")
# Delete Group
resource_client.resource_groups.begin_delete(
GROUP_NAME
).result()
if __name__ == "__main__":
main()
下一步
在本文中,您已建立資源群組和實驗室計劃。 身為管理員,您可以深入了解 Azure PowerShell 模組 (部分機器翻譯) 和 Az.LabServices Cmdlet (部分機器翻譯)。
使用 Python 和 Azure Python SDK 建立實驗室 (部分機器翻譯)