From b3654e257dd56a2c8b4cac69905b9926e17310f9 Mon Sep 17 00:00:00 2001 From: Harshavardhan Musanalli Date: Fri, 16 Feb 2024 01:34:17 +0100 Subject: [PATCH] #24358: Support vm_template for azurerm_virtual_desktop_host_pool resource (#24369) * #24358: Support vmTemplate for azurerm_virtual_desktop_host_pool resource * #24358: Changes as per review comments * #24358: Fix error * #24358: Adding testcase for vm_template --- .../virtual_desktop_host_pool_resource.go | 22 ++++++++ ...virtual_desktop_host_pool_resource_test.go | 55 +++++++++++++++++++ .../r/virtual_desktop_host_pool.html.markdown | 2 + 3 files changed, 79 insertions(+) diff --git a/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource.go b/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource.go index 4b8639e694098..c25fff5b43087 100644 --- a/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource.go +++ b/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource.go @@ -196,6 +196,13 @@ func resourceVirtualDesktopHostPool() *pluginsdk.Resource { }, }, + "vm_template": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringIsJSON, + DiffSuppressFunc: pluginsdk.SuppressJsonDiff, + }, + "tags": commonschema.Tags(), }, } @@ -220,6 +227,15 @@ func resourceVirtualDesktopHostPoolCreate(d *pluginsdk.ResourceData, meta interf } personalDesktopAssignmentType := hostpool.PersonalDesktopAssignmentType(d.Get("personal_desktop_assignment_type").(string)) + vmTemplate := d.Get("vm_template").(string) + if vmTemplate != "" { + // we have no use with the json object as azure accepts string only + // merely here for validation + _, err := pluginsdk.ExpandJsonFromString(vmTemplate) + if err != nil { + return fmt.Errorf("expanding JSON for `vm_template`: %+v", err) + } + } payload := hostpool.HostPool{ Location: utils.String(location.Normalize(d.Get("location").(string))), Tags: tags.Expand(d.Get("tags").(map[string]interface{})), @@ -235,6 +251,7 @@ func resourceVirtualDesktopHostPoolCreate(d *pluginsdk.ResourceData, meta interf PersonalDesktopAssignmentType: &personalDesktopAssignmentType, PreferredAppGroupType: hostpool.PreferredAppGroupType(d.Get("preferred_app_group_type").(string)), AgentUpdate: expandAgentUpdateCreate(d.Get("scheduled_agent_updates").([]interface{})), + VMTemplate: &vmTemplate, }, } @@ -305,6 +322,10 @@ func resourceVirtualDesktopHostPoolUpdate(d *pluginsdk.ResourceData, meta interf if d.HasChanges("scheduled_agent_updates") { payload.Properties.AgentUpdate = expandAgentUpdatePatch(d.Get("scheduled_agent_updates").([]interface{})) } + + if d.HasChanges("vm_template") { + payload.Properties.VMTemplate = utils.String(d.Get("vm_template").(string)) + } } if _, err := client.Update(ctx, *id, payload); err != nil { @@ -365,6 +386,7 @@ func resourceVirtualDesktopHostPoolRead(d *pluginsdk.ResourceData, meta interfac d.Set("type", string(props.HostPoolType)) d.Set("validate_environment", props.ValidationEnvironment) d.Set("scheduled_agent_updates", flattenAgentUpdate(props.AgentUpdate)) + d.Set("vm_template", props.VMTemplate) } return nil diff --git a/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource_test.go b/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource_test.go index 18d6785aec357..38de97b451071 100644 --- a/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource_test.go +++ b/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource_test.go @@ -33,6 +33,21 @@ func TestAccVirtualDesktopHostPool_basic(t *testing.T) { }) } +func TestAccVirtualDesktopHostPool_vmTemplate(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_virtual_desktop_host_pool", "test") + r := VirtualDesktopHostPoolResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.vmTemplate(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("vm_template").IsNotEmpty(), + ), + }, + }) +} + func TestAccVirtualDesktopHostPool_agentupdates(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_virtual_desktop_host_pool", "test") r := VirtualDesktopHostPoolResource{} @@ -253,6 +268,46 @@ resource "azurerm_virtual_desktop_host_pool" "test" { `, data.RandomInteger, data.Locations.Secondary, data.RandomString) } +func (VirtualDesktopHostPoolResource) vmTemplate(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-vdesktophp-%d" + location = "%s" +} + +resource "azurerm_virtual_desktop_host_pool" "test" { + name = "acctestHP%s" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + type = "Pooled" + validate_environment = true + load_balancer_type = "DepthFirst" + + vm_template = <