Skip to content

feat(provisioner): add support for .tf.json templates #7835

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

Merged
merged 3 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(provisionersdk,provisioner): add support for .tf.json templates
  • Loading branch information
TECHNOFAB11 committed Jun 4, 2023
commit 90d7e1b354c44fea34766b544df6230a928dfdc5
8 changes: 6 additions & 2 deletions provisioner/terraform/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ func rawRichParameterNames(workdir string) ([]string, error) {

var coderParameterNames []string
for _, entry := range entries {
if !strings.HasSuffix(entry.Name(), ".tf") {
if !strings.HasSuffix(entry.Name(), ".tf") || !strings.HasSuffix(entry.Name(), ".tf.json") {
continue
}

hclFilepath := path.Join(workdir, entry.Name())
parser := hclparse.NewParser()
parsedHCL, diags := parser.ParseHCLFile(hclFilepath)
if strings.HasSuffix(entry.Name(), ".tf.json") {
parsedHCL, diags := parser.ParseJSONFile(hclFilepath)
} else {
parsedHCL, diags := parser.ParseHCLFile(hclFilepath)
}
if diags.HasErrors() {
return nil, hcl.Diagnostics{
{
Expand Down
8 changes: 6 additions & 2 deletions provisioner/terraform/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func loadEnabledFeatures(moduleDir string) (map[string]bool, hcl.Diagnostics) {

var found bool
for _, entry := range entries {
if !strings.HasSuffix(entry.Name(), ".tf") {
if !strings.HasSuffix(entry.Name(), ".tf") || !strings.HasSuffix(entry.Name(), ".tf.json") {
continue
}

Expand Down Expand Up @@ -131,7 +131,11 @@ func parseFeatures(hclFilepath string) (map[string]bool, bool, hcl.Diagnostics)
}

parser := hclparse.NewParser()
parsedHCL, diags := parser.ParseHCLFile(hclFilepath)
if strings.HasSuffix(hclFilepath, ".tf.json") {
parsedHCL, diags := parser.ParseJSONFile(hclFilepath)
} else {
parsedHCL, diags := parser.ParseHCLFile(hclFilepath)
}
if diags.HasErrors() {
return flags, false, diags
}
Expand Down
14 changes: 8 additions & 6 deletions provisionersdk/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ const (
TemplateArchiveLimit = 1 << 20
)

func dirHasExt(dir string, ext string) (bool, error) {
func dirHasExt(dir string, exts ...string) (bool, error) {
dirEnts, err := os.ReadDir(dir)
if err != nil {
return false, err
}

for _, fi := range dirEnts {
if strings.HasSuffix(fi.Name(), ext) {
return true, nil
for _, ext := range exts {
if strings.HasSuffix(fi.Name(), ext) {
return true, nil
}
}
}

Expand All @@ -35,8 +37,8 @@ func Tar(w io.Writer, directory string, limit int64) error {
tarWriter := tar.NewWriter(w)
totalSize := int64(0)

const tfExt = ".tf"
hasTf, err := dirHasExt(directory, tfExt)
tfExts := []string{".tf", ".tf.json"}
hasTf, err := dirHasExt(directory, tfExts...)
if err != nil {
return err
}
Expand All @@ -50,7 +52,7 @@ func Tar(w io.Writer, directory string, limit int64) error {
// useless.
return xerrors.Errorf(
"%s is not a valid template since it has no %s files",
absPath, tfExt,
absPath, tfExts,
)
}

Expand Down
9 changes: 9 additions & 0 deletions provisionersdk/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func TestTar(t *testing.T) {
err = provisionersdk.Tar(io.Discard, dir, 1024)
require.NoError(t, err)
})
t.Run("ValidJSON", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
file, err := os.CreateTemp(dir, "*.tf.json")
require.NoError(t, err)
_ = file.Close()
err = provisionersdk.Tar(io.Discard, dir, 1024)
require.NoError(t, err)
})
t.Run("HiddenFiles", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
Expand Down