Skip to content

Commit

Permalink
azurerm_private_endpoint - read private endpoint connection insensi…
Browse files Browse the repository at this point in the history
…tively
  • Loading branch information
myc2h6o committed Feb 14, 2023
1 parent c4f783f commit dc2bc4f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 48 deletions.
44 changes: 44 additions & 0 deletions internal/services/mysql/parse/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
82 changes: 34 additions & 48 deletions internal/services/network/private_endpoint_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit dc2bc4f

Please sign in to comment.