Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove typeName aliases #3911

Draft
wants to merge 1 commit into
base: v3
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 42 additions & 45 deletions provider/pkg/gen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Versioning interface {
ShouldInclude(moduleName openapi.ModuleName, version *openapi.ApiVersion, typeName, token string) bool
GetDeprecation(token string) (ResourceDeprecation, bool)
GetAllVersions(openapi.ModuleName, openapi.ResourceName) []openapi.ApiVersion
GetPreviousCompatibleTokensLookup() (*CompatibleTokensLookup, error)
}

type GenerationResult struct {
Expand All @@ -70,6 +71,7 @@ type GenerationResult struct {
// A map of module -> resource -> set of paths, to record resources that have conflicts where the same resource
// maps to more than one API path.
PathConflicts map[openapi.ModuleName]map[openapi.ResourceName]map[string][]openapi.ApiVersion
TokenPaths map[string]string
}

// PulumiSchema will generate a Pulumi schema for the given Azure modules and resources map.
Expand Down Expand Up @@ -283,6 +285,14 @@ func PulumiSchema(rootDir string, modules openapi.AzureModules, versioning Versi
flattenedPropertyConflicts := map[string]map[string]any{}
exampleMap := make(map[string][]resources.AzureAPIExample)
resourcesPathTracker := newResourcesPathConflictsTracker()
previousCompatibleTokensLookup, err := versioning.GetPreviousCompatibleTokensLookup()
if err != nil {
return nil, err
}

if !previousCompatibleTokensLookup.IsPopulated() && providerVersion.Major >= 3 {
return nil, fmt.Errorf("GetPreviousCompatibleTokensLookup is not populated. This is likely due to v%d-token-paths.json being missing or empty", providerVersion.Major-1)
}

for moduleName, moduleVersions := range util.MapOrdered(modules) {
resourcePaths := map[openapi.ResourceName]map[string][]openapi.ApiVersion{}
Expand All @@ -301,19 +311,20 @@ func PulumiSchema(rootDir string, modules openapi.AzureModules, versioning Versi
}

gen := packageGenerator{
pkg: &pkg,
metadata: &metadata,
moduleName: moduleName,
apiVersion: apiVersion,
sdkVersion: sdkVersion,
allVersions: allVersions,
examples: exampleMap,
versioning: versioning,
caseSensitiveTypes: caseSensitiveTypes,
rootDir: rootDir,
flattenedPropertyConflicts: flattenedPropertyConflicts,
majorVersion: int(providerVersion.Major),
resourcePaths: map[openapi.ResourceName]map[string]openapi.ApiVersion{},
pkg: &pkg,
metadata: &metadata,
moduleName: moduleName,
apiVersion: apiVersion,
sdkVersion: sdkVersion,
allVersions: allVersions,
examples: exampleMap,
versioning: versioning,
caseSensitiveTypes: caseSensitiveTypes,
rootDir: rootDir,
flattenedPropertyConflicts: flattenedPropertyConflicts,
majorVersion: int(providerVersion.Major),
resourcePaths: map[openapi.ResourceName]map[string]openapi.ApiVersion{},
previousCompatibleTokensLookup: previousCompatibleTokensLookup,
}

// Populate C#, Java, Python and Go module mapping.
Expand Down Expand Up @@ -350,10 +361,10 @@ func PulumiSchema(rootDir string, modules openapi.AzureModules, versioning Versi
// When a resource maps to more than one API path, it's a conflict and we need to detect and report it. #2495
isReleaseBuild := len(providerVersion.Build) == 0
if providerVersion.Major >= 3 && isReleaseBuild && resourcesPathTracker.hasConflicts() {
return nil, fmt.Errorf("path conflicts detected. You probably need to add a case to resources.go/ResourceName.\n%+v", resourcesPathTracker.pathConflicts)
return nil, fmt.Errorf("path conflicts detected. You probably need to add a case to schema.go/dedupResourceNameByPath.\n%+v", resourcesPathTracker.pathConflicts)
}

err := genMixins(&pkg, &metadata)
err = genMixins(&pkg, &metadata)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -405,6 +416,10 @@ version using infrastructure as code, which Pulumi then uses to drive the ARM AP
"packages": javaPackages,
})

tokenPaths := map[string]string{}
for token, resource := range metadata.Resources {
tokenPaths[token] = resource.Path
}
return &GenerationResult{
Schema: &pkg,
Metadata: &metadata,
Expand All @@ -413,6 +428,7 @@ version using infrastructure as code, which Pulumi then uses to drive the ARM AP
TypeCaseConflicts: caseSensitiveTypes.findCaseConflicts(),
FlattenedPropertyConflicts: flattenedPropertyConflicts,
PathConflicts: resourcesPathTracker.pathConflicts,
TokenPaths: tokenPaths,
}, nil
}

Expand Down Expand Up @@ -670,7 +686,8 @@ type packageGenerator struct {
majorVersion int
// A resource -> path -> API version map to record API paths per resource and later detect conflicts.
// Each packageGenerator instance is only used for a single API version, so there won't be conflicting paths here.
resourcePaths map[openapi.ResourceName]map[string]openapi.ApiVersion
resourcePaths map[openapi.ResourceName]map[string]openapi.ApiVersion
previousCompatibleTokensLookup *CompatibleTokensLookup
}

func (g *packageGenerator) genResources(typeName string, resource *openapi.ResourceSpec, nestedResourceBodyRefs []string) error {
Expand Down Expand Up @@ -929,7 +946,7 @@ func (g *packageGenerator) genResourceVariant(apiSpec *openapi.ResourceSpec, res
},
InputProperties: resourceRequest.specs,
RequiredInputs: resourceRequest.requiredSpecs.SortedValues(),
Aliases: g.generateAliases(resource, typeNameAliases...),
Aliases: g.generateAliases(resourceTok, resource, typeNameAliases...),
DeprecationMessage: resource.deprecationMessage,
}
g.pkg.Resources[resourceTok] = resourceSpec
Expand Down Expand Up @@ -1037,45 +1054,25 @@ func isSingleton(resource *resourceVariant) bool {
return resource.PathItem.Delete == nil || customresources.IsSingleton(resource.Path)
}

func (g *packageGenerator) generateAliases(resource *resourceVariant, typeNameAliases ...string) []pschema.AliasSpec {
func (g *packageGenerator) generateAliases(resourceTok string, resource *resourceVariant, typeNameAliases ...string) []pschema.AliasSpec {
typeAliases := collections.NewOrderableSet[string]()

// Add an alias for the same version of the resource, but in its old module.
if resource.ModuleNaming.PreviousName != nil {
typeAliases.Add(generateTok(*resource.ModuleNaming.PreviousName, resource.typeName, g.sdkVersion))
}

for _, alias := range typeNameAliases {
typeAliases.Add(generateTok(g.moduleName, alias, g.sdkVersion))
// Add an alias for the same alias, but in its old module.
if resource.ModuleNaming.PreviousName != nil {
typeAliases.Add(generateTok(*resource.ModuleNaming.PreviousName, alias, g.sdkVersion))
}
previousCompatibleTokens := g.previousCompatibleTokensLookup.FindCompatibleTokens(resource.ModuleNaming.ResolvedName, resource.typeName, resource.Path)
for _, token := range previousCompatibleTokens {
typeAliases.Add(token)
}

// Add an alias for each API version that has the same path in it.
for _, version := range resource.CompatibleVersions {
typeAliases.Add(generateTok(g.moduleName, resource.typeName, version.ToSdkVersion()))

// Add an alias for the other versions, but from its old module.
if resource.ModuleNaming.PreviousName != nil {
typeAliases.Add(generateTok(*resource.ModuleNaming.PreviousName, resource.typeName, version.ToSdkVersion()))
}

// Add type name aliases for each compatible version.
for _, alias := range typeNameAliases {
typeAliases.Add(generateTok(g.moduleName, alias, version.ToSdkVersion()))

// Add an alias for the other version, with alias, from its old module.
if resource.ModuleNaming.PreviousName != nil {
typeAliases.Add(generateTok(*resource.ModuleNaming.PreviousName, alias, version.ToSdkVersion()))
}
}
}

var aliasSpecs []pschema.AliasSpec
for _, v := range typeAliases.SortedValues() {
aliasSpecs = append(aliasSpecs, pschema.AliasSpec{Type: &v})
// Skip aliasing to itself.
if v != resourceTok {
aliasSpecs = append(aliasSpecs, pschema.AliasSpec{Type: &v})
}
}
return aliasSpecs
}
Expand Down