From 17bab2d8888e5f72c775fccabb7f57c0e773414f Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Wed, 4 Nov 2020 11:27:37 +0100 Subject: [PATCH] desktopvirtualization: fixing the host pool id/adding validation --- .../validate/virtual_desktop_host_pool.go | 22 +++++++++++++++++++ ...tual_desktop_application_group_resource.go | 19 +++++++++++++--- ...desktop_application_group_resource_test.go | 1 - .../virtual_desktop_host_pool_resource.go | 3 +-- ...virtual_desktop_host_pool_resource_test.go | 2 -- ..._application_group_association_resource.go | 3 ++- 6 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 azurerm/internal/services/desktopvirtualization/validate/virtual_desktop_host_pool.go diff --git a/azurerm/internal/services/desktopvirtualization/validate/virtual_desktop_host_pool.go b/azurerm/internal/services/desktopvirtualization/validate/virtual_desktop_host_pool.go new file mode 100644 index 000000000000..dec4054571c3 --- /dev/null +++ b/azurerm/internal/services/desktopvirtualization/validate/virtual_desktop_host_pool.go @@ -0,0 +1,22 @@ +package validate + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/desktopvirtualization/parse" +) + +func VirtualDesktopHostPoolID(i interface{}, k string) (warnings []string, errors []error) { + v, ok := i.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) + return + } + + if _, err := parse.VirtualDesktopHostPoolID(v); err != nil { + errors = append(errors, fmt.Errorf("Can not parse %q as a resource id: %v", k, err)) + return + } + + return warnings, errors +} diff --git a/azurerm/internal/services/desktopvirtualization/virtual_desktop_application_group_resource.go b/azurerm/internal/services/desktopvirtualization/virtual_desktop_application_group_resource.go index 11873b23b884..a5d4ea831d2a 100644 --- a/azurerm/internal/services/desktopvirtualization/virtual_desktop_application_group_resource.go +++ b/azurerm/internal/services/desktopvirtualization/virtual_desktop_application_group_resource.go @@ -14,6 +14,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/desktopvirtualization/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/desktopvirtualization/validate" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" @@ -70,8 +71,9 @@ func resourceArmVirtualDesktopApplicationGroup() *schema.Resource { }, "host_pool_id": { - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.VirtualDesktopHostPoolID, }, "friendly_name": { @@ -145,6 +147,7 @@ func resourceArmVirtualDesktopApplicationGroupCreateUpdate(d *schema.ResourceDat func resourceArmVirtualDesktopApplicationGroupRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DesktopVirtualization.ApplicationGroupsClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() @@ -174,8 +177,18 @@ func resourceArmVirtualDesktopApplicationGroupRead(d *schema.ResourceData, meta if props := resp.ApplicationGroupProperties; props != nil { d.Set("friendly_name", props.FriendlyName) d.Set("description", props.Description) - d.Set("host_pool_id", props.HostPoolArmPath) d.Set("type", string(props.ApplicationGroupType)) + + hostPoolIdStr := "" + if props.HostPoolArmPath != nil { + hostPoolId, err := parse.VirtualDesktopHostPoolID(*props.HostPoolArmPath) + if err != nil { + return fmt.Errorf("parsing Host Pool ID %q: %+v", *props.HostPoolArmPath, err) + } + + hostPoolIdStr = hostPoolId.ID(subscriptionId) + } + d.Set("host_pool_id", hostPoolIdStr) } return tags.FlattenAndSet(d, resp.Tags) diff --git a/azurerm/internal/services/desktopvirtualization/virtual_desktop_application_group_resource_test.go b/azurerm/internal/services/desktopvirtualization/virtual_desktop_application_group_resource_test.go index e7da2b381634..97c59e556aed 100644 --- a/azurerm/internal/services/desktopvirtualization/virtual_desktop_application_group_resource_test.go +++ b/azurerm/internal/services/desktopvirtualization/virtual_desktop_application_group_resource_test.go @@ -190,7 +190,6 @@ resource "azurerm_virtual_desktop_application_group" "test" { type = "Desktop" host_pool_id = azurerm_virtual_desktop_host_pool.test.id } - `, data.RandomInteger, data.Locations.Secondary, data.RandomIntOfLength(8)) } diff --git a/azurerm/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource.go b/azurerm/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource.go index be3d348296fc..32cd2564448d 100644 --- a/azurerm/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource.go +++ b/azurerm/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/validation" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/desktopvirtualization/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" @@ -44,7 +43,7 @@ func resourceArmVirtualDesktopHostPool() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validate.DevSpaceName(), + ValidateFunc: validation.StringIsNotEmpty, }, "location": azure.SchemaLocation(), diff --git a/azurerm/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource_test.go b/azurerm/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource_test.go index 5917c0aad485..3a9324c78ef8 100644 --- a/azurerm/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource_test.go +++ b/azurerm/internal/services/desktopvirtualization/virtual_desktop_host_pool_resource_test.go @@ -185,7 +185,6 @@ resource "azurerm_virtual_desktop_host_pool" "test" { validate_environment = true load_balancer_type = "BreadthFirst" } - `, data.RandomInteger, data.Locations.Secondary, data.RandomString) } @@ -240,7 +239,6 @@ resource "azurerm_virtual_desktop_host_pool" "import" { location = azurerm_virtual_desktop_host_pool.test.location resource_group_name = azurerm_virtual_desktop_host_pool.test.resource_group_name validate_environment = azurerm_virtual_desktop_host_pool.test.validate_environment - description = azurerm_virtual_desktop_host_pool.test.description type = azurerm_virtual_desktop_host_pool.test.type load_balancer_type = azurerm_virtual_desktop_host_pool.test.load_balancer_type } diff --git a/azurerm/internal/services/desktopvirtualization/virtual_desktop_workspace_application_group_association_resource.go b/azurerm/internal/services/desktopvirtualization/virtual_desktop_workspace_application_group_association_resource.go index 877d06a74d72..297d6c7c9508 100644 --- a/azurerm/internal/services/desktopvirtualization/virtual_desktop_workspace_application_group_association_resource.go +++ b/azurerm/internal/services/desktopvirtualization/virtual_desktop_workspace_application_group_association_resource.go @@ -3,6 +3,7 @@ package desktopvirtualization import ( "fmt" "log" + "strings" "time" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -131,7 +132,7 @@ func resourceArmVirtualDesktopWorkspaceApplicationGroupAssociationRead(d *schema applicationGroupId := id.ApplicationGroup.ID(subscriptionId) if props := workspace.WorkspaceProperties; props != nil && props.ApplicationGroupReferences != nil { for _, reference := range *props.ApplicationGroupReferences { - exists = reference == applicationGroupId + exists = strings.EqualFold(reference, applicationGroupId) if exists { break }