pulumi/tf2pulumi

a name or type with / is invalid HCL

Closed this issue · 1 comments

This test case fails

func TestConvertForPulumi(t *testing.T) {
	testCases := []struct {
		rootPath    string
		filePath    string
		fileContent string
		opts        Options
		outmainname string
		outmain string
	}{
		{
			"/",
			"/groups.tf",
			`resource "okta_group" "all" {
				name = "Everyone"
				description = "Everyone"
			}
			data "okta_group" "all" {
				name = "Everyone"
			}
			`,
			Options{
				TargetLanguage:     "python",
				ProviderInfoSource: il.PluginProviderInfoSource,
			},
			"__main__.py",
			"import pulumi\nimport pulumi_okta as okta\n\nall_group = okta.group.get_group(name=\"Everyone\")\nall_group_group_group = okta.group.Group(\"allGroup_groupGroup\",\n    description=\"Everyone\",\n    name=\"Everyone\")\n",
		},
	}
	for i, tc := range testCases[0:] {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			var appFs = afero.NewMemMapFs()
			err := afero.WriteFile(appFs, tc.filePath, []byte(tc.fileContent), 0644)
			if err != nil {
				t.Fatal(err)
			}
			readContent, err := afero.ReadFile(appFs, tc.filePath)
			if err != nil {
				t.Log(err)
			}
			assert.Equal(t, string(readContent), tc.fileContent)
			tc.opts.Root = appFs
			file, diag, err := Convert(tc.opts)
			if err != nil {
				t.Fatal(err)
			}
			if len(diag.All.Errs()) > 0 {
				t.Fatalf("%s", spew.Sdump(diag.All.Errs()))
			}
			assert.Equal(t, tc.outmain, string(file[tc.outmainname]))
		})
	}
}

with this error
groups.tf.pp:4,1-9: Argument or block definition required; An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.

This can be fixed by adding a strings.ReplaceAll like so.

	// Determine the resource's Pulumi package, module, and type name. These will be used during the disambiguation
	// process. If these names cannot be determined, return an ugly name comprised of the TF type and name.
	packageName, moduleName, typeName, diags := hcl2.DecomposeToken(n.token, hcl.Range{})
	if len(diags) != 0 {
		return cleanName(n.typeName + "_" + n.name)
	}
	moduleName = strings.ReplaceAll(moduleName, "/", "_")
func TestDisambiguateResourceName(t *testing.T) {
	testCases := []struct {
		nt           *nameTable
		assigned     bool
		resource     *resource
		expectedName string
	}{
		{
			&nameTable{
				assigned: map[string]bool{
					"resource0":      true,
					"resource0type0": true,
				},
			},
			false,
			&resource{
				name:  "Resource0",
				token: "package:mod/ule:type0",
			},
			"resource0Mod_uletype0",
		},
	}
	for i, tc := range testCases[0:] {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			got := tc.nt.disambiguateResourceName(tc.resource)
			assert.Equal(t, tc.expectedName, got)
			assert.Equal(t, tc.assigned, tc.nt.assigned[tc.expectedName])
		})
	}
}

I suspect the new converter will handle this as we use quoted identifiers for names and types now.