From 71d7a9e480cf3b20244ec5680129925b7c604e92 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 9 May 2018 16:56:23 -0700 Subject: [PATCH] core: AttachSchemaTransformer fail gracefully with no schema Having a missing schema is a programming error, but at the time of this commit we're in the midst of introducing schema all over Terraform and so there are inevitably some places -- particularly in older unit tests -- where schema isn't yet being provided. This error allows us to catch those cases and fail gracefully, rather than panicking further down here when we access t.Components methods. Also includes some additional logging to aid debugging of this transformer. --- terraform/transform_attach_schema.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/terraform/transform_attach_schema.go b/terraform/transform_attach_schema.go index 4c5158229..23c655177 100644 --- a/terraform/transform_attach_schema.go +++ b/terraform/transform_attach_schema.go @@ -38,15 +38,23 @@ type AttachSchemaTransformer struct { func (t *AttachSchemaTransformer) Transform(g *Graph) error { + if t.Components == nil { + // Should never happen with a reasonable caller, but we'll return a + // proper error here anyway so that we'll fail gracefully. + return fmt.Errorf("AttachSchemaTransformer used with nil Components") + } + // First we'll figure out which provider types we need to fetch schemas for. needProviders := make(map[string]struct{}) for _, v := range g.Vertices() { switch tv := v.(type) { case GraphNodeAttachResourceSchema: providerAddr, _ := tv.ProvidedBy() + log.Printf("[TRACE] AttachSchemaTransformer: %q (%T) needs %s", dag.VertexName(v), v, providerAddr) needProviders[providerAddr.ProviderConfig.Type] = struct{}{} case GraphNodeAttachProviderConfigSchema: providerAddr := tv.ProviderAddr() + log.Printf("[TRACE] AttachSchemaTransformer: %q (%T) needs %s", dag.VertexName(v), v, providerAddr) needProviders[providerAddr.ProviderConfig.Type] = struct{}{} } }