How can I fix ExactlyOneOf: foo references unknown attribute (bar) at part (bar) from terraform-plugin-sdk?

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

How can I fix ExactlyOneOf: foo references unknown attribute (bar) at part (bar) from terraform-plugin-sdk?

问题

一些背景信息:我正在编写自己的TF提供程序,具有以下模式:

func Provider() *schema.Provider {
	log.Printf("[INFO] Creating Cool Provider")
	return &schema.Provider{
		Schema: map[string]*schema.Schema{
			...
			"foo": {
				Type:         schema.TypeString,
				Optional:     true,
				Default:      "bar",
				ExactlyOneOf: []string{"bar"},
			},
		},
		ConfigureContextFunc: providerConfigure,
		ResourcesMap: map[string]*schema.Resource{
			...
		},
	}
}

当我构建并运行tf plan时,我遇到了以下问题:

│ 内部验证提供程序失败!这始终是提供程序本身的错误,而不是用户问题。请报告此错误:
│
│ 发生了1个错误:
│       * ExactlyOneOf: foo引用了未知属性(bar),位于部分(bar)

这似乎是由于terraform-plugin-sdk引起的:链接。例如,当我注释掉ExactlyOneOf部分时,tf plan没有错误,并且按预期输出计划。

英文:

Some context: I'm in the process of writing my own TF provider with the following schema:

func Provider() *schema.Provider {
	log.Printf("[INFO] Creating Cool Provider")
	return &schema.Provider{
		Schema: map[string]*schema.Schema{
			...
			"foo": {
				Type:         schema.TypeString,
				Optional:     true,
				Default:      "bar",
				ExactlyOneOf: []string{"bar"},
			},
		},
		ConfigureContextFunc: providerConfigure,
		ResourcesMap: map[string]*schema.Resource{
			...
		},
	}
}

When I build it and run tf plan I run into:

│ 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:
│       * ExactlyOneOf: foo references unknown attribute (bar) at part (bar)

which seems to be triggering from terraform-plugin-sdk: link. E.g., when I comment out ExactlyOneOf part, there're no errors for tf plan and it outputs the plan as expected.

答案1

得分: 3

正如错误消息所述,属性foo在其模式结构中引用了另一个未定义的属性bar。根据您显示的代码,您目前没有定义其他属性bar,因此您需要定义它或引用一个已定义的属性。

如果您添加了以下属性bar

Schema: map[string]*schema.Schema{
  ...
  "foo": {
    Type:         schema.TypeString,
    Optional:     true,
    Default:      "bar",
    ExactlyOneOf: []string{"bar"},
  },
  "bar": {
    Type:     schema.TypeBool,
    Optional: true,
    Default:  false,
  },
},

那么这将修复您的代码。请注意,在模式结构的文档中,ExactlyOneOf的说明如下:

// ExactlyOneOf是一组模式键,当设置时,该列表中的一个键可以被指定。如果没有指定任何键,它将报错。
ExactlyOneOf  []string
英文:

As the error message states, the attribute foo is referencing another undefined attribute bar in its schema struct. You currently have no other attribute bar defined according to your displayed code, so you would need to define it or reference a defined attribute.

If you added an attribute bar like the following:

Schema: map[string]*schema.Schema{
  ...
  "foo": {
    Type:         schema.TypeString,
    Optional:     true,
    Default:      "bar",
    ExactlyOneOf: []string{"bar"},
  },
  "bar": {
    Type:     schema.TypeBool,
    Optional: true,
    Default:  false,
  },
},

then this would fix your code. Note in the schema struct documentation for ExactlyOneOf:

// ExactlyOneOf is a set of schema keys that, when set, only one of the
// keys in that list can be specified. It will error if none are
// specified as well.
ExactlyOneOf  []string

huangapple
  • 本文由 发表于 2021年7月28日 02:41:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/68550233.html
匿名

发表评论

匿名网友

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

确定