github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 // ResourceProviderCloser is an interface that providers that can close 75 // connections that aren't needed anymore must implement. 76 type ResourceProviderCloser interface { 77 Close() error 78 } 79 80 // ResourceType is a type of resource that a resource provider can manage. 81 type ResourceType struct { 82 Name string 83 } 84 85 // ResourceProviderFactory is a function type that creates a new instance 86 // of a resource provider. 87 type ResourceProviderFactory func() (ResourceProvider, error) 88 89 // ResourceProviderFactoryFixed is a helper that creates a 90 // ResourceProviderFactory that just returns some fixed provider. 91 func ResourceProviderFactoryFixed(p ResourceProvider) ResourceProviderFactory { 92 return func() (ResourceProvider, error) { 93 return p, nil 94 } 95 } 96 97 func ProviderSatisfies(p ResourceProvider, n string) bool { 98 for _, rt := range p.Resources() { 99 if rt.Name == n { 100 return true 101 } 102 } 103 104 return false 105 }