-
Notifications
You must be signed in to change notification settings - Fork 53
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
Extra arguments environment variables #150
Changes from 2 commits
2e19ec3
0990230
3c6d43e
f742498
df0edb3
8311c79
8a85827
6078938
6fea2c0
637cbeb
db31eda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package server | ||
|
||
import ( | ||
"strings" | ||
|
||
version "github.com/hashicorp/go-version" | ||
) | ||
|
||
// populateRuntimeEnvironmentVariables populates the terraform extra vars specified in the project config file | ||
// with atlantis specific environment variables | ||
func populateRuntimeEnvironmentVariables(extraArgs []string, workspaceDir string, tfEnv string, tfVersion *version.Version) []string { | ||
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. This is the function that replaces the arguments for both 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. We've been using object-oriented style functions throughout the codebase so I think we should stick with that for now. That means associating this method with an actual struct. |
||
var extraArgsFinal []string | ||
for _, v := range extraArgs { | ||
if strings.Contains(v, "${ENVIRONMENT}") || strings.Contains(v, "${ATLANTIS_TERRAFORM_VERSION}") || strings.Contains(v, "${WORKSPACE}") { | ||
v = strings.Replace(v, "${ENVIRONMENT}", tfEnv, -1) | ||
v = strings.Replace(v, "${ATLANTIS_TERRAFORM_VERSION}", tfVersion.String(), -1) | ||
v = strings.Replace(v, "${WORKSPACE}", workspaceDir, -1) | ||
} | ||
extraArgsFinal = append(extraArgsFinal, v) | ||
} | ||
return extraArgsFinal | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
version "github.com/hashicorp/go-version" | ||
. "github.com/hootsuite/atlantis/testing_util" | ||
) | ||
|
||
func TestPopulateRuntimeEnvironmentVariables(t *testing.T) { | ||
// test environment variable ${ENVIRONMENT} | ||
extraArgs := []string{"-backend-config=env/${ENVIRONMENT}.tfvars", "-no-color"} | ||
expectedArgs := []string{"-backend-config=env/testing.tfvars", "-no-color"} | ||
tfVersion, _ := version.NewVersion("0.1.1") | ||
args := populateRuntimeEnvironmentVariables(extraArgs, "./workspace", "testing", tfVersion) | ||
Equals(t, expectedArgs, args) | ||
|
||
// test environment variable ${WORKSPACE} | ||
extraArgs = []string{"-from-module=${WORKSPACE}/module", "-no-color"} | ||
expectedArgs = []string{"-from-module=./path/to/workspace/module", "-no-color"} | ||
tfVersion, _ = version.NewVersion("0.1.1") | ||
args = populateRuntimeEnvironmentVariables(extraArgs, "./path/to/workspace", "testing", tfVersion) | ||
Equals(t, expectedArgs, args) | ||
|
||
// test environment variable ${ATLANTIS_TERRAFORM_VERSION} | ||
extraArgs = []string{"-backend-config=env/${ATLANTIS_TERRAFORM_VERSION}/testing.tfvars", "-no-color"} | ||
expectedArgs = []string{"-backend-config=env/0.1.1/testing.tfvars", "-no-color"} | ||
tfVersion, _ = version.NewVersion("0.1.1") | ||
args = populateRuntimeEnvironmentVariables(extraArgs, "./path/to/workspace", "testing", tfVersion) | ||
Equals(t, expectedArgs, args) | ||
|
||
// test all environment variables together | ||
extraArgs = []string{"-backend-config=${WORKSPACE}/env/${ATLANTIS_TERRAFORM_VERSION}/${ENVIRONMENT}.tfvars", "-no-color"} | ||
expectedArgs = []string{"-backend-config=./path/to/workspace/env/0.1.1/testing.tfvars", "-no-color"} | ||
tfVersion, _ = version.NewVersion("0.1.1") | ||
args = populateRuntimeEnvironmentVariables(extraArgs, "./path/to/workspace", "testing", tfVersion) | ||
Equals(t, expectedArgs, args) | ||
} |
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.
Added
utils.go
to be used for functions like these.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 think this doesn't belong in a server util. It is used for the execution so can we not put it in a command runner? Util classes should be reserved for the most generic of functions. Otherwise it just becomes a dumping ground. This function doesn't strike me as generic enough.