From dc2bc4f08c199d14b9c768cef03a8ae52454cced Mon Sep 17 00:00:00 2001 From: Yichun Ma Date: Tue, 14 Feb 2023 16:13:41 +0800 Subject: [PATCH] `azurerm_private_endpoint` - read private endpoint connection insensitively --- internal/services/mysql/parse/server.go | 44 ++++++++++ .../network/private_endpoint_resource.go | 82 ++++++++----------- 2 files changed, 78 insertions(+), 48 deletions(-) diff --git a/internal/services/mysql/parse/server.go b/internal/services/mysql/parse/server.go index d05075b10ec5c..d2eb1c0edccda 100644 --- a/internal/services/mysql/parse/server.go +++ b/internal/services/mysql/parse/server.go @@ -67,3 +67,47 @@ func ServerID(input string) (*ServerId, error) { return &resourceId, nil } + +// ServerIDInsensitively parses a Server ID into an ServerId struct, insensitively +// This should only be used to parse an ID for rewriting, the ServerID +// method should be used instead for validation etc. +// +// Whilst this may seem strange, this enables Terraform have consistent casing +// which works around issues in Core, whilst handling broken API responses. +func ServerIDInsensitively(input string) (*ServerId, error) { + id, err := resourceids.ParseAzureResourceID(input) + if err != nil { + return nil, err + } + + resourceId := ServerId{ + SubscriptionId: id.SubscriptionID, + ResourceGroup: id.ResourceGroup, + } + + if resourceId.SubscriptionId == "" { + return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + } + + if resourceId.ResourceGroup == "" { + return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + } + + // find the correct casing for the 'servers' segment + serversKey := "servers" + for key := range id.Path { + if strings.EqualFold(key, serversKey) { + serversKey = key + break + } + } + if resourceId.Name, err = id.PopSegment(serversKey); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &resourceId, nil +} diff --git a/internal/services/network/private_endpoint_resource.go b/internal/services/network/private_endpoint_resource.go index 5f2099145a9b4..bab3bcc932541 100644 --- a/internal/services/network/private_endpoint_resource.go +++ b/internal/services/network/private_endpoint_resource.go @@ -11,8 +11,8 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" "github.com/hashicorp/go-azure-helpers/resourcemanager/location" - mariaDB "github.com/hashicorp/go-azure-sdk/resource-manager/mariadb/2018-06-01/servers" - "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2017-12-01/servers" + mariadbServers "github.com/hashicorp/go-azure-sdk/resource-manager/mariadb/2018-06-01/servers" + postgresqlServers "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2017-12-01/servers" "github.com/hashicorp/go-azure-sdk/resource-manager/privatedns/2018-09-01/privatezones" "github.com/hashicorp/go-azure-sdk/resource-manager/redis/2021-06-01/redis" "github.com/hashicorp/go-azure-sdk/resource-manager/signalr/2022-02-01/signalr" @@ -857,34 +857,7 @@ func flattenPrivateLinkEndpointServiceConnection(serviceConnections *[]network.P if strings.HasSuffix(privateConnectionId, ".azure.privatelinkservice") { attrs["private_connection_resource_alias"] = privateConnectionId } else { - // There is a bug from service, the PE created from portal could be with the connection id for postgresql server "Microsoft.DBForPostgreSQL" instead of "Microsoft.DBforPostgreSQL" - // and for Mysql and MariaDB - if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.dbforpostgresql") { - if serverId, err := servers.ParseServerID(privateConnectionId); err == nil { - privateConnectionId = serverId.ID() - } - } - if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.dbformysql") { - if serverId, err := mysqlParse.ServerID(privateConnectionId); err == nil { - privateConnectionId = serverId.ID() - } - } - if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.dbformariadb") { - if serverId, err := mariaDB.ParseServerID(privateConnectionId); err == nil { - privateConnectionId = serverId.ID() - } - } - if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.redis") { - if cacheId, err := redis.ParseRediIDInsensitively(privateConnectionId); err == nil { - privateConnectionId = cacheId.ID() - } - } - if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.signalrservice") { - if serviceId, err := signalr.ParseSignalRIDInsensitively(privateConnectionId); err == nil { - privateConnectionId = serviceId.ID() - } - } - attrs["private_connection_resource_id"] = privateConnectionId + attrs["private_connection_resource_id"] = normalizePrivateConnectionId(privateConnectionId) } results = append(results, attrs) @@ -924,24 +897,7 @@ func flattenPrivateLinkEndpointServiceConnection(serviceConnections *[]network.P if strings.HasSuffix(privateConnectionId, ".azure.privatelinkservice") { attrs["private_connection_resource_alias"] = privateConnectionId } else { - // There is a bug from service, the PE created from portal could be with the connection id for postgresql server "Microsoft.DBForPostgreSQL" instead of "Microsoft.DBforPostgreSQL" - // and for Mysql and MariaDB - if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.dbforpostgresql") { - if serverId, err := servers.ParseServerID(privateConnectionId); err == nil { - privateConnectionId = serverId.ID() - } - } - if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.dbformysql") { - if serverId, err := mysqlParse.ServerID(privateConnectionId); err == nil { - privateConnectionId = serverId.ID() - } - } - if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.dbformariadb") { - if serverId, err := mariaDB.ParseServerID(privateConnectionId); err == nil { - privateConnectionId = serverId.ID() - } - } - attrs["private_connection_resource_id"] = privateConnectionId + attrs["private_connection_resource_id"] = normalizePrivateConnectionId(privateConnectionId) } results = append(results, attrs) @@ -1171,3 +1127,33 @@ func validatePrivateEndpointSettings(d *pluginsdk.ResourceData) error { return nil } + +// normalize the PrivateConnectionId due to the casing change at service side +func normalizePrivateConnectionId(privateConnectionId string) string { + if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.dbforpostgresql") { + if serverId, err := postgresqlServers.ParseServerIDInsensitively(privateConnectionId); err == nil { + privateConnectionId = serverId.ID() + } + } + if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.dbformysql") { + if serverId, err := mysqlParse.ServerIDInsensitively(privateConnectionId); err == nil { + privateConnectionId = serverId.ID() + } + } + if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.dbformariadb") { + if serverId, err := mariadbServers.ParseServerIDInsensitively(privateConnectionId); err == nil { + privateConnectionId = serverId.ID() + } + } + if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.redis") { + if cacheId, err := redis.ParseRediIDInsensitively(privateConnectionId); err == nil { + privateConnectionId = cacheId.ID() + } + } + if strings.Contains(strings.ToLower(privateConnectionId), "microsoft.signalrservice") { + if serviceId, err := signalr.ParseSignalRIDInsensitively(privateConnectionId); err == nil { + privateConnectionId = serviceId.ID() + } + } + return privateConnectionId +}