Terraform提供程序CRUD API洞察力

huangapple go评论76阅读模式
英文:

terraform provider CRUD api insights

问题

我正在尝试创建一个 Terraform 插件,并且需要了解资源的生命周期。对于生命周期,我们有资源的 CRUD 方法。知道这四个方法何时被调用会非常有帮助。

根据我的理解:

  1. CRUD 中的 Read 方法将在计划阶段被调用。根据其结果,将决定是创建还是更新。

  2. 创建和更新将在应用阶段进行。

  3. 删除将在销毁阶段被调用。

然后,我尝试了文档中提供的示例,将 Read 函数保留为空,我发现即使没有 Read 功能,创建、更新和删除资源仍然正常工作。因此,Read 函数的用途仍然是个谜。

附注:在阅读文档时,我注意到 SetId() 和 ID() 似乎很重要,但我仍然无法理解它们的用途。

英文:

I am trying to create a terraform plugin and need to understand the life cycle of the resource.
For the life cycle we have the CRUD methods for a resource.
It would be really helpful to know when are these four called ?

// See Resource documentation.
type CreateContextFunc func(context.Context, *ResourceData, interface{}) diag.Diagnostics

// See Resource documentation.
type ReadContextFunc func(context.Context, *ResourceData, interface{}) diag.Diagnostics

// See Resource documentation.
type UpdateContextFunc func(context.Context, *ResourceData, interface{}) diag.Diagnostics

// See Resource documentation.
type DeleteContextFunc func(context.Context, *ResourceData, interface{}) diag.Diagnostics

yes I went through this.
My understanding after reading is below:

1. The Read of the CRUD will be called at the plan phase.
   On that basis it will be decided, it it is a create or update.

2. The create and update will be done on apply phase. 

3. The delete will be called on destroy phase. 

Then I tried out the example provided by keeping the read function empty and I can see that the create, update and deletion of resources works like charm even with the Read functionality also. So the use of Read function is still a mystery.

PS: While reading the documentation I can see that the SetId() and ID() seems important, but I am still not able to understand the use of them.

答案1

得分: 1

这个问题的翻译如下:

一种思考方式是理解Terraform的资源实例更改生命周期(从底层提供程序协议的角度编写),然后考虑每个协议级别调用如何与Terraform SDKv2功能对应。

以下是它们之间的关系总结:

  • ValidateResourceConfig在SDK中没有直接的类比,但它会导致各种各样的ValidateFunc函数按属性逐个运行。
  • PlanResourceChange对应于SDK中的CustomizeDiff
  • ApplyResourceChange根据计划的操作不同,在SDK中对应不同的函数:
    • 如果计划的操作是“将被创建”,SDK调用Create
    • 如果计划的操作是“将在原地更新”,SDK调用Update
    • 如果计划的操作是“将被销毁”,SDK调用Delete
    • 如果计划的操作是“必须被替换”,那实际上是一个创建后跟一个删除,或者反过来,因此ApplyResourceChange将有两个单独的调用,分别处理这两个更简单的操作。
  • UpgradeResourceState对应于状态迁移机制
  • ReadResource对应于SDK中的Read
  • ImportResourceState对应于Importer

这些类比不是_直接_的,因为SDKv2有意将抽象提升到Terraform Core理解的低级协议之上,但希望至少能帮助您看到在过程中这些函数将在哪些点运行,以及一个函数的结果如何传递给下游的其他函数。

SDKv2在调用您在提供程序中定义的函数之前或之后自动执行各种行为,因此您的提供程序代码体验这些操作的方式可能与原始协议要求有所不同,但SDK旨在帮助您正确实现“资源实例更改生命周期”文档中编写的细节。

英文:

One way to think about this is to understand Terraform's resource instance change lifecycle (written from the perspective of the underlying provider protocol) and then consider how each of those protocol-level calls corresponds with Terraform SDKv2 features.

Here's a summary of how they relate:

  • ValidateResourceConfig doesn't have a direct analogy in the SDK, but it does cause your various individual ValidateFunc functions to run, on a per-attribute basis.
  • PlanResourceChange corresponds to CustomizeDiff in the SDK.
  • ApplyResourceChange corresponds to different functions in the SDK depending on which action was planned:
    • If the planned action is "will be created", SDK calls Create.
    • If the planned action is "will be updated in place", SDK calls Update.
    • If the planned action is "will be destroyed", SDK calls Delete.
    • If the planned action is "must be replaced" then that's really a shorthand for either a create followed by a delete or the other way around, so there will be two separate calls to ApplyResourceChange handling each of those simpler actions separately.
  • UpgradeResourceState corresponds to the state migration mechanisms.
  • ReadResource corresponds to Read in the SDK.
  • ImportResourceState corresponds to Importer.

These analogies are not direct because SDKv2 intentionally raises the abstraction above the low-level protocol that Terraform Core understands, but it will hopefully still at least help you see at which points in the process these functions will run and how the results of one will feed into others downstream.

SDKv2 has various behaviors of its own which it performs automatically before or after calling into the functions you defined in your provider, so the way your provider code experiences these operations will tend to be a little different than what the raw protocol requires, but the SDK is aiming to help you correctly implement the details written in that "resource instance change lifecycle" document.

huangapple
  • 本文由 发表于 2022年7月14日 16:30:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/72977533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定