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
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-02-05 20:01:58 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/getproviders"
|
2021-05-17 21:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2018-12-19 20:08:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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"`
|
2021-12-10 18:48:32 +01:00
|
|
|
FullName string `json:"full_name,omitempty"`
|
2019-02-20 23:27:49 +01:00
|
|
|
Alias string `json:"alias,omitempty"`
|
|
|
|
VersionConstraint string `json:"version_constraint,omitempty"`
|
|
|
|
ModuleAddress string `json:"module_address,omitempty"`
|
|
|
|
Expressions map[string]interface{} `json:"expressions,omitempty"`
|
2021-12-10 18:48:32 +01:00
|
|
|
parentKey string
|
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"`
|
2020-08-19 23:38:55 +02:00
|
|
|
DependsOn []string `json:"depends_on,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"`
|
2021-03-26 18:49:35 +01:00
|
|
|
Sensitive bool `json:"sensitive,omitempty"`
|
2019-02-12 21:03:07 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
2021-12-10 18:48:32 +01:00
|
|
|
normalizeModuleProviderKeys(&rootModule, pcs)
|
|
|
|
|
|
|
|
for name, pc := range pcs {
|
|
|
|
if pc.parentKey != "" {
|
|
|
|
delete(pcs, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.ProviderConfigs = pcs
|
|
|
|
|
2018-12-19 20:08:25 +01:00
|
|
|
ret, err := json.Marshal(output)
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalProviderConfigs(
|
|
|
|
c *configs.Config,
|
|
|
|
schemas *terraform.Schemas,
|
|
|
|
m map[string]providerConfig,
|
|
|
|
) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:01:58 +01:00
|
|
|
// We want to determine only the provider requirements from this module,
|
|
|
|
// ignoring any descendants. Disregard any diagnostics when determining
|
|
|
|
// requirements because we want this marshalling to succeed even if there
|
|
|
|
// are invalid constraints.
|
|
|
|
reqs, _ := c.ProviderRequirementsShallow()
|
|
|
|
|
|
|
|
// Add an entry for each provider configuration block in the module.
|
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-07-14 21:28:31 +02:00
|
|
|
providerFqn := c.ProviderForConfigAddr(addrs.LocalProviderConfig{LocalName: pc.Name})
|
2020-02-03 14:18:04 +01:00
|
|
|
schema := schemas.ProviderConfig(providerFqn)
|
2021-02-05 20:01:58 +01:00
|
|
|
|
|
|
|
p := providerConfig{
|
|
|
|
Name: pc.Name,
|
2021-12-10 18:48:32 +01:00
|
|
|
FullName: providerFqn.String(),
|
2021-02-05 20:01:58 +01:00
|
|
|
Alias: pc.Alias,
|
|
|
|
ModuleAddress: c.Path.String(),
|
|
|
|
Expressions: marshalExpressions(pc.Config, schema),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the fully resolved provider version constraint, rather than
|
|
|
|
// using the version argument in the configuration block. This is both
|
|
|
|
// future proof (for when we finish the deprecation of the provider config
|
|
|
|
// version argument) and more accurate (as it reflects the full set of
|
|
|
|
// constraints, in case there are multiple).
|
|
|
|
if vc, ok := reqs[providerFqn]; ok {
|
|
|
|
p.VersionConstraint = getproviders.VersionConstraintsString(vc)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := opaqueProviderKey(k, c.Path.String())
|
|
|
|
|
|
|
|
m[key] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that any required providers with no associated configuration
|
|
|
|
// block are included in the set.
|
|
|
|
for k, pr := range c.Module.ProviderRequirements.RequiredProviders {
|
2021-12-10 18:48:32 +01:00
|
|
|
// If a provider has aliases defined, process those first.
|
|
|
|
for _, alias := range pr.Aliases {
|
|
|
|
// If there exists a value for this provider, we have nothing to add
|
|
|
|
// to it, so skip.
|
|
|
|
key := opaqueProviderKey(alias.StringCompact(), c.Path.String())
|
|
|
|
if _, exists := m[key]; exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Given no provider configuration block exists, the only fields we can
|
|
|
|
// fill here are the local name, FQN, module address, and version
|
|
|
|
// constraints.
|
|
|
|
p := providerConfig{
|
|
|
|
Name: pr.Name,
|
|
|
|
FullName: pr.Type.String(),
|
|
|
|
ModuleAddress: c.Path.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if vc, ok := reqs[pr.Type]; ok {
|
|
|
|
p.VersionConstraint = getproviders.VersionConstraintsString(vc)
|
|
|
|
}
|
|
|
|
|
|
|
|
m[key] = p
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:01:58 +01:00
|
|
|
// If there exists a value for this provider, we have nothing to add
|
|
|
|
// to it, so skip.
|
|
|
|
key := opaqueProviderKey(k, c.Path.String())
|
|
|
|
if _, exists := m[key]; exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given no provider configuration block exists, the only fields we can
|
|
|
|
// fill here are the local name, module address, and version
|
|
|
|
// constraints.
|
2019-02-20 23:27:49 +01:00
|
|
|
p := providerConfig{
|
2021-02-05 20:01:58 +01:00
|
|
|
Name: pr.Name,
|
2021-12-10 18:48:32 +01:00
|
|
|
FullName: pr.Type.String(),
|
2021-02-05 20:01:58 +01:00
|
|
|
ModuleAddress: c.Path.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if vc, ok := reqs[pr.Type]; ok {
|
|
|
|
p.VersionConstraint = getproviders.VersionConstraintsString(vc)
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
2019-02-20 23:27:49 +01:00
|
|
|
|
2021-02-05 20:01:58 +01:00
|
|
|
m[key] = p
|
2018-12-19 20:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Must also visit our child modules, recursively.
|
2021-12-10 18:48:32 +01:00
|
|
|
for name, mc := range c.Module.ModuleCalls {
|
|
|
|
// Keys in c.Children are guaranteed to match those in c.Module.ModuleCalls
|
|
|
|
cc := c.Children[name]
|
|
|
|
|
|
|
|
// Add provider config map entries for passed provider configs,
|
|
|
|
// pointing at the passed configuration
|
|
|
|
for _, ppc := range mc.Providers {
|
|
|
|
// These provider names include aliases, if set
|
|
|
|
moduleProviderName := ppc.InChild.String()
|
|
|
|
parentProviderName := ppc.InParent.String()
|
|
|
|
|
|
|
|
// Look up the provider FQN from the module context, using the non-aliased local name
|
|
|
|
providerFqn := cc.ProviderForConfigAddr(addrs.LocalProviderConfig{LocalName: ppc.InChild.Name})
|
|
|
|
|
|
|
|
// The presence of passed provider configs means that we cannot have
|
|
|
|
// any configuration expressions or version constraints here
|
|
|
|
p := providerConfig{
|
|
|
|
Name: moduleProviderName,
|
|
|
|
FullName: providerFqn.String(),
|
|
|
|
ModuleAddress: cc.Path.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
key := opaqueProviderKey(moduleProviderName, cc.Path.String())
|
|
|
|
parentKey := opaqueProviderKey(parentProviderName, cc.Parent.Path.String())
|
|
|
|
|
|
|
|
// Traverse up the module call tree until we find the provider
|
|
|
|
// configuration which has no linked parent config. This is then
|
|
|
|
// the source of the configuration used in this module call, so
|
|
|
|
// we link to it directly
|
|
|
|
for {
|
|
|
|
parent, exists := m[parentKey]
|
|
|
|
if !exists {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.parentKey = parentKey
|
|
|
|
parentKey = parent.parentKey
|
|
|
|
if parentKey == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m[key] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, marshal any other provider configs within the called module.
|
|
|
|
// It is safe to do this last because it is invalid to configure a
|
|
|
|
// provider which has passed provider configs in the module call.
|
2018-12-19 20:08:25 +01:00
|
|
|
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,
|
2021-03-26 18:49:35 +01:00
|
|
|
Sensitive: v.Sensitive,
|
2019-02-12 21:03:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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{
|
Refactoring of module source addresses and module installation
It's been a long while since we gave close attention to the codepaths for
module source address parsing and external module package installation.
Due to their age, these codepaths often diverged from our modern practices
such as representing address types in the addrs package, and encapsulating
package installation details only in a particular location.
In particular, this refactor makes source address parsing a separate step
from module installation, which therefore makes the result of that parsing
available to other Terraform subsystems which work with the configuration
representation objects.
This also presented the opportunity to better encapsulate our use of
go-getter into a new package "getmodules" (echoing "getproviders"), which
is intended to be the only part of Terraform that directly interacts with
go-getter.
This is largely just a refactor of the existing functionality into a new
code organization, but there is one notable change in behavior here: the
source address parsing now happens during configuration loading rather
than module installation, which may cause errors about invalid addresses
to be returned in different situations than before. That counts as
backward compatible because we only promise to remain compatible with
configurations that are _valid_, which means that they can be initialized,
planned, and applied without any errors. This doesn't introduce any new
error cases, and instead just makes a pre-existing error case be detected
earlier.
Our module registry client is still using its own special module address
type from registry/regsrc for now, with a small shim from the new
addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also
rework the registry client to work with the new address type, but this
commit is already big enough as it is.
2021-05-28 04:24:59 +02:00
|
|
|
// We're intentionally echoing back exactly what the user entered
|
|
|
|
// here, rather than the normalized version in SourceAddr, because
|
|
|
|
// historically we only _had_ the raw address and thus it would be
|
|
|
|
// a (admittedly minor) breaking change to start normalizing them
|
|
|
|
// now, in case consumers of this data are expecting a particular
|
|
|
|
// non-normalized syntax.
|
|
|
|
Source: mc.SourceAddrRaw,
|
2019-03-01 22:59:12 +01:00
|
|
|
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)
|
2021-12-10 18:48:32 +01:00
|
|
|
|
|
|
|
module, _ := marshalModule(c, schemas, c.Path.String())
|
|
|
|
|
2019-03-01 22:59:12 +01:00
|
|
|
ret.Module = module
|
2018-12-19 20:08:25 +01:00
|
|
|
|
2020-08-19 23:38:55 +02:00
|
|
|
if len(mc.DependsOn) > 0 {
|
|
|
|
dependencies := make([]string, len(mc.DependsOn))
|
|
|
|
for i, d := range mc.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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.DependsOn = dependencies
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-12-10 18:48:32 +01:00
|
|
|
providerConfigKey := opaqueProviderKey(v.ProviderConfigAddr().StringCompact(), moduleAddr)
|
2018-12-19 20:08:25 +01:00
|
|
|
r := resource{
|
|
|
|
Address: v.Addr().String(),
|
|
|
|
Type: v.Type,
|
|
|
|
Name: v.Name,
|
2021-12-10 18:48:32 +01:00
|
|
|
ProviderConfigKey: providerConfigKey,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 01:14:34 +01:00
|
|
|
schema, schemaVer := schemas.ResourceTypeConfig(
|
2020-04-03 00:45:19 +02:00
|
|
|
v.Provider,
|
2019-01-24 01:14:34 +01:00
|
|
|
v.Mode,
|
|
|
|
v.Type,
|
|
|
|
)
|
|
|
|
if schema == nil {
|
2020-04-03 00:45:19 +02:00
|
|
|
return nil, fmt.Errorf("no schema found for %s (in provider %s)", v.Addr().String(), v.Provider)
|
2019-01-24 01:14:34 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2021-12-10 18:48:32 +01:00
|
|
|
// Flatten all resource provider keys in a module and its descendents, such
|
|
|
|
// that any resources from providers using a configuration passed through the
|
|
|
|
// module call have a direct refernce to that provider configuration.
|
|
|
|
func normalizeModuleProviderKeys(m *module, pcs map[string]providerConfig) {
|
|
|
|
for i, r := range m.Resources {
|
|
|
|
if pc, exists := pcs[r.ProviderConfigKey]; exists {
|
|
|
|
if _, hasParent := pcs[pc.parentKey]; hasParent {
|
|
|
|
m.Resources[i].ProviderConfigKey = pc.parentKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mc := range m.ModuleCalls {
|
|
|
|
normalizeModuleProviderKeys(&mc.Module, pcs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|