在 Terraform 的数据源中,属性是否可以同时计算和可选?
Can an attribute be both computed and optional in data source in Terraform?
我正在尝试添加支持 2 种输入方法的数据源data_source_person
:
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"first_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
即,应指定 id
或 first_name
的值,但不能同时指定两者(这是 oneOf
关系)。
如果指定了“id”,代码将执行 GET /people/{ID}
,否则必须指定“display_name”,在这种情况下,将调用 GET /people
,这将 return 人员列表和代码将 运行 过滤器以查找具有 response.data.people[i].display_name == {providedDisplayName}
.
的人员的 ID
TF数据源是否支持这种配置(两个属性都是计算出来的,可选)?
可以在架构中指定该属性既是 computed
又是 optional
,但您更常在资源架构中看到这一点。在 resource
和 data
源模式中,optional
表示可选输入,computed
表示其值可以在供应期间计算。这些可能 co-exist,因为参数或块可以是输入或输出。例如,这是不可能的。 required
为真,computed
为真,因为那将是一个输入和输出,它们会发生冲突。
此外,根据您问题中的描述,您还想利用 ExactlyOneOf
:
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"id", "first_name"},
},
"first_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"id", "first_name"},
},
},
Optional
和 Computed
一起是作为一种过滤约束的任何参数的不错选择,用户可以指定它以约束请求 或省略它以了解远程系统中配置的该属性的值。
您可以在 AWS 提供商中看到这方面的一些真实示例。例如,the aws_vpc
data source uses this pattern for its cidr_block
argument,既可以用作约束,也可以用作输出:
"cidr_block": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
使用此模式时,提供者应区分正在设置或未设置的参数,如果已设置,则将其视为对查询的额外约束,如果未设置,则使其不受约束。那么在后一种情况下,从查询到远程系统的相应值将作为结果的一部分写入属性。
我正在尝试添加支持 2 种输入方法的数据源data_source_person
:
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"first_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
即,应指定 id
或 first_name
的值,但不能同时指定两者(这是 oneOf
关系)。
如果指定了“id”,代码将执行 GET /people/{ID}
,否则必须指定“display_name”,在这种情况下,将调用 GET /people
,这将 return 人员列表和代码将 运行 过滤器以查找具有 response.data.people[i].display_name == {providedDisplayName}
.
TF数据源是否支持这种配置(两个属性都是计算出来的,可选)?
可以在架构中指定该属性既是 computed
又是 optional
,但您更常在资源架构中看到这一点。在 resource
和 data
源模式中,optional
表示可选输入,computed
表示其值可以在供应期间计算。这些可能 co-exist,因为参数或块可以是输入或输出。例如,这是不可能的。 required
为真,computed
为真,因为那将是一个输入和输出,它们会发生冲突。
此外,根据您问题中的描述,您还想利用 ExactlyOneOf
:
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"id", "first_name"},
},
"first_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"id", "first_name"},
},
},
Optional
和 Computed
一起是作为一种过滤约束的任何参数的不错选择,用户可以指定它以约束请求 或省略它以了解远程系统中配置的该属性的值。
您可以在 AWS 提供商中看到这方面的一些真实示例。例如,the aws_vpc
data source uses this pattern for its cidr_block
argument,既可以用作约束,也可以用作输出:
"cidr_block": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
使用此模式时,提供者应区分正在设置或未设置的参数,如果已设置,则将其视为对查询的额外约束,如果未设置,则使其不受约束。那么在后一种情况下,从查询到远程系统的相应值将作为结果的一部分写入属性。