Skip to content

Commit

Permalink
Maintain logical name of keyword overlap resources. (#210)
Browse files Browse the repository at this point in the history
Followup to #207 that keeps resource logical names the same to maintain
state.

This also fixes a merged failing test (woops) that didn't run
PULUMI_ACCEPT
  • Loading branch information
brandonpollack23 authored Oct 15, 2024
1 parent fa60cd9 commit 123edef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[
"warning:name_overlap/main.tf:1,1-33:resource renamed to prevent keyword overlap:Renaming resource for to of type simple_resource to for_resource_pcl to prevent overlap"
"warning:name_overlap/main.tf:1,1-33:resource renamed to prevent keyword overlap:Renaming resource for of type simple_resource to for_resource_ to prevent overlap"
]
8 changes: 4 additions & 4 deletions pkg/convert/testdata/programs/name_overlap/pcl/main.pp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
resource "forResourcePcl" "simple:index:resource" {
__logicalName = "for_resource_pcl"
resource "forResource_" "simple:index:resource" {
__logicalName = "for"
inputOne = "hello"
inputTwo = true
}

resource "dependsOnFor" "simple:index:resource" {
options {
dependsOn = [forResourcePcl]
dependsOn = [forResource_]
}
inputOne = forResourcePcl.result
inputOne = forResource_.result
inputTwo = false
}
22 changes: 19 additions & 3 deletions pkg/convert/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ type convertState struct {

type pclOverlapRenames struct {
nameToRename map[string]string
renameToName map[string]string
}

func (s *convertState) disableRewritingObjectKeys(f func()) {
Expand Down Expand Up @@ -807,6 +808,7 @@ func (s *convertState) renamePclOverlap(kind string, hclType *string, name strin
if hclType != nil && isPclKeyword(*hclType) {
*newType = fmt.Sprintf("%s_%sType_", *hclType, kind)
s.typeRenames.nameToRename[name] = *newType
s.typeRenames.renameToName[newName] = name

s.appendDiagnostic(&hcl.Diagnostic{
Subject: hclRange,
Expand All @@ -819,6 +821,7 @@ func (s *convertState) renamePclOverlap(kind string, hclType *string, name strin
if isPclKeyword(name) {
newName = fmt.Sprintf("%s_%s_", name, kind)
s.renames.nameToRename[name] = newName
s.renames.renameToName[newName] = name

s.appendDiagnostic(&hcl.Diagnostic{
Subject: hclRange,
Expand All @@ -831,13 +834,20 @@ func (s *convertState) renamePclOverlap(kind string, hclType *string, name strin
return *newType, newName
}

func (s *convertState) getNameIfRenamed(name string) string {
func (s *convertState) getNewNameIfRenamed(name string) string {
if newName, ok := s.renames.nameToRename[name]; ok {
return newName
}
return name
}

func (s *convertState) getOriginalNameIfRenamed(name string) string {
if newName, ok := s.renames.renameToName[name]; ok {
return newName
}
return name
}

func isPclKeyword(s string) bool {
switch s {
case "for", "if", "else":
Expand Down Expand Up @@ -1390,7 +1400,7 @@ func rewriteTraversal(
} else if maybeFirstAttr != nil {
// This is a lookup of a resource or an attribute lookup on a local variable etc, we need to
// rewrite this traversal such that the root is now the pulumi invoked value instead.
rewriteName := state.getNameIfRenamed(maybeFirstAttr.Name)
rewriteName := state.getNewNameIfRenamed(maybeFirstAttr.Name)

// First see if this is a resource
path := root.Name + "." + rewriteName
Expand Down Expand Up @@ -2300,7 +2310,8 @@ func convertManagedResources(state *convertState,
// If the pulumi name differs from the terraform name we should set __logicalName so that we don't change
// the name of the resource in state.
if pulumiName != managedResource.Name {
blockBody.SetAttributeRaw("__logicalName", hclwrite.TokensForValue(cty.StringVal(managedResource.Name)))
logicalName := state.getOriginalNameIfRenamed(managedResource.Name)
blockBody.SetAttributeRaw("__logicalName", hclwrite.TokensForValue(cty.StringVal(logicalName)))
}

var options *hclwrite.Block
Expand Down Expand Up @@ -2621,6 +2632,11 @@ func translateModuleSourceCode(
rewriteObjectKeys: true,
renames: pclOverlapRenames{
nameToRename: make(map[string]string),
renameToName: make(map[string]string),
},
typeRenames: pclOverlapRenames{
nameToRename: make(map[string]string),
renameToName: make(map[string]string),
},
}

Expand Down

0 comments on commit 123edef

Please sign in to comment.