core: fail gracefully if provider schemas aren't available

The only reason these cases are arising right now is because we have tests
that haven't yet been updated to properly support schema, but it can't
hurt to add some robustness here to reduce the risk of real crashes.
This commit is contained in:
Martin Atkins 2018-05-04 19:43:28 -07:00
parent 5e8445b7a5
commit 0dd7677d1f
3 changed files with 26 additions and 3 deletions

View File

@ -190,8 +190,15 @@ func (ctx *BuiltinEvalContext) ConfigureProvider(addr addrs.ProviderConfig, cfg
diags = diags.Append(fmt.Errorf("%s not initialized", addr))
return diags
}
providerSchema := ctx.ProviderSchema(absAddr)
if providerSchema == nil {
diags = diags.Append(fmt.Errorf("schema for %s is not available", absAddr))
return diags
}
// FIXME: The provider API isn't yet updated to take a cty.Value directly.
rc := NewResourceConfigShimmed(cfg, ctx.ProviderSchema(absAddr).Provider)
rc := NewResourceConfigShimmed(cfg, providerSchema.Provider)
err := p.Configure(rc)
if err != nil {
diags = diags.Append(err)

View File

@ -314,6 +314,10 @@ type EvalValidateResource struct {
}
func (n *EvalValidateResource) Eval(ctx EvalContext) (interface{}, error) {
if n.ProviderSchema == nil {
return nil, fmt.Errorf("EvalValidateResource has nil schema for %s", n.Addr)
}
var diags tfdiags.Diagnostics
provider := *n.Provider
cfg := *n.Config

View File

@ -104,11 +104,16 @@ func (t *AttachSchemaTransformer) Transform(g *Graph) error {
typeName := addr.Resource.Type
providerAddr, _ := tv.ProvidedBy()
var schema *configschema.Block
providerSchema := schemas[providerAddr.ProviderConfig.Type]
if providerSchema == nil {
log.Printf("[ERROR] AttachSchemaTransformer: No schema available for %s because provider schema for %q is missing", addr, providerAddr.ProviderConfig.Type)
continue
}
switch mode {
case addrs.ManagedResourceMode:
schema = schemas[providerAddr.ProviderConfig.Type].ResourceTypes[typeName]
schema = providerSchema.ResourceTypes[typeName]
case addrs.DataResourceMode:
schema = schemas[providerAddr.ProviderConfig.Type].DataSources[typeName]
schema = providerSchema.DataSources[typeName]
}
if schema != nil {
log.Printf("[TRACE] AttachSchemaTransformer: attaching schema to %s", dag.VertexName(v))
@ -118,7 +123,14 @@ func (t *AttachSchemaTransformer) Transform(g *Graph) error {
}
case GraphNodeAttachProviderConfigSchema:
providerAddr := tv.ProviderAddr()
providerSchema := schemas[providerAddr.ProviderConfig.Type]
if providerSchema == nil {
log.Printf("[ERROR] AttachSchemaTransformer: No schema available for %s because the whole provider schema is missing", providerAddr)
continue
}
schema := schemas[providerAddr.ProviderConfig.Type].Provider
if schema != nil {
log.Printf("[TRACE] AttachSchemaTransformer: attaching schema to %s", dag.VertexName(v))
tv.AttachProviderConfigSchema(schema)