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

Update YAML function !terraform.output #1048

Merged
merged 8 commits into from
Feb 11, 2025
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
2 changes: 1 addition & 1 deletion examples/quick-start-advanced/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG GEODESIC_OS=debian
# https://atmos.tools/
# https://github.com/cloudposse/atmos
# https://github.com/cloudposse/atmos/releases
ARG ATMOS_VERSION=1.160.0
ARG ATMOS_VERSION=1.160.4

# Terraform: https://github.com/hashicorp/terraform/releases
ARG TF_VERSION=1.5.7
Expand Down
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions internal/exec/terraform_outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,15 @@ func execTerraformOutput(
if ok {
envMap, ok2 := envSection.(map[string]any)
if ok2 && len(envMap) > 0 {
l.Debug("Setting environment variables", "env", envMap)
m := u.MapOfInterfacesToMapOfStrings(envMap)
err = tf.SetEnv(m)
l.Debug("Setting environment variables from the component's 'env' section", "env", envMap)
// Get all environment variables from the parent process
environMap := u.EnvironToMap()
// Add/override the environment variables from the component's 'env' section
for k, v := range envMap {
environMap[k] = fmt.Sprintf("%v", v)
}
// Set the environment variables in the process that executes the `tfexec` functions
err = tf.SetEnv(environMap)
if err != nil {
return nil, err
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/utils/env_utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package utils

import "fmt"
import (
"fmt"
"os"
)

// ConvertEnvVars convert ENV vars from a map to a list of strings in the format ["key1=val1", "key2=val2", "key3=val3" ...]
// ConvertEnvVars converts ENV vars from a map to a list of strings in the format ["key1=val1", "key2=val2", "key3=val3" ...]
func ConvertEnvVars(envVarsMap map[string]any) []string {
res := []string{}

Expand All @@ -13,3 +16,15 @@ func ConvertEnvVars(envVarsMap map[string]any) []string {
}
return res
}

// EnvironToMap converts all the environment variables in the environment into a map of strings
func EnvironToMap() map[string]string {
envMap := make(map[string]string)
for _, env := range os.Environ() {
pair := SplitStringAtFirstOccurrence(env, "=")
k := pair[0]
v := pair[1]
envMap[k] = v
}
return envMap
}
9 changes: 9 additions & 0 deletions pkg/utils/string_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ func SplitStringByDelimiter(str string, delimiter rune) ([]string, error) {

return parts, nil
}

// SplitStringAtFirstOccurrence splits a string into two parts at the first occurrence of the separator
func SplitStringAtFirstOccurrence(s string, sep string) [2]string {
parts := strings.SplitN(s, sep, 2)
if len(parts) == 1 {
return [2]string{parts[0], ""}
}
return [2]string{parts[0], parts[1]}
}
3 changes: 1 addition & 2 deletions tests/cli_terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func TestCLITerraformClean(t *testing.T) {

// Verify if state files have been deleted after clean
verifyStateFilesDeleted(t, stateFiles)

}

// runTerraformApply runs the terraform apply command for a given environment.
Expand Down Expand Up @@ -148,6 +147,7 @@ func runCLITerraformCleanComponent(t *testing.T, binaryPath, environment string)
t.Fatalf("Failed to run terraform clean: %v", stderr.String())
}
}

func runCLITerraformClean(t *testing.T, binaryPath string) {
cmd := exec.Command(binaryPath, "terraform", "clean")
var stdout, stderr bytes.Buffer
Expand All @@ -158,7 +158,6 @@ func runCLITerraformClean(t *testing.T, binaryPath string) {
if err != nil {
t.Fatalf("Failed to run terraform clean: %v", stderr.String())
}

}

func runTerraformCleanCommand(t *testing.T, binaryPath string, args ...string) {
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/components/terraform/mock/main.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
variable "foo" {
type = string
type = string
default = "foo"
}

variable "bar" {
type = string
type = string
default = "bar"
}

variable "baz" {
type = string
type = string
default = "baz"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ components:
foo: !terraform.output component-1 foo
bar: !terraform.output component-2 bar
baz: !terraform.output component-3 baz

component-5:
metadata:
component: mock
vars:
foo: !env ATMOS_TEST_1
component-6:
metadata:
component: mock
vars:
foo: !terraform.output component-5 foo
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ workflows:
- command: terraform deploy component-2 -s nonprod
- command: terraform deploy component-3 -s nonprod
- command: terraform deploy component-4 -s nonprod
- command: terraform deploy component-5 -s nonprod
- command: terraform deploy component-6 -s nonprod
36 changes: 36 additions & 0 deletions tests/test-cases/atmos-functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,39 @@ tests:
- "Fetching baz output from component-3 in nonprod"
- "Fetching foo output from component-1 in nonprod"
- "Fetching bar output from component-2 in nonprod"

- name: "!env function test"
enabled: true
tty: false
description: "Ensure the !env function works."
workdir: "fixtures/scenarios/atmos-functions/"
command: "atmos"
args:
- "terraform"
- "deploy"
- "component-5"
- "-s"
- "nonprod"
env:
ATMOS_TEST_1: "test-env-and-terraform-output-functions"
expect:
exit_code: 0
stdout:
- "test-env-and-terraform-output-functions"

- name: "!terraform.output from component with !env function test"
enabled: true
tty: false
description: "Ensure !terraform.output from component with !env function works."
workdir: "fixtures/scenarios/atmos-functions/"
command: "atmos"
args:
- "describe"
- "component"
- "component-6"
- "-s"
- "nonprod"
expect:
exit_code: 0
stdout:
- "test-env-and-terraform-output-functions"
2 changes: 1 addition & 1 deletion website/docs/integrations/atlantis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ on:
branches: [ main ]

env:
ATMOS_VERSION: 1.160.0
ATMOS_VERSION: 1.160.4
ATMOS_CLI_CONFIG_PATH: ./

jobs:
Expand Down
Loading