Skip to content

Commit

Permalink
command/jsonconfig: display module variables in config output (hashic…
Browse files Browse the repository at this point in the history
…orp#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
  • Loading branch information
mildwonkey authored Feb 12, 2019
1 parent 2ad995a commit f783ed0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
32 changes: 32 additions & 0 deletions command/jsonconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions command/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
17 changes: 14 additions & 3 deletions command/test-fixtures/show-json/basic-create/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
}
27 changes: 20 additions & 7 deletions command/test-fixtures/show-json/basic-delete/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
Expand All @@ -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"
}
}
}
}
}
22 changes: 17 additions & 5 deletions command/test-fixtures/show-json/basic-update/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
},
"prior_state": {
"format_version": "0.1",
"terraform_version": "0.12.0",
"values": {
"root_module": {
"resources": [
Expand All @@ -75,10 +76,11 @@
"mode": "managed",
"type": "test_instance",
"name": "test",
"schema_version": 0,
"provider_name": "test",
"values": {
"ami": {},
"id": {}
"ami": "bar",
"id": "placeholder"
}
}
]
Expand All @@ -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"
}
}
}
}
}

0 comments on commit f783ed0

Please sign in to comment.