doufen5175 2019-09-04 08:42
浏览 34

Terraform提供程序应如何处理服务器端应用的默认值?

Context: I am implementing (my first) Terraform plugin/provider as a wrapper towards existing public API.

One of the create operations in the API specifies an integer field which takes positive values or -1 serving as a default. If you specify -1 in the create API call, the value gets replaced by some default on the server side (say field = 1000) and is stored as 1000 from now on.

If I present this to my Terraform plugin (terraform apply):

resource "something" "mysomething" {
  name  = "someName"
  field = -1
}

the call is not idempotent. Terraform continues to see a drift and subsequently offers:

  # something.mysomething will be updated in-place
  ~ resource "something" "mysomething" {
        id               = "165-1567498530352"
        name             = "someName"
      ~ field            = 1000 -> -1
    }

Plan: 0 to add, 1 to change, 0 to destroy.

How to deal with such API?

  • 写回答

2条回答 默认 最新

  • doujie7346 2019-09-04 09:31
    关注

    You can use the DiffSuppressFunc flag on schema attributes to conditionally suppress a diff so that Terraform doesn't choose to do anything with the diff.

    Something like this should work for you:

    package something
    
    import (
        "github.com/hashicorp/terraform/helper/schema"
    )
    
    func somethingSomething() *schema.Resource {
        return &schema.Resource{
            // ...
            Schema: map[string]*schema.Schema{
                // ...
                "field": {
                    Type:     schema.TypeInt,
                    Optional: true,
                    Default:  -1,
                    DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
                        if new == "-1" {
                            return true
                        }
                        return false
                    },
                },
            },
        }
    }
    

    Martin's answer provides probably a better alternative by using the Computed flag and making the attribute Optional. For this to work nicely you'd want to prevent people from specifying -1 as a value for this which you can do with a ValidateFunc, using the IntAtLeast validator from the list of predefined validations in the core SDK:

    package something
    
    import (
        "github.com/hashicorp/terraform/helper/schema"
        "github.com/hashicorp/terraform/helper/validation"
    )
    
    func somethingSomething() *schema.Resource {
        return &schema.Resource{
            // ...
            Schema: map[string]*schema.Schema{
                // ...
                "field": {
                    Type:         schema.TypeInt,
                    Optional:     true,
                    Computed:     true,
                    ValidateFunc: validation.IntAtLeast(1),
                },
            },
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?