【问题标题】:Make a resource schema dependant on another variable使资源模式依赖于另一个变量
【发布时间】:2019-05-26 02:11:23
【问题描述】:

我正在 Terraform 中创建一个插件,我想向架构中添加一个字段,该字段只有在提供另一个字段时才能调用。

    "host_name": &schema.Schema{
        Type:        schema.TypeString,
        Optional:    true,
        DefaultFunc: schema.EnvDefaultFunc("host_name", nil),
        Description: "Should give name in FQDN if being used for DNS puposes .",
    },
    "enableDns": &schema.Schema{
       Type:        schema.TypeString,
       Required:    true,
       DefaultFunc: schema.EnvDefaultFunc("host_name", nil),
       Description: "Should give name in FQDN if being used for DNS puposes .",

这里我想只在传递host_name 时传递.tf 文件中的enableDns 字符串。如果它没有给出并且我通过enableDns 它应该在计划期间抛出一个错误。

【问题讨论】:

    标签: go terraform


    【解决方案1】:

    Terraform 提供者实际上并没有一流的方法来根据 ConflictsWith 属性以外的其他参数有条件地做事。

    有一种使用CustomizeDiff 来做一些交叉参数工作的hacky 方法,但它只在真正需要它的几个地方真正使用。

    通常,提供者会简单地验证各个参数,如果提供者使用的 API 需要交叉参数验证,那么这只会在 API 返回错误的应用时看到。

    有关在进行交叉参数验证时使用CustomizeDiff 引发计划时间错误的示例,请参阅the aws_elasticache_cluster resource

        CustomizeDiff: customdiff.Sequence(
            func(diff *schema.ResourceDiff, v interface{}) error {
                // Plan time validation for az_mode
                // InvalidParameterCombination: Must specify at least two cache nodes in order to specify AZ Mode of 'cross-az'.
                if v, ok := diff.GetOk("az_mode"); !ok || v.(string) != elasticache.AZModeCrossAz {
                    return nil
                }
                if v, ok := diff.GetOk("num_cache_nodes"); !ok || v.(int) != 1 {
                    return nil
                }
                return errors.New(`az_mode "cross-az" is not supported with num_cache_nodes = 1`)
            },
    

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 2019-11-04
      • 1970-01-01
      相关资源
      最近更新 更多