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: Added inputs variables dereferencing support [CFG-1501] #7

Merged
merged 1 commit into from
Feb 13, 2022

Conversation

YairZ101
Copy link
Contributor

@YairZ101 YairZ101 commented Feb 9, 2022

What this does

Adds supporting reading variable values in Terraform (.tf) files.

This is done to help on our vision to create better TF support for our product and find more issues that might have been ignored so far because they would be coming from variable values.
This PR is to support input variables, with or without default values.

Background context

In the Decision Document, we decided to create a new library in Go where the implementation for this would live.

We already have played a spike on how this could look like.

We plan to build a parser that can deal with (end goal):

  • Variable blocks inside HCL (.tf) files
  • Local blocks inside HCL (.tf) files
  • Variables with default values
  • Variables in the terraform.tfvars default file
  • A definition of input variables can be found here.

As part of this ticket, we will only be adding support for input variables (and defaults).

Additional information

@YairZ101 YairZ101 requested a review from a team as a code owner February 9, 2022 14:22
@YairZ101 YairZ101 requested review from karniwl and ipapast and removed request for karniwl February 9, 2022 14:22
@CLAassistant
Copy link

CLAassistant commented Feb 9, 2022

CLA assistant check
All committers have signed the CLA.

@YairZ101 YairZ101 force-pushed the feat/add-inputs-vars-deref branch 4 times, most recently from 0c0bd46 to 1cd7ce1 Compare February 9, 2022 15:00
@ofekatr ofekatr changed the title feat: Added inputs vars deref support Feat: Added inputs vars deref support [CFG-1501] Feb 9, 2022
@ofekatr ofekatr force-pushed the feat/add-inputs-vars-deref branch 3 times, most recently from 2fe3299 to 0902b59 Compare February 10, 2022 08:51
@ofekatr ofekatr changed the title Feat: Added inputs vars deref support [CFG-1501] Feat: Added inputs variables dereferencing support [CFG-1501] Feb 10, 2022
terraform/constants.go Outdated Show resolved Hide resolved
terraform/variables.go Outdated Show resolved Hide resolved
Copy link
Contributor

@ipapast ipapast left a comment

Choose a reason for hiding this comment

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

I left two small comments, non blocking. I think it looks good in general and it works! I tested this commit with the test.js from Snyk CLI locally as well and the integration works and still returning results for the defaults. 🙌

@YairZ101 YairZ101 force-pushed the feat/add-inputs-vars-deref branch 5 times, most recently from 575c1dd to 5793283 Compare February 10, 2022 16:08
@YairZ101
Copy link
Contributor Author

@ipapast and @teodora-sandu can you please do a quick re-review to the changes I did following your feedback? :)

@YairZ101 YairZ101 force-pushed the feat/add-inputs-vars-deref branch 2 times, most recently from 67ffb7a to 2914910 Compare February 10, 2022 16:11
}

type VariableMap map[string]cty.Value
func extractFromTfFile(file *hcl.File) (VariableMap, hcl.Diagnostics) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Would it be nicer to name this extractVariableBlocks or something like this?

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 think that extractVariableBlocks sounds more abstract but I personally think that extractFromTfFile makes it more explicit what this function does and what it's being used for.

@@ -531,48 +532,115 @@ block "label_one" {
}

func TestExtractVariablesSuccess(t *testing.T) {
type TestInput struct {
fileName string
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Do we need the fileName here? It was defaulting to "test" before and the simpler this test table is the better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We added it to prepare for the next ticket where we will have different logic for .tf and .tfvars files.

Copy link
Contributor

@teodora-sandu teodora-sandu left a comment

Choose a reason for hiding this comment

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

LGTM 🎉 Left some minor nits but up to you if you want to implement them. Great job!

@YairZ101 YairZ101 merged commit d7cd063 into main Feb 13, 2022
@ipapast ipapast deleted the feat/add-inputs-vars-deref branch February 14, 2022 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants