diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 5ecddce..3d559cf 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -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 diff --git a/pkg/convert/testdata/mappings/simple.json b/pkg/convert/testdata/mappings/simple.json index caec20a..8d37c74 100644 --- a/pkg/convert/testdata/mappings/simple.json +++ b/pkg/convert/testdata/mappings/simple.json @@ -31,6 +31,16 @@ "type": 4, "computed": true } + }, + "simple_another_resource": { + "input_one": { + "type": 4, + "optional": true + }, + "result": { + "type": 4, + "computed": true + } } } }, @@ -42,6 +52,9 @@ "resources": { "simple_resource": { "tok": "simple:index:resource" + }, + "simple_another_resource": { + "tok": "simple:index:anotherResource" } } } \ No newline at end of file diff --git a/pkg/convert/testdata/programs/name_conflict/main.tf b/pkg/convert/testdata/programs/name_conflict/main.tf index 6446123..e7005b9 100644 --- a/pkg/convert/testdata/programs/name_conflict/main.tf +++ b/pkg/convert/testdata/programs/name_conflict/main.tf @@ -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 } diff --git a/pkg/convert/testdata/programs/name_conflict/pcl/main.pp b/pkg/convert/testdata/programs/name_conflict/pcl/main.pp index 5640bc2..614202f 100644 --- a/pkg/convert/testdata/programs/name_conflict/pcl/main.pp +++ b/pkg/convert/testdata/programs/name_conflict/pcl/main.pp @@ -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 } diff --git a/pkg/convert/testdata/programs/name_conflict_renames/main.tf b/pkg/convert/testdata/programs/name_conflict_renames/main.tf new file mode 100644 index 0000000..c4efe7d --- /dev/null +++ b/pkg/convert/testdata/programs/name_conflict_renames/main.tf @@ -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" + } +} \ No newline at end of file diff --git a/pkg/convert/testdata/programs/name_conflict_renames/pcl/main.pp b/pkg/convert/testdata/programs/name_conflict_renames/pcl/main.pp new file mode 100644 index 0000000..c9b7169 --- /dev/null +++ b/pkg/convert/testdata/programs/name_conflict_renames/pcl/main.pp @@ -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" + } +}) diff --git a/pkg/convert/testdata/schemas/simple.json b/pkg/convert/testdata/schemas/simple.json index b6efaf9..e2d0b9a 100644 --- a/pkg/convert/testdata/schemas/simple.json +++ b/pkg/convert/testdata/schemas/simple.json @@ -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": { diff --git a/pkg/convert/tf.go b/pkg/convert/tf.go index c2a9eb8..46ad8e0 100644 --- a/pkg/convert/tf.go +++ b/pkg/convert/tf.go @@ -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. @@ -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, "") @@ -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 {