Skip to content

Commit dca70c8

Browse files
authored
feat: add terramate.stack.parent.id. (#1955)
## What this PR does / why we need it: Implements `terramate.stack.parent.id` metadata for stacks that has parent stacks. ## Which issue(s) this PR fixes: none ## Special notes for your reviewer: ## Does this PR introduce a user-facing change? ``` yes, add a new feature ```
2 parents 13e6443 + ddd34dc commit dca70c8

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
2222

2323
## Unreleased
2424

25+
### Added
26+
27+
- Add `terramate.stack.parent.id` metadata to stacks that are part of a parent-child hierarchy.
28+
2529
## v0.11.1
2630

2731
### Added

config/stack.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,21 @@ func (s *Stack) RuntimeValues(root *Root) map[string]cty.Value {
236236
if s.ID != "" {
237237
stackMapVals["id"] = cty.StringVal(s.ID)
238238
}
239+
cfg, _ := root.Lookup(s.Dir)
240+
var parentStack *Tree
241+
242+
for cfg.Parent != nil {
243+
cfg = cfg.Parent
244+
if cfg.IsStack() {
245+
parentStack = cfg
246+
break
247+
}
248+
}
249+
if parentStack != nil {
250+
stackMapVals["parent"] = cty.ObjectVal(map[string]cty.Value{
251+
"id": cty.StringVal(parentStack.Node.Stack.ID),
252+
})
253+
}
239254
stack := cty.ObjectVal(stackMapVals)
240255
return map[string]cty.Value{
241256
"name": cty.StringVal(s.Name), // DEPRECATED

e2etests/core/debug_show_metadata_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,72 @@ stack "/stack":
246246
terramate.stack.path.basename="stack"
247247
terramate.stack.path.relative="stack"
248248
terramate.stack.path.to_root=".."
249+
`,
250+
},
251+
},
252+
{
253+
name: "one stack with parent stack",
254+
layout: []string{
255+
`s:parent:id=parent-stack`,
256+
`s:parent/child`,
257+
},
258+
want: RunExpected{
259+
Stdout: `Available metadata:
260+
261+
project metadata:
262+
terramate.stacks.list=[/parent /parent/child]
263+
264+
stack "/parent":
265+
terramate.stack.id="parent-stack"
266+
terramate.stack.name="parent"
267+
terramate.stack.description=""
268+
terramate.stack.tags=[]
269+
terramate.stack.path.absolute="/parent"
270+
terramate.stack.path.basename="parent"
271+
terramate.stack.path.relative="parent"
272+
terramate.stack.path.to_root=".."
273+
274+
stack "/parent/child":
275+
terramate.stack.name="child"
276+
terramate.stack.description=""
277+
terramate.stack.tags=[]
278+
terramate.stack.path.absolute="/parent/child"
279+
terramate.stack.path.basename="child"
280+
terramate.stack.path.relative="parent/child"
281+
terramate.stack.path.to_root="../.."
282+
`,
283+
},
284+
},
285+
{
286+
name: "one stack with parent stack separated by directories",
287+
layout: []string{
288+
`s:parent:id=parent-stack`,
289+
`s:parent/some/child/stack`,
290+
},
291+
want: RunExpected{
292+
Stdout: `Available metadata:
293+
294+
project metadata:
295+
terramate.stacks.list=[/parent /parent/some/child/stack]
296+
297+
stack "/parent":
298+
terramate.stack.id="parent-stack"
299+
terramate.stack.name="parent"
300+
terramate.stack.description=""
301+
terramate.stack.tags=[]
302+
terramate.stack.path.absolute="/parent"
303+
terramate.stack.path.basename="parent"
304+
terramate.stack.path.relative="parent"
305+
terramate.stack.path.to_root=".."
306+
307+
stack "/parent/some/child/stack":
308+
terramate.stack.name="stack"
309+
terramate.stack.description=""
310+
terramate.stack.tags=[]
311+
terramate.stack.path.absolute="/parent/some/child/stack"
312+
terramate.stack.path.basename="stack"
313+
terramate.stack.path.relative="parent/some/child/stack"
314+
terramate.stack.path.to_root="../../../.."
249315
`,
250316
},
251317
},

e2etests/core/run_sharing_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,52 @@ func TestRunSharing(t *testing.T) {
7171
assert.EqualStrings(t, expected, string(s.RootEntry().ReadFile("s2/file.txt")))
7272
},
7373
},
74+
{
75+
name: "parent/child sharing",
76+
layout: []string{
77+
"f:backend.tm:" + Block("sharing_backend",
78+
Labels("name"),
79+
Expr("type", "terraform"),
80+
Str("filename", "sharing.tf"),
81+
Command("terraform", "output", "-json"),
82+
).String(),
83+
"s:s1:id=s1",
84+
"f:s1/main.tf:" + Doc(
85+
Block("resource",
86+
Labels("local_file", "s1_file"),
87+
Str("content", "s1_content"),
88+
Str("filename", "${path.module}/file.txt"),
89+
),
90+
).String(),
91+
"f:s1/output.tm:" + Output(
92+
Labels("s1_output"),
93+
Str("backend", "name"),
94+
Expr("value", "resource.local_file.s1_file.content"),
95+
).String(),
96+
"s:s1/s2",
97+
"f:s1/s2/input.tm:" + Input(
98+
Labels("s2_input"),
99+
Str("backend", "name"),
100+
Expr("value", "outputs.s1_output.value"),
101+
Expr("from_stack_id", `terramate.stack.parent.id`),
102+
).String(),
103+
"f:s1/s2/main.tf:" + Doc(
104+
Block("resource",
105+
Labels("local_file", "s2_file"),
106+
Expr("content", "var.s2_input"),
107+
Str("filename", "${path.module}/file.txt"),
108+
),
109+
).String(),
110+
},
111+
check: func(t *testing.T, s *sandbox.S, res RunResult) {
112+
AssertRunResult(t, res, RunExpected{
113+
IgnoreStdout: true,
114+
})
115+
const expected = "s1_content"
116+
assert.EqualStrings(t, expected, string(s.RootEntry().ReadFile("s1/file.txt")))
117+
assert.EqualStrings(t, expected, string(s.RootEntry().ReadFile("s1/s2/file.txt")))
118+
},
119+
},
74120
{
75121
name: "input with no output counterpart",
76122
layout: []string{

0 commit comments

Comments
 (0)