你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。

使用 Python 和 Azure Python 库 (SDK) 在 Azure 实验室服务中创建实验室计划

重要

Azure 实验室服务将于 2027 年 6 月 28 日停用。 有关详细信息,请参阅停用指南。 为了简化迁移,Microsoft已发布自动化脚本来帮助清理实验室服务资源,这些脚本在 Azure 实验室服务停用脚本 GitHub 存储库中提供。

本文介绍如何使用 Python 和 Azure Python SDK 创建实验室计划。 为 Azure 实验室服务创建实验室时,将使用实验室计划。 你还将添加角色分配,以便教师可以根据实验室计划创建实验室。 有关 Azure 实验室服务的概述,请参阅 Azure 实验室服务简介。

先决条件

  • 具有活动订阅的 Azure 帐户。 如果没有 Azure 订阅,请在开始之前创建一个免费帐户。

创建实验室计划

以下步骤将展示如何创建实验室计划。 实验室计划中设置的任何属性都将在通过此计划创建的实验室中使用。

# --------------------------------------------------------------------------
# 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。