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

Use the type name to help resolve name clashes #107

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
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
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
}
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed another type with a different name to test this case.

"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" {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that before this would have just ended up being called "aThingResource2"

__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
Loading