terraform: outputs work with multi-variables

This commit is contained in:
Mitchell Hashimoto 2014-07-05 10:48:47 -07:00
parent 07f98f7ee7
commit a01d979c1b
4 changed files with 144 additions and 27 deletions

View File

@ -109,7 +109,7 @@ func (c *Context) Apply() (*State, error) {
if err == nil && len(c.config.Outputs) > 0 { if err == nil && len(c.config.Outputs) > 0 {
s.Outputs = make(map[string]string) s.Outputs = make(map[string]string)
for _, o := range c.config.Outputs { for _, o := range c.config.Outputs {
if err = c.computeVars(s, o.RawConfig); err != nil { if err = c.computeVars(o.RawConfig); err != nil {
break break
} }
@ -247,7 +247,7 @@ func (c *Context) Validate() ([]string, []error) {
// computeVars takes the State and given RawConfig and processes all // computeVars takes the State and given RawConfig and processes all
// the variables. This dynamically discovers the attributes instead of // the variables. This dynamically discovers the attributes instead of
// using a static map[string]string that the genericWalkFn uses. // using a static map[string]string that the genericWalkFn uses.
func (c *Context) computeVars(s *State, raw *config.RawConfig) error { func (c *Context) computeVars(raw *config.RawConfig) error {
// If there are on variables, then we're done // If there are on variables, then we're done
if len(raw.Variables) == 0 { if len(raw.Variables) == 0 {
return nil return nil
@ -258,22 +258,15 @@ func (c *Context) computeVars(s *State, raw *config.RawConfig) error {
for n, rawV := range raw.Variables { for n, rawV := range raw.Variables {
switch v := rawV.(type) { switch v := rawV.(type) {
case *config.ResourceVariable: case *config.ResourceVariable:
r, ok := s.Resources[v.ResourceId()] var attr string
if !ok { var err error
return fmt.Errorf( if v.Multi {
"Resource '%s' not found for variable '%s'", attr, err = c.computeResourceMultiVariable(v)
v.ResourceId(), } else {
v.FullKey()) attr, err = c.computeResourceVariable(v)
} }
if err != nil {
attr, ok := r.Attributes[v.Field] return err
if !ok {
return fmt.Errorf(
"Resource '%s' does not have attribute '%s' "+
"for variable '%s'",
v.ResourceId(),
v.Field,
v.FullKey())
} }
vs[n] = attr vs[n] = attr
@ -286,6 +279,75 @@ func (c *Context) computeVars(s *State, raw *config.RawConfig) error {
return raw.Interpolate(vs) return raw.Interpolate(vs)
} }
func (c *Context) computeResourceVariable(
v *config.ResourceVariable) (string, error) {
r, ok := c.state.Resources[v.ResourceId()]
if !ok {
return "", fmt.Errorf(
"Resource '%s' not found for variable '%s'",
v.ResourceId(),
v.FullKey())
}
attr, ok := r.Attributes[v.Field]
if !ok {
return "", fmt.Errorf(
"Resource '%s' does not have attribute '%s' "+
"for variable '%s'",
v.ResourceId(),
v.Field,
v.FullKey())
}
return attr, nil
}
func (c *Context) computeResourceMultiVariable(
v *config.ResourceVariable) (string, error) {
// Get the resource from the configuration so we can know how
// many of the resource there is.
var cr *config.Resource
for _, r := range c.config.Resources {
if r.Id() == v.ResourceId() {
cr = r
break
}
}
if cr == nil {
return "", fmt.Errorf(
"Resource '%s' not found for variable '%s'",
v.ResourceId(),
v.FullKey())
}
var values []string
for i := 0; i < cr.Count; i++ {
id := fmt.Sprintf("%s.%d", v.ResourceId(), i)
r, ok := c.state.Resources[id]
if !ok {
continue
}
attr, ok := r.Attributes[v.Field]
if !ok {
continue
}
values = append(values, attr)
}
if len(values) == 0 {
return "", fmt.Errorf(
"Resource '%s' does not have attribute '%s' "+
"for variable '%s'",
v.ResourceId(),
v.Field,
v.FullKey())
}
return strings.Join(values, ","), nil
}
func (c *Context) graph() (*depgraph.Graph, error) { func (c *Context) graph() (*depgraph.Graph, error) {
return Graph(&GraphOpts{ return Graph(&GraphOpts{
Config: c.config, Config: c.config,
@ -701,17 +763,9 @@ func computeAggregateVars(
if !ok { if !ok {
continue continue
} }
if !rv.Multi {
idx := strings.Index(rv.Field, ".")
if idx == -1 {
// It isn't an aggregated var
continue continue
} }
if rv.Field[:idx] != "*" {
// It isn't an aggregated var
continue
}
field := rv.Field[idx+1:]
// Get the meta node so that we can determine the count // Get the meta node so that we can determine the count
key := fmt.Sprintf("%s.%s", rv.Type, rv.Name) key := fmt.Sprintf("%s.%s", rv.Type, rv.Name)
@ -731,7 +785,7 @@ func computeAggregateVars(
rv.Type, rv.Type,
rv.Name, rv.Name,
i, i,
field) rv.Field)
if v, ok := vs[key]; ok { if v, ok := vs[key]; ok {
values = append(values, v) values = append(values, v)
} }

View File

@ -536,6 +536,34 @@ func TestContextApply_output(t *testing.T) {
} }
} }
func TestContextApply_outputMulti(t *testing.T) {
c := testConfig(t, "apply-output-multi")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext(t, &ContextOpts{
Config: c,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if _, err := ctx.Plan(nil); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyOutputMultiStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
}
func TestContextApply_unknownAttribute(t *testing.T) { func TestContextApply_unknownAttribute(t *testing.T) {
c := testConfig(t, "apply-unknown") c := testConfig(t, "apply-unknown")
p := testProvider("aws") p := testProvider("aws")

View File

@ -121,6 +121,29 @@ Outputs:
foo_num = 2 foo_num = 2
` `
const testTerraformApplyOutputMultiStr = `
aws_instance.bar.0:
ID = foo
foo = bar
type = aws_instance
aws_instance.bar.1:
ID = foo
foo = bar
type = aws_instance
aws_instance.bar.2:
ID = foo
foo = bar
type = aws_instance
aws_instance.foo:
ID = foo
num = 2
type = aws_instance
Outputs:
foo_num = bar,bar,bar
`
const testTerraformApplyUnknownAttrStr = ` const testTerraformApplyUnknownAttrStr = `
aws_instance.foo: aws_instance.foo:
ID = foo ID = foo

View File

@ -0,0 +1,12 @@
resource "aws_instance" "foo" {
num = "2"
}
resource "aws_instance" "bar" {
foo = "bar"
count = 3
}
output "foo_num" {
value = "${aws_instance.bar.*.foo}"
}