Currently, we are using the convert package to convert JSON to HCL, which works well for modules and inputs but generates problematic HCL code for providers and outputs. The issue is similar to the problem discussed in a blog about handling arrays and maps in HCL.
The convert package generates incorrect HCL code where terraform "required_providers" and terraform "backend" are mixed up and not properly structured.
My Code
package terraform
import (
v1 "saas/pkg/gen/cops/v1"
"github.com/genelet/determined/convert"
)
// ParseTerraform formats and beautifies the given Terraform content.
func ParseTerraform(content string, file, key string, cloud *v1.Cloud) ([]byte, error) {
// Convert JSON content to HCL if it's neither "terraform.tf" nor "output.tf"
hcl, err := convert.JSONToHCL([]byte(content))
if err != nil {
return []byte(""), err
}
return hcl, nil
}
Currently, we are using the convert package to convert JSON to HCL, which works well for modules and inputs but generates problematic HCL code for providers and outputs. The issue is similar to the problem discussed in a blog about handling arrays and maps in HCL.
Examples of the Problem
Example 1: Providers in provider.tf
Generated HCL Output (Incorrect):
The convert package generates incorrect HCL code where terraform "required_providers" and terraform "backend" are mixed up and not properly structured.
My Code