config: allow applying variables to resources
This commit is contained in:
parent
e904fca3da
commit
bd1f235b9b
|
@ -7,6 +7,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/depgraph"
|
"github.com/hashicorp/terraform/depgraph"
|
||||||
|
"github.com/mitchellh/reflectwalk"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ResourceGraphRoot is the name of the resource graph root that should be
|
// ResourceGraphRoot is the name of the resource graph root that should be
|
||||||
|
@ -81,6 +82,25 @@ func (r *Resource) Id() string {
|
||||||
return fmt.Sprintf("%s.%s", r.Type, r.Name)
|
return fmt.Sprintf("%s.%s", r.Type, r.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReplaceVariables replaces the variables in the configuration
|
||||||
|
// with the given values.
|
||||||
|
//
|
||||||
|
// This replacement is not in place. Instead, this function will
|
||||||
|
// return a new resource with the variables replaced.
|
||||||
|
func (r *Resource) ReplaceVariables(vs map[string]string) *Resource {
|
||||||
|
result := *r
|
||||||
|
|
||||||
|
w := &variableReplaceWalker{
|
||||||
|
Values: vs,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := reflectwalk.Walk(result.Config, w); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result
|
||||||
|
}
|
||||||
|
|
||||||
// ResourceGraph returns a dependency graph of the resources from this
|
// ResourceGraph returns a dependency graph of the resources from this
|
||||||
// Terraform configuration.
|
// Terraform configuration.
|
||||||
func (c *Config) ResourceGraph() *depgraph.Graph {
|
func (c *Config) ResourceGraph() *depgraph.Graph {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -63,6 +64,49 @@ func TestNewUserVariable(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResourceReplaceVariables(t *testing.T) {
|
||||||
|
r := &Resource{
|
||||||
|
Name: "foo",
|
||||||
|
Type: "bar",
|
||||||
|
Config: map[string]interface{}{
|
||||||
|
"foo": "${var.bar}",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
values := map[string]string{
|
||||||
|
"var.bar": "value",
|
||||||
|
}
|
||||||
|
|
||||||
|
r2 := r.ReplaceVariables(values)
|
||||||
|
|
||||||
|
expected := &Resource{
|
||||||
|
Name: "foo",
|
||||||
|
Type: "bar",
|
||||||
|
Config: map[string]interface{}{
|
||||||
|
"foo": "value",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(r2, expected) {
|
||||||
|
t.Fatalf("bad: %#v", r2)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO(mitchellh): Eventually, preserve original config...
|
||||||
|
|
||||||
|
expectedOriginal := &Resource{
|
||||||
|
Name: "foo",
|
||||||
|
Type: "bar",
|
||||||
|
Config: map[string]interface{}{
|
||||||
|
"foo": "${var.bar}",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(r, expectedOriginal) {
|
||||||
|
t.Fatalf("bad: %#v", r)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
const resourceGraphValue = `
|
const resourceGraphValue = `
|
||||||
root: root
|
root: root
|
||||||
root -> aws_security_group.firewall
|
root -> aws_security_group.firewall
|
||||||
|
|
Loading…
Reference in New Issue