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: support passing terraform.workspace name #225

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion gotfparse/cmd/tfparse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

//export Parse
func Parse(a *C.char, stopHCL C.int, debug C.int, allowDownloads C.int, num_vars_files C.int, vars_files **C.char) (resp C.parseResponse) {
func Parse(a *C.char, stopHCL C.int, debug C.int, allowDownloads C.int, workspaceName *C.char, num_vars_files C.int, vars_files **C.char) (resp C.parseResponse) {
input := C.GoString(a)

options := []converter.TerraformConverterOption{}
Expand All @@ -33,6 +33,8 @@ func Parse(a *C.char, stopHCL C.int, debug C.int, allowDownloads C.int, num_vars
options = append(options, converter.WithAllowDownloads(false))
}

options = append(options, converter.WithWorkspaceName(C.GoString(workspaceName)))

var varFiles []string
for _, v := range unsafe.Slice(vars_files, num_vars_files) {
varFiles = append(varFiles, C.GoString(v))
Expand Down
5 changes: 5 additions & 0 deletions gotfparse/pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ func (t *terraformConverter) SetTFVarsPaths(paths ...string) {
t.parserOptions = append(t.parserOptions, parser.OptionWithTFVarsPaths(paths...))
}

// SetWorkspaceName is a TerraformConverter option that sets the value for the workspace name.
func (t *terraformConverter) SetWorkspaceName(workspace string) {
t.parserOptions = append(t.parserOptions, parser.OptionWithWorkspaceName(workspace))
}

func getModuleName(b *terraform.Block) string {
// This field is unexported, but necessary to generate the path of the
// module. Hopefully aquasecurity/defsec exports this in a future release.
Expand Down
8 changes: 8 additions & 0 deletions gotfparse/pkg/converter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type TerraformConverterOptions interface {
SetStopOnHCLError()
SetAllowDownloads(allowed bool)
SetTFVarsPaths(paths ...string)
SetWorkspaceName(workspace string)
}

type TerraformConverterOption func(t TerraformConverterOptions)
Expand Down Expand Up @@ -38,3 +39,10 @@ func WithTFVarsPaths(paths ...string) TerraformConverterOption {
t.SetTFVarsPaths(paths...)
}
}

// WithWorkspaceName sets the Terraform workspace name.
func WithWorkspaceName(workspace string) TerraformConverterOption {
return func(t TerraformConverterOptions) {
t.SetWorkspaceName(workspace)
}
}
15 changes: 15 additions & 0 deletions tests/terraform/workspace/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
locals {
map = {
default = "DEFAULT"
other = "OTHER"
}
value = local.map[terraform.workspace]
}

output "workspace" {
value = terraform.workspace
}

output "value" {
value = local.value
}
14 changes: 14 additions & 0 deletions tests/test_tfparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,17 @@ def test_funcs(tmp_path):
"modules_list": ["x", "y", "z"],
}
assert len(actual["check_fileset_abs_path"]) > 0


def test_workspace(tmp_path):
mod_path = init_module("workspace", tmp_path)

parsed = load_from_path(mod_path)
value, workspace = parsed["output"]
assert workspace["value"] == "default"
assert value["value"] == "DEFAULT"

parsed = load_from_path(mod_path, workspace_name="other")
value, workspace = parsed["output"]
assert workspace["value"] == "other"
assert value["value"] == "OTHER"
14 changes: 10 additions & 4 deletions tfparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def load_from_path(
stop_on_hcl_error: bool = False,
debug: bool = False,
allow_downloads: bool = False,
workspace_name: str = "default",
vars_paths=None, # list[str]
) -> tp.Dict:
if not isinstance(filePath, (str, Path)):
raise ValueError("filePath must be str or Path, got %s" % type(filePath))

filePath = str(filePath).encode("utf8")

s = ffi.new("char[]", filePath)
path = ffi.new("char[]", str(filePath).encode("utf8"))
workspace = ffi.new("char[]", str(workspace_name).encode("utf8"))

vars_paths = vars_paths or []
num_var_paths = len(vars_paths)
Expand All @@ -44,7 +44,13 @@ def load_from_path(
]

ret = lib.Parse(
s, stop_on_hcl_error, debug, allow_downloads, num_var_paths, c_var_paths
path,
stop_on_hcl_error,
debug,
allow_downloads,
workspace,
num_var_paths,
c_var_paths,
Comment on lines -47 to +53
Copy link
Member

Choose a reason for hiding this comment

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

I find the formatting + variable rename combo to be as valuable as the new param to be honest :)

)

if ret.err != ffi.NULL:
Expand Down
2 changes: 1 addition & 1 deletion tfparse/build_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
char *err;
} parseResponse;
parseResponse Parse(char* a, int stop_on_error, int debug, int allow_downloads, int num_vars_files, char** vars_files);
parseResponse Parse(char* a, int stop_on_error, int debug, int allow_downloads, char* workspace_name, int num_vars_files, char** vars_files);
void free(void *ptr);
""" # noqa
)
Expand Down
Loading