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