2018-12-19 20:08:25 +01:00
|
|
|
package jsonconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-01-12 00:13:55 +01:00
|
|
|
"sort"
|
2018-12-19 20:08:25 +01:00
|
|
|
|
2019-01-25 18:17:40 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config represents the complete configuration source
|
|
|
|
type config struct {
|
|
|
|
ProviderConfigs map[string]providerConfig `json:"provider_config,omitempty"`
|
|
|
|
RootModule module `json:"root_module,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProviderConfig describes all of the provider configurations throughout the
|
|
|
|
// configuration tree, flattened into a single map for convenience since
|
|
|
|
// provider configurations are the one concept in Terraform that can span across
|
|
|
|
// module boundaries.
|
|
|
|
type providerConfig struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Alias string `json:"alias,omitempty"`
|
|
|
|
ModuleAddress string `json:"module_address,omitempty"`
|
|
|
|
Expressions map[string]interface{} `json:"expressions,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type module struct {
|
2019-01-25 18:17:40 +01:00
|
|
|
Outputs map[string]configOutput `json:"outputs,omitempty"`
|
|
|
|
// Resources are sorted in a user-friendly order that is undefined at this
|
|
|
|
// time, but consistent.
|
|
|
|
Resources []resource `json:"resources,omitempty"`
|
|
|
|
ModuleCalls []moduleCall `json:"module_calls,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type moduleCall struct {
|
|
|
|
ResolvedSource string `json:"resolved_source,omitempty"`
|
|
|
|
Expressions map[string]interface{} `json:"expressions,omitempty"`
|
2019-01-29 00:53:53 +01:00
|
|
|
CountExpression *expression `json:"count_expression,omitempty"`
|
|
|
|
ForEachExpression *expression `json:"for_each_expression,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
Module module `json:"module,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resource is the representation of a resource in the config
|
|
|
|
type resource struct {
|
|
|
|
// Address is the absolute resource address
|
|
|
|
Address string `json:"address,omitempty"`
|
|
|
|
|
|
|
|
// Mode can be "managed" or "data"
|
|
|
|
Mode string `json:"mode,omitempty"`
|
|
|
|
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
|
|
|
|
// ProviderConfigKey is the key into "provider_configs" (shown above) for
|
|
|
|
// the provider configuration that this resource is associated with.
|
|
|
|
ProviderConfigKey string `json:"provider_config_key,omitempty"`
|
|
|
|
|
|
|
|
// Provisioners is an optional field which describes any provisioners.
|
|
|
|
// Connection info will not be included here.
|
|
|
|
Provisioners []provisioner `json:"provisioners,omitempty"`
|
|
|
|
|
|
|
|
// Expressions" describes the resource-type-specific content of the
|
|
|
|
// configuration block.
|
|
|
|
Expressions map[string]interface{} `json:"expressions,omitempty"`
|
|
|
|
|
|
|
|
// SchemaVersion indicates which version of the resource type schema the
|
|
|
|
// "values" property conforms to.
|
|
|
|
SchemaVersion uint64 `json:"schema_version"`
|
|
|
|
|
|
|
|
// CountExpression and ForEachExpression describe the expressions given for
|
|
|
|
// the corresponding meta-arguments in the resource configuration block.
|
|
|
|
// These are omitted if the corresponding argument isn't set.
|
2019-01-29 00:53:53 +01:00
|
|
|
CountExpression *expression `json:"count_expression,omitempty"`
|
|
|
|
ForEachExpression *expression `json:"for_each_expression,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type configOutput struct {
|
|
|
|
Sensitive bool `json:"sensitive,omitempty"`
|
|
|
|
Expression expression `json:"expression,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type provisioner struct {
|
2019-01-25 00:28:53 +01:00
|
|
|
Type string `json:"type,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
Expressions map[string]interface{} `json:"expressions,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal returns the json encoding of terraform configuration.
|
|
|
|
func Marshal(c *configs.Config, schemas *terraform.Schemas) ([]byte, error) {
|
|
|
|
var output config
|
|
|
|
|
|
|
|
pcs := make(map[string]providerConfig)
|
|
|
|
marshalProviderConfigs(c, schemas, pcs)
|
|
|
|
output.ProviderConfigs = pcs
|
|
|
|
|
|
|
|
rootModule, err := marshalModule(c, schemas)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
output.RootModule = rootModule
|
|
|
|
|
|
|
|
ret, err := json.Marshal(output)
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalProviderConfigs(
|
|
|
|
c *configs.Config,
|
|
|
|
schemas *terraform.Schemas,
|
|
|
|
m map[string]providerConfig,
|
|
|
|
) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
for k, pc := range c.Module.ProviderConfigs {
|
2018-12-19 20:08:25 +01:00
|
|
|
schema := schemas.ProviderConfig(pc.Name)
|
mildwonkey/b-show-state (#20032)
* command/show: properly marshal attribute values to json
marshalAttributeValues in jsonstate and jsonplan packages was returning
a cty.Value, which json/encoding could not marshal. These functions now
convert those cty.Values into json.RawMessages.
* command/jsonplan: planned values should include resources that are not changing
* command/jsonplan: return a filtered list of proposed 'after' attributes
Previously, proposed 'after' attributes were not being shown if the
attributes were not WhollyKnown. jsonplan now iterates through all the
`after` attributes, omitting those which are not wholly known.
The same was roughly true for after_unknown, and that structure is now
correctly populated. In the future we may choose to filter the
after_unknown structure to _only_ display unknown attributes, instead of
all attributes.
* command/jsonconfig: use a unique key for providers so that aliased
providers don't get munged together
This now uses the same "provider" key from configs.Module, e.g.
`providername.provideralias`.
* command/jsonplan: unknownAsBool needs to iterate through objects that are not wholly known
* command/jsonplan: properly display actions as strings according to the RFC,
instead of a plans.Action string.
For example:
a plans.Action string DeleteThenCreate should be displayed as ["delete",
"create"]
Tests have been updated to reflect this.
* command/jsonplan: return "null" for unknown list items.
The length of a list could be meaningful on its own, so we will turn
unknowns into "null". The same is less likely true for maps and objects,
so we will continue to omit unknown values from those.
2019-01-23 20:46:53 +01:00
|
|
|
m[k] = providerConfig{
|
2018-12-19 20:08:25 +01:00
|
|
|
Name: pc.Name,
|
|
|
|
Alias: pc.Alias,
|
|
|
|
ModuleAddress: c.Path.String(),
|
|
|
|
Expressions: marshalExpressions(pc.Config, schema),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must also visit our child modules, recursively.
|
|
|
|
for _, cc := range c.Children {
|
|
|
|
marshalProviderConfigs(cc, schemas, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalModule(c *configs.Config, schemas *terraform.Schemas) (module, error) {
|
|
|
|
var module module
|
|
|
|
var rs []resource
|
|
|
|
|
|
|
|
managedResources, err := marshalResources(c.Module.ManagedResources, schemas)
|
|
|
|
if err != nil {
|
|
|
|
return module, err
|
|
|
|
}
|
|
|
|
dataResources, err := marshalResources(c.Module.DataResources, schemas)
|
|
|
|
if err != nil {
|
|
|
|
return module, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rs = append(managedResources, dataResources...)
|
|
|
|
module.Resources = rs
|
|
|
|
|
|
|
|
outputs := make(map[string]configOutput)
|
|
|
|
for _, v := range c.Module.Outputs {
|
|
|
|
outputs[v.Name] = configOutput{
|
|
|
|
Sensitive: v.Sensitive,
|
|
|
|
Expression: marshalExpression(v.Expr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.Outputs = outputs
|
|
|
|
module.ModuleCalls = marshalModuleCalls(c, schemas)
|
|
|
|
return module, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalModuleCalls(c *configs.Config, schemas *terraform.Schemas) []moduleCall {
|
|
|
|
var ret []moduleCall
|
|
|
|
for _, v := range c.Module.ModuleCalls {
|
|
|
|
mc := moduleCall{
|
|
|
|
ResolvedSource: v.SourceAddr,
|
|
|
|
}
|
|
|
|
cExp := marshalExpression(v.Count)
|
|
|
|
if !cExp.Empty() {
|
2019-01-29 00:53:53 +01:00
|
|
|
mc.CountExpression = &cExp
|
2018-12-19 20:08:25 +01:00
|
|
|
} else {
|
|
|
|
fExp := marshalExpression(v.ForEach)
|
|
|
|
if !fExp.Empty() {
|
2019-01-29 00:53:53 +01:00
|
|
|
mc.ForEachExpression = &fExp
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
schema := &configschema.Block{}
|
|
|
|
schema.Attributes = make(map[string]*configschema.Attribute)
|
|
|
|
for _, variable := range c.Module.Variables {
|
|
|
|
schema.Attributes[variable.Name] = &configschema.Attribute{
|
|
|
|
Required: variable.Default == cty.NilVal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mc.Expressions = marshalExpressions(v.Config, schema)
|
|
|
|
|
|
|
|
for _, cc := range c.Children {
|
|
|
|
childModule, _ := marshalModule(cc, schemas)
|
|
|
|
mc.Module = childModule
|
|
|
|
}
|
|
|
|
ret = append(ret, mc)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalResources(resources map[string]*configs.Resource, schemas *terraform.Schemas) ([]resource, error) {
|
|
|
|
var rs []resource
|
|
|
|
for _, v := range resources {
|
|
|
|
r := resource{
|
|
|
|
Address: v.Addr().String(),
|
|
|
|
Type: v.Type,
|
|
|
|
Name: v.Name,
|
|
|
|
ProviderConfigKey: v.ProviderConfigAddr().String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v.Mode {
|
|
|
|
case addrs.ManagedResourceMode:
|
|
|
|
r.Mode = "managed"
|
|
|
|
case addrs.DataResourceMode:
|
|
|
|
r.Mode = "data"
|
|
|
|
default:
|
|
|
|
return rs, fmt.Errorf("resource %s has an unsupported mode %s", r.Address, v.Mode.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
cExp := marshalExpression(v.Count)
|
|
|
|
if !cExp.Empty() {
|
2019-01-29 00:53:53 +01:00
|
|
|
r.CountExpression = &cExp
|
2018-12-19 20:08:25 +01:00
|
|
|
} else {
|
|
|
|
fExp := marshalExpression(v.ForEach)
|
|
|
|
if !fExp.Empty() {
|
2019-01-29 00:53:53 +01:00
|
|
|
r.ForEachExpression = &fExp
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 01:14:34 +01:00
|
|
|
schema, schemaVer := schemas.ResourceTypeConfig(
|
|
|
|
v.ProviderConfigAddr().StringCompact(),
|
|
|
|
v.Mode,
|
|
|
|
v.Type,
|
|
|
|
)
|
|
|
|
if schema == nil {
|
|
|
|
return nil, fmt.Errorf("no schema found for %s", v.Addr().String())
|
|
|
|
}
|
|
|
|
r.SchemaVersion = schemaVer
|
2018-12-19 20:08:25 +01:00
|
|
|
|
|
|
|
r.Expressions = marshalExpressions(v.Config, schema)
|
|
|
|
|
|
|
|
// Managed is populated only for Mode = addrs.ManagedResourceMode
|
|
|
|
if v.Managed != nil && len(v.Managed.Provisioners) > 0 {
|
|
|
|
var provisioners []provisioner
|
|
|
|
for _, p := range v.Managed.Provisioners {
|
|
|
|
schema := schemas.ProvisionerConfig(p.Type)
|
|
|
|
prov := provisioner{
|
2019-01-25 00:28:53 +01:00
|
|
|
Type: p.Type,
|
2018-12-19 20:08:25 +01:00
|
|
|
Expressions: marshalExpressions(p.Config, schema),
|
|
|
|
}
|
|
|
|
provisioners = append(provisioners, prov)
|
|
|
|
}
|
|
|
|
r.Provisioners = provisioners
|
|
|
|
}
|
|
|
|
|
|
|
|
rs = append(rs, r)
|
|
|
|
}
|
2019-01-12 00:13:55 +01:00
|
|
|
sort.Slice(rs, func(i, j int) bool {
|
|
|
|
return rs[i].Address < rs[j].Address
|
|
|
|
})
|
2018-12-19 20:08:25 +01:00
|
|
|
return rs, nil
|
|
|
|
}
|