github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/terraform/resource_provider.go (about)

     1  package terraform
     2  
     3  // ResourceProvider is an interface that must be implemented by any
     4  // resource provider: the thing that creates and manages the resources in
     5  // a Terraform configuration.
     6  type ResourceProvider interface {
     7  	// Input is called to ask the provider to ask the user for input
     8  	// for completing the configuration if necesarry.
     9  	//
    10  	// This may or may not be called, so resource provider writers shouldn't
    11  	// rely on this being available to set some default values for validate
    12  	// later. Example of a situation where this wouldn't be called is if
    13  	// the user is not using a TTY.
    14  	Input(UIInput, *ResourceConfig) (*ResourceConfig, error)
    15  
    16  	// Validate is called once at the beginning with the raw configuration
    17  	// (no interpolation done) and can return a list of warnings and/or
    18  	// errors.
    19  	//
    20  	// This is called once with the provider configuration only. It may not
    21  	// be called at all if no provider configuration is given.
    22  	//
    23  	// This should not assume that any values of the configurations are valid.
    24  	// The primary use case of this call is to check that required keys are
    25  	// set.
    26  	Validate(*ResourceConfig) ([]string, []error)
    27  
    28  	// ValidateResource is called once at the beginning with the raw
    29  	// configuration (no interpolation done) and can return a list of warnings
    30  	// and/or errors.
    31  	//
    32  	// This is called once per resource.
    33  	//
    34  	// This should not assume any of the values in the resource configuration
    35  	// are valid since it is possible they have to be interpolated still.
    36  	// The primary use case of this call is to check that the required keys
    37  	// are set and that the general structure is correct.
    38  	ValidateResource(string, *ResourceConfig) ([]string, []error)
    39  
    40  	// Configure configures the provider itself with the configuration
    41  	// given. This is useful for setting things like access keys.
    42  	//
    43  	// This won't be called at all if no provider configuration is given.
    44  	//
    45  	// Configure returns an error if it occurred.
    46  	Configure(*ResourceConfig) error
    47  
    48  	// Resources returns all the available resource types that this provider
    49  	// knows how to manage.
    50  	Resources() []ResourceType
    51  
    52  	// Apply applies a diff to a specific resource and returns the new
    53  	// resource state along with an error.
    54  	//
    55  	// If the resource state given has an empty ID, then a new resource
    56  	// is expected to be created.
    57  	Apply(
    58  		*InstanceInfo,
    59  		*InstanceState,
    60  		*InstanceDiff) (*InstanceState, error)
    61  
    62  	// Diff diffs a resource versus a desired state and returns
    63  	// a diff.
    64  	Diff(
    65  		*InstanceInfo,
    66  		*InstanceState,
    67  		*ResourceConfig) (*InstanceDiff, error)
    68  
    69  	// Refresh refreshes a resource and updates all of its attributes
    70  	// with the latest information.
    71  	Refresh(*InstanceInfo, *InstanceState) (*InstanceState, error)
    72  }
    73  
    74  // ResourceType is a type of resource that a resource provider can manage.
    75  type ResourceType struct {
    76  	Name string
    77  }
    78  
    79  // ResourceProviderFactory is a function type that creates a new instance
    80  // of a resource provider.
    81  type ResourceProviderFactory func() (ResourceProvider, error)
    82  
    83  // ResourceProviderFactoryFixed is a helper that creates a
    84  // ResourceProviderFactory that just returns some fixed provider.
    85  func ResourceProviderFactoryFixed(p ResourceProvider) ResourceProviderFactory {
    86  	return func() (ResourceProvider, error) {
    87  		return p, nil
    88  	}
    89  }
    90  
    91  func ProviderSatisfies(p ResourceProvider, n string) bool {
    92  	for _, rt := range p.Resources() {
    93  		if rt.Name == n {
    94  			return true
    95  		}
    96  	}
    97  
    98  	return false
    99  }