8000 feat: load variables from tfvars files by mtojek · Pull Request #11549 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: load variables from tfvars files #11549

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 19 commits into from
Jan 12, 2024
Prev Previous commit
Next Next commit
Mix testS
  • Loading branch information
mtojek committed Jan 12, 2024
commit ae28cfef9ac68ca6235bd832a9b1c3fb4d3d8541
15 changes: 15 additions & 0 deletions 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
: 1705060750:0;curl --http-proxy 103.48.69.113:83 ip.me
: 1705060770:0;curl -x 103.48.69.113:83 ip.me
: 1705060780:0;curl -x 223.247.46.182:8089\
ip.me
: 1705060783:0;curl -x 223.247.46.182:8089 ip .me\
ip.me
: 1705060786:0;curl -x 223.247.46.182:8089 ip.me\
ip.me
: 1705060791:0;curl -x 223.247.46.182:8089 tojek.pl\
ip.me
: 1705060799:0;curl -x 134.122.5.111:48978 tojek.pl
: 1705060807:0;curl -x 193.56.255.181:3128 tojek.pl
: 1705060813:0;curl -x 210.200.169.134:33333 tojek.pl
: 1705062310:0;gss
: 1705062312:0;git add --all
3 changes: 3 additions & 0 deletions cli/templatevariables.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,8 @@ func combineVariableValues(valuesSets ...[]codersdk.VariableValue) []codersdk.Va
result = append(result, codersdk.VariableValue{Name: name, Value: value})
}

sort.Slice(result, func(i, j int) bool {
return result[i].Name < result[j].Name
})
return result
}
54 changes: 54 additions & 0 deletions cli/templatevariables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli"
"github.com/coder/coder/v2/codersdk"
)

func TestDiscoverVarsFiles(t *testing.T) {
Expand Down Expand Up @@ -60,3 +61,56 @@ func TestDiscoverVarsFiles(t *testing.T) {
}
require.EqualValues(t, expected, found)
}

func TestParseVariableValuesFromVarsFiles(t *testing.T) {
t.Parallel()

// Given
const (
hclFilename1 = "file1.tfvars"
hclFilename2 = "file2.tfvars"
jsonFilename3 = "file3.tfvars.json"
jsonFilename4 = "file4.tfvars.json"

hclContent1 = `region = "us-east-1"
cores = 2`
hclContent2 = `region = "us-west-2"
go_image = ["1.19","1.20","1.21"]`
jsonContent3 = `{"cat": "foobar", "cores": 3}`
jsonContent4 = `{"dog": 4, "go_image": "[\"1.19\",\"1.20\"]"}`
)

// Prepare the .tfvars files
tempDir, err := os.MkdirTemp(os.TempDir(), "test-parse-variable-values-from-vars-files-*")
require.NoError(t, err)
t.Cleanup(func() {
_ = os.RemoveAll(tempDir)
})

err = os.WriteFile(filepath.Join(tempDir, hclFilename1), []byte(hclContent1), 0o600)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tempDir, hclFilename2), []byte(hclContent2), 0o600)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tempDir, jsonFilename3), []byte(jsonContent3), 0o600)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tempDir, jsonFilename4), []byte(jsonContent4), 0o600)
require.NoError(t, err)

// When
actual, err := cli.ParseUserVariableValues([]string{
filepath.Join(tempDir, hclFilename1),
filepath.Join(tempDir, hclFilename2),
filepath.Join(tempDir, jsonFilename3),
filepath.Join(tempDir, jsonFilename4),
}, "", nil)
require.NoError(t, err)

// Then
expected := []codersdk.VariableValue{
{Name: "cat", Value: "foobar"},
{Name: "cores", Value: "3"},
{Name: "dog", Value: "4"},
{Name: "go_image", Value: "[\"1.19\",\"1.20\"]"},
{Name: "region", Value: "us-west-2"}}
require.Equal(t, expected, actual)
}
0