|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create a lab with REST API' |
| 3 | +description: In this quickstart, you create a lab in Azure DevTest Labs by using an Azure REST API. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 10/27/2021 |
| 6 | +# Customer intent: As an administrator, I want to set up a lab so that my developers have a test environment. |
| 7 | +--- |
| 8 | + |
| 9 | +# Quickstart: Create a lab in Azure DevTest Labs using Azure REST API |
| 10 | + |
| 11 | +Get started with Azure DevTest Labs by using the Azure REST API. Azure DevTest Labs encompasses a group of resources, such as Azure virtual machines (VMs) and networks. This infrastructure lets you better manage those resources by specifying limits and quotas. The Azure REST API allows you to manage operations on services hosted in the Azure platform. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 16 | + |
| 17 | +- The PowerShell [Az Module](/powershell/azure/new-azureps-module-az) installed. Ensure you have the latest version. If necessary, run `Update-Module -Name Az`. |
| 18 | + |
| 19 | +## Prepare request body |
| 20 | + |
| 21 | +Prepare the [request body](/rest/api/dtl/labs/create-or-update.md#request-body) to be consumed by the REST call. |
| 22 | + |
| 23 | +Copy and paste the following JSON syntax into a file called `body.json`. Save the file on your local machine or in an Azure storage account. |
| 24 | + |
| 25 | +```json |
| 26 | +{ |
| 27 | + "properties": { |
| 28 | + "labStorageType": "Standard" |
| 29 | + }, |
| 30 | + "location": "westus2", |
| 31 | + "tags": { |
| 32 | + "Env": "alpha" |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +## Sign in to your Azure subscription |
| 38 | + |
| 39 | +1. Provide an appropriate value for the variables and then execute the script. |
| 40 | + |
| 41 | + ```powershell |
| 42 | + $subscription = "subscriptionID" |
| 43 | + $resourceGroup = "resourceGroupName" |
| 44 | + $labName = "labName" |
| 45 | + $file = "path\body.json" |
| 46 | + ``` |
| 47 | +
|
| 48 | +1. From your workstation, sign in to your Azure subscription with the PowerShell [Connect-AzAccount](/powershell/module/Az.Accounts/Connect-AzAccount) cmdlet and follow the on-screen directions. |
| 49 | +
|
| 50 | + ```powershell |
| 51 | + # Sign in to your Azure subscription |
| 52 | + $sub = Get-AzSubscription -ErrorAction SilentlyContinue |
| 53 | + if(-not($sub)) |
| 54 | + { |
| 55 | + Connect-AzAccount |
| 56 | + } |
| 57 | + |
| 58 | + # If you have multiple subscriptions, set the one to use |
| 59 | + # Set-AzContext -SubscriptionId $subscription |
| 60 | + ``` |
| 61 | +
|
| 62 | +## Build request body for submission |
| 63 | +
|
| 64 | +The syntax for the PUT request is:<br> |
| 65 | + `https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{name}?api-version=2018-09-15`. |
| 66 | +
|
| 67 | + Execute the following PowerShell scripts to pass the request value to a parameter. The contents of the request body is passed to a parameter as well. |
| 68 | +
|
| 69 | +```powershell |
| 70 | +# build URI |
| 71 | +$URI = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DevTestLab/labs/$labName`?api-version=2018-09-15" |
| 72 | +
|
| 73 | +# build body |
| 74 | +$body = Get-Content $file |
| 75 | +``` |
| 76 | + |
| 77 | +## Obtain an authentication token |
| 78 | + |
| 79 | +Use the following commands to retrieve an authentication token: |
| 80 | + |
| 81 | +```powershell |
| 82 | +$azContext = Get-AzContext |
| 83 | +$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile |
| 84 | +$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile) |
| 85 | +$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId) |
| 86 | +$authHeader = @{ |
| 87 | + 'Content-Type'='application/json' |
| 88 | + 'Authorization'='Bearer ' + $token.AccessToken |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Invoke the REST API |
| 93 | + |
| 94 | +Use the following commands to invoke the REST API and review the response. |
| 95 | + |
| 96 | +```powershell |
| 97 | +# Invoke the REST API |
| 98 | +$response = Invoke-RestMethod -Uri $URI -Method PUT -Headers $authHeader -Body $body |
| 99 | +
|
| 100 | +# Review output |
| 101 | +$response | ConvertTo-Json |
| 102 | +``` |
| 103 | + |
| 104 | +The response should look similar to the following text: |
| 105 | + |
| 106 | +```output |
| 107 | +{ |
| 108 | + "properties": { |
| 109 | + "labStorageType": "Standard", |
| 110 | + "mandatoryArtifactsResourceIdsLinux": [ |
| 111 | +
|
| 112 | + ], |
| 113 | + "mandatoryArtifactsResourceIdsWindows": [ |
| 114 | +
|
| 115 | + ], |
| 116 | + "createdDate": "2021-10-27T20:22:49.7495913+00:00", |
| 117 | + "premiumDataDisks": "Disabled", |
| 118 | + "environmentPermission": "Reader", |
| 119 | + "announcement": { |
| 120 | + "title": "", |
| 121 | + "markdown": "", |
| 122 | + "enabled": "Disabled", |
| 123 | + "expired": false |
| 124 | + }, |
| 125 | + "support": { |
| 126 | + "enabled": "Disabled", |
| 127 | + "markdown": "" |
| 128 | + }, |
| 129 | + "provisioningState": "Creating", |
| 130 | + "uniqueIdentifier": "uniqueID" |
| 131 | + }, |
| 132 | + "id": "/subscriptions/ContosoID/resourcegroups/groupcontoso/providers/microsoft.devtestlab/labs/myotherlab", |
| 133 | +
|
| 134 | + "name": "myOtherLab", |
| 135 | + "type": "Microsoft.DevTestLab/labs", |
| 136 | + "location": "westus2", |
| 137 | + "tags": { |
| 138 | + "Env": "alpha" |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +## Clean up resources |
| 144 | + |
| 145 | +If you're not going to continue to use this lab, delete it with the following steps: |
| 146 | + |
| 147 | +1. Provide an appropriate value for the variables and then execute the script. |
| 148 | + |
| 149 | + ```powershell |
| 150 | + $subscription = "subscriptionID" |
| 151 | + $resourceGroup = "resourceGroupName" |
| 152 | + $labName = "labName" |
| 153 | + ``` |
| 154 | +
|
| 155 | +1. Execute the following script to remove the named lab from Azure DevTest Labs. |
| 156 | +
|
| 157 | + ```powershell |
| 158 | + # build URI |
| 159 | + $URI = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DevTestLab/labs/$labName`?api-version=2018-09-15" |
| 160 | + |
| 161 | + # obtain access token |
| 162 | + $azContext = Get-AzContext |
| 163 | + $azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile |
| 164 | + $profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile) |
| 165 | + $token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId) |
| 166 | + $authHeader = @{ |
| 167 | + 'Content-Type'='application/json' |
| 168 | + 'Authorization'='Bearer ' + $token.AccessToken |
| 169 | + } |
| 170 | + |
| 171 | + # Invoke the REST API |
| 172 | + Invoke-RestMethod -Uri $URI -Method DELETE -Headers $authHeader |
| 173 | + ``` |
| 174 | +
|
| 175 | +## Next steps |
| 176 | +
|
| 177 | +In this quickstart, you created a lab using the Azure REST API. To learn how to access the lab, advance to the next tutorial: |
| 178 | +
|
| 179 | +> [!div class="nextstepaction"] |
| 180 | +> [Tutorial: Access the lab](../tutorial-use-custom-lab.md) |
0 commit comments