|
| 1 | +--- |
| 2 | +title: Azure Lab Services quickstart - Create a lab plan using Python |
| 3 | +description: In this quickstart, you learn how to create an Azure Lab Services lab plan using Python and the Azure Python SDK. |
| 4 | +author: RogerBestMSFT |
| 5 | +ms.topic: quickstart |
| 6 | +ms.date: 02/15/2022 |
| 7 | +ms.custom: template-quickstart |
| 8 | +--- |
| 9 | + |
| 10 | +# Quickstart: Create a lab plan using Python and the Azure libraries (SDK) for Python |
| 11 | + |
| 12 | +In this article you, as the admin, use Python and the Azure Python SDK to create a lab plan. Lab plans are used when creating labs for Azure Lab Services. You'll also add a role assignment so an educator can create labs based on the lab plan. For an overview of Azure Lab Services, see [An introduction to Azure Lab Services](lab-services-overview.md). |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free). |
| 17 | +- [Setup Local Python dev environment for Azure](/azure/developer/python/configure-local-development-environment). |
| 18 | +- [The requirements.txt can be downloaded from the Azure Python samples](https://github.com/RogerBestMsft/azure-samples-python-management/blob/rbest_ALSSample/samples/labservices/requirements.txt) |
| 19 | + |
| 20 | +## Create a lab plan |
| 21 | + |
| 22 | +The following steps will show you how to create a lab plan. Any properties set in the lab plan will be used in labs created with this plan. |
| 23 | + |
| 24 | +```python |
| 25 | +# -------------------------------------------------------------------------- |
| 26 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 27 | +# Licensed under the MIT License. See License.txt in the project root for |
| 28 | +# license information. |
| 29 | +# -------------------------------------------------------------------------- |
| 30 | + |
| 31 | +import os |
| 32 | +import time |
| 33 | +from datetime import timedelta |
| 34 | +from azure.identity import DefaultAzureCredential |
| 35 | +from azure.mgmt.labservices import LabServicesClient |
| 36 | +from azure.mgmt.resource import ResourceManagementClient |
| 37 | + |
| 38 | +def main(): |
| 39 | + |
| 40 | + SUBSCRIPTION_ID = "<Subscription ID>" |
| 41 | + TIME = str(time.time()).replace('.','') |
| 42 | + GROUP_NAME = "BellowsCollege_rg" |
| 43 | + LABPLAN = "BellowsCollege_labplan" |
| 44 | + LAB = "BellowsCollege_lab" |
| 45 | + LOCATION = 'southcentralus' |
| 46 | + |
| 47 | + # Create clients |
| 48 | + # # For other authentication approaches, please see: https://pypi.org/project/azure-identity/ |
| 49 | + resource_client = ResourceManagementClient( |
| 50 | + credential=DefaultAzureCredential(), |
| 51 | + subscription_id=SUBSCRIPTION_ID |
| 52 | + ) |
| 53 | + |
| 54 | + labservices_client = LabServicesClient( |
| 55 | + credential=DefaultAzureCredential(), |
| 56 | + subscription_id=SUBSCRIPTION_ID |
| 57 | + ) |
| 58 | + |
| 59 | + # Create resource group |
| 60 | + resource_client.resource_groups.create_or_update( |
| 61 | + GROUP_NAME, |
| 62 | + {"location": LOCATION} |
| 63 | + ) |
| 64 | + |
| 65 | + # Create lab services lab plan |
| 66 | + LABPLANBODY = { |
| 67 | + "location" : LOCATION, |
| 68 | + "properties" : { |
| 69 | + "defaultConnectionProfile" : { |
| 70 | + "webSshAccess" : "None", |
| 71 | + "webRdpAccess" : "None", |
| 72 | + "clientSshAccess" : "None", |
| 73 | + "clientRdpAccess" : "Public" |
| 74 | + }, |
| 75 | + "defaultAutoShutdownProfile" : { |
| 76 | + "shutdownOnDisconnect" : "Disabled", |
| 77 | + "shutdownWhenNotConnected" : "Disabled", |
| 78 | + "shutdownOnIdle" : "None" |
| 79 | + }, |
| 80 | + "allowedRegions" : [LOCATION], |
| 81 | + "supportInfo" : { |
| 82 | + |
| 83 | + "phone" : "123-123-1234", |
| 84 | + "instructions" : "Bellows College support." |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + #Create Lab Plan |
| 90 | + poller = labservices_client.lab_plans.begin_create_or_update( |
| 91 | + GROUP_NAME, |
| 92 | + LABPLAN, |
| 93 | + LABPLANBODY |
| 94 | + ) |
| 95 | + |
| 96 | + # Poll for long running execution. |
| 97 | + labplan_result = poller.result() |
| 98 | + print(f"Created Lab Plan: {labplan_result.name}") |
| 99 | + |
| 100 | + # Get LabServices Lab Plans by resource group |
| 101 | + labservices_client.lab_plans.list_by_resource_group( |
| 102 | + GROUP_NAME |
| 103 | + ) |
| 104 | + |
| 105 | + #Get single LabServices Lab Plan |
| 106 | + labservices_labplan = labservices_client.lab_plans.get(GROUP_NAME, LABPLAN) |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
| 110 | +``` |
| 111 | + |
| 112 | +## Clean up resources |
| 113 | + |
| 114 | +If you're not going to continue to use this application, delete the lab with the following steps: |
| 115 | + |
| 116 | +```python |
| 117 | +# -------------------------------------------------------------------------- |
| 118 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 119 | +# Licensed under the MIT License. See License.txt in the project root for |
| 120 | +# license information. |
| 121 | +# -------------------------------------------------------------------------- |
| 122 | + |
| 123 | +from datetime import timedelta |
| 124 | +import time |
| 125 | +from azure.identity import DefaultAzureCredential |
| 126 | +from azure.mgmt.labservices import LabServicesClient |
| 127 | +from azure.mgmt.resource import ResourceManagementClient |
| 128 | + |
| 129 | +# - other dependence - |
| 130 | +# - end - |
| 131 | +# |
| 132 | + |
| 133 | +def main(): |
| 134 | + |
| 135 | + SUBSCRIPTION_ID = "<Subscription ID>" |
| 136 | + TIME = str(time.time()).replace('.','') |
| 137 | + GROUP_NAME = "BellowsCollege_rg" + TIME |
| 138 | + LABPLAN = "BellowsCollege_labplan" + TIME |
| 139 | + LAB = "BellowsCollege_lab" + TIME |
| 140 | + LOCATION = 'southcentralus' |
| 141 | + |
| 142 | + # Create clients |
| 143 | + # # For other authentication approaches, please see: https://pypi.org/project/azure-identity/ |
| 144 | + resource_client = ResourceManagementClient( |
| 145 | + credential=DefaultAzureCredential(), |
| 146 | + subscription_id=SUBSCRIPTION_ID |
| 147 | + ) |
| 148 | + |
| 149 | + labservices_client = LabServicesClient( |
| 150 | + credential=DefaultAzureCredential(), |
| 151 | + subscription_id=SUBSCRIPTION_ID |
| 152 | + ) |
| 153 | + |
| 154 | + # Delete Lab |
| 155 | + labservices_client.labs.begin_delete( |
| 156 | + GROUP_NAME, |
| 157 | + LAB |
| 158 | + ).result() |
| 159 | + print("Deleted lab.\n") |
| 160 | + |
| 161 | + # Delete Group |
| 162 | + resource_client.resource_groups.begin_delete( |
| 163 | + GROUP_NAME |
| 164 | + ).result() |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + main() |
| 169 | +``` |
| 170 | +## Next steps |
| 171 | + |
| 172 | +In this QuickStart, you created a resource group and a lab plan. As an admin, you can learn more about [Azure PowerShell module](/powershell/azure) and [Az.LabServices cmdlets](/powershell/module/az.labservices/). |
| 173 | + |
| 174 | +> [!div class="nextstepaction"] |
| 175 | +> [Quickstart: Create a lab using Python and the Azure Python SDK](quick-create-lab-python.md) |
0 commit comments