github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/legacy/terraform/resource_provider.go (about) 1 package terraform 2 3 // ResourceProvider is a legacy interface for providers. 4 // 5 // This is retained only for compatibility with legacy code. The current 6 // interface for providers is providers.Interface, in the sibling directory 7 // named "providers". 8 type ResourceProvider interface { 9 /********************************************************************* 10 * Functions related to the provider 11 *********************************************************************/ 12 13 // ProviderSchema returns the config schema for the main provider 14 // configuration, as would appear in a "provider" block in the 15 // configuration files. 16 // 17 // Currently not all providers support schema. Callers must therefore 18 // first call Resources and DataSources and ensure that at least one 19 // resource or data source has the SchemaAvailable flag set. 20 GetSchema(*ProviderSchemaRequest) (*ProviderSchema, error) 21 22 // Input was used prior to v0.12 to ask the provider to prompt the user 23 // for input to complete the configuration. 24 // 25 // From v0.12 onwards this method is never called because Terraform Core 26 // is able to handle the necessary input logic itself based on the 27 // schema returned from GetSchema. 28 Input(UIInput, *ResourceConfig) (*ResourceConfig, error) 29 30 // Validate is called once at the beginning with the raw configuration 31 // (no interpolation done) and can return a list of warnings and/or 32 // errors. 33 // 34 // This is called once with the provider configuration only. It may not 35 // be called at all if no provider configuration is given. 36 // 37 // This should not assume that any values of the configurations are valid. 38 // The primary use case of this call is to check that required keys are 39 // set. 40 Validate(*ResourceConfig) ([]string, []error) 41 42 // Configure configures the provider itself with the configuration 43 // given. This is useful for setting things like access keys. 44 // 45 // This won't be called at all if no provider configuration is given. 46 // 47 // Configure returns an error if it occurred. 48 Configure(*ResourceConfig) error 49 50 // Resources returns all the available resource types that this provider 51 // knows how to manage. 52 Resources() []ResourceType 53 54 // Stop is called when the provider should halt any in-flight actions. 55 // 56 // This can be used to make a nicer Ctrl-C experience for Terraform. 57 // Even if this isn't implemented to do anything (just returns nil), 58 // Terraform will still cleanly stop after the currently executing 59 // graph node is complete. However, this API can be used to make more 60 // efficient halts. 61 // 62 // Stop doesn't have to and shouldn't block waiting for in-flight actions 63 // to complete. It should take any action it wants and return immediately 64 // acknowledging it has received the stop request. Terraform core will 65 // automatically not make any further API calls to the provider soon 66 // after Stop is called (technically exactly once the currently executing 67 // graph nodes are complete). 68 // 69 // The error returned, if non-nil, is assumed to mean that signaling the 70 // stop somehow failed and that the user should expect potentially waiting 71 // a longer period of time. 72 Stop() error 73 74 /********************************************************************* 75 * Functions related to individual resources 76 *********************************************************************/ 77 78 // ValidateResource is called once at the beginning with the raw 79 // configuration (no interpolation done) and can return a list of warnings 80 // and/or errors. 81 // 82 // This is called once per resource. 83 // 84 // This should not assume any of the values in the resource configuration 85 // are valid since it is possible they have to be interpolated still. 86 // The primary use case of this call is to check that the required keys 87 // are set and that the general structure is correct. 88 ValidateResource(string, *ResourceConfig) ([]string, []error) 89 90 // Apply applies a diff to a specific resource and returns the new 91 // resource state along with an error. 92 // 93 // If the resource state given has an empty ID, then a new resource 94 // is expected to be created. 95 Apply( 96 *InstanceInfo, 97 *InstanceState, 98 *InstanceDiff) (*InstanceState, error) 99 100 // Diff diffs a resource versus a desired state and returns 101 // a diff. 102 Diff( 103 *InstanceInfo, 104 *InstanceState, 105 *ResourceConfig) (*InstanceDiff, error) 106 107 // Refresh refreshes a resource and updates all of its attributes 108 // with the latest information. 109 Refresh(*InstanceInfo, *InstanceState) (*InstanceState, error) 110 111 /********************************************************************* 112 * Functions related to importing 113 *********************************************************************/ 114 115 // ImportState requests that the given resource be imported. 116 // 117 // The returned InstanceState only requires ID be set. Importing 118 // will always call Refresh after the state to complete it. 119 // 120 // IMPORTANT: InstanceState doesn't have the resource type attached 121 // to it. A type must be specified on the state via the Ephemeral 122 // field on the state. 123 // 124 // This function can return multiple states. Normally, an import 125 // will map 1:1 to a physical resource. However, some resources map 126 // to multiple. For example, an AWS security group may contain many rules. 127 // Each rule is represented by a separate resource in Terraform, 128 // therefore multiple states are returned. 129 ImportState(*InstanceInfo, string) ([]*InstanceState, error) 130 131 /********************************************************************* 132 * Functions related to data resources 133 *********************************************************************/ 134 135 // ValidateDataSource is called once at the beginning with the raw 136 // configuration (no interpolation done) and can return a list of warnings 137 // and/or errors. 138 // 139 // This is called once per data source instance. 140 // 141 // This should not assume any of the values in the resource configuration 142 // are valid since it is possible they have to be interpolated still. 143 // The primary use case of this call is to check that the required keys 144 // are set and that the general structure is correct. 145 ValidateDataSource(string, *ResourceConfig) ([]string, []error) 146 147 // DataSources returns all of the available data sources that this 148 // provider implements. 149 DataSources() []DataSource 150 151 // ReadDataDiff produces a diff that represents the state that will 152 // be produced when the given data source is read using a later call 153 // to ReadDataApply. 154 ReadDataDiff(*InstanceInfo, *ResourceConfig) (*InstanceDiff, error) 155 156 // ReadDataApply initializes a data instance using the configuration 157 // in a diff produced by ReadDataDiff. 158 ReadDataApply(*InstanceInfo, *InstanceDiff) (*InstanceState, error) 159 } 160 161 // ResourceProviderCloser is an interface that providers that can close 162 // connections that aren't needed anymore must implement. 163 type ResourceProviderCloser interface { 164 Close() error 165 } 166 167 // ResourceType is a type of resource that a resource provider can manage. 168 type ResourceType struct { 169 Name string // Name of the resource, example "instance" (no provider prefix) 170 Importable bool // Whether this resource supports importing 171 172 // SchemaAvailable is set if the provider supports the ProviderSchema, 173 // ResourceTypeSchema and DataSourceSchema methods. Although it is 174 // included on each resource type, it's actually a provider-wide setting 175 // that's smuggled here only because that avoids a breaking change to 176 // the plugin protocol. 177 SchemaAvailable bool 178 } 179 180 // DataSource is a data source that a resource provider implements. 181 type DataSource struct { 182 Name string 183 184 // SchemaAvailable is set if the provider supports the ProviderSchema, 185 // ResourceTypeSchema and DataSourceSchema methods. Although it is 186 // included on each resource type, it's actually a provider-wide setting 187 // that's smuggled here only because that avoids a breaking change to 188 // the plugin protocol. 189 SchemaAvailable bool 190 } 191 192 // ResourceProviderFactory is a function type that creates a new instance 193 // of a resource provider. 194 type ResourceProviderFactory func() (ResourceProvider, error) 195 196 // ResourceProviderFactoryFixed is a helper that creates a 197 // ResourceProviderFactory that just returns some fixed provider. 198 func ResourceProviderFactoryFixed(p ResourceProvider) ResourceProviderFactory { 199 return func() (ResourceProvider, error) { 200 return p, nil 201 } 202 } 203 204 func ProviderHasResource(p ResourceProvider, n string) bool { 205 for _, rt := range p.Resources() { 206 if rt.Name == n { 207 return true 208 } 209 } 210 211 return false 212 } 213 214 func ProviderHasDataSource(p ResourceProvider, n string) bool { 215 for _, rt := range p.DataSources() { 216 if rt.Name == n { 217 return true 218 } 219 } 220 221 return false 222 } 223 224 const errPluginInit = ` 225 Plugin reinitialization required. Please run "terraform init". 226 227 Plugins are external binaries that Terraform uses to access and manipulate 228 resources. The configuration provided requires plugins which can't be located, 229 don't satisfy the version constraints, or are otherwise incompatible. 230 231 Terraform automatically discovers provider requirements from your 232 configuration, including providers used in child modules. To see the 233 requirements and constraints, run "terraform providers". 234 235 %s 236 `