Skip to content

Commit

Permalink
Use the type name to help resolve name clashes (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frassle authored Feb 12, 2024
1 parent a83ab78 commit 51d619c
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
### Improvements

- Better conflict resolution for conflicting source names. They'll now use the objects type name to help build a unique name.

### Bug Fixes
13 changes: 13 additions & 0 deletions pkg/convert/testdata/mappings/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
"type": 4,
"computed": true
}
},
"simple_another_resource": {
"input_one": {
"type": 4,
"optional": true
},
"result": {
"type": 4,
"computed": true
}
}
}
},
Expand All @@ -42,6 +52,9 @@
"resources": {
"simple_resource": {
"tok": "simple:index:resource"
},
"simple_another_resource": {
"tok": "simple:index:anotherResource"
}
}
}
4 changes: 4 additions & 0 deletions pkg/convert/testdata/programs/name_conflict/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ data "simple_data_source" "a_thing" {
input_two = local.a_thing
}

resource "simple_another_resource" "a_thing" {
input_one = "Hello ${simple_resource.a_thing.result}"
}

output "a_thing" {
value = data.simple_data_source.a_thing.result
}
9 changes: 7 additions & 2 deletions pkg/convert/testdata/programs/name_conflict/pcl/main.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
inputTwo = myaThing
}

aThingData = invoke("simple:index:dataSource", {
aThingDataSource = invoke("simple:index:dataSource", {
inputOne = "Hello ${aThingResource.result}"
inputTwo = myaThing
})

resource "aThingAnotherResource" "simple:index:anotherResource" {
__logicalName = "a_thing"
inputOne = "Hello ${aThingResource.result}"
}

output "aThing" {
value = aThingData.result
value = aThingDataSource.result
}
13 changes: 13 additions & 0 deletions pkg/convert/testdata/programs/name_conflict_renames/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "renames_resource" "a_thing" {
a_number = 1
a_resource {
inner_string = "hello"
}
}

data "renames_data_source" "a_thing" {
a_number = 2
a_resource {
inner_string = "hello"
}
}
14 changes: 14 additions & 0 deletions pkg/convert/testdata/programs/name_conflict_renames/pcl/main.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resource "aThingResource" "renames:index/index:resource" {
__logicalName = "a_thing"
theNumber = 1
theResource = {
theInnerString = "hello"
}
}

aThing = invoke("renames:index/index:dataSource", {
theNumber = 2
theResource = {
theInnerString = "hello"
}
})
30 changes: 30 additions & 0 deletions pkg/convert/testdata/schemas/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@
"description": "The provider type for the simple package. By default, resources use package-wide configuration\nsettings, however an explicit `Provider` instance may be created and passed during resource\nconstruction to achieve fine-grained programmatic control over provider settings. See the\n[documentation](https://www.pulumi.com/docs/reference/programming-model/#providers) for more information.\n"
},
"resources": {
"simple:index:anotherResource": {
"properties": {
"inputOne": {
"type": "string"
},
"result": {
"type": "string"
}
},
"required": [
"result"
],
"inputProperties": {
"inputOne": {
"type": "string"
}
},
"stateInputs": {
"description": "Input properties used for looking up and filtering anotherResource resources.\n",
"properties": {
"inputOne": {
"type": "string"
},
"result": {
"type": "string"
}
},
"type": "object"
}
},
"simple:index:resource": {
"properties": {
"inputOne": {
Expand Down
26 changes: 20 additions & 6 deletions pkg/convert/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2491,9 +2491,9 @@ func translateModuleSourceCode(
if item.data != nil {
dataResource := item.data
key := "data." + dataResource.Type + "." + dataResource.Name
scopes.getOrAddPulumiName(key, "", "Data")
// Try to grab the info for this data type
provider := impliedProvider(dataResource.Type)
root := PathInfo{}
if provider != "template" {
// We rewrite uses of template because it's really common but the provider for it is
// deprecated. As such we don't want to try and do a mapping lookup for it.
Expand All @@ -2509,19 +2509,25 @@ func translateModuleSourceCode(
}

if providerInfo != nil {
root := scopes.roots[key]
root.Resource = providerInfo.P.DataSourcesMap().Get(dataResource.Type)
root.DataSourceInfo = providerInfo.DataSources[dataResource.Type]
scopes.roots[key] = root
}
}

invokeToken := impliedToken(dataResource.Type)
if root.DataSourceInfo != nil {
invokeToken = root.DataSourceInfo.Tok.String()
}
tokenParts := strings.Split(invokeToken, ":")
suffix := strings.Title(tokenParts[len(tokenParts)-1])
root.Name = scopes.getOrAddPulumiName(key, "", suffix)
scopes.roots[key] = root
}
}
for _, item := range items {
if item.resource != nil {
managedResource := item.resource
key := managedResource.Type + "." + managedResource.Name
scopes.getOrAddPulumiName(key, "", "Resource")
// Try to grab the info for this resource type
provider := impliedProvider(managedResource.Type)
providerInfo, err := info.GetProviderInfo("", "", provider, "")
Expand All @@ -2534,12 +2540,20 @@ func translateModuleSourceCode(
})
}

root := PathInfo{}
if providerInfo != nil {
root := scopes.roots[key]
root.Resource = providerInfo.P.ResourcesMap().Get(managedResource.Type)
root.ResourceInfo = providerInfo.Resources[managedResource.Type]
scopes.roots[key] = root
}

resourceToken := impliedToken(managedResource.Type)
if root.ResourceInfo != nil {
resourceToken = root.ResourceInfo.Tok.String()
}
tokenParts := strings.Split(resourceToken, ":")
suffix := strings.Title(tokenParts[len(tokenParts)-1])
root.Name = scopes.getOrAddPulumiName(key, "", suffix)
scopes.roots[key] = root
}
}
for _, item := range items {
Expand Down

0 comments on commit 51d619c

Please sign in to comment.