github.com/opentofu/opentofu@v1.7.1/internal/providers/provider.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package providers 7 8 import ( 9 "github.com/zclconf/go-cty/cty" 10 11 "github.com/opentofu/opentofu/internal/configs/configschema" 12 "github.com/opentofu/opentofu/internal/states" 13 "github.com/opentofu/opentofu/internal/tfdiags" 14 ) 15 16 // Interface represents the set of methods required for a complete resource 17 // provider plugin. 18 type Interface interface { 19 // GetMetadata is not yet implemented or used at this time. It may 20 // be used in the future to avoid loading a provider's full schema 21 // for initial validation. This could result in some potential 22 // memory savings. 23 24 // GetSchema returns the complete schema for the provider. 25 GetProviderSchema() GetProviderSchemaResponse 26 27 // ValidateProviderConfig allows the provider to validate the configuration. 28 // The ValidateProviderConfigResponse.PreparedConfig field is unused. The 29 // final configuration is not stored in the state, and any modifications 30 // that need to be made must be made during the Configure method call. 31 ValidateProviderConfig(ValidateProviderConfigRequest) ValidateProviderConfigResponse 32 33 // ValidateResourceConfig allows the provider to validate the resource 34 // configuration values. 35 ValidateResourceConfig(ValidateResourceConfigRequest) ValidateResourceConfigResponse 36 37 // ValidateDataResourceConfig allows the provider to validate the data source 38 // configuration values. 39 ValidateDataResourceConfig(ValidateDataResourceConfigRequest) ValidateDataResourceConfigResponse 40 41 // UpgradeResourceState is called when the state loader encounters an 42 // instance state whose schema version is less than the one reported by the 43 // currently-used version of the corresponding provider, and the upgraded 44 // result is used for any further processing. 45 UpgradeResourceState(UpgradeResourceStateRequest) UpgradeResourceStateResponse 46 47 // Configure configures and initialized the provider. 48 ConfigureProvider(ConfigureProviderRequest) ConfigureProviderResponse 49 50 // Stop is called when the provider should halt any in-flight actions. 51 // 52 // Stop should not block waiting for in-flight actions to complete. It 53 // should take any action it wants and return immediately acknowledging it 54 // has received the stop request. OpenTofu will not make any further API 55 // calls to the provider after Stop is called. 56 // 57 // The error returned, if non-nil, is assumed to mean that signaling the 58 // stop somehow failed and that the user should expect potentially waiting 59 // a longer period of time. 60 Stop() error 61 62 // ReadResource refreshes a resource and returns its current state. 63 ReadResource(ReadResourceRequest) ReadResourceResponse 64 65 // PlanResourceChange takes the current state and proposed state of a 66 // resource, and returns the planned final state. 67 PlanResourceChange(PlanResourceChangeRequest) PlanResourceChangeResponse 68 69 // ApplyResourceChange takes the planned state for a resource, which may 70 // yet contain unknown computed values, and applies the changes returning 71 // the final state. 72 ApplyResourceChange(ApplyResourceChangeRequest) ApplyResourceChangeResponse 73 74 // ImportResourceState requests that the given resource be imported. 75 ImportResourceState(ImportResourceStateRequest) ImportResourceStateResponse 76 77 // ReadDataSource returns the data source's current state. 78 ReadDataSource(ReadDataSourceRequest) ReadDataSourceResponse 79 80 // GetFunctions returns a full list of functions defined in this provider. It should be a super 81 // set of the functions returned in GetProviderSchema() 82 GetFunctions() GetFunctionsResponse 83 84 // CallFunction requests that the given function is called and response returned. 85 CallFunction(CallFunctionRequest) CallFunctionResponse 86 87 // Close shuts down the plugin process if applicable. 88 Close() error 89 } 90 91 // GetProviderSchemaResponse is the return type for GetProviderSchema, and 92 // should only be used when handling a value for that method. The handling of 93 // of schemas in any other context should always use ProviderSchema, so that 94 // the in-memory representation can be more easily changed separately from the 95 // RCP protocol. 96 type GetProviderSchemaResponse struct { 97 // Provider is the schema for the provider itself. 98 Provider Schema 99 100 // ProviderMeta is the schema for the provider's meta info in a module 101 ProviderMeta Schema 102 103 // ResourceTypes map the resource type name to that type's schema. 104 ResourceTypes map[string]Schema 105 106 // DataSources maps the data source name to that data source's schema. 107 DataSources map[string]Schema 108 109 // Diagnostics contains any warnings or errors from the method call. 110 Diagnostics tfdiags.Diagnostics 111 112 // ServerCapabilities lists optional features supported by the provider. 113 ServerCapabilities ServerCapabilities 114 115 // Functions lists all functions supported by this provider. 116 Functions map[string]FunctionSpec 117 } 118 119 // Schema pairs a provider or resource schema with that schema's version. 120 // This is used to be able to upgrade the schema in UpgradeResourceState. 121 // 122 // This describes the schema for a single object within a provider. Type 123 // "Schemas" (plural) instead represents the overall collection of schemas 124 // for everything within a particular provider. 125 type Schema struct { 126 Version int64 127 Block *configschema.Block 128 } 129 130 // ServerCapabilities allows providers to communicate extra information 131 // regarding supported protocol features. This is used to indicate availability 132 // of certain forward-compatible changes which may be optional in a major 133 // protocol version, but cannot be tested for directly. 134 type ServerCapabilities struct { 135 // PlanDestroy signals that this provider expects to receive a 136 // PlanResourceChange call for resources that are to be destroyed. 137 PlanDestroy bool 138 139 // The GetProviderSchemaOptional capability indicates that this 140 // provider does not require calling GetProviderSchema to operate 141 // normally, and the caller can used a cached copy of the provider's 142 // schema. 143 // In other words, the providers for which GetProviderSchemaOptional is false 144 // require their schema to be read after EVERY instantiation to function normally. 145 GetProviderSchemaOptional bool 146 } 147 148 type FunctionSpec struct { 149 // List of parameters required to call the function 150 Parameters []FunctionParameterSpec 151 // Optional Spec for variadic parameters 152 VariadicParameter *FunctionParameterSpec 153 // Type which the function will return 154 Return cty.Type 155 // Human-readable shortened documentation for the function 156 Summary string 157 // Human-readable documentation for the function 158 Description string 159 // Formatting type of the Description field 160 DescriptionFormat TextFormatting 161 // Human-readable message present if the function is deprecated 162 DeprecationMessage string 163 } 164 165 type FunctionParameterSpec struct { 166 // Human-readable display name for the parameter 167 Name string 168 // Type constraint for the parameter 169 Type cty.Type 170 // Null values alowed for the parameter 171 AllowNullValue bool 172 // Unknown Values allowed for the parameter 173 // Implies the Return type of the function is also Unknown 174 AllowUnknownValues bool 175 // Human-readable documentation for the parameter 176 Description string 177 // Formatting type of the Description field 178 DescriptionFormat TextFormatting 179 } 180 181 type TextFormatting string 182 183 const TextFormattingPlain = TextFormatting("Plain") 184 const TextFormattingMarkdown = TextFormatting("Markdown") 185 186 type ValidateProviderConfigRequest struct { 187 // Config is the raw configuration value for the provider. 188 Config cty.Value 189 } 190 191 type ValidateProviderConfigResponse struct { 192 // PreparedConfig is unused and will be removed with support for plugin protocol v5. 193 PreparedConfig cty.Value 194 // Diagnostics contains any warnings or errors from the method call. 195 Diagnostics tfdiags.Diagnostics 196 } 197 198 type ValidateResourceConfigRequest struct { 199 // TypeName is the name of the resource type to validate. 200 TypeName string 201 202 // Config is the configuration value to validate, which may contain unknown 203 // values. 204 Config cty.Value 205 } 206 207 type ValidateResourceConfigResponse struct { 208 // Diagnostics contains any warnings or errors from the method call. 209 Diagnostics tfdiags.Diagnostics 210 } 211 212 type ValidateDataResourceConfigRequest struct { 213 // TypeName is the name of the data source type to validate. 214 TypeName string 215 216 // Config is the configuration value to validate, which may contain unknown 217 // values. 218 Config cty.Value 219 } 220 221 type ValidateDataResourceConfigResponse struct { 222 // Diagnostics contains any warnings or errors from the method call. 223 Diagnostics tfdiags.Diagnostics 224 } 225 226 type UpgradeResourceStateRequest struct { 227 // TypeName is the name of the resource type being upgraded 228 TypeName string 229 230 // Version is version of the schema that created the current state. 231 Version int64 232 233 // RawStateJSON and RawStateFlatmap contiain the state that needs to be 234 // upgraded to match the current schema version. Because the schema is 235 // unknown, this contains only the raw data as stored in the state. 236 // RawStateJSON is the current json state encoding. 237 // RawStateFlatmap is the legacy flatmap encoding. 238 // Only on of these fields may be set for the upgrade request. 239 RawStateJSON []byte 240 RawStateFlatmap map[string]string 241 } 242 243 type UpgradeResourceStateResponse struct { 244 // UpgradedState is the newly upgraded resource state. 245 UpgradedState cty.Value 246 247 // Diagnostics contains any warnings or errors from the method call. 248 Diagnostics tfdiags.Diagnostics 249 } 250 251 type ConfigureProviderRequest struct { 252 // OpenTofu version is the version string from the running instance of 253 // tofu. Providers can use TerraformVersion to verify compatibility, 254 // and to store for informational purposes. 255 TerraformVersion string 256 257 // Config is the complete configuration value for the provider. 258 Config cty.Value 259 } 260 261 type ConfigureProviderResponse struct { 262 // Diagnostics contains any warnings or errors from the method call. 263 Diagnostics tfdiags.Diagnostics 264 } 265 266 type ReadResourceRequest struct { 267 // TypeName is the name of the resource type being read. 268 TypeName string 269 270 // PriorState contains the previously saved state value for this resource. 271 PriorState cty.Value 272 273 // Private is an opaque blob that will be stored in state along with the 274 // resource. It is intended only for interpretation by the provider itself. 275 Private []byte 276 277 // ProviderMeta is the configuration for the provider_meta block for the 278 // module and provider this resource belongs to. Its use is defined by 279 // each provider, and it should not be used without coordination with 280 // HashiCorp. It is considered experimental and subject to change. 281 ProviderMeta cty.Value 282 } 283 284 type ReadResourceResponse struct { 285 // NewState contains the current state of the resource. 286 NewState cty.Value 287 288 // Diagnostics contains any warnings or errors from the method call. 289 Diagnostics tfdiags.Diagnostics 290 291 // Private is an opaque blob that will be stored in state along with the 292 // resource. It is intended only for interpretation by the provider itself. 293 Private []byte 294 } 295 296 type PlanResourceChangeRequest struct { 297 // TypeName is the name of the resource type to plan. 298 TypeName string 299 300 // PriorState is the previously saved state value for this resource. 301 PriorState cty.Value 302 303 // ProposedNewState is the expected state after the new configuration is 304 // applied. This is created by directly applying the configuration to the 305 // PriorState. The provider is then responsible for applying any further 306 // changes required to create the proposed final state. 307 ProposedNewState cty.Value 308 309 // Config is the resource configuration, before being merged with the 310 // PriorState. Any value not explicitly set in the configuration will be 311 // null. Config is supplied for reference, but Provider implementations 312 // should prefer the ProposedNewState in most circumstances. 313 Config cty.Value 314 315 // PriorPrivate is the previously saved private data returned from the 316 // provider during the last apply. 317 PriorPrivate []byte 318 319 // ProviderMeta is the configuration for the provider_meta block for the 320 // module and provider this resource belongs to. Its use is defined by 321 // each provider, and it should not be used without coordination with 322 // HashiCorp. It is considered experimental and subject to change. 323 ProviderMeta cty.Value 324 } 325 326 type PlanResourceChangeResponse struct { 327 // PlannedState is the expected state of the resource once the current 328 // configuration is applied. 329 PlannedState cty.Value 330 331 // RequiresReplace is the list of the attributes that are requiring 332 // resource replacement. 333 RequiresReplace []cty.Path 334 335 // PlannedPrivate is an opaque blob that is not interpreted by tofu 336 // core. This will be saved and relayed back to the provider during 337 // ApplyResourceChange. 338 PlannedPrivate []byte 339 340 // Diagnostics contains any warnings or errors from the method call. 341 Diagnostics tfdiags.Diagnostics 342 343 // LegacyTypeSystem is set only if the provider is using the legacy SDK 344 // whose type system cannot be precisely mapped into the OpenTofu type 345 // system. We use this to bypass certain consistency checks that would 346 // otherwise fail due to this imprecise mapping. No other provider or SDK 347 // implementation is permitted to set this. 348 LegacyTypeSystem bool 349 } 350 351 type ApplyResourceChangeRequest struct { 352 // TypeName is the name of the resource type being applied. 353 TypeName string 354 355 // PriorState is the current state of resource. 356 PriorState cty.Value 357 358 // Planned state is the state returned from PlanResourceChange, and should 359 // represent the new state, minus any remaining computed attributes. 360 PlannedState cty.Value 361 362 // Config is the resource configuration, before being merged with the 363 // PriorState. Any value not explicitly set in the configuration will be 364 // null. Config is supplied for reference, but Provider implementations 365 // should prefer the PlannedState in most circumstances. 366 Config cty.Value 367 368 // PlannedPrivate is the same value as returned by PlanResourceChange. 369 PlannedPrivate []byte 370 371 // ProviderMeta is the configuration for the provider_meta block for the 372 // module and provider this resource belongs to. Its use is defined by 373 // each provider, and it should not be used without coordination with 374 // HashiCorp. It is considered experimental and subject to change. 375 ProviderMeta cty.Value 376 } 377 378 type ApplyResourceChangeResponse struct { 379 // NewState is the new complete state after applying the planned change. 380 // In the event of an error, NewState should represent the most recent 381 // known state of the resource, if it exists. 382 NewState cty.Value 383 384 // Private is an opaque blob that will be stored in state along with the 385 // resource. It is intended only for interpretation by the provider itself. 386 Private []byte 387 388 // Diagnostics contains any warnings or errors from the method call. 389 Diagnostics tfdiags.Diagnostics 390 391 // LegacyTypeSystem is set only if the provider is using the legacy SDK 392 // whose type system cannot be precisely mapped into the OpenTofu type 393 // system. We use this to bypass certain consistency checks that would 394 // otherwise fail due to this imprecise mapping. No other provider or SDK 395 // implementation is permitted to set this. 396 LegacyTypeSystem bool 397 } 398 399 type ImportResourceStateRequest struct { 400 // TypeName is the name of the resource type to be imported. 401 TypeName string 402 403 // ID is a string with which the provider can identify the resource to be 404 // imported. 405 ID string 406 } 407 408 type ImportResourceStateResponse struct { 409 // ImportedResources contains one or more state values related to the 410 // imported resource. It is not required that these be complete, only that 411 // there is enough identifying information for the provider to successfully 412 // update the states in ReadResource. 413 ImportedResources []ImportedResource 414 415 // Diagnostics contains any warnings or errors from the method call. 416 Diagnostics tfdiags.Diagnostics 417 } 418 419 // ImportedResource represents an object being imported into OpenTofu with the 420 // help of a provider. An ImportedObject is a RemoteObject that has been read 421 // by the provider's import handler but hasn't yet been committed to state. 422 type ImportedResource struct { 423 // TypeName is the name of the resource type associated with the 424 // returned state. It's possible for providers to import multiple related 425 // types with a single import request. 426 TypeName string 427 428 // State is the state of the remote object being imported. This may not be 429 // complete, but must contain enough information to uniquely identify the 430 // resource. 431 State cty.Value 432 433 // Private is an opaque blob that will be stored in state along with the 434 // resource. It is intended only for interpretation by the provider itself. 435 Private []byte 436 } 437 438 // AsInstanceObject converts the receiving ImportedObject into a 439 // ResourceInstanceObject that has status ObjectReady. 440 // 441 // The returned object does not know its own resource type, so the caller must 442 // retain the ResourceType value from the source object if this information is 443 // needed. 444 // 445 // The returned object also has no dependency addresses, but the caller may 446 // freely modify the direct fields of the returned object without affecting 447 // the receiver. 448 func (ir ImportedResource) AsInstanceObject() *states.ResourceInstanceObject { 449 return &states.ResourceInstanceObject{ 450 Status: states.ObjectReady, 451 Value: ir.State, 452 Private: ir.Private, 453 } 454 } 455 456 type ReadDataSourceRequest struct { 457 // TypeName is the name of the data source type to Read. 458 TypeName string 459 460 // Config is the complete configuration for the requested data source. 461 Config cty.Value 462 463 // ProviderMeta is the configuration for the provider_meta block for the 464 // module and provider this resource belongs to. Its use is defined by 465 // each provider, and it should not be used without coordination with 466 // HashiCorp. It is considered experimental and subject to change. 467 ProviderMeta cty.Value 468 } 469 470 type ReadDataSourceResponse struct { 471 // State is the current state of the requested data source. 472 State cty.Value 473 474 // Diagnostics contains any warnings or errors from the method call. 475 Diagnostics tfdiags.Diagnostics 476 } 477 478 type GetFunctionsResponse struct { 479 Functions map[string]FunctionSpec 480 481 Diagnostics tfdiags.Diagnostics 482 } 483 484 type CallFunctionRequest struct { 485 Name string 486 Arguments []cty.Value 487 } 488 489 type CallFunctionResponse struct { 490 Result cty.Value 491 Error error 492 } 493 494 type CallFunctionArgumentError struct { 495 Text string 496 FunctionArgument int 497 } 498 499 func (err *CallFunctionArgumentError) Error() string { 500 return err.Text 501 }