英文:
id is a reserved field name
问题
我正在为 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
的引用吗?它不在资源模式中。
英文:
I'm writing a provider for terraform to interface with an API, here's the resource schema I have:
&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,
},
},
}
There are no compile or install errors with go install
, and I'm trying this out locally, so I've set up my .terraformrc
to point to my go bin folder.
Terraform seemingly finds an id somewhere, and complains:
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
The code is here https://github.com/UbiquitousBear/terraform-provider-onedev. Does anyone know where I should be removing the reference to id
? It's not in the resource schema.
答案1
得分: 1
你的go.mod
文件表明你正在使用SDK版本1.17.2,其中id
确实被记录为保留的属性名。
然而,在最新的SDK版本2.6.1中,似乎不再存在该属性。根据问题#607,这个策略的改变似乎是由于这个问题,该改变首次在SDK版本v2.1.0中发布。
虽然我无法解释你分享的代码为什么会引发该错误,但你可以通过升级到最新的SDK版本来避免这个问题。由于这是一个新的主要版本,可能会有一些需要考虑的破坏性变化。有一个Terraform SDK v2升级指南描述了这些变化,并包含一个链接到tf-sdk-migrator工具,该工具可以帮助进行升级。
英文:
Your go.mod
file suggests that you are using SDK version 1.17.2, where id
is indeed recorded as a reserved attribute name.
However, it no longer seems to be present in the latest SDK release, 2.6.1. It seems that this policy changed as a result of issue #607, and the change was released for the first time in SDK release v2.1.0.
While I can't explain why the code you've shared would be raising that error, you may be able to avoid the problem by upgrading to the latest SDK version. Since it's a new major release there may be some breaking changes to consider elsewhere in the API. There's a Terraform SDK v2 upgrade guide which describes the changes and also includes a link to the tf-sdk-migrator
tool which has some automation to help with the upgrade.
答案2
得分: 0
这个问题有一个解决方法,而不需要升级到SDK v2,你可以尝试在你的模式(schema)中给它一个其他的名称,比如使用"id1"代替"id",像这样:
"id1": {
Type: schema.TypeInt,
Optional: true,
Description: "",
},
然后将这个属性的值解析到你的结构体中,像这样:
c.ID = d.Get("id1").(int)
然后尝试构建。
现在唯一的缺点是你需要在你的hcl文件中提到"id1"而不是"id"。
这对我来说是有效的,但升级到SDKv2仍然是更好的解决方案。
英文:
There's one workaround for this issue without upgrading to SDK v2, you can try to give other name like "id1" instead of "id" on you schema like
"id1": {
Type: schema.TypeInt,
Optional: true,
Description: "",
},
and parse value of this attribute onto you struct as
c.ID = d.Get("id1").(int)
and try to build.
now the only drawback is you need to mention "id1" instead of "id" into your hcl file.
this is working for me, but still upgrading to SDKv2 is better solution
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论