Skip to content

Commit

Permalink
Move resource name disambiguation into ResourceName
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas11 committed Jan 21, 2025
1 parent 49fbbc4 commit 8fa60df
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 27 deletions.
127 changes: 103 additions & 24 deletions provider/pkg/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,31 +422,72 @@ func ResourceName(operationID, path string) (string, *NameDisambiguation) {

resourceName := name + subName

var nameDisambiuation *NameDisambiguation

// Special cases
return handleResourceNameSpecialCases(resourceName, operationID, path)
}

// Manual override to resolve ambiguity between public and private RecordSet.
// See https://github.com/pulumi/pulumi-azure-native/issues/583.
// To be removed with https://github.com/pulumi/pulumi-azure-native/issues/690.
if resourceName == "RecordSet" && strings.Contains(path, "/providers/Microsoft.Network/privateDnsZones/") {
newName := "PrivateRecordSet"
nameDisambiuation = &NameDisambiguation{
func handleResourceNameSpecialCases(resourceName, operationID, path string) (string, *NameDisambiguation) {
newName := func(newName string) (string, *NameDisambiguation) {
nameDisambiguation := &NameDisambiguation{
OperationID: operationID,
Path: path,
GeneratedName: resourceName,
DisambiguatedName: newName,
}
resourceName = newName
return newName, nameDisambiguation
}

var nameDisambiguation *NameDisambiguation
lowerPath := strings.ToLower(path)

// Microsoft.Cache
if strings.Contains(lowerPath, "/providers/microsoft.cache/redis/") {
resourceName, nameDisambiguation = newName("Redis" + resourceName)
} else if strings.Contains(lowerPath, "/providers/microsoft.cache/redisenterprise/") {
resourceName, nameDisambiguation = newName("RedisEnterprise" + resourceName)
}

// Cognitive Services has global and per-account commitment plans with the same name.
// Microsoft.CognitiveServices has global and per-account commitment plans with the same name.
// The global ones are new, introduced in 2022-12-01, so we rename them.
// TODO,tkappler The global plan still has the description "Cognitive Services account
// commitment plan." - upstream issue?
// The global plan still has the description "Cognitive Services account commitment plan." - upstream issue?
if resourceName == "CommitmentPlan" && strings.Contains(path, "/providers/Microsoft.CognitiveServices/commitmentPlans/") {
newName := "SharedCommitmentPlan"
nameDisambiuation = &NameDisambiguation{
resourceName, nameDisambiguation = newName("SharedCommitmentPlan")
}

// Microsoft.DBforMySQL
if strings.Contains(lowerPath, "/providers/microsoft.dbformysql/servers/") {
resourceName, nameDisambiguation = newName("SingleServer" + resourceName)
}

// Microsoft.DBforPostgreSQL
if strings.Contains(lowerPath, "/providers/microsoft.dbforpostgresql/servers/") {
newResourceName := "SingleServer" + resourceName
if resourceName == "Server" {
newResourceName = "SingleServer"
}
resourceName, nameDisambiguation = newName(newResourceName)
} else if strings.Contains(lowerPath, "/providers/microsoft.dbforpostgresql/servergroupsv2/") {
resourceName, nameDisambiguation = newName("ServerGroup" + resourceName)
}

// Microsoft.DocumentDB
if strings.Contains(lowerPath, "/providers/microsoft.documentdb/mongoclusters/") {
resourceName, nameDisambiguation = newName("MongoCluster" + resourceName)
}

// Microsoft.HDInsight
if strings.Contains(lowerPath, "/providers/microsoft.hdinsight/clusterpools/") {
resourceName, nameDisambiguation = newName("ClusterPool" + resourceName)
}

// Microsoft.HybridContainerService
if strings.Contains(lowerPath, "/providers/microsoft.hybridcontainerservice/provisionedclusterinstances/") {
resourceName, nameDisambiguation = newName("ClusterInstance" + resourceName)
}

// Microsoft.LabServices
if strings.Contains(lowerPath, "/providers/microsoft.labservices/labaccounts/") {
newName := "LabAccount" + resourceName
nameDisambiguation = &NameDisambiguation{
OperationID: operationID,
Path: path,
GeneratedName: resourceName,
Expand All @@ -455,12 +496,49 @@ func ResourceName(operationID, path string) (string, *NameDisambiguation) {
resourceName = newName
}

// Redis and RedisEnterprise are essentially distinct resources sharing the Microsoft.Cache
// namespace. It works out ok because each API version has only one of them, and of the shared
// types only PrivateEndpointConnection clashes.
if resourceName == "PrivateEndpointConnection" && strings.Contains(path, "/providers/Microsoft.Cache/redisEnterprise/") {
newName := "EnterprisePrivateEndpointConnection"
nameDisambiuation = &NameDisambiguation{
// Microsoft.Migrate
if strings.Contains(lowerPath, "/providers/microsoft.migrate/assessmentprojects/") {
newName := "AssessmentProjects" + resourceName
nameDisambiguation = &NameDisambiguation{
OperationID: operationID,
Path: path,
GeneratedName: resourceName,
DisambiguatedName: newName,
}
resourceName = newName
}

// Microsoft.MobileNetwork
if strings.Contains(lowerPath, "/providers/microsoft.mobilenetwork/simgroups/") {
newName := "SimGroup" + resourceName
nameDisambiguation = &NameDisambiguation{
OperationID: operationID,
Path: path,
GeneratedName: resourceName,
DisambiguatedName: newName,
}
resourceName = newName
}

// Microsoft.NetApp
if strings.Contains(lowerPath, "/providers/microsoft.netapp/backupvaults/") {
newName := "BackupVault" + resourceName
nameDisambiguation = &NameDisambiguation{
OperationID: operationID,
Path: path,
GeneratedName: resourceName,
DisambiguatedName: newName,
}
resourceName = newName
}

// Microsoft.Network
// Manual override to resolve ambiguity between public and private RecordSet.
// See https://github.com/pulumi/pulumi-azure-native/issues/583.
// To be removed with https://github.com/pulumi/pulumi-azure-native/issues/690.
if resourceName == "RecordSet" && strings.Contains(path, "/providers/Microsoft.Network/privateDnsZones/") {
newName := "PrivateRecordSet"
nameDisambiguation = &NameDisambiguation{
OperationID: operationID,
Path: path,
GeneratedName: resourceName,
Expand All @@ -469,13 +547,14 @@ func ResourceName(operationID, path string) (string, *NameDisambiguation) {
resourceName = newName
}

// Microsoft.Network
// Both are virtual network links, but the other side of the link is a different resource and
// the links have different properties.
// https://learn.microsoft.com/en-us/azure/dns/dns-private-resolver-overview#virtual-network-links
// https://learn.microsoft.com/en-us/azure/dns/private-dns-virtual-network-links
if resourceName == "VirtualNetworkLink" && strings.Contains(path, "/providers/Microsoft.Network/dnsForwardingRulesets/") {
newName := "PrivateResolverVirtualNetworkLink"
nameDisambiuation = &NameDisambiguation{
nameDisambiguation = &NameDisambiguation{
OperationID: operationID,
Path: path,
GeneratedName: resourceName,
Expand All @@ -492,7 +571,7 @@ func ResourceName(operationID, path string) (string, *NameDisambiguation) {
resourceName == "ApplicationTypeVersion" ||
resourceName == "Service") {
newName := "ManagedCluster" + resourceName
nameDisambiuation = &NameDisambiguation{
nameDisambiguation = &NameDisambiguation{
OperationID: operationID,
Path: path,
GeneratedName: resourceName,
Expand All @@ -501,7 +580,7 @@ func ResourceName(operationID, path string) (string, *NameDisambiguation) {
resourceName = newName
}

return resourceName, nameDisambiuation
return resourceName, nameDisambiguation
}

var referenceNameReplacer = strings.NewReplacer("CreateOrUpdateParameters", "", "Create", "", "Request", "")
Expand Down
29 changes: 26 additions & 3 deletions provider/pkg/resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type resourceNameTestCase struct {
Expand All @@ -14,9 +15,9 @@ type resourceNameTestCase struct {
expected string
}

func TestResourceName(t *testing.T) {
func TestStandardResourceNames(t *testing.T) {
// Empty path means "doesn't matter", not that these resources don't have a path.
testCases2 := []resourceNameTestCase{
testCases := []resourceNameTestCase{
{"GetUserSettings", "", "UserSettings"},
{"Mediaservices_Get", "", "MediaService"},
{"Redis_Get", "", "Redis"},
Expand All @@ -33,6 +34,27 @@ func TestResourceName(t *testing.T) {
{"WebApps_ListApplicationSettings", "", "WebAppApplicationSettings"},
{"Products_GetProducts", "", "Products"},
{"PowerBIResources_ListByResourceName", "", "PowerBIResource"},
}

for _, tc := range testCases {
require.NotEmpty(t, tc.expected) // test invariant
actual, _ := ResourceName(tc.operationID, tc.path)
assert.Equal(t, tc.expected, actual)
}
}

func TestSpecialResourceNames(t *testing.T) {
testCases := []resourceNameTestCase{
{
"Servers_Get",
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{serverName}",
"Server",
},
{
"Servers_Get",
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/servers/{serverName}",
"SingleServer",
},

// An exception for https://github.com/pulumi/pulumi-azure-native/issues/583, disambiguated by path.
{
Expand All @@ -47,7 +69,8 @@ func TestResourceName(t *testing.T) {
},
}

for _, tc := range testCases2 {
for _, tc := range testCases {
require.NotEmpty(t, tc.expected) // test invariant
actual, _ := ResourceName(tc.operationID, tc.path)
assert.Equal(t, tc.expected, actual)
}
Expand Down

0 comments on commit 8fa60df

Please sign in to comment.