mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
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:
parent
2ad995a859
commit
f783ed0d45
@ -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
|
||||
}
|
||||
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user