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"
|
2019-02-12 21:03:07 +01:00
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
2019-01-25 18:17:40 +01:00
|
|
|
|
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 {
|
2019-02-20 23:27:49 +01:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Alias string `json:"alias,omitempty"`
|
|
|
|
VersionConstraint string `json:"version_constraint,omitempty"`
|
|
|
|
ModuleAddress string `json:"module_address,omitempty"`
|
|
|
|
Expressions map[string]interface{} `json:"expressions,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type module struct {
|
2019-02-20 01:31:10 +01:00
|
|
|
Outputs map[string]output `json:"outputs,omitempty"`
|
2019-01-25 18:17:40 +01:00
|
|
|
// Resources are sorted in a user-friendly order that is undefined at this
|
|
|
|
// time, but consistent.
|
2019-02-01 22:47:18 +01:00
|
|
|
Resources []resource `json:"resources,omitempty"`
|
|
|
|
ModuleCalls map[string]moduleCall `json:"module_calls,omitempty"`
|
2019-02-12 21:03:07 +01:00
|
|
|
Variables variables `json:"variables,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type moduleCall struct {
|
2019-02-19 17:12:33 +01:00
|
|
|
Source string `json:"source,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
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"`
|
2019-02-19 17:12:33 +01:00
|
|
|
VersionConstraint string `json:"version_constraint,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 21:03:07 +01:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
// 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.
|
2019-02-20 23:27:49 +01:00
|
|
|
//
|
|
|
|
// NOTE: If a given resource is in a ModuleCall, and the provider was
|
|
|
|
// configured outside of the module (in a higher level configuration file),
|
|
|
|
// the ProviderConfigKey will not match a key in the ProviderConfigs map.
|
2018-12-19 20:08:25 +01:00
|
|
|
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"`
|
2019-02-20 01:31:10 +01:00
|
|
|
|
|
|
|
DependsOn []string `json:"depends_on,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:31:10 +01:00
|
|
|
type output struct {
|
|
|
|
Sensitive bool `json:"sensitive,omitempty"`
|
|
|
|
Expression expression `json:"expression,omitempty"`
|
|
|
|
DependsOn []string `json:"depends_on,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-02-20 23:27:49 +01:00
|
|
|
rootModule, err := marshalModule(c, schemas, "")
|
2018-12-19 20:08:25 +01:00
|
|
|
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 {
|
2020-02-03 14:18:04 +01:00
|
|
|
// FIXME: lookup providerFqn from config
|
|
|
|
providerFqn := addrs.NewLegacyProvider(pc.Name)
|
|
|
|
schema := schemas.ProviderConfig(providerFqn)
|
2019-02-20 23:27:49 +01:00
|
|
|
p := providerConfig{
|
|
|
|
Name: pc.Name,
|
|
|
|
Alias: pc.Alias,
|
|
|
|
ModuleAddress: c.Path.String(),
|
|
|
|
Expressions: marshalExpressions(pc.Config, schema),
|
|
|
|
VersionConstraint: pc.Version.Required.String(),
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
2019-02-20 23:27:49 +01:00
|
|
|
absPC := opaqueProviderKey(k, c.Path.String())
|
|
|
|
|
|
|
|
m[absPC] = p
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Must also visit our child modules, recursively.
|
|
|
|
for _, cc := range c.Children {
|
|
|
|
marshalProviderConfigs(cc, schemas, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 23:27:49 +01:00
|
|
|
func marshalModule(c *configs.Config, schemas *terraform.Schemas, addr string) (module, error) {
|
2018-12-19 20:08:25 +01:00
|
|
|
var module module
|
|
|
|
var rs []resource
|
|
|
|
|
2019-02-20 23:27:49 +01:00
|
|
|
managedResources, err := marshalResources(c.Module.ManagedResources, schemas, addr)
|
2018-12-19 20:08:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return module, err
|
|
|
|
}
|
2019-02-20 23:27:49 +01:00
|
|
|
dataResources, err := marshalResources(c.Module.DataResources, schemas, addr)
|
2018-12-19 20:08:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return module, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rs = append(managedResources, dataResources...)
|
|
|
|
module.Resources = rs
|
|
|
|
|
2019-02-20 01:31:10 +01:00
|
|
|
outputs := make(map[string]output)
|
2018-12-19 20:08:25 +01:00
|
|
|
for _, v := range c.Module.Outputs {
|
2019-02-20 01:31:10 +01:00
|
|
|
o := output{
|
2018-12-19 20:08:25 +01:00
|
|
|
Sensitive: v.Sensitive,
|
|
|
|
Expression: marshalExpression(v.Expr),
|
|
|
|
}
|
2019-02-20 01:31:10 +01:00
|
|
|
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
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
module.Outputs = outputs
|
2019-03-01 22:59:12 +01:00
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
module.ModuleCalls = marshalModuleCalls(c, schemas)
|
2019-02-12 21:03:07 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
return module, nil
|
|
|
|
}
|
|
|
|
|
2019-02-01 22:47:18 +01:00
|
|
|
func marshalModuleCalls(c *configs.Config, schemas *terraform.Schemas) map[string]moduleCall {
|
|
|
|
ret := make(map[string]moduleCall)
|
2018-12-19 20:08:25 +01:00
|
|
|
|
2019-03-01 22:59:12 +01:00
|
|
|
for name, mc := range c.Module.ModuleCalls {
|
|
|
|
mcConfig := c.Children[name]
|
|
|
|
ret[name] = marshalModuleCall(mcConfig, mc, schemas)
|
|
|
|
}
|
2019-02-01 22:47:18 +01:00
|
|
|
|
2019-03-01 22:59:12 +01:00
|
|
|
return ret
|
|
|
|
}
|
2018-12-19 20:08:25 +01:00
|
|
|
|
2019-03-01 22:59:12 +01:00
|
|
|
func marshalModuleCall(c *configs.Config, mc *configs.ModuleCall, schemas *terraform.Schemas) moduleCall {
|
2019-06-03 20:00:46 +02:00
|
|
|
// It is possible to have a module call with a nil config.
|
2019-06-03 17:19:03 +02:00
|
|
|
if c == nil {
|
|
|
|
return moduleCall{}
|
|
|
|
}
|
|
|
|
|
2019-03-01 22:59:12 +01:00
|
|
|
ret := moduleCall{
|
|
|
|
Source: mc.SourceAddr,
|
|
|
|
VersionConstraint: mc.Version.Required.String(),
|
|
|
|
}
|
|
|
|
cExp := marshalExpression(mc.Count)
|
|
|
|
if !cExp.Empty() {
|
|
|
|
ret.CountExpression = &cExp
|
|
|
|
} else {
|
|
|
|
fExp := marshalExpression(mc.ForEach)
|
|
|
|
if !fExp.Empty() {
|
|
|
|
ret.ForEachExpression = &fExp
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 22:59:12 +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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Expressions = marshalExpressions(mc.Config, schema)
|
|
|
|
module, _ := marshalModule(c, schemas, mc.Name)
|
|
|
|
ret.Module = module
|
2018-12-19 20:08:25 +01:00
|
|
|
|
2019-03-01 22:59:12 +01:00
|
|
|
return ret
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 23:27:49 +01:00
|
|
|
func marshalResources(resources map[string]*configs.Resource, schemas *terraform.Schemas, moduleAddr string) ([]resource, error) {
|
2018-12-19 20:08:25 +01:00
|
|
|
var rs []resource
|
|
|
|
for _, v := range resources {
|
|
|
|
r := resource{
|
|
|
|
Address: v.Addr().String(),
|
|
|
|
Type: v.Type,
|
|
|
|
Name: v.Name,
|
2019-02-20 23:27:49 +01:00
|
|
|
ProviderConfigKey: opaqueProviderKey(v.ProviderConfigAddr().StringCompact(), moduleAddr),
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:18:04 +01:00
|
|
|
// TODO: get actual providerFqn
|
|
|
|
providerFqn := addrs.NewLegacyProvider(v.ProviderConfigAddr().LocalName)
|
2019-01-24 01:14:34 +01:00
|
|
|
schema, schemaVer := schemas.ResourceTypeConfig(
|
2020-02-03 14:18:04 +01:00
|
|
|
providerFqn,
|
2019-01-24 01:14:34 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:31:10 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
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
|
|
|
|
}
|
2019-02-20 23:27:49 +01:00
|
|
|
|
|
|
|
// opaqueProviderKey generates a unique absProviderConfig-like string from the module
|
|
|
|
// address and provider
|
|
|
|
func opaqueProviderKey(provider string, addr string) (key string) {
|
|
|
|
key = provider
|
|
|
|
if addr != "" {
|
|
|
|
key = fmt.Sprintf("%s:%s", addr, provider)
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|