github.com/ojiry/terraform@v0.8.2-0.20161218223921-e50cec712c4a/terraform/context_components.go (about)

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