Make apigee variables non-nullable

This commit is contained in:
Julio Castillo 2023-08-24 18:15:15 +02:00
parent 6c33d34c28
commit 3fdf0dfe99
3 changed files with 52 additions and 57 deletions

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2022 Google LLC * Copyright 2023 Google LLC
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,12 +15,9 @@
*/ */
locals { locals {
org_id = try(google_apigee_organization.organization[0].id, "organizations/${var.project_id}") instance_ips = merge([
org_name = try(google_apigee_organization.organization[0].name, var.project_id) org_id = try(google_apigee_organization.organization[0].id, "organizations/${var.project_id}")
envgroups = coalesce(var.envgroups, {}) org_name = try(google_apigee_organization.organization[0].name, var.project_id)
environments = coalesce(var.environments, {})
instances = coalesce(var.instances, {})
endpoint_attachments = coalesce(var.endpoint_attachments, {})
} }
resource "google_apigee_organization" "organization" { resource "google_apigee_organization" "organization" {
@ -35,14 +32,14 @@ resource "google_apigee_organization" "organization" {
} }
resource "google_apigee_envgroup" "envgroups" { resource "google_apigee_envgroup" "envgroups" {
for_each = local.envgroups for_each = var.envgroups
name = each.key name = each.key
hostnames = each.value hostnames = each.value
org_id = local.org_id org_id = local.org_id
} }
resource "google_apigee_environment" "environments" { resource "google_apigee_environment" "environments" {
for_each = local.environments for_each = var.environments
name = each.key name = each.key
display_name = each.value.display_name display_name = each.value.display_name
description = each.value.description description = each.value.description
@ -64,7 +61,7 @@ resource "google_apigee_environment" "environments" {
} }
resource "google_apigee_envgroup_attachment" "envgroup_attachments" { resource "google_apigee_envgroup_attachment" "envgroup_attachments" {
for_each = merge(concat([for k1, v1 in local.environments : { for_each = merge(concat([for k1, v1 in var.environments : {
for v2 in coalesce(v1.envgroups, []) : "${k1}-${v2}" => { for v2 in coalesce(v1.envgroups, []) : "${k1}-${v2}" => {
environment = k1 environment = k1
envgroup = v2 envgroup = v2
@ -75,7 +72,7 @@ resource "google_apigee_envgroup_attachment" "envgroup_attachments" {
} }
resource "google_apigee_environment_iam_binding" "binding" { resource "google_apigee_environment_iam_binding" "binding" {
for_each = merge(concat([for k1, v1 in local.environments : { for_each = merge(concat([for k1, v1 in var.environments : {
for k2, v2 in coalesce(v1.iam, {}) : "${k1}-${k2}" => { for k2, v2 in coalesce(v1.iam, {}) : "${k1}-${k2}" => {
environment = "${k1}" environment = "${k1}"
role = k2 role = k2
@ -89,7 +86,7 @@ resource "google_apigee_environment_iam_binding" "binding" {
} }
resource "google_apigee_instance" "instances" { resource "google_apigee_instance" "instances" {
for_each = local.instances for_each = var.instances
name = "instance-${each.key}" name = "instance-${each.key}"
display_name = each.value.display_name display_name = each.value.display_name
description = each.value.description description = each.value.description
@ -101,17 +98,13 @@ resource "google_apigee_instance" "instances" {
} }
resource "google_apigee_nat_address" "apigee_nat" { resource "google_apigee_nat_address" "apigee_nat" {
for_each = { for_each = local.instance_ips
for instance_name, instance_config in local.instances : name = each.key
instance_name => instance_config.nat_required ? instance_config : null instance_id = each.value
}
name = "nat-${each.key}"
instance_id = google_apigee_instance.instances[each.key].id
} }
resource "google_apigee_instance_attachment" "instance_attachments" { resource "google_apigee_instance_attachment" "instance_attachments" {
for_each = merge(concat([for k1, v1 in local.environments : { for_each = merge(concat([for k1, v1 in var.environments : {
for v2 in coalesce(v1.regions, []) : for v2 in coalesce(v1.regions, []) :
"${k1}-${v2}" => { "${k1}-${v2}" => {
environment = k1 environment = k1
@ -124,7 +117,7 @@ resource "google_apigee_instance_attachment" "instance_attachments" {
} }
resource "google_apigee_endpoint_attachment" "endpoint_attachments" { resource "google_apigee_endpoint_attachment" "endpoint_attachments" {
for_each = local.endpoint_attachments for_each = var.endpoint_attachments
org_id = local.org_id org_id = local.org_id
endpoint_attachment_id = each.key endpoint_attachment_id = each.key
location = each.value.region location = each.value.region
@ -132,39 +125,37 @@ resource "google_apigee_endpoint_attachment" "endpoint_attachments" {
} }
resource "google_apigee_addons_config" "test_organization" { resource "google_apigee_addons_config" "test_organization" {
org = local.org_name for_each = toset(var.addons_config == null ? [] : [""])
dynamic "addons_config" { org = local.org_name
for_each = var.addons_config == null ? [] : [""] addons_config {
content { dynamic "advanced_api_ops_config" {
dynamic "advanced_api_ops_config" { for_each = var.addons_config.advanced_api_ops ? [] : [""]
for_each = var.addons_config.advanced_api_ops ? [] : [""] content {
content { enabled = true
enabled = true
}
} }
dynamic "api_security_config" { }
for_each = var.addons_config.api_security ? [] : [""] dynamic "api_security_config" {
content { for_each = var.addons_config.api_security ? [] : [""]
enabled = true content {
} enabled = true
} }
dynamic "connectors_platform_config" { }
for_each = var.addons_config.connectors_platform ? [] : [""] dynamic "connectors_platform_config" {
content { for_each = var.addons_config.connectors_platform ? [] : [""]
enabled = true content {
} enabled = true
} }
dynamic "integration_config" { }
for_each = var.addons_config.integration ? [] : [""] dynamic "integration_config" {
content { for_each = var.addons_config.integration ? [] : [""]
enabled = true content {
} enabled = true
} }
dynamic "monetization_config" { }
for_each = var.addons_config.monetization ? [] : [""] dynamic "monetization_config" {
content { for_each = var.addons_config.monetization ? [] : [""]
enabled = true content {
} enabled = true
} }
} }
} }

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2022 Google LLC * Copyright 2023 Google LLC
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -57,4 +57,4 @@ output "organization" {
output "service_attachments" { output "service_attachments" {
description = "Service attachments." description = "Service attachments."
value = { for k, v in google_apigee_instance.instances : k => v.service_attachment } value = { for k, v in google_apigee_instance.instances : k => v.service_attachment }
} }

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2022 Google LLC * Copyright 2023 Google LLC
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,13 +32,15 @@ variable "endpoint_attachments" {
region = string region = string
service_attachment = string service_attachment = string
})) }))
default = null default = {}
nullable = false
} }
variable "envgroups" { variable "envgroups" {
description = "Environment groups (NAME => [HOSTNAMES])." description = "Environment groups (NAME => [HOSTNAMES])."
type = map(list(string)) type = map(list(string))
default = null default = {}
nullable = false
} }
variable "environments" { variable "environments" {
@ -56,7 +58,8 @@ variable "environments" {
envgroups = optional(list(string)) envgroups = optional(list(string))
regions = optional(list(string)) regions = optional(list(string))
})) }))
default = null default = {}
nullable = false
} }
variable "instances" { variable "instances" {
@ -70,7 +73,8 @@ variable "instances" {
consumer_accept_list = optional(list(string)) consumer_accept_list = optional(list(string))
nat_required = optional(bool, false) nat_required = optional(bool, false)
})) }))
default = null default = {}
nullable = false
} }
variable "organization" { variable "organization" {