github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/context_components.go (about)

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