github.com/ljosa/terraform@v0.7.0-rc2.0.20160617205345-fe540b408f59/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 /********************************************************************* 8 * Functions related to the provider 9 *********************************************************************/ 10 11 // Input is called to ask the provider to ask the user for input 12 // for completing the configuration if necesarry. 13 // 14 // This may or may not be called, so resource provider writers shouldn't 15 // rely on this being available to set some default values for validate 16 // later. Example of a situation where this wouldn't be called is if 17 // the user is not using a TTY. 18 Input(UIInput, *ResourceConfig) (*ResourceConfig, error) 19 20 // Validate is called once at the beginning with the raw configuration 21 // (no interpolation done) and can return a list of warnings and/or 22 // errors. 23 // 24 // This is called once with the provider configuration only. It may not 25 // be called at all if no provider configuration is given. 26 // 27 // This should not assume that any values of the configurations are valid. 28 // The primary use case of this call is to check that required keys are 29 // set. 30 Validate(*ResourceConfig) ([]string, []error) 31 32 // Configure configures the provider itself with the configuration 33 // given. This is useful for setting things like access keys. 34 // 35 // This won't be called at all if no provider configuration is given. 36 // 37 // Configure returns an error if it occurred. 38 Configure(*ResourceConfig) error 39 40 // Resources returns all the available resource types that this provider 41 // knows how to manage. 42 Resources() []ResourceType 43 44 /********************************************************************* 45 * Functions related to individual resources 46 *********************************************************************/ 47 48 // ValidateResource is called once at the beginning with the raw 49 // configuration (no interpolation done) and can return a list of warnings 50 // and/or errors. 51 // 52 // This is called once per resource. 53 // 54 // This should not assume any of the values in the resource configuration 55 // are valid since it is possible they have to be interpolated still. 56 // The primary use case of this call is to check that the required keys 57 // are set and that the general structure is correct. 58 ValidateResource(string, *ResourceConfig) ([]string, []error) 59 60 // Apply applies a diff to a specific resource and returns the new 61 // resource state along with an error. 62 // 63 // If the resource state given has an empty ID, then a new resource 64 // is expected to be created. 65 Apply( 66 *InstanceInfo, 67 *InstanceState, 68 *InstanceDiff) (*InstanceState, error) 69 70 // Diff diffs a resource versus a desired state and returns 71 // a diff. 72 Diff( 73 *InstanceInfo, 74 *InstanceState, 75 *ResourceConfig) (*InstanceDiff, error) 76 77 // Refresh refreshes a resource and updates all of its attributes 78 // with the latest information. 79 Refresh(*InstanceInfo, *InstanceState) (*InstanceState, error) 80 81 /********************************************************************* 82 * Functions related to importing 83 *********************************************************************/ 84 85 // ImportState requests that the given resource be imported. 86 // 87 // The returned InstanceState only requires ID be set. Importing 88 // will always call Refresh after the state to complete it. 89 // 90 // IMPORTANT: InstanceState doesn't have the resource type attached 91 // to it. A type must be specified on the state via the Ephemeral 92 // field on the state. 93 // 94 // This function can return multiple states. Normally, an import 95 // will map 1:1 to a physical resource. However, some resources map 96 // to multiple. For example, an AWS security group may contain many rules. 97 // Each rule is represented by a separate resource in Terraform, 98 // therefore multiple states are returned. 99 ImportState(*InstanceInfo, string) ([]*InstanceState, error) 100 101 /********************************************************************* 102 * Functions related to data resources 103 *********************************************************************/ 104 105 // ValidateDataSource is called once at the beginning with the raw 106 // configuration (no interpolation done) and can return a list of warnings 107 // and/or errors. 108 // 109 // This is called once per data source instance. 110 // 111 // This should not assume any of the values in the resource configuration 112 // are valid since it is possible they have to be interpolated still. 113 // The primary use case of this call is to check that the required keys 114 // are set and that the general structure is correct. 115 ValidateDataSource(string, *ResourceConfig) ([]string, []error) 116 117 // DataSources returns all of the available data sources that this 118 // provider implements. 119 DataSources() []DataSource 120 121 // ReadDataDiff produces a diff that represents the state that will 122 // be produced when the given data source is read using a later call 123 // to ReadDataApply. 124 ReadDataDiff(*InstanceInfo, *ResourceConfig) (*InstanceDiff, error) 125 126 // ReadDataApply initializes a data instance using the configuration 127 // in a diff produced by ReadDataDiff. 128 ReadDataApply(*InstanceInfo, *InstanceDiff) (*InstanceState, error) 129 } 130 131 // ResourceProviderCloser is an interface that providers that can close 132 // connections that aren't needed anymore must implement. 133 type ResourceProviderCloser interface { 134 Close() error 135 } 136 137 // ResourceType is a type of resource that a resource provider can manage. 138 type ResourceType struct { 139 Name string // Name of the resource, example "instance" (no provider prefix) 140 Importable bool // Whether this resource supports importing 141 } 142 143 // DataSource is a data source that a resource provider implements. 144 type DataSource struct { 145 Name string 146 } 147 148 // ResourceProviderFactory is a function type that creates a new instance 149 // of a resource provider. 150 type ResourceProviderFactory func() (ResourceProvider, error) 151 152 // ResourceProviderFactoryFixed is a helper that creates a 153 // ResourceProviderFactory that just returns some fixed provider. 154 func ResourceProviderFactoryFixed(p ResourceProvider) ResourceProviderFactory { 155 return func() (ResourceProvider, error) { 156 return p, nil 157 } 158 } 159 160 func ProviderHasResource(p ResourceProvider, n string) bool { 161 for _, rt := range p.Resources() { 162 if rt.Name == n { 163 return true 164 } 165 } 166 167 return false 168 } 169 170 func ProviderHasDataSource(p ResourceProvider, n string) bool { 171 for _, rt := range p.DataSources() { 172 if rt.Name == n { 173 return true 174 } 175 } 176 177 return false 178 }