Skip to content

Commit

Permalink
Fix JSON output variable and provider file (GoogleCloudPlatform#910)
Browse files Browse the repository at this point in the history
* fix json output format

* lint
  • Loading branch information
skarimo authored May 4, 2021
1 parent e31a0f6 commit c55e014
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ func printService(provider terraformutils.ProviderGenerator, serviceName string,
}
variables["data"]["terraform_remote_state"][k] = map[string]interface{}{
"backend": "local",
"config": [1]interface{}{map[string]interface{}{
"config": map[string]interface{}{
"path": strings.Repeat("../", strings.Count(path, "/")) + strings.ReplaceAll(path, serviceName, k) + "terraform.tfstate",
}},
},
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions terraformutils/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,18 @@ func terraform12Adjustments(formatted []byte, mapsObjects map[string]struct{}) [

func terraform13Adjustments(formatted []byte) []byte {
s := string(formatted)
requiredProvidersRe := regexp.MustCompile("required_providers \".*\" {")
oldRequiredProviders := "\"required_providers\""
newRequiredProviders := "required_providers"
lines := strings.Split(s, "\n")
providerRequirementDefinition := false
for i, line := range lines {
if providerRequirementDefinition {
line = strings.ReplaceAll(line, " {", " = {")
if requiredProvidersRe.MatchString(line) {
parts := strings.Split(strings.TrimSpace(line), " ")
provider := strings.ReplaceAll(parts[1], "\"", "")
lines[i] = "\t" + newRequiredProviders + " {"
lines[i+1] = "\t\t" + provider + " = {\n\t" + lines[i+1] + "\n\t\t}"
}
providerRequirementDefinition = strings.Contains(line, newRequiredProviders)
lines[i] = strings.Replace(line, oldRequiredProviders, newRequiredProviders, 1)
lines[i] = strings.Replace(lines[i], oldRequiredProviders, newRequiredProviders, 1)
}
s = strings.Join(lines, "\n")
return []byte(s)
Expand Down
4 changes: 2 additions & 2 deletions terraformutils/terraformoutput/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func OutputHclFiles(resources []terraformutils.Resource, provider terraformutils
providerData := provider.GetProviderData()
providerData["terraform"] = map[string]interface{}{
"required_providers": []map[string]interface{}{{
provider.GetName(): []map[string]interface{}{{
provider.GetName(): map[string]interface{}{
"version": providerwrapper.GetProviderVersion(provider.GetName()),
}},
},
}},
}

Expand Down

0 comments on commit c55e014

Please sign in to comment.