-
Notifications
You must be signed in to change notification settings - Fork 0
Add terraform output variables to environment #1104
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| package env | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
@@ -25,6 +26,16 @@ type TerraformEnvPrinter struct { | |
| BaseEnvPrinter | ||
| } | ||
|
|
||
| // TerraformOutput a struct that mimics the terraform output JSON | ||
| type TerraformOutput struct { | ||
| Value interface{} `json:"value"` | ||
| Type interface{} `json:"type"` | ||
| Sensitive bool `json:"sensitive"` | ||
| } | ||
|
|
||
| // TerraformOutputs a map of output names to TerraformOutput | ||
| type TerraformOutputs map[string]TerraformOutput | ||
|
|
||
| // ============================================================================= | ||
| // Constructor | ||
| // ============================================================================= | ||
|
|
@@ -129,6 +140,15 @@ func (e *TerraformEnvPrinter) GetEnvVars() (map[string]string, error) { | |
| envVars["TF_VAR_os_type"] = "unix" | ||
| } | ||
|
|
||
| outputVars, err := e.getTerraformOutputs(projectPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error deriving output vars: %w", err) | ||
| } | ||
|
|
||
| for k, v := range outputVars { | ||
| envVars[k] = v | ||
| } | ||
|
|
||
| return envVars, nil | ||
| } | ||
|
|
||
|
|
@@ -150,6 +170,53 @@ func (e *TerraformEnvPrinter) Print() error { | |
| // Private Methods | ||
| // ============================================================================= | ||
|
|
||
| // getTerraformOutputs retrieves Terraform outputs from the specified project path. | ||
| // It executes 'terraform output -json' and converts the outputs into environment variables. | ||
| // Each output is prefixed with 'TF_VAR_' to make it available as a Terraform variable. | ||
| // Returns a map of environment variable names to their formatted values, or an error if the command fails. | ||
| func (e *TerraformEnvPrinter) getTerraformOutputs(projectPath string) (map[string]string, error) { | ||
| cmd := e.shims.Command("terraform", "-chdir="+projectPath, "output", "-json") | ||
| outputJSON, err := cmd.Output() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to run terraform output: %w", err) | ||
| } | ||
| var outputs TerraformOutputs | ||
| if err := json.Unmarshal(outputJSON, &outputs); err != nil { | ||
|
Contributor
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 would be shimmed for test coverage |
||
| return nil, fmt.Errorf("failed to parse terraform output: %w", err) | ||
| } | ||
|
|
||
| envVars := make(map[string]string) | ||
| for name, output := range outputs { | ||
| envVars["TF_VAR_"+name] = formatTerraformValue(output.Value) | ||
| } | ||
| return envVars, nil | ||
| } | ||
|
|
||
| // formatTerraformValue converts a Terraform output value to a string representation. | ||
| // It handles different value types: | ||
| // - Strings are returned as-is | ||
| // - Numbers and booleans are converted to strings | ||
| // - Lists are joined with commas | ||
| // - Complex types are JSON marshaled | ||
| // Returns a string representation of the value suitable for environment variables. | ||
| func formatTerraformValue(val interface{}) string { | ||
| switch v := val.(type) { | ||
| case string: | ||
| return v | ||
| case float64, int, bool: | ||
| return fmt.Sprintf("%v", v) | ||
| case []interface{}: | ||
| parts := make([]string, len(v)) | ||
| for i, elem := range v { | ||
| parts[i] = fmt.Sprintf("%v", elem) | ||
| } | ||
| return strings.Join(parts, ",") | ||
| default: | ||
| b, _ := json.Marshal(v) | ||
| return string(b) | ||
| } | ||
| } | ||
|
|
||
| // generateBackendOverrideTf creates the backend_override.tf file for the project by determining | ||
| // the backend type and writing the appropriate configuration to the file. | ||
| func (e *TerraformEnvPrinter) generateBackendOverrideTf() error { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
For command/exec specifically, the
shellpackage is used when executing commands. This approach abstracts execution so that in the future, we could execute commands against other types of shells--docker exec, SSH, etc.You can find examples of this elsewhere, but it will end up being something like
shell.ExecSilent("terraform", "-chdir=...")