2023-07-03 11:37:50 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
2023-07-03 11:37:50 -05:00
|
|
|
)
|
|
|
|
|
2023-07-05 16:27:16 -05:00
|
|
|
// SchemaCache is a global cache of Schemas.
|
2023-07-03 11:37:50 -05:00
|
|
|
// This will be accessed by both core and the provider clients to ensure that
|
|
|
|
// large schemas are stored in a single location.
|
|
|
|
var SchemaCache = &schemaCache{
|
2023-07-06 09:35:33 -05:00
|
|
|
m: make(map[addrs.Provider]ProviderSchema),
|
2023-07-03 11:37:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Global cache for provider schemas
|
|
|
|
// Cache the entire response to ensure we capture any new fields, like
|
|
|
|
// ServerCapabilities. This also serves to capture errors so that multiple
|
|
|
|
// concurrent calls resulting in an error can be handled in the same manner.
|
|
|
|
type schemaCache struct {
|
|
|
|
mu sync.Mutex
|
2023-07-06 09:35:33 -05:00
|
|
|
m map[addrs.Provider]ProviderSchema
|
2023-07-03 11:37:50 -05:00
|
|
|
}
|
|
|
|
|
2023-07-06 09:35:33 -05:00
|
|
|
func (c *schemaCache) Set(p addrs.Provider, s ProviderSchema) {
|
2023-07-03 11:37:50 -05:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
c.m[p] = s
|
|
|
|
}
|
|
|
|
|
2023-07-06 09:35:33 -05:00
|
|
|
func (c *schemaCache) Get(p addrs.Provider) (ProviderSchema, bool) {
|
2023-07-03 11:37:50 -05:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
s, ok := c.m[p]
|
|
|
|
return s, ok
|
|
|
|
}
|