6 "github.com/hashicorp/terraform/plugin/discovery"
9 // Resolver is an interface implemented by objects that are able to resolve
10 // a given set of resource provider version constraints into Factory
12 type Resolver interface {
13 // Given a constraint map, return a Factory for each requested provider.
14 // If some or all of the constraints cannot be satisfied, return a non-nil
15 // slice of errors describing the problems.
16 ResolveProviders(reqd discovery.PluginRequirements) (map[string]Factory, []error)
19 // ResolverFunc wraps a callback function and turns it into a Resolver
20 // implementation, for convenience in situations where a function and its
21 // associated closure are sufficient as a resolver implementation.
22 type ResolverFunc func(reqd discovery.PluginRequirements) (map[string]Factory, []error)
24 // ResolveProviders implements Resolver by calling the
26 func (f ResolverFunc) ResolveProviders(reqd discovery.PluginRequirements) (map[string]Factory, []error) {
30 // ResolverFixed returns a Resolver that has a fixed set of provider factories
31 // provided by the caller. The returned resolver ignores version constraints
32 // entirely and just returns the given factory for each requested provider
35 // This function is primarily used in tests, to provide mock providers or
36 // in-process providers under test.
37 func ResolverFixed(factories map[string]Factory) Resolver {
38 return ResolverFunc(func(reqd discovery.PluginRequirements) (map[string]Factory, []error) {
39 ret := make(map[string]Factory, len(reqd))
41 for name := range reqd {
42 if factory, exists := factories[name]; exists {
45 errs = append(errs, fmt.Errorf("provider %q is not available", name))
52 // Factory is a function type that creates a new instance of a resource
53 // provider, or returns an error if that is impossible.
54 type Factory func() (Interface, error)
56 // FactoryFixed is a helper that creates a Factory that just returns some given
59 // Unlike usual factories, the exact same instance is returned for each call
60 // to the factory and so this must be used in only specialized situations where
61 // the caller can take care to either not mutate the given provider at all
62 // or to mutate it in ways that will not cause unexpected behavior for others
63 // holding the same reference.
64 func FactoryFixed(p Interface) Factory {
65 return func() (Interface, error) {
70 // ProviderHasResource is a helper that requests schema from the given provider
71 // and checks if it has a resource type of the given name.
73 // This function is more expensive than it may first appear since it must
74 // retrieve the entire schema from the underlying provider, and so it should
75 // be used sparingly and especially not in tight loops.
77 // Since retrieving the provider may fail (e.g. if the provider is accessed
78 // over an RPC channel that has operational problems), this function will
79 // return false if the schema cannot be retrieved, under the assumption that
80 // a subsequent call to do anything with the resource type would fail
82 func ProviderHasResource(provider Interface, typeName string) bool {
83 resp := provider.GetSchema()
84 if resp.Diagnostics.HasErrors() {
88 _, exists := resp.ResourceTypes[typeName]
92 // ProviderHasDataSource is a helper that requests schema from the given
93 // provider and checks if it has a data source of the given name.
95 // This function is more expensive than it may first appear since it must
96 // retrieve the entire schema from the underlying provider, and so it should
97 // be used sparingly and especially not in tight loops.
99 // Since retrieving the provider may fail (e.g. if the provider is accessed
100 // over an RPC channel that has operational problems), this function will
101 // return false if the schema cannot be retrieved, under the assumption that
102 // a subsequent call to do anything with the data source would fail
104 func ProviderHasDataSource(provider Interface, dataSourceName string) bool {
105 resp := provider.GetSchema()
106 if resp.Diagnostics.HasErrors() {
110 _, exists := resp.DataSources[dataSourceName]