Skip to content
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

feat: dereference variables from terraform.tfvars and *.auto.tfvars [CFG-1502] #12

Merged
merged 1 commit into from
Feb 21, 2022

Conversation

teodora-sandu
Copy link
Contributor

This PR enables support for dereferencing variables defined interraform.tfvars and *.auto.tfvars . It does not aim to support terraform.tfvars.json and *.auto.tfvars.json files, or files provided via the -var and -var-fileflags. Tfvars files are explained here

It keeps in mind variable definition precedences, as per this doc. So default variables can be overridden by terraform.tfvars variables, then by *.auto.tfvars variables with the last file in alphabetical order overriding everything (if already defined).

The ParseModule was modified so that files ending in .tf and .tfvars have variables extracted from them, then the precedence is dealt with when merging variables in mergeVariables by ordering the file names based on priority. Then, we only parse .tf files.

Jira ticket: https://snyksec.atlassian.net/browse/CFG-1502

terraform/utils.go Outdated Show resolved Hide resolved
return false
}

func orderFilesByPriority(fileNames []string) []string {
Copy link
Contributor

@YairZ101 YairZ101 Feb 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing wrong with the current function but I do want to share another idea about how to implement it and discuss it.
It looks like we are going to end up with a bunch of loops and each loop will contain its own unique logic for the specific file type that we are looking for, what if instead of that we will do something like the following:

1. Separately create a new enum that will contain the file type and its priority (e.g. .tf = 1)
2. Sort the files in lexical order
3. Iterate through the files and attach to each one of them its priority based on the file name
4. Sort the files by the priority while keeping the lexical order
5. Return the sorted list

This way when we would like to add support for a new file type we will only need to add its priority to the enum.
WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give it a go and see how readable it is, thanks! 👍

@teodora-sandu teodora-sandu force-pushed the feat/tfvars-values branch 3 times, most recently from c9d0686 to 8070daa Compare February 21, 2022 11:11
Copy link
Contributor

@YairZ101 YairZ101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the feedback I left below you did excellent job! 🎉
Thank you for working it, it's very exciting to see the progress we are making with this 😃

terraform/utils.go Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants