command/show enhancements and bugfixes
* command/jsonconfig: provider config marshaling enhancements This PR fixes a bug wherein the keys in "provider_config" were the "addrs.ProviderConfig", and therefore being overwritten for each module, instead of the intended "addrs.AbsProviderConfig". We realized that there was still opportunity for ambiguity, for example if a user made a provider alias that was the same name as a module, so we opted to use the syntax `modulename:providername(.provideralias)` * command/json*: fixed a bug where we were attempting to lookup schemas with the provider name, instead of provider type.
This commit is contained in:
parent
1d35233a03
commit
0c94e20a83
|
@ -25,10 +25,11 @@ type config struct {
|
||||||
// provider configurations are the one concept in Terraform that can span across
|
// provider configurations are the one concept in Terraform that can span across
|
||||||
// module boundaries.
|
// module boundaries.
|
||||||
type providerConfig struct {
|
type providerConfig struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
Alias string `json:"alias,omitempty"`
|
Alias string `json:"alias,omitempty"`
|
||||||
ModuleAddress string `json:"module_address,omitempty"`
|
VersionConstraint string `json:"version_constraint,omitempty"`
|
||||||
Expressions map[string]interface{} `json:"expressions,omitempty"`
|
ModuleAddress string `json:"module_address,omitempty"`
|
||||||
|
Expressions map[string]interface{} `json:"expressions,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type module struct {
|
type module struct {
|
||||||
|
@ -71,6 +72,10 @@ type resource struct {
|
||||||
|
|
||||||
// ProviderConfigKey is the key into "provider_configs" (shown above) for
|
// ProviderConfigKey is the key into "provider_configs" (shown above) for
|
||||||
// the provider configuration that this resource is associated with.
|
// the provider configuration that this resource is associated with.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
ProviderConfigKey string `json:"provider_config_key,omitempty"`
|
ProviderConfigKey string `json:"provider_config_key,omitempty"`
|
||||||
|
|
||||||
// Provisioners is an optional field which describes any provisioners.
|
// Provisioners is an optional field which describes any provisioners.
|
||||||
|
@ -114,7 +119,7 @@ func Marshal(c *configs.Config, schemas *terraform.Schemas) ([]byte, error) {
|
||||||
marshalProviderConfigs(c, schemas, pcs)
|
marshalProviderConfigs(c, schemas, pcs)
|
||||||
output.ProviderConfigs = pcs
|
output.ProviderConfigs = pcs
|
||||||
|
|
||||||
rootModule, err := marshalModule(c, schemas)
|
rootModule, err := marshalModule(c, schemas, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -135,12 +140,16 @@ func marshalProviderConfigs(
|
||||||
|
|
||||||
for k, pc := range c.Module.ProviderConfigs {
|
for k, pc := range c.Module.ProviderConfigs {
|
||||||
schema := schemas.ProviderConfig(pc.Name)
|
schema := schemas.ProviderConfig(pc.Name)
|
||||||
m[k] = providerConfig{
|
p := providerConfig{
|
||||||
Name: pc.Name,
|
Name: pc.Name,
|
||||||
Alias: pc.Alias,
|
Alias: pc.Alias,
|
||||||
ModuleAddress: c.Path.String(),
|
ModuleAddress: c.Path.String(),
|
||||||
Expressions: marshalExpressions(pc.Config, schema),
|
Expressions: marshalExpressions(pc.Config, schema),
|
||||||
|
VersionConstraint: pc.Version.Required.String(),
|
||||||
}
|
}
|
||||||
|
absPC := opaqueProviderKey(k, c.Path.String())
|
||||||
|
|
||||||
|
m[absPC] = p
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must also visit our child modules, recursively.
|
// Must also visit our child modules, recursively.
|
||||||
|
@ -149,15 +158,15 @@ func marshalProviderConfigs(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalModule(c *configs.Config, schemas *terraform.Schemas) (module, error) {
|
func marshalModule(c *configs.Config, schemas *terraform.Schemas, addr string) (module, error) {
|
||||||
var module module
|
var module module
|
||||||
var rs []resource
|
var rs []resource
|
||||||
|
|
||||||
managedResources, err := marshalResources(c.Module.ManagedResources, schemas)
|
managedResources, err := marshalResources(c.Module.ManagedResources, schemas, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return module, err
|
return module, err
|
||||||
}
|
}
|
||||||
dataResources, err := marshalResources(c.Module.DataResources, schemas)
|
dataResources, err := marshalResources(c.Module.DataResources, schemas, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return module, err
|
return module, err
|
||||||
}
|
}
|
||||||
|
@ -245,8 +254,8 @@ func marshalModuleCalls(c *configs.Config, schemas *terraform.Schemas) map[strin
|
||||||
|
|
||||||
retMC.Expressions = marshalExpressions(mc.Config, schema)
|
retMC.Expressions = marshalExpressions(mc.Config, schema)
|
||||||
|
|
||||||
for _, cc := range c.Children {
|
for name, cc := range c.Children {
|
||||||
childModule, _ := marshalModule(cc, schemas)
|
childModule, _ := marshalModule(cc, schemas, name)
|
||||||
retMC.Module = childModule
|
retMC.Module = childModule
|
||||||
}
|
}
|
||||||
ret[mc.Name] = retMC
|
ret[mc.Name] = retMC
|
||||||
|
@ -256,14 +265,14 @@ func marshalModuleCalls(c *configs.Config, schemas *terraform.Schemas) map[strin
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalResources(resources map[string]*configs.Resource, schemas *terraform.Schemas) ([]resource, error) {
|
func marshalResources(resources map[string]*configs.Resource, schemas *terraform.Schemas, moduleAddr string) ([]resource, error) {
|
||||||
var rs []resource
|
var rs []resource
|
||||||
for _, v := range resources {
|
for _, v := range resources {
|
||||||
r := resource{
|
r := resource{
|
||||||
Address: v.Addr().String(),
|
Address: v.Addr().String(),
|
||||||
Type: v.Type,
|
Type: v.Type,
|
||||||
Name: v.Name,
|
Name: v.Name,
|
||||||
ProviderConfigKey: v.ProviderConfigAddr().String(),
|
ProviderConfigKey: opaqueProviderKey(v.ProviderConfigAddr().StringCompact(), moduleAddr),
|
||||||
}
|
}
|
||||||
|
|
||||||
switch v.Mode {
|
switch v.Mode {
|
||||||
|
@ -286,7 +295,7 @@ func marshalResources(resources map[string]*configs.Resource, schemas *terraform
|
||||||
}
|
}
|
||||||
|
|
||||||
schema, schemaVer := schemas.ResourceTypeConfig(
|
schema, schemaVer := schemas.ResourceTypeConfig(
|
||||||
v.ProviderConfigAddr().StringCompact(),
|
v.ProviderConfigAddr().Type,
|
||||||
v.Mode,
|
v.Mode,
|
||||||
v.Type,
|
v.Type,
|
||||||
)
|
)
|
||||||
|
@ -332,3 +341,13 @@ func marshalResources(resources map[string]*configs.Resource, schemas *terraform
|
||||||
})
|
})
|
||||||
return rs, nil
|
return rs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
|
@ -177,7 +177,11 @@ func (p *plan) marshalResourceChanges(changes *plans.Changes, schemas *terraform
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
schema, _ := schemas.ResourceTypeConfig(rc.ProviderAddr.ProviderConfig.StringCompact(), addr.Resource.Resource.Mode, addr.Resource.Resource.Type)
|
schema, _ := schemas.ResourceTypeConfig(
|
||||||
|
rc.ProviderAddr.ProviderConfig.Type,
|
||||||
|
addr.Resource.Resource.Mode,
|
||||||
|
addr.Resource.Resource.Type,
|
||||||
|
)
|
||||||
if schema == nil {
|
if schema == nil {
|
||||||
return fmt.Errorf("no schema found for %s", r.Address)
|
return fmt.Errorf("no schema found for %s", r.Address)
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,7 +154,7 @@ func marshalPlanResources(changes *plans.Changes, ris []addrs.AbsResourceInstanc
|
||||||
}
|
}
|
||||||
|
|
||||||
schema, schemaVer := schemas.ResourceTypeConfig(
|
schema, schemaVer := schemas.ResourceTypeConfig(
|
||||||
resource.ProviderName,
|
r.ProviderAddr.ProviderConfig.Type,
|
||||||
r.Addr.Resource.Resource.Mode,
|
r.Addr.Resource.Resource.Mode,
|
||||||
resource.Type,
|
resource.Type,
|
||||||
)
|
)
|
||||||
|
|
|
@ -267,7 +267,7 @@ func marshalResources(resources map[string]*states.Resource, schemas *terraform.
|
||||||
}
|
}
|
||||||
|
|
||||||
schema, _ := schemas.ResourceTypeConfig(
|
schema, _ := schemas.ResourceTypeConfig(
|
||||||
r.ProviderConfig.ProviderConfig.StringCompact(),
|
r.ProviderConfig.ProviderConfig.Type,
|
||||||
r.Addr.Mode,
|
r.Addr.Mode,
|
||||||
r.Addr.Type,
|
r.Addr.Type,
|
||||||
)
|
)
|
||||||
|
|
|
@ -142,7 +142,7 @@
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
"provider_config_key": "provider.test",
|
"provider_config_key": "test",
|
||||||
"schema_version": 0,
|
"schema_version": 0,
|
||||||
"expressions": {
|
"expressions": {
|
||||||
"ami": {
|
"ami": {
|
||||||
|
|
|
@ -132,7 +132,7 @@
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
"provider_config_key": "provider.test",
|
"provider_config_key": "test",
|
||||||
"schema_version": 0,
|
"schema_version": 0,
|
||||||
"expressions": {
|
"expressions": {
|
||||||
"ami": {
|
"ami": {
|
||||||
|
|
|
@ -103,7 +103,7 @@
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
"provider_config_key": "provider.test",
|
"provider_config_key": "test",
|
||||||
"schema_version": 0,
|
"schema_version": 0,
|
||||||
"expressions": {
|
"expressions": {
|
||||||
"ami": {
|
"ami": {
|
||||||
|
|
|
@ -9,3 +9,5 @@ resource "test_instance" "test" {
|
||||||
output "test" {
|
output "test" {
|
||||||
value = var.test_var
|
value = var.test_var
|
||||||
}
|
}
|
||||||
|
|
||||||
|
provider "test" {}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
module "test" {
|
module "module_test" {
|
||||||
source = "./foo"
|
source = "./foo"
|
||||||
test_var = "baz"
|
test_var = "baz"
|
||||||
}
|
}
|
||||||
|
|
||||||
output "test" {
|
output "test" {
|
||||||
value = module.test.test
|
value = module.module_test.test
|
||||||
depends_on = [module.test]
|
depends_on = [module.module_test]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
{"Modules":[{"Key":"","Source":"","Dir":"/Users/kristin/go/src/github.com/hashicorp/terraform/command/test-fixtures/show-json/modules"},{"Key":"test","Source":"./foo","Dir":"/Users/kristin/go/src/github.com/hashicorp/terraform/command/test-fixtures/show-json/modules/foo"}]}
|
|
|
@ -12,7 +12,7 @@
|
||||||
{
|
{
|
||||||
"resources": [
|
"resources": [
|
||||||
{
|
{
|
||||||
"address": "module.test.test_instance.test[0]",
|
"address": "module.module_test.test_instance.test[0]",
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
@ -24,7 +24,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"address": "module.test.test_instance.test[1]",
|
"address": "module.module_test.test_instance.test[1]",
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
@ -36,7 +36,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"address": "module.test.test_instance.test[2]",
|
"address": "module.module_test.test_instance.test[2]",
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
@ -48,15 +48,15 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"address": "module.test"
|
"address": "module.module_test"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"resource_changes": [
|
"resource_changes": [
|
||||||
{
|
{
|
||||||
"address": "module.test.test_instance.test[0]",
|
"address": "module.module_test.test_instance.test[0]",
|
||||||
"module_address": "module.test",
|
"module_address": "module.module_test",
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
@ -76,8 +76,8 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"address": "module.test.test_instance.test[1]",
|
"address": "module.module_test.test_instance.test[1]",
|
||||||
"module_address": "module.test",
|
"module_address": "module.module_test",
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
@ -97,8 +97,8 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"address": "module.test.test_instance.test[2]",
|
"address": "module.module_test.test_instance.test[2]",
|
||||||
"module_address": "module.test",
|
"module_address": "module.module_test",
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
|
@ -134,16 +134,16 @@
|
||||||
"test": {
|
"test": {
|
||||||
"expression": {
|
"expression": {
|
||||||
"references": [
|
"references": [
|
||||||
"module.test.test"
|
"module.module_test.test"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"depends_on": [
|
"depends_on": [
|
||||||
"module.test"
|
"module.module_test"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"module_calls": {
|
"module_calls": {
|
||||||
"test": {
|
"module_test": {
|
||||||
"source": "./foo",
|
"source": "./foo",
|
||||||
"expressions": {
|
"expressions": {
|
||||||
"test_var": {
|
"test_var": {
|
||||||
|
@ -166,7 +166,7 @@
|
||||||
"mode": "managed",
|
"mode": "managed",
|
||||||
"type": "test_instance",
|
"type": "test_instance",
|
||||||
"name": "test",
|
"name": "test",
|
||||||
"provider_config_key": "provider.test",
|
"provider_config_key": "module_test:test",
|
||||||
"expressions": {
|
"expressions": {
|
||||||
"ami": {
|
"ami": {
|
||||||
"references": [
|
"references": [
|
||||||
|
@ -188,6 +188,12 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"provider_config": {
|
||||||
|
"module_test:test": {
|
||||||
|
"module_address": "module_test",
|
||||||
|
"name": "test"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue