Skip to content

Commit

Permalink
Merge branch 'main' into feature/dev-1780-shell-tab-completion
Browse files Browse the repository at this point in the history
  • Loading branch information
osterman authored Feb 11, 2025
2 parents 95eaff4 + a155dd3 commit bbca500
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 16 deletions.
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

0 comments on commit bbca500

Please sign in to comment.