kyuz0
kyuz0

Reputation: 111

Azure SDK C# - Plan Virtual Machine Auto Shutdown

I'm looking for documentation / examples on how to plan the shutdown of an Azure virtual machine from the Azure C# SDK.

The idea is that on creating a VM from the SDK I want set a time when the VM will automatically shutdown. I don't seem to be able to find any function in the API to do that in an obvious way.

Upvotes: 1

Views: 504

Answers (1)

Architect Jamie
Architect Jamie

Reputation: 2589

The auto-shutdown options for a virtual machine in the Portal use the Microsoft.DevTestLab provider. This is a bit of a hack on Microsoft's part, and it's likely to change in the future.

Unfortunately, the Microsoft.Azure.Management.DevTestLabs namespace of the SDK only contains methods built to interact with true DevTest Labs, meaning that all calls to the management API have "/labs/" tacked on to the URI thus invalidating the API requests; The actual resource is:

https://management.azure.com/subscriptions/{Subsciption-Id}/resourceGroups/{Resource-Group}/providers/Microsoft.DevTestLab/schedules/shutdown-computevm-{VM-Name}?api-version=2018-09-15

(Note that the URI does not contain "/labs/")

It can be deployed using ARM, and it can be deployed/updated by calling the management API using the proper URI.

Here is an example:

PUT https://management.azure.com/subscriptions/{SUBSCRIPTION-ID}/resourceGroups/{RESOURCE-GROUP-NAME}/providers/Microsoft.DevTestLab/schedules/shutdown-computevm-{VMNAME}?api-version=2018-09-15 HTTP/1.1
User-Agent: Fiddler
Authorization: Bearer {BEARER-TOKEN-HERE}
Host: management.azure.com
Content-Type: application/json
Content-Length: 1048

{
  "id": "/subscriptions/{SUBSCRIPTION-ID}/resourcegroups/{RESOURCE-GROUP-NAME}/providers/microsoft.devtestlab/schedules/shutdown-computevm-{VMNAME}",
  "identity": null,
  "kind": null,
  "location": "{VM-LOCATION}",
  "managedBy": null,
  "name": "shutdown-computevm-{VM-NAME}",
  "plan": null,
  "properties": {
    "createdDate": "2019-10-29T19:45:57.0033318+00:00",
    "dailyRecurrence": {
      "time": "{4-DIGIT-24HOUR-TIME-HHmm}"
    },
    "notificationSettings": {
      "notificationLocale": "en",
      "status": "Disabled",
      "timeInMinutes": 30
    },
    "provisioningState": "Succeeded",
    "status": "Enabled",
    "targetResourceId": "/subscriptions/{SUBSCIPTION-ID}/resourceGroups/{RESOURCE-GROUP}/providers/Microsoft.Compute/virtualMachines/{VMNAME}",
    "taskType": "ComputeVmShutdownTask",
    "timeZoneId": "GMT Standard Time",
    "uniqueIdentifier": "7c394xxx-3fdd-4xxa-8cdc-7c7e6xxxxxxc"
  },
  "resourceGroup": "{RESOURCE-GROUP-NAME}",
  "sku": null,
  "tags": null,
  "type": "microsoft.devtestlab/schedules"
}

You can use this JSON, substituting the values enclosed with braces, and create or update the resource as required. If the resource already exists it will be overwritten.

Upvotes: 1

Related Questions