2016-10-04 03:23:37 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-08-16 17:40:08 +02:00
|
|
|
|
2019-12-04 17:30:20 +01:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-08-16 17:40:08 +02:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
2018-08-17 18:42:07 +02:00
|
|
|
"github.com/hashicorp/terraform/provisioners"
|
2016-10-04 03:23:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// contextComponentFactory is the interface that Context uses
|
|
|
|
// to initialize various components such as providers and provisioners.
|
|
|
|
// This factory gets more information than the raw maps using to initialize
|
|
|
|
// a Context. This information is used for debugging.
|
|
|
|
type contextComponentFactory interface {
|
2020-02-05 21:45:26 +01:00
|
|
|
// ResourceProvider creates a new ResourceProvider with the given type.
|
2020-02-14 15:53:55 +01:00
|
|
|
ResourceProvider(typ addrs.Provider) (providers.Interface, error)
|
2016-10-04 03:23:37 +02:00
|
|
|
ResourceProviders() []string
|
|
|
|
|
2020-02-05 21:45:26 +01:00
|
|
|
// ResourceProvisioner creates a new ResourceProvisioner with the given
|
|
|
|
// type.
|
|
|
|
ResourceProvisioner(typ string) (provisioners.Interface, error)
|
2016-10-04 03:23:37 +02:00
|
|
|
ResourceProvisioners() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// basicComponentFactory just calls a factory from a map directly.
|
|
|
|
type basicComponentFactory struct {
|
2019-12-04 17:30:20 +01:00
|
|
|
providers map[addrs.Provider]providers.Factory
|
2018-08-17 18:42:07 +02:00
|
|
|
provisioners map[string]ProvisionerFactory
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *basicComponentFactory) ResourceProviders() []string {
|
2019-12-06 14:00:18 +01:00
|
|
|
var result []string
|
2018-10-02 22:06:12 +02:00
|
|
|
for k := range c.providers {
|
2019-12-04 17:30:20 +01:00
|
|
|
result = append(result, k.LegacyString())
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *basicComponentFactory) ResourceProvisioners() []string {
|
2019-12-06 14:00:18 +01:00
|
|
|
var result []string
|
2018-10-02 22:06:12 +02:00
|
|
|
for k := range c.provisioners {
|
2016-10-04 03:23:37 +02:00
|
|
|
result = append(result, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-14 15:53:55 +01:00
|
|
|
func (c *basicComponentFactory) ResourceProvider(typ addrs.Provider) (providers.Interface, error) {
|
|
|
|
f, ok := c.providers[typ]
|
2016-10-04 03:23:37 +02:00
|
|
|
if !ok {
|
2020-02-14 15:53:55 +01:00
|
|
|
return nil, fmt.Errorf("unknown provider %q", typ.LegacyString())
|
2016-10-04 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return f()
|
|
|
|
}
|
|
|
|
|
2020-02-05 21:45:26 +01:00
|
|
|
func (c *basicComponentFactory) ResourceProvisioner(typ string) (provisioners.Interface, error) {
|
2016-10-04 03:23:37 +02:00
|
|
|
f, ok := c.provisioners[typ]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown provisioner %q", typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f()
|
|
|
|
}
|