如何有条件地在Terraform中填充参数值?

时间:2019-04-24 19:08:31

标签: terraform terraform-provider-aws terraform-provider-gcp

我正在编写一个Terraform脚本来启动Google Cloud Platform中的资源。
某些资源仅在设置了另一个参数时才需要一个参数,那么仅当另一个参数被填充(或其他类似条件)时才如何填充一个参数?

例如:

resource "google_compute_router" "compute_router" {
  name    = "my-router"
  network = "${google_compute_network.foobar.name}"
  bgp {
    asn               = 64514
    advertise_mode    = "CUSTOM"
    advertised_groups = ["ALL_SUBNETS"]
    advertised_ip_ranges {
      range = "1.2.3.4"
    }
    advertised_ip_ranges {
      range = "6.7.0.0/16"
    }
  }
}

在上面的资源(google_compute_router中,对advertised_groupsadvertised_ip_ranges的描述都说仅当advertise_mode为CUSTOM且被广告给以下对象的所有对等方时,才能填充此字段路由器。

现在,如果我将advertise_mode的值保留为 DEFAULT ,我的代码如下所示:

resource "google_compute_router" "compute_router" {
  name    = "my-router"
  network = "${google_compute_network.foobar.name}"
  bgp {
    asn               = 64514

    #Changin only the value below
    advertise_mode    = "DEFAULT"

    advertised_groups = ["ALL_SUBNETS"]
    advertised_ip_ranges {
      range = "1.2.3.4"
    }
    advertised_ip_ranges {
      range = "6.7.0.0/16"
    }
  }
}

但是上述脚本在运行时会出现以下错误:

* google_compute_router.compute_router_default: Error creating Router: googleapi: Error 400: Invalid value for field 'resource.bgp.advertiseMode': 'DEFAULT'. Router cannot have a custom advertisement configurati
on in default mode., invalid

作为上述解决方法,我创建了两个名称不同的资源,几乎可以完成相同的工作。该脚本如下所示:

resource "google_compute_router" "compute_router_default" {
  count               = "${var.advertise_mode == "DEFAULT" ? 1 : 0}"
  name                = "${var.router_name}"
  region              = "${var.region}"
  network             = "${var.network_name}"

  bgp {
    asn               = "${var.asn}"
    advertise_mode    = "${var.advertise_mode}"
    #Removed some codes from here
  }
}

resource "google_compute_router" "compute_router_custom" {
  count               = "${var.advertise_mode == "CUSTOM" ? 1 : 0}"
  name                = "${var.router_name}"
  region              = "${var.region}"
  network             = "${var.network_name}"

  bgp {
    asn               = "${var.asn}"
    advertise_mode    = "${var.advertise_mode}"
    advertised_groups = ["${var.advertised_groups}"]

    advertised_ip_ranges {
      range = "${var.advertised_ip_range}"
      description = "${var.advertised_ip_description}"
    }
  }
}

上面的脚本可以正常工作,但是对我和黑客来说,似乎有很多代码重复。同样,对于两个选项(具有相关属性),也可以,但是,如果还有更多选项(例如5),那么对于这么小的事情,代码重复就太多了。 有没有更好的方法可以实现我想要实现的目标?

2 个答案:

答案 0 :(得分:1)

这几乎是您在Terraform <0.12中受限的内容。一些资源允许您使用空字符串来省略基本值,并且提供程序会将其解释为空值,而不是将其传递给API端点,因此它不会抱怨其设置不正确。但是从我与GCP提供者的短暂经验来看,那里的大多数事情并非如此。

Terraform 0.12引入了nullable arguments,您可以使用以下内容有条件地进行设置:

variable "advertise_mode" {}

resource "google_compute_router" "compute_router" {
  name    = "my-router"
  network = "${google_compute_network.foobar.name}"
  bgp {
    asn               = 64514
    advertise_mode    = "${var.advertise_mode}"
    advertised_groups = ["${var.advertise_mode == "DYNAMIC" ? ALL_SUBNETS : null}"]
    advertised_ip_ranges {
      range = "${var.advertise_mode == "DYNAMIC" ? 1.2.3.4 : null}"
    }
    advertised_ip_ranges {
      range = "${var.advertise_mode == "DYNAMIC" ? 6.7.0.0/16 : null}"
    }
  }
}

它还将引入dynamic blocks,您可以对其进行循环,因此您还可以动态地拥有advertised_ip_ranges个块。

答案 1 :(得分:0)

以上答案不正确,因为'advertised_ip_ranges'不会接受空值;解决方案是利用动态块,该块可以为该资源处理空值,并进一步使该资源接受可变数量的ip范围。

variable custom_ranges {
  default = ["172.16.31.0/24","172.16.32.0/24"]
}

resource "google_compute_router" "router_01" {
  name    = "cr-bgp-${var.gcp_bgp_asn}"
  region  = var.gcp_region
  project = var.gcp_project
  network = var.gcp_network

  bgp {
    asn = var.gcp_bgp_asn
    advertise_mode = var.advertise_mode
    advertised_groups = var.advertise_mode == "CUSTOM" ? ["ALL_SUBNETS"] : null
    dynamic "advertised_ip_ranges" {
      for_each = var.advertise_mode == "CUSTOM" ? var.custom_ranges : []
      content { 
        range = advertised_ip_ranges.value
      }
    }
  }
}

其他搜索键:google_compute_router“ bgp.0.advertised_ip_ranges.0.range”将不接受空值。