Skip to content

Commit 2f1c103

Browse files
committed
update atmos.Component template function
1 parent b067d5a commit 2f1c103

File tree

8 files changed

+60
-33
lines changed

8 files changed

+60
-33
lines changed

internal/exec/describe_stacks.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ func ExecuteDescribeStacks(
376376

377377
componentSectionProcessed, err := ProcessTmplWithDatasources(
378378
atmosConfig,
379+
configAndStacksInfo,
379380
settingsSectionStruct,
380381
"describe-stacks-all-sections",
381382
componentSectionStr,
@@ -577,6 +578,7 @@ func ExecuteDescribeStacks(
577578

578579
componentSectionProcessed, err := ProcessTmplWithDatasources(
579580
atmosConfig,
581+
configAndStacksInfo,
580582
settingsSectionStruct,
581583
"templates-describe-stacks-all-atmos-sections",
582584
componentSectionStr,

internal/exec/template_funcs.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,28 @@ import (
1616
)
1717

1818
// FuncMap creates and returns a map of template functions
19-
func FuncMap(atmosConfig schema.AtmosConfiguration, ctx context.Context, gomplateData *data.Data) template.FuncMap {
20-
atmosFuncs := &AtmosFuncs{atmosConfig, ctx, gomplateData}
19+
func FuncMap(
20+
atmosConfig schema.AtmosConfiguration,
21+
configAndStacksInfo schema.ConfigAndStacksInfo,
22+
ctx context.Context,
23+
gomplateData *data.Data,
24+
) template.FuncMap {
25+
atmosFuncs := &AtmosFuncs{atmosConfig, configAndStacksInfo, ctx, gomplateData}
2126

2227
return map[string]any{
2328
"atmos": func() any { return atmosFuncs },
2429
}
2530
}
2631

2732
type AtmosFuncs struct {
28-
atmosConfig schema.AtmosConfiguration
29-
ctx context.Context
30-
gomplateData *data.Data
33+
atmosConfig schema.AtmosConfiguration
34+
configAndStacksInfo schema.ConfigAndStacksInfo
35+
ctx context.Context
36+
gomplateData *data.Data
3137
}
3238

3339
func (f AtmosFuncs) Component(component string, stack string) (any, error) {
34-
return componentFunc(f.atmosConfig, component, stack)
40+
return componentFunc(f.atmosConfig, f.configAndStacksInfo, component, stack)
3541
}
3642

3743
func (f AtmosFuncs) GomplateDatasource(alias string, args ...string) (any, error) {

internal/exec/template_funcs_component.go

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ import (
66

77
"github.com/samber/lo"
88

9+
cfg "github.com/cloudposse/atmos/pkg/config"
910
"github.com/cloudposse/atmos/pkg/schema"
1011
u "github.com/cloudposse/atmos/pkg/utils"
1112
)
1213

1314
var componentFuncSyncMap = sync.Map{}
1415

15-
func componentFunc(atmosConfig schema.AtmosConfiguration, component string, stack string) (any, error) {
16+
func componentFunc(
17+
atmosConfig schema.AtmosConfiguration,
18+
configAndStacksInfo schema.ConfigAndStacksInfo,
19+
component string,
20+
stack string,
21+
) (any, error) {
1622
u.LogTrace(fmt.Sprintf("Executing template function 'atmos.Component(%s, %s)'", component, stack))
1723

1824
stackSlug := fmt.Sprintf("%s-%s", stack, component)
@@ -42,41 +48,47 @@ func componentFunc(atmosConfig schema.AtmosConfiguration, component string, stac
4248
return nil, err
4349
}
4450

51+
// Process Terraform remote state
4552
var terraformOutputs map[string]any
46-
47-
// Check if the component in the stack is configured with the 'static' remote state backend,
48-
// in which case get the `output` from the static remote state instead of executing `terraform output`
49-
remoteStateBackendStaticTypeOutputs, err := GetComponentRemoteStateBackendStaticType(sections)
50-
if err != nil {
51-
return nil, err
52-
}
53-
54-
if remoteStateBackendStaticTypeOutputs != nil {
55-
terraformOutputs = remoteStateBackendStaticTypeOutputs
56-
} else {
57-
// Execute `terraform output`
58-
terraformOutputs, err = execTerraformOutput(&atmosConfig, component, stack, sections)
53+
if configAndStacksInfo.ComponentType == cfg.TerraformComponentType {
54+
// Check if the component in the stack is configured with the 'static' remote state backend,
55+
// in which case get the `output` from the static remote state instead of executing `terraform output`
56+
remoteStateBackendStaticTypeOutputs, err := GetComponentRemoteStateBackendStaticType(sections)
5957
if err != nil {
6058
return nil, err
6159
}
62-
}
6360

64-
outputs := map[string]any{
65-
"outputs": terraformOutputs,
66-
}
61+
if remoteStateBackendStaticTypeOutputs != nil {
62+
terraformOutputs = remoteStateBackendStaticTypeOutputs
63+
} else {
64+
// Execute `terraform output`
65+
terraformOutputs, err = execTerraformOutput(&atmosConfig, component, stack, sections)
66+
if err != nil {
67+
return nil, err
68+
}
69+
}
6770

68-
sections = lo.Assign(sections, outputs)
71+
outputs := map[string]any{
72+
"outputs": terraformOutputs,
73+
}
74+
75+
sections = lo.Assign(sections, outputs)
76+
}
6977

7078
// Cache the result
7179
componentFuncSyncMap.Store(stackSlug, sections)
7280

7381
if atmosConfig.Logs.Level == u.LogLevelTrace {
74-
u.LogTrace(fmt.Sprintf("Executed template function 'atmos.Component(%s, %s)'\n\n'outputs' section:", component, stack))
75-
y, err2 := u.ConvertToYAML(terraformOutputs)
76-
if err2 != nil {
77-
u.LogError(err2)
78-
} else {
79-
u.LogTrace(y)
82+
u.LogTrace(fmt.Sprintf("Executed template function 'atmos.Component(%s, %s)'", component, stack))
83+
84+
if configAndStacksInfo.ComponentType == cfg.TerraformComponentType {
85+
u.LogTrace("'outputs' section:")
86+
y, err2 := u.ConvertToYAML(terraformOutputs)
87+
if err2 != nil {
88+
u.LogError(err2)
89+
} else {
90+
u.LogTrace(y)
91+
}
8092
}
8193
}
8294

internal/exec/template_utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func ProcessTmpl(
3131
ctx := context.TODO()
3232

3333
// Add Gomplate, Sprig and Atmos template functions
34-
funcs := lo.Assign(gomplate.CreateFuncs(ctx, &d), sprig.FuncMap(), FuncMap(schema.AtmosConfiguration{}, ctx, &d))
34+
funcs := lo.Assign(gomplate.CreateFuncs(ctx, &d), sprig.FuncMap(), FuncMap(schema.AtmosConfiguration{}, schema.ConfigAndStacksInfo{}, ctx, &d))
3535

3636
t, err := template.New(tmplName).Funcs(funcs).Parse(tmplValue)
3737
if err != nil {
@@ -63,6 +63,7 @@ func ProcessTmpl(
6363
// ProcessTmplWithDatasources parses and executes Go templates with datasources
6464
func ProcessTmplWithDatasources(
6565
atmosConfig schema.AtmosConfiguration,
66+
configAndStacksInfo schema.ConfigAndStacksInfo,
6667
settingsSection schema.Settings,
6768
tmplName string,
6869
tmplValue string,
@@ -145,7 +146,7 @@ func ProcessTmplWithDatasources(
145146
}
146147

147148
// Atmos functions
148-
funcs = lo.Assign(funcs, FuncMap(atmosConfig, context.TODO(), &d))
149+
funcs = lo.Assign(funcs, FuncMap(atmosConfig, configAndStacksInfo, context.TODO(), &d))
149150

150151
// Process and add environment variables
151152
for k, v := range templateSettings.Env {

internal/exec/terraform_generate_backends.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func ExecuteTerraformGenerateBackends(
238238

239239
componentSectionProcessed, err := ProcessTmplWithDatasources(
240240
atmosConfig,
241+
configAndStacksInfo,
241242
settingsSectionStruct,
242243
"terraform-generate-backends",
243244
componentSectionStr,

internal/exec/terraform_generate_varfiles.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func ExecuteTerraformGenerateVarfiles(
247247

248248
componentSectionProcessed, err := ProcessTmplWithDatasources(
249249
atmosConfig,
250+
configAndStacksInfo,
250251
settingsSectionStruct,
251252
"terraform-generate-varfiles",
252253
componentSectionStr,

internal/exec/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ func ProcessStacks(
512512

513513
componentSectionProcessed, err := ProcessTmplWithDatasources(
514514
atmosConfig,
515+
configAndStacksInfo,
515516
settingsSectionStruct,
516517
"templates-all-atmos-sections",
517518
componentSectionStr,

pkg/config/const.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ const (
3939
HelpFlag1 = "-h"
4040
HelpFlag2 = "--help"
4141

42+
TerraformComponentType = "terraform"
43+
HelmfileComponentType = "helmfile"
44+
4245
ComponentVendorConfigFileName = "component.yaml"
4346
AtmosVendorConfigFileName = "vendor"
4447

0 commit comments

Comments
 (0)