command/format: fix an issue where data resources were not displaying (#20386)

Fixes #20245
This commit is contained in:
Kristin Laemmert 2019-02-19 14:18:47 -08:00 committed by GitHub
parent c59a274e96
commit 874b333962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -116,7 +116,12 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
switch addr.Mode { switch addr.Mode {
case addrs.ManagedResourceMode: case addrs.ManagedResourceMode:
if _, exists := schemas.Providers[provider].ResourceTypes[addr.Type]; !exists { schema, _ = schemas.ResourceTypeConfig(
provider,
addr.Mode,
addr.Type,
)
if schema == nil {
p.buf.WriteString(fmt.Sprintf( p.buf.WriteString(fmt.Sprintf(
"# missing schema for provider %q resource type %s\n\n", provider, addr.Type)) "# missing schema for provider %q resource type %s\n\n", provider, addr.Type))
continue continue
@ -127,9 +132,13 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
addr.Type, addr.Type,
addr.Name, addr.Name,
)) ))
schema = schemas.Providers[provider].ResourceTypes[addr.Type]
case addrs.DataResourceMode: case addrs.DataResourceMode:
if _, exists := schemas.Providers[provider].ResourceTypes[addr.Type]; !exists { schema, _ = schemas.ResourceTypeConfig(
provider,
addr.Mode,
addr.Type,
)
if schema == nil {
p.buf.WriteString(fmt.Sprintf( p.buf.WriteString(fmt.Sprintf(
"# missing schema for provider %q data source %s\n\n", provider, addr.Type)) "# missing schema for provider %q data source %s\n\n", provider, addr.Type))
continue continue
@ -140,7 +149,6 @@ func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraf
addr.Type, addr.Type,
addr.Name, addr.Name,
)) ))
schema = schemas.Providers[provider].DataSources[addr.Type]
default: default:
// should never happen, since the above is exhaustive // should never happen, since the above is exhaustive
p.buf.WriteString(addr.String()) p.buf.WriteString(addr.String())

View File

@ -42,6 +42,21 @@ func TestState(t *testing.T) {
Type: "test", Type: "test",
}.Absolute(addrs.RootModuleInstance), }.Absolute(addrs.RootModuleInstance),
) )
rootModule.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.DataResourceMode,
Type: "test_data_source",
Name: "data",
}.Instance(addrs.NoKey),
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"compute":"sure"}`),
},
addrs.ProviderConfig{
Type: "test",
}.Absolute(addrs.RootModuleInstance),
)
tests := []struct { tests := []struct {
State *StateOpts State *StateOpts
@ -142,7 +157,12 @@ func testSchemas() *terraform.Schemas {
} }
} }
const TestOutput = `# test_resource.baz[0]: const TestOutput = `# data.test_data_source.data:
data "test_data_source" "data" {
compute = "sure"
}
# test_resource.baz[0]:
resource "test_resource" "baz" { resource "test_resource" "baz" {
woozles = "confuzles" woozles = "confuzles"
} }