From b14472f22cd06d8deb791a1f0da4ec1ad991d426 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Tue, 19 Feb 2019 16:31:10 -0800 Subject: [PATCH] command/jsonconfig: add missing fields from configuration output (#20387) Display depends_on for resources and outputs, and description for outputs. --- command/jsonconfig/config.go | 48 ++++++++++++++++--- .../test-fixtures/show-json/modules/main.tf | 3 +- .../show-json/modules/output.json | 8 ++-- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/command/jsonconfig/config.go b/command/jsonconfig/config.go index e0a5eb30ce..f673ab0fa6 100644 --- a/command/jsonconfig/config.go +++ b/command/jsonconfig/config.go @@ -32,7 +32,7 @@ type providerConfig struct { } type module struct { - Outputs map[string]configOutput `json:"outputs,omitempty"` + Outputs map[string]output `json:"outputs,omitempty"` // Resources are sorted in a user-friendly order that is undefined at this // time, but consistent. Resources []resource `json:"resources,omitempty"` @@ -90,11 +90,15 @@ type resource struct { // These are omitted if the corresponding argument isn't set. CountExpression *expression `json:"count_expression,omitempty"` ForEachExpression *expression `json:"for_each_expression,omitempty"` + + DependsOn []string `json:"depends_on,omitempty"` } -type configOutput struct { - Sensitive bool `json:"sensitive,omitempty"` - Expression expression `json:"expression,omitempty"` +type output struct { + Sensitive bool `json:"sensitive,omitempty"` + Expression expression `json:"expression,omitempty"` + DependsOn []string `json:"depends_on,omitempty"` + Description string `json:"description,omitempty"` } type provisioner struct { @@ -161,12 +165,30 @@ func marshalModule(c *configs.Config, schemas *terraform.Schemas) (module, error rs = append(managedResources, dataResources...) module.Resources = rs - outputs := make(map[string]configOutput) + outputs := make(map[string]output) for _, v := range c.Module.Outputs { - outputs[v.Name] = configOutput{ + o := output{ Sensitive: v.Sensitive, Expression: marshalExpression(v.Expr), } + if v.Description != "" { + o.Description = v.Description + } + if len(v.DependsOn) > 0 { + dependencies := make([]string, len(v.DependsOn)) + for i, d := range v.DependsOn { + ref, diags := addrs.ParseRef(d) + // we should not get an error here, because `terraform validate` + // would have complained well before this point, but if we do we'll + // silenty skip it. + if !diags.HasErrors() { + dependencies[i] = ref.Subject.String() + } + } + o.DependsOn = dependencies + } + + outputs[v.Name] = o } module.Outputs = outputs module.ModuleCalls = marshalModuleCalls(c, schemas) @@ -289,6 +311,20 @@ func marshalResources(resources map[string]*configs.Resource, schemas *terraform r.Provisioners = provisioners } + if len(v.DependsOn) > 0 { + dependencies := make([]string, len(v.DependsOn)) + for i, d := range v.DependsOn { + ref, diags := addrs.ParseRef(d) + // we should not get an error here, because `terraform validate` + // would have complained well before this point, but if we do we'll + // silenty skip it. + if !diags.HasErrors() { + dependencies[i] = ref.Subject.String() + } + } + r.DependsOn = dependencies + } + rs = append(rs, r) } sort.Slice(rs, func(i, j int) bool { diff --git a/command/test-fixtures/show-json/modules/main.tf b/command/test-fixtures/show-json/modules/main.tf index 4b81fcf5ff..252923d246 100644 --- a/command/test-fixtures/show-json/modules/main.tf +++ b/command/test-fixtures/show-json/modules/main.tf @@ -4,5 +4,6 @@ module "test" { } output "test" { - value = module.test.test + value = module.test.test + depends_on = [module.test] } diff --git a/command/test-fixtures/show-json/modules/output.json b/command/test-fixtures/show-json/modules/output.json index cbc284c7ee..216bf893bd 100644 --- a/command/test-fixtures/show-json/modules/output.json +++ b/command/test-fixtures/show-json/modules/output.json @@ -61,7 +61,6 @@ "type": "test_instance", "name": "test", "index": 0, - "deposed": true, "change": { "actions": [ "create" @@ -83,7 +82,6 @@ "type": "test_instance", "name": "test", "index": 1, - "deposed": true, "change": { "actions": [ "create" @@ -105,7 +103,6 @@ "type": "test_instance", "name": "test", "index": 2, - "deposed": true, "change": { "actions": [ "create" @@ -139,7 +136,10 @@ "references": [ "module.test.test" ] - } + }, + "depends_on": [ + "module.test" + ] } }, "module_calls": {