我正在编写一个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_groups
都说advertised_ip_ranges
只有当 Advertisement_mode 是 CUSTOM 并且向路由器的所有对等方通告时,才能填充此字段。
现在,如果我保持DEFAULT的值,我的代码如下所示advertise_mode
:
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,那么对于这样一个小东西的代码重复就太多了。有没有更好的方法来做我想要实现的目标?