github.com/opentofu/opentofu@v1.7.1/internal/legacy/tofu/context_components.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package tofu 7 8 import ( 9 "fmt" 10 11 "github.com/opentofu/opentofu/internal/addrs" 12 "github.com/opentofu/opentofu/internal/providers" 13 "github.com/opentofu/opentofu/internal/provisioners" 14 ) 15 16 // contextComponentFactory is the interface that Context uses 17 // to initialize various components such as providers and provisioners. 18 // This factory gets more information than the raw maps using to initialize 19 // a Context. This information is used for debugging. 20 type contextComponentFactory interface { 21 // ResourceProvider creates a new ResourceProvider with the given type. 22 ResourceProvider(typ addrs.Provider) (providers.Interface, error) 23 ResourceProviders() []string 24 25 // ResourceProvisioner creates a new ResourceProvisioner with the given 26 // type. 27 ResourceProvisioner(typ string) (provisioners.Interface, error) 28 ResourceProvisioners() []string 29 } 30 31 // basicComponentFactory just calls a factory from a map directly. 32 type basicComponentFactory struct { 33 providers map[addrs.Provider]providers.Factory 34 provisioners map[string]ProvisionerFactory 35 } 36 37 func (c *basicComponentFactory) ResourceProviders() []string { 38 var result []string 39 for k := range c.providers { 40 result = append(result, k.String()) 41 } 42 return result 43 } 44 45 func (c *basicComponentFactory) ResourceProvisioners() []string { 46 var result []string 47 for k := range c.provisioners { 48 result = append(result, k) 49 } 50 51 return result 52 } 53 54 func (c *basicComponentFactory) ResourceProvider(typ addrs.Provider) (providers.Interface, error) { 55 f, ok := c.providers[typ] 56 if !ok { 57 return nil, fmt.Errorf("unknown provider %q", typ.String()) 58 } 59 60 return f() 61 } 62 63 func (c *basicComponentFactory) ResourceProvisioner(typ string) (provisioners.Interface, error) { 64 f, ok := c.provisioners[typ] 65 if !ok { 66 return nil, fmt.Errorf("unknown provisioner %q", typ) 67 } 68 69 return f() 70 }