Terraform-根据文本从输出中提取值

时间:2019-06-13 19:00:40

标签: google-cloud-platform terraform

我有以下模块可在GCP上创建子网:

/******************************************
    Subnet configuration
 *****************************************/
resource "google_compute_subnetwork" "subnetwork" {
  count = "${length(var.subnets)}"

  name                     = "${lookup(var.subnets[count.index], "subnet_name")}"
  ip_cidr_range            = "${lookup(var.subnets[count.index], "subnet_ip")}"
  region                   = "${lookup(var.subnets[count.index], "subnet_region")}"
  private_ip_google_access = "${lookup(var.subnets[count.index], "subnet_private_access", "false")}"
  enable_flow_logs         = "${lookup(var.subnets[count.index], "subnet_flow_logs", "false")}"
  network                  = "${google_compute_network.network.name}"
  project                  = "${var.project_id}"

  secondary_ip_range = "${var.secondary_ranges[lookup(var.subnets[count.index], "subnet_name")]}"
}

data "google_compute_subnetwork" "created_subnets" {
  count = "${length(var.subnets)}"

  name    = "${element(google_compute_subnetwork.subnetwork.*.name, count.index)}"
  region  = "${element(google_compute_subnetwork.subnetwork.*.region, count.index)}"
  project = "${var.project_id}"
}

我的输出如下:

output "subnets_self_links" {
  value       = "${google_compute_subnetwork.subnetwork.*.self_link}"
  description = "The self-links of subnets being created"
}

此输出产生子网列表。

我需要能够通过搜索子网名称来提取以下内容。在这种情况下,它是“ subnet-01”:

subnetwork = "https://www.googleapis.com/compute/v1/projects/abc-network-hub/regions/us-central1/subnetworks/subnet-01"

如何构造查找以按文本搜索?

subnetwork = "${module.test-vpc.subnets_self_links}"

以上返回:

  

“ module.compute-o057qdb2-l30.var.subnetwork:位于变量子网中   模块compute-o057qdb2-l30应该是字符串类型,得到列表“

subnetwork = "${lookup(module.test-vpc.subnets_self_links, "subnet-01, 0")}"

以上返回:

  
      
  • module.compute-o057qdb2-l30.var.subnetwork:在第3列第1行:查找:参数1应该是类型map,类型列表位于:
  •   
     

$ {lookup(module.test-vpc.subnets_self_links,“ subnet-01,0”)}

subnetwork = "${module.test-vpc.subnets_self_links[0]}"

上面的方法起作用,因为仅创建了一个子网,我可以按列表索引进行引用。我需要能够按子网名称搜索。我觉得我应该能够从“数据”中提取值。

这是我正在使用的模块:https://github.com/terraform-google-modules/terraform-google-network

1 个答案:

答案 0 :(得分:0)

Terraform 0.12使此操作变得容易:

def leaky_bucket(name, interval=30, size=3, message="Too many request."):
    """Rate limiter that allows bursts.

    Args:
        name:     the service to limit (several views can share a service)
        interval: the timperiod (in seconds)
        size:     maximum number of activities in a timeperiod
        message:  message to display to the user when rate limited

    """
    def decorator(fn):
        def wrap(request, *args, **kwargs):
            r = redis.Redis()
            key = _ratelimit_key(name, request)
            if r.exists(key):
                val = r.hgetall(key)
                value = float(val['value'])
                now = time.time()

                # leak the bucket
                elapsed = now - float(val['timestamp'])
                value -= max(0.0, elapsed / float(interval) * size)

                if value + 1 > size:
                    r.hmset(key, dict(timestamp=now, value=value))
                    r.expire(key, interval)
                    return http.HttpResponse(message, status=409)
                else:
                    value += 1.0
                    r.hmset(key, dict(timestamp=now, value=value))
                    r.expire(key, interval)
                    return fn(request, *args, **kwargs)

            else:
                r.hmset(key, dict(timestamp=time.time(), value=1.0))
                r.expire(key, interval)
                return fn(request, *args, **kwargs)

        return wrap
    return decorator

在HCL 2之前,这种事情很麻烦。