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

interpolate maps in dynamic blocks #9921

Merged
merged 3 commits into from
Jan 29, 2021
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ require (
github.com/hashicorp/go-version v1.2.1-0.20191009193637-2046c9d0f0b0
github.com/hashicorp/golang-lru v0.5.4
github.com/hashicorp/hcl v1.0.1-0.20201016140508-a07e7d50bbee
github.com/hashicorp/hcl/v2 v2.7.1-0.20201020204811-68a97f93bb48
github.com/hashicorp/hcl/v2 v2.7.1-0.20210129140708-3000d85e32a9
github.com/hashicorp/logutils v1.0.0
github.com/hashicorp/memberlist v0.2.2
github.com/hashicorp/net-rpc-msgpackrpc v0.0.0-20151116020338-a14192a58a69
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+l
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.1-0.20201016140508-a07e7d50bbee h1:8B4HqvMUtYSjsGkYjiQGStc9pXffY2J+Z2SPQAj+wMY=
github.com/hashicorp/hcl v1.0.1-0.20201016140508-a07e7d50bbee/go.mod h1:gwlu9+/P9MmKtYrMsHeFRZPXj2CTPm11TDnMeaRHS7g=
github.com/hashicorp/hcl/v2 v2.7.1-0.20201020204811-68a97f93bb48 h1:iaau0VStfX9CgOlpbceawI94uVEM3sliqnjpHSVQqUo=
github.com/hashicorp/hcl/v2 v2.7.1-0.20201020204811-68a97f93bb48/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
github.com/hashicorp/hcl/v2 v2.7.1-0.20210129140708-3000d85e32a9 h1:5s4yY6Efzd33dTozKsEe6PeqPPN1jhXb8ijCBZllS1c=
github.com/hashicorp/hcl/v2 v2.7.1-0.20210129140708-3000d85e32a9/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
github.com/hashicorp/hil v0.0.0-20160711231837-1e86c6b523c5/go.mod h1:KHvg/R2/dPtaePb16oW4qIyzkMxXOL38xjRN64adsts=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand Down
46 changes: 38 additions & 8 deletions jobspec2/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,39 @@ func TestParseDynamic(t *testing.T) {
hcl := `
job "example" {

dynamic "group" {
for_each = ["groupA", "groupB", "groupC"]
labels = [group.value]

content {
task "simple" {
driver = "raw_exec"
dynamic "group" {
for_each = [
{ name = "groupA", idx = 1 },
{ name = "groupB", idx = 2 },
{ name = "groupC", idx = 3 },
]
labels = [group.value.name]

content {
count = group.value.idx

service {
port = group.value.name
}

task "simple" {
driver = "raw_exec"
config {
command = group.value.name
}
meta {
VERSION = group.value.idx
}
env {
ID = format("id:%s", group.value.idx)
}
resources {
cpu = group.value.idx
}
}
}
}
}
}
`
out, err := ParseWithConfig(&ParseConfig{
Path: "input.hcl",
Expand All @@ -305,6 +326,15 @@ dynamic "group" {
require.Equal(t, "groupA", *out.TaskGroups[0].Name)
require.Equal(t, "groupB", *out.TaskGroups[1].Name)
require.Equal(t, "groupC", *out.TaskGroups[2].Name)
require.Equal(t, 1, *out.TaskGroups[0].Tasks[0].Resources.CPU)
require.Equal(t, "groupA", out.TaskGroups[0].Services[0].PortLabel)

// interpolation inside maps
require.Equal(t, "groupA", out.TaskGroups[0].Tasks[0].Config["command"])
require.Equal(t, "1", out.TaskGroups[0].Tasks[0].Meta["VERSION"])
require.Equal(t, "id:1", out.TaskGroups[0].Tasks[0].Env["ID"])
tgross marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, "id:2", out.TaskGroups[1].Tasks[0].Env["ID"])
require.Equal(t, "3", out.TaskGroups[2].Tasks[0].Meta["VERSION"])
}

func TestParse_InvalidScalingSyntax(t *testing.T) {
Expand Down

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ github.com/hashicorp/hcl/hcl/token
github.com/hashicorp/hcl/json/parser
github.com/hashicorp/hcl/json/scanner
github.com/hashicorp/hcl/json/token
# github.com/hashicorp/hcl/v2 v2.7.1-0.20201020204811-68a97f93bb48
# github.com/hashicorp/hcl/v2 v2.7.1-0.20210129140708-3000d85e32a9
## explicit
github.com/hashicorp/hcl/v2
github.com/hashicorp/hcl/v2/ext/customdecode
Expand Down