关于如何在Terraform中解决”attr-as-block”模式的真实示例:

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

Real examples of how to work-around attr-as-block mode in Terraform

问题

我正在使用资源 google_compute_subnetwork,注意到当删除数值时,secondary_ip_range 动态块似乎不会更新。文档中提到了这一点,这可能可以解释:

此字段使用属性作为块模式,以避免在 0.12 升级期间破坏用户。要显式发送零对象的列表,您必须使用以下语法:example=[]

我已阅读了 Attributes as Blocks 链接,但不明白应该做什么。我直到 v0.13 才开始使用 Terraform,因此没有在 v0.11 或 v0.12 中工作的经验。对于其他所有情况,如果将 [] 传递给 for_each,动态块将被删除。这个是不同的吗?

还是这是一个提供程序/ API 的限制,上面的消息只是一个误导?

英文:

I'm working with resource google_compute_subnetwork and have noticed the secondary_ip_range dynamic block doesn't seem to get updated when values are removed. The doc says this, which may explain why:

> This field uses attr-as-block mode to avoid breaking users during the
> 0.12 upgrade. To explicitly send a list of zero objects you must use the following syntax: example=[]

I have read the Attributes as Blocks link, but don't understand what I should be doing. I didn't start working with Terraform until v0.13, so don't have any experience working in v0.11 or v0.12. With everything else, if a pass a [] to the for_each, the dynamic block is removed. Is this one different?

Or is this a provider/api limitation and the above message just a red herring?

答案1

得分: 1

我不确定文档为什么在谈论特定属性名称时使用占位符“example”,但基本上它试图告诉您编写以下内容以声明您打算具有零个次要 IP 范围:

  secondary_ip_range = []

如果您仅写下“nothing”(没有类型为secondary_ip_range的块),则提供程序会将其解释为“忽略远程 API 中已存在的任何次要 IP 范围”,而不是“应该没有次要 IP 范围”。

正如文档所暗示的,这是为了适应提供程序中的历史设计问题,现在必须向后兼容。

英文:

I'm not sure why the documentation uses the placeholder "example" when it's already talking about a specific attribute name, but essentially it's trying to tell you to write the following to declare that you intend to have zero secondary IP ranges:

  secondary_ip_range = []

If you just write nothing (no blocks of type secondary_ip_range) then the provider understands that as "ignore whatever secondary IP ranges already exist in the remote API", rather than as "there should be zero secondary IP ranges".

As the documentation implies, this is accommodating a historical design problem in the provider which it must now be backward-compatible with.

答案2

得分: 0

解决方法实际上是根本不使用动态块,而是将secondary_ip_range写成资源内的属性。我同意代码中提到的 'example = []' 令人困惑,因为我解释为输入,并发现它没有任何区别。

现在有两个带有修复代码的线程:

英文:

The solution is actually to not use a dynamic block at all and instead write secondary_ip_range as an attribute inside the resource. I do agree the code mentioning 'example = []' was confusing because I interpreted that as input, and found it made no difference.

There are now two threads showing sample code with the fix:

huangapple
  • 本文由 发表于 2023年6月12日 06:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452785.html
匿名

发表评论

匿名网友

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

确定