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

config: Add getenv interpolation function #14166

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions config/interpolate_funcs.go
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"math"
"net"
"os"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -70,6 +71,7 @@ func Funcs() map[string]ast.Function {
"distinct": interpolationFuncDistinct(),
"element": interpolationFuncElement(),
"file": interpolationFuncFile(),
"getenv": interpolationFuncGetenv(),
"matchkeys": interpolationFuncMatchKeys(),
"floor": interpolationFuncFloor(),
"format": interpolationFuncFormat(),
Expand Down Expand Up @@ -1344,3 +1346,17 @@ func interpolationFuncSubstr() ast.Function {
},
}
}

// interpolationFuncGetenv implements the "getenv" function, which literally
// passes off to os.Getenv in the Go standard library. This function returns
// the value of the requested environment variable, or an empty string if it
// does not exist.
func interpolationFuncGetenv() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
return os.Getenv(args[0].(string)), nil
},
}
}
32 changes: 32 additions & 0 deletions config/interpolate_funcs_test.go
Expand Up @@ -2317,3 +2317,35 @@ func TestInterpolateFuncSubstr(t *testing.T) {
},
})
}

func TestInterpolateFuncGetenv(t *testing.T) {
os.Setenv("TF_INTERP_FUNC_GETENV_TEST", "foobar")
testFunction(t, testFunctionConfig{
Vars: map[string]ast.Variable{
"var.getenv_test": interfaceToVariableSwallowError("TF_INTERP_FUNC_GETENV_TEST"),
"var.getenv_test_missing": interfaceToVariableSwallowError("TF_INTERP_FUNC_GETENV_TEST_MISSING"),
},
Cases: []testFunctionCase{
{
`${getenv("TF_INTERP_FUNC_GETENV_TEST")}`,
"foobar",
false,
},
{
`${getenv("TF_INTERP_FUNC_GETENV_TEST_MISSING")}`,
"",
false,
},
{
`${getenv(var.getenv_test)}`,
"foobar",
false,
},
{
`${getenv(var.getenv_test_missing)}`,
"",
false,
},
},
})
}
Expand Up @@ -99,3 +99,11 @@ tests, but by setting this variable you can force these tests to be skipped.
export TF_SKIP_REMOTE_TESTS=1
make test
```

## The `getenv` Interpolation Function

The [`getenv`](/docs/configuration/interpolation.html#getenv-key-) interpolation
function can be used to get the value of any environment variable available to
the Terraform process. Note that this function is subject to the same rules as
any other interpolation function and as such will not work within `variable`
blocks - use `TF_VAR_` variables to assign values instead.
8 changes: 8 additions & 0 deletions website/source/docs/configuration/interpolation.html.md
Expand Up @@ -246,6 +246,14 @@ The supported built-in functions are:
`formatlist("instance %v has private ip %v", aws_instance.foo.*.id, aws_instance.foo.*.private_ip)`.
Passing lists with different lengths to formatlist results in an error.

* `getenv(key)` - Gets the value for the environment variable defined by
`key`. Returns an empty string if the environment variable does not exist.
Note that this functionality differs from the
[`TF_VAR_`](/docs/configuration/environment-variables.html#tf_var_name) pattern
in that it does not assign values to
[variables](/docs/configuration/variables.html), and allows you to access any
environment variable available to the Terraform process.

* `index(list, elem)` - Finds the index of a given element in a list.
This function only works on flat lists.
Example: `index(aws_instance.foo.*.tags.Name, "foo-test")`
Expand Down