github.com/kevinklinger/open_terraform@v1.3.6/noninternal/legacy/terraform/context_components.go (about)

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