command/jsonconfig: display module variables in config output (#20311)

* command/jsonconfig: display module variables in config output

The tests have been updated to reflect this change.

* command/jsonconfig: properly handle variables with nil defaults
This commit is contained in:
Kristin Laemmert 2019-02-12 12:03:07 -08:00 committed by GitHub
parent 2ad995a859
commit f783ed0d45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import (
"sort"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
@ -36,6 +37,7 @@ type module struct {
// time, but consistent.
Resources []resource `json:"resources,omitempty"`
ModuleCalls map[string]moduleCall `json:"module_calls,omitempty"`
Variables variables `json:"variables,omitempty"`
}
type moduleCall struct {
@ -46,6 +48,15 @@ type moduleCall struct {
Module module `json:"module,omitempty"`
}
// variables is the JSON representation of the variables provided to the current
// plan.
type variables map[string]*variable
type variable struct {
Default json.RawMessage `json:"default,omitempty"`
Description string `json:"description,omitempty"`
}
// Resource is the representation of a resource in the config
type resource struct {
// Address is the absolute resource address
@ -158,6 +169,27 @@ func marshalModule(c *configs.Config, schemas *terraform.Schemas) (module, error
}
module.Outputs = outputs
module.ModuleCalls = marshalModuleCalls(c, schemas)
if len(c.Module.Variables) > 0 {
vars := make(variables, len(c.Module.Variables))
for k, v := range c.Module.Variables {
var defaultValJSON []byte
if v.Default == cty.NilVal {
defaultValJSON = nil
} else {
defaultValJSON, err = ctyjson.Marshal(v.Default, v.Default.Type())
if err != nil {
return module, err
}
}
vars[k] = &variable{
Default: defaultValJSON,
Description: v.Description,
}
}
module.Variables = vars
}
return module, nil
}

View File

@ -321,6 +321,6 @@ type plan struct {
PlannedValues map[string]interface{} `json:"planned_values,omitempty"`
ResourceChanges []interface{} `json:"resource_changes,omitempty"`
OutputChanges map[string]interface{} `json:"output_changes,omitempty"`
PriorState string `json:"prior_state,omitempty"`
Config string `json:"configuration,omitempty"`
PriorState map[string]interface{} `json:"prior_state,omitempty"`
Config map[string]interface{} `json:"configuration,omitempty"`
}

View File

@ -147,12 +147,23 @@
"name": "test",
"provider_config_key": "provider.test",
"schema_version": 0,
"expressions": {
"ami": {
"references": [
"var.test_var"
]
}
},
"count_expression": {
"constant_value": 3
},
"for_each_expression": {}
}
}
]
],
"variables": {
"test_var": {
"default": "bar"
}
}
}
}
}

View File

@ -85,29 +85,32 @@
},
"prior_state": {
"format_version": "0.1",
"terraform_version": "0.12.0",
"values": {
"root_module": {
"resources": [
{
"address": "test_instance.test",
"schema_version": 0,
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider_name": "test",
"values": {
"ami": {},
"id": {}
"ami": "foo",
"id": "placeholder"
}
},
{
"address": "test_instance.test-delete",
"schema_version": 0,
"mode": "managed",
"type": "test_instance",
"name": "test-delete",
"provider_name": "test",
"values": {
"ami": {},
"id": {}
"ami": "foo",
"id": "placeholder"
}
}
]
@ -133,10 +136,20 @@
"name": "test",
"provider_config_key": "provider.test",
"schema_version": 0,
"count_expression": {},
"for_each_expression": {}
"expressions": {
"ami": {
"references": [
"var.test_var"
]
}
}
}
]
],
"variables": {
"test_var": {
"default": "bar"
}
}
}
}
}

View File

@ -67,6 +67,7 @@
},
"prior_state": {
"format_version": "0.1",
"terraform_version": "0.12.0",
"values": {
"root_module": {
"resources": [
@ -75,10 +76,11 @@
"mode": "managed",
"type": "test_instance",
"name": "test",
"schema_version": 0,
"provider_name": "test",
"values": {
"ami": {},
"id": {}
"ami": "bar",
"id": "placeholder"
}
}
]
@ -104,10 +106,20 @@
"name": "test",
"provider_config_key": "provider.test",
"schema_version": 0,
"count_expression": {},
"for_each_expression": {}
"expressions": {
"ami": {
"references": [
"var.test_var"
]
}
}
}
]
],
"variables": {
"test_var": {
"default": "bar"
}
}
}
}
}