hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.59k stars 4.63k forks source link

Feature Request: DevTest Lab Schedules support for ComputeVmShutdownTask task type #5863

Open mikebranstein opened 4 years ago

mikebranstein commented 4 years ago

Community Note

Description

As of the 1.33.0 release of DevTest Labs Schedules, a schedule can only be applied to a DevTest Lab via the lab_name property. Azure also uses these schedules to perform automated VM shutdowns for VMs outside of DevTest Labs.

For example, the ARM JSON for such a shutdown schedule is:

{
  "type": "microsoft.devtestlab/schedules",
  "apiVersion": "2016-05-15",
  "name": "<dev-test-lab-schedule-name>",
  "location": "<location>",
  "properties": {
    "status": "Enabled",
    "taskType": "ComputeVmShutdownTask",
    "dailyRecurrence": {
      "time": "1900"
    },
    "timeZoneId": "Eastern Standard Time",
    "notificationSettings": {
      "status": "Disabled",
      "timeInMinutes": 30
    },
    "targetResourceId": "<vm-external-id>",
    "provisioningState": "Succeeded",
    "uniqueIdentifier": "<resource-uuid>"
  }
}

As you can see, the targetResourceId points to a VM's external id instead of a DevTest Lab. The task type is also different from what is supported by the v1.33.0 resource currently.

The external id is formatted as: /subscriptions/<subscription_id>/resourceGroups/<resource-group-name>/providers/Microsoft.Compute/virtualMachines/<virtual-machine-name>

New or Affected Resource(s)

Potential Terraform Configuration

This could mean lab_name becomes an optional parameter, and require the name or id of the virtual machine to be optional. The ComputeVmShutdownTask would need added as a valid task type.

References

NOTE: this was previously submitted as issue #4135, and closed. citing that it was a duplicate of #2234 and resolved by #3554. This is not correct. This is not a duplicate. #3554 does not resolve the issue.

See notes attached to #4135 for additional context and history.

philthynz commented 4 years ago

Highly agree that is a requirement for DevTest Lab Schedules. I've seen a pull request to add azurerm_dev_test_global_shutdown_schedule which looks promising.

As a work around, for anyone looking to do the same. You can do it via AzueRM templates in Terraform. Not ideal as you can't manage the resource, but it can create and modify it.

resource "azurerm_template_deployment" "shutdown_schedule_template" {
  count               = var.instance_numbers
  name                = "${var.team_name}_${var.release}_shutdown_schedule_template_${count.index}"
  resource_group_name = "${var.team_name}_${var.release}_${var.owner}_${var.intention}"
  deployment_mode = "Incremental"

  parameters = {
   "location" = var.loc
   "virtualMachineName" = element(azurerm_windows_virtual_machine.win_vm.*.name, count.index)
   "autoShutdownStatus" = "Enabled"
   "autoShutdownTime"   = "18:00"
   "autoShutdownTimeZone" = "New Zealand Standard Time"
   "autoShutdownNotificationStatus" = "Disabled"
   "autoShutdownNotificationLocale" = "en"
  }

  template_body = <<DEPLOY
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
      "parameters": {
          "location": {
              "type": "string"
          },
          "virtualMachineName": {
              "type": "string"
          },
          "autoShutdownStatus": {
              "type": "string"
          },
          "autoShutdownTime": {
              "type": "string"
          },
          "autoShutdownTimeZone": {
              "type": "string"
          },
          "autoShutdownNotificationStatus": {
              "type": "string"
          },
          "autoShutdownNotificationLocale": {
              "type": "string"
          }
      },
      "resources": [
        {
            "name": "[concat('shutdown-computevm-', parameters('virtualMachineName'))]",
            "type": "Microsoft.DevTestLab/schedules",
            "apiVersion": "2018-09-15",
            "location": "[parameters('location')]",
            "properties": {
                "status": "[parameters('autoShutdownStatus')]",
                "taskType": "ComputeVmShutdownTask",
                "dailyRecurrence": {
                    "time": "[parameters('autoShutdownTime')]"
                },
                "timeZoneId": "[parameters('autoShutdownTimeZone')]",
                "targetResourceId": "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
                "notificationSettings": {
                    "status": "[parameters('autoShutdownNotificationStatus')]",
                    "notificationLocale": "[parameters('autoShutdownNotificationLocale')]",
                    "timeInMinutes": "30"
                }
            }
        }
    ]
  }
  DEPLOY
}