Bagikan melalui


Membuat paket lab di Azure Lab Services menggunakan Python dan pustaka Azure (SDK) untuk Python

Penting

Azure Lab Services akan dihentikan pada 28 Juni 2027. Untuk informasi selengkapnya, lihat panduan penghentian. Untuk menyederhanakan migrasi Anda, Microsoft telah menerbitkan skrip otomatisasi untuk membantu Anda membersihkan sumber daya Lab Services, ini tersedia di repositori GitHub Azure Lab Services Retirement Scripts.

Dalam artikel ini, Anda mempelajari cara menggunakan Python dan Azure Python SDK untuk membuat rencana lab. Paket lab digunakan saat membuat lab untuk Azure Lab Services. Anda juga akan menambahkan penetapan peran sehingga pendidik dapat membuat lab berdasarkan rencana lab. Untuk gambaran umum Azure Lab Services, lihat Pengantar Azure Lab Services.

Prasyarat

  • Akun Azure dengan langganan aktif. Jika Anda tidak memiliki langganan Azure, buat akun gratis sebelum Anda memulai.

Membuat rencana lab

Langkah-langkah berikut akan menunjukkan kepada Anda cara membuat paket lab. Properti apa pun yang diatur dalam rencana lab akan digunakan di lab yang dibuat dengan paket ini.

# --------------------------------------------------------------------------
# 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()

Membersihkan sumber daya

Jika Anda tidak akan terus menggunakan aplikasi ini, hapus lab dengan langkah-langkah berikut:

# --------------------------------------------------------------------------
# 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()

Langkah berikutnya

Dalam artikel ini, Anda membuat grup sumber daya dan paket lab. Sebagai admin, Anda dapat mempelajari selengkapnya tentang modul Azure PowerShell dan cmdlet Az.LabServices.