mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 18:01:01 -06:00
config: special detect ResourceVariables for multi-access
This commit is contained in:
parent
4b5f5aec65
commit
07f98f7ee7
@ -63,9 +63,10 @@ type InterpolatedVariable interface {
|
||||
// A ResourceVariable is a variable that is referencing the field
|
||||
// of a resource, such as "${aws_instance.foo.ami}"
|
||||
type ResourceVariable struct {
|
||||
Type string
|
||||
Name string
|
||||
Field string
|
||||
Type string // Resource type, i.e. "aws_instance"
|
||||
Name string // Resource name
|
||||
Field string // Resource field
|
||||
Multi bool // True if multi-variable: aws_instance.foo.*.id
|
||||
|
||||
key string
|
||||
}
|
||||
@ -203,10 +204,19 @@ func (v *Variable) Required() bool {
|
||||
|
||||
func NewResourceVariable(key string) (*ResourceVariable, error) {
|
||||
parts := strings.SplitN(key, ".", 3)
|
||||
field := parts[2]
|
||||
multi := false
|
||||
|
||||
if idx := strings.Index(field, "."); idx != -1 && field[:idx] == "*" {
|
||||
multi = true
|
||||
field = field[idx+1:]
|
||||
}
|
||||
|
||||
return &ResourceVariable{
|
||||
Type: parts[0],
|
||||
Name: parts[1],
|
||||
Field: parts[2],
|
||||
Field: field,
|
||||
Multi: multi,
|
||||
key: key,
|
||||
}, nil
|
||||
}
|
||||
|
@ -58,12 +58,35 @@ func TestNewResourceVariable(t *testing.T) {
|
||||
if v.Field != "baz" {
|
||||
t.Fatalf("bad: %#v", v)
|
||||
}
|
||||
if v.Multi {
|
||||
t.Fatal("should not be multi")
|
||||
}
|
||||
|
||||
if v.FullKey() != "foo.bar.baz" {
|
||||
t.Fatalf("bad: %#v", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceVariable_Multi(t *testing.T) {
|
||||
v, err := NewResourceVariable("foo.bar.*.baz")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
if v.Type != "foo" {
|
||||
t.Fatalf("bad: %#v", v)
|
||||
}
|
||||
if v.Name != "bar" {
|
||||
t.Fatalf("bad: %#v", v)
|
||||
}
|
||||
if v.Field != "baz" {
|
||||
t.Fatalf("bad: %#v", v)
|
||||
}
|
||||
if !v.Multi {
|
||||
t.Fatal("should be multi")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewUserVariable(t *testing.T) {
|
||||
v, err := NewUserVariable("var.bar")
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user