id 是保留字段名称

时间:2021-06-13 15:31:48

标签: go terraform

我正在为 terraform 编写一个提供程序以与 API 交互,这是我拥有的资源架构:

&schema.Resource{
        Create: resourceProjectCreate,
        Read:   resourceProjectRead,
        Update: resourceProjectUpdate,
        Delete: resourceProjectDelete,
        Importer: &schema.ResourceImporter{
            State: schema.ImportStatePassthrough,
        },

        CustomizeDiff: customdiff.Sequence(
            customdiff.ComputedIf("slug", func(d *schema.ResourceDiff, meta interface{}) bool {
                return d.HasChange("name")
            }),
        ),

        Schema: map[string]*schema.Schema{
            "name": {
                Type:     schema.TypeString,
                Required: true,
                ValidateFunc: validateName,
            },
            "description": {
                Type:     schema.TypeString,
                Optional: true,
            },
            "issueManagementEnabled": {
                Type:     schema.TypeBool,
                Required: true,
            },
            "forkedFromId": {
                Type:     schema.TypeInt,
                Required: false,
            },
        },
    }

go install 没有编译或安装错误,我正在本地尝试此操作,因此我已将 .terraformrc 设置为指向我的 go bin 文件夹。

Terraform 似乎在某处找到了一个 id,然后抱怨:

Error: Internal validation of the provider failed! This is always a bug
with the provider itself, and not a user issue. Please report
this bug:

1 error occurred:
    * resource onedev_project: id is a reserved field name

代码在这里https://github.com/UbiquitousBear/terraform-provider-onedev。有谁知道我应该在哪里删除对 id 的引用?它不在资源架构中。

2 个答案:

答案 0 :(得分:1)

您的 go.mod 文件表明您使用的是 SDK 版本 1.17.2,其中 id is indeed recorded as a reserved attribute name

然而,it no longer seems to be present in the latest SDK release, 2.6.1。此政策似乎因 issue #607 而发生更改,并且该更改在 SDK 版本 v2.1.0 中首次发布。

虽然我无法解释为什么您共享的代码会引发该错误,但您可以通过升级到最新的 SDK 版本来避免该问题。由于它是一个新的主要版本,可能会在 API 的其他地方考虑一些重大更改。 a Terraform SDK v2 upgrade guide 描述了更改,还包含指向 the tf-sdk-migrator tool 的链接,该链接具有一些自动化功能以帮助升级。

答案 1 :(得分:0)

有一种解决此问题的方法,无需升级到 SDK v2,您可以尝试在架构上提供其他名称,例如“id1”而不是“id”

"id1": {
                Type:        schema.TypeInt,
                Optional:    true,
                Description: "",
            },

并将此属性的值解析为结构体

c.ID = d.Get("id1").(int)

并尝试构建。

现在唯一的缺点是您需要在 hcl 文件中提及“id1”而不是“id”。

这对我有用,但升级到 SDKv2 仍然是更好的解决方案