-
Notifications
You must be signed in to change notification settings - Fork 881
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
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3f4994d
Discover tfvars files
mtojek 9076dd6
Consider stdin
mtojek b245d5e
linter
mtojek 6097029
select parser
mtojek 56ec679
Parse HCL
mtojek 667ffd6
Fix nil assignment
mtojek ac6bc4e
Parse HCL format
mtojek 9ad5003
polishing
mtojek c19242b
First test
mtojek ec65ea3
fix: lint
mtojek 2c9e7ba
WIP
mtojek 029b2df
WIP
mtojek ae28cfe
Mix testS
mtojek e537684
Fix
mtojek 6acbbee
fix
mtojek 85c0a76
fmt
mtojek 710f025
Comment on tfvars
mtojek cdf2ce6
Negative tests
mtojek 0494c7d
Fix: pebkac
mtojek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,65 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/hashicorp/hcl/v2/hclparse" | ||
"github.com/zclconf/go-cty/cty" | ||
|
||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
||
func ParseUserVariableValues(variablesFile string, commandLineVariables []string) ([]codersdk.VariableValue, error) { | ||
/** | ||
* DiscoverVarsFiles function loads vars files in a predefined order: | ||
* 1. terraform.tfvars | ||
* 2. terraform.tfvars.json | ||
* 3. *.auto.tfvars | ||
* 4. *.auto.tfvars.json | ||
*/ | ||
func DiscoverVarsFiles(workDir string) ([]string, error) { | ||
var found []string | ||
|
||
fi, err := os.Stat(filepath.Join(workDir, "terraform.tfvars")) | ||
if err == nil { | ||
found = append(found, filepath.Join(workDir, fi.Name())) | ||
} else if !os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
|
||
fi, err = os.Stat(filepath.Join(workDir, "terraform.tfvars.json")) | ||
if err == nil { | ||
found = append(found, filepath.Join(workDir, fi.Name())) | ||
} else if !os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
|
||
dirEntries, err := os.ReadDir(workDir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, dirEntry := range dirEntries { | ||
if strings.HasSuffix(dirEntry.Name(), ".auto.tfvars") || strings.HasSuffix(dirEntry.Name(), ".auto.tfvars.json") { | ||
found = append(found, filepath.Join(workDir, dirEntry.Name())) | ||
} | ||
} | ||
return found, nil | ||
} | ||
|
||
func ParseUserVariableValues(varsFiles []string, variablesFile string, commandLineVariables []string) ([]codersdk.VariableValue, error) { | ||
fromVars, err := parseVariableValuesFromVarsFiles(varsFiles) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fromFile, err := parseVariableValuesFromFile(variablesFile) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -21,7 +70,131 @@ func ParseUserVariableValues(variablesFile string, commandLineVariables []string | |
return nil, err | ||
} | ||
|
||
return combineVariableValues(fromFile, fromCommandLine), nil | ||
return combineVariableValues(fromVars, fromFile, fromCommandLine), nil | ||
} | ||
|
||
func parseVariableValuesFromVarsFiles(varsFiles []string) ([]codersdk.VariableValue, error) { | ||
var parsed []codersdk.VariableValue | ||
for _, varsFile := range varsFiles { | ||
content, err := os.ReadFile(varsFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var t []codersdk.VariableValue | ||
ext := filepath.Ext(varsFile) | ||
switch ext { | ||
case ".tfvars": | ||
t, err = parseVariableValuesFromHCL(content) | ||
if err != nil { | ||
return nil, xerrors.Errorf("unable to parse HCL content: %w", err) | ||
} | ||
case ".json": | ||
t, err = parseVariableValuesFromJSON(content) | ||
if err != nil { | ||
return nil, xerrors.Errorf("unable to parse JSON content: %w", err) | ||
} | ||
default: | ||
return nil, xerrors.Errorf("unexpected tfvars format: %s", ext) | ||
} | ||
|
||
parsed = append(parsed, t...) | ||
} | ||
return parsed, nil | ||
} | ||
|
||
func parseVariableValuesFromHCL(content []byte) ([]codersdk.VariableValue, error) { | ||
parser := hclparse.NewParser() | ||
hclFile, diags := parser.ParseHCL(content, "file.hcl") | ||
if diags.HasErrors() { | ||
return nil, diags | ||
} | ||
|
||
attrs, diags := hclFile.Body.JustAttributes() | ||
if diags.HasErrors() { | ||
return nil, diags | ||
} | ||
|
||
stringData := map[string]string{} | ||
for _, attribute := range attrs { | ||
ctyValue, diags := attribute.Expr.Value(nil) | ||
if diags.HasErrors() { | ||
return nil, diags | ||
} | ||
|
||
ctyType := ctyValue.Type() | ||
if ctyType.Equals(cty.String) { | ||
stringData[attribute.Name] = ctyValue.AsString() | ||
} else if ctyType.Equals(cty.Number) { | ||
stringData[attribute.Name] = ctyValue.AsBigFloat().String() | ||
} else if ctyType.IsTupleType() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I immediately saw this, thought 🤔 "y no switch", and then saw how annoying this is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, unfortunately the API isn't too friendly :) |
||
// In case of tuples, Coder only supports the list(string) type. | ||
var items []string | ||
var err error | ||
_ = ctyValue.ForEachElement(func(key, val cty.Value) (stop bool) { | ||
if !val.Type().Equals(cty.String) { | ||
err = xerrors.Errorf("unsupported tuple item type: %s ", val.GoString()) | ||
return true | ||
} | ||
items = append(items, val.AsString()) | ||
return false | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
m, err := json.Marshal(items) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stringData[attribute.Name] = string(m) | ||
} else { | ||
return nil, xerrors.Errorf("unsupported value type (name: %s): %s", attribute.Name, ctyType.GoString()) | ||
} | ||
} | ||
|
||
return convertMapIntoVariableValues(stringData), nil | ||
} | ||
|
||
// parseVariableValuesFromJSON converts the .tfvars.json content into template variables. | ||
// The function visits only root-level properties as template variables do not support nested | ||
// structures. | ||
func parseVariableValuesFromJSON(content []byte) ([]codersdk.VariableValue, error) { | ||
var data map[string]interface{} | ||
err := json.Unmarshal(content, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stringData := map[string]string{} | ||
for key, value := range data { | ||
switch value.(type) { | ||
case string, int, bool: | ||
stringData[key] = fmt.Sprintf("%v", value) | ||
default: | ||
m, err := json.Marshal(value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stringData[key] = string(m) | ||
} | ||
} | ||
|
||
return convertMapIntoVariableValues(stringData), nil | ||
} | ||
|
||
func convertMapIntoVariableValues(m map[string]string) []codersdk.VariableValue { | ||
var parsed []codersdk.VariableValue | ||
for key, value := range m { | ||
parsed = append(parsed, codersdk.VariableValue{ | ||
Name: key, | ||
Value: value, | ||
}) | ||
} | ||
sort.Slice(parsed, func(i, j int) bool { | ||
return parsed[i].Name < parsed[j].Name | ||
}) | ||
return parsed | ||
} | ||
|
||
func parseVariableValuesFromFile(variablesFile string) ([]codersdk.VariableValue, error) { | ||
|
@@ -94,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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could imagine a case where someone had tfvars files lying around unused before, worked around the issue, and left them there. Now we're going to auto-discover them. I'm not sure what this will break.
Should at the very least add an info message about the auto-discovered vars files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I changed the code to print a message alerting about the presence of tfvars 👍