mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 17:01:04 -06:00
549aede792
Back when we first introduced provider versioning in Terraform 0.10, we did the provider version resolution in terraform.NewContext because we weren't sure yet how exactly our versioning model was going to play out (whether different versions could be selected per provider configuration, for example) and because we were building around the limitations of our existing filesystem-based plugin discovery model. However, the new installer codepath is new able to do all of the selections up front during installation, so we don't need such a heavy inversion of control abstraction to get this done: the command package can select the exact provider versions and pass their factories directly to terraform.NewContext as a simple static map. The result of this commit is that CLI commands other than "init" are now able to consume the local cache directory and selections produced by the installation process in "terraform init", passing all of the selected providers down to the terraform.NewContext function for use in implementing the main operations. This commit is just enough to get the providers passing into the terraform.Context. There's still plenty more to do here, including to repair all of the tests this change has additionally broken.
64 lines
2.4 KiB
Go
64 lines
2.4 KiB
Go
package providers
|
|
|
|
// Factory is a function type that creates a new instance of a resource
|
|
// provider, or returns an error if that is impossible.
|
|
type Factory func() (Interface, error)
|
|
|
|
// FactoryFixed is a helper that creates a Factory that just returns some given
|
|
// single provider.
|
|
//
|
|
// Unlike usual factories, the exact same instance is returned for each call
|
|
// to the factory and so this must be used in only specialized situations where
|
|
// the caller can take care to either not mutate the given provider at all
|
|
// or to mutate it in ways that will not cause unexpected behavior for others
|
|
// holding the same reference.
|
|
func FactoryFixed(p Interface) Factory {
|
|
return func() (Interface, error) {
|
|
return p, nil
|
|
}
|
|
}
|
|
|
|
// ProviderHasResource is a helper that requests schema from the given provider
|
|
// and checks if it has a resource type of the given name.
|
|
//
|
|
// This function is more expensive than it may first appear since it must
|
|
// retrieve the entire schema from the underlying provider, and so it should
|
|
// be used sparingly and especially not in tight loops.
|
|
//
|
|
// Since retrieving the provider may fail (e.g. if the provider is accessed
|
|
// over an RPC channel that has operational problems), this function will
|
|
// return false if the schema cannot be retrieved, under the assumption that
|
|
// a subsequent call to do anything with the resource type would fail
|
|
// anyway.
|
|
func ProviderHasResource(provider Interface, typeName string) bool {
|
|
resp := provider.GetSchema()
|
|
if resp.Diagnostics.HasErrors() {
|
|
return false
|
|
}
|
|
|
|
_, exists := resp.ResourceTypes[typeName]
|
|
return exists
|
|
}
|
|
|
|
// ProviderHasDataSource is a helper that requests schema from the given
|
|
// provider and checks if it has a data source of the given name.
|
|
//
|
|
// This function is more expensive than it may first appear since it must
|
|
// retrieve the entire schema from the underlying provider, and so it should
|
|
// be used sparingly and especially not in tight loops.
|
|
//
|
|
// Since retrieving the provider may fail (e.g. if the provider is accessed
|
|
// over an RPC channel that has operational problems), this function will
|
|
// return false if the schema cannot be retrieved, under the assumption that
|
|
// a subsequent call to do anything with the data source would fail
|
|
// anyway.
|
|
func ProviderHasDataSource(provider Interface, dataSourceName string) bool {
|
|
resp := provider.GetSchema()
|
|
if resp.Diagnostics.HasErrors() {
|
|
return false
|
|
}
|
|
|
|
_, exists := resp.DataSources[dataSourceName]
|
|
return exists
|
|
}
|