From 541d295c6da7bcb073cabfd261df31c34439d3b3 Mon Sep 17 00:00:00 2001
From: Ryan Ahearn <ryan.ahearn@gsa.gov>
Date: Wed, 8 May 2024 15:00:47 -0400
Subject: [PATCH 1/4] Optionally allow specifying 0:n target apps for domain
 service

---
 domain/main.tf      | 12 +++++++++---
 domain/variables.tf |  9 ++++++++-
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/domain/main.tf b/domain/main.tf
index 04b8e2c..f9f199a 100644
--- a/domain/main.tf
+++ b/domain/main.tf
@@ -1,6 +1,7 @@
 locals {
   service_name = (var.name == "" ? "${data.cloudfoundry_app.app.name}-${var.domain_name}" : var.name)
   endpoint     = (var.host_name != null ? "${var.host_name}.${var.domain_name}" : var.domain_name)
+  target_apps  = (var.app_name_or_id != null ? [var.app_name_or_id] : var.app_names_or_ids)
 }
 
 data "cloudfoundry_space" "space" {
@@ -9,7 +10,8 @@ data "cloudfoundry_space" "space" {
 }
 
 data "cloudfoundry_app" "app" {
-  name_or_id = var.app_name_or_id
+  for_each   = toset(local.target_apps)
+  name_or_id = each.key
   space      = data.cloudfoundry_space.space.id
 }
 
@@ -29,8 +31,12 @@ resource "cloudfoundry_route" "origin_route" {
   domain   = data.cloudfoundry_domain.origin_url.id
   hostname = var.host_name
   space    = data.cloudfoundry_space.space.id
-  target {
-    app = data.cloudfoundry_app.app.id
+
+  dynamic "target" {
+    for_each = data.cloudfoundry_app.app
+    content = {
+      app = target.id
+    }
   }
 }
 
diff --git a/domain/variables.tf b/domain/variables.tf
index ae3866e..1d6f800 100644
--- a/domain/variables.tf
+++ b/domain/variables.tf
@@ -10,7 +10,14 @@ variable "cf_space_name" {
 
 variable "app_name_or_id" {
   type        = string
-  description = "base application name to be accessed at this domain name"
+  description = "base application name or id to be accessed at this domain name. Conflicts with var.app_names_or_ids"
+  default     = null
+}
+
+variable "app_names_or_ids" {
+  type        = list(string)
+  description = "base application names or ids to be accessed at this domain name. Overwritten by var.app_name_or_id"
+  default     = []
 }
 
 variable "name" {

From ddff0bee8acd7255b3138265a5484b840672819c Mon Sep 17 00:00:00 2001
From: Ryan Ahearn <ryan.ahearn@gsa.gov>
Date: Wed, 8 May 2024 15:30:26 -0400
Subject: [PATCH 2/4] Fix terraform validate issues

---
 domain/main.tf      | 4 ++--
 domain/variables.tf | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/domain/main.tf b/domain/main.tf
index f9f199a..103f731 100644
--- a/domain/main.tf
+++ b/domain/main.tf
@@ -1,5 +1,5 @@
 locals {
-  service_name = (var.name == "" ? "${data.cloudfoundry_app.app.name}-${var.domain_name}" : var.name)
+  service_name = (var.name == "" ? "${data.cloudfoundry_app.app[0].name}-${var.domain_name}" : var.name)
   endpoint     = (var.host_name != null ? "${var.host_name}.${var.domain_name}" : var.domain_name)
   target_apps  = (var.app_name_or_id != null ? [var.app_name_or_id] : var.app_names_or_ids)
 }
@@ -34,7 +34,7 @@ resource "cloudfoundry_route" "origin_route" {
 
   dynamic "target" {
     for_each = data.cloudfoundry_app.app
-    content = {
+    content {
       app = target.id
     }
   }
diff --git a/domain/variables.tf b/domain/variables.tf
index 1d6f800..df8c7b0 100644
--- a/domain/variables.tf
+++ b/domain/variables.tf
@@ -22,7 +22,7 @@ variable "app_names_or_ids" {
 
 variable "name" {
   type        = string
-  description = "name of the service instance"
+  description = "name of the service instance. Required if not passing in app names or ids"
   default     = ""
 }
 

From 68c5f268a63d7662f9be975972fdebbdc9e8fb9c Mon Sep 17 00:00:00 2001
From: Ryan Ahearn <ryan.ahearn@gsa.gov>
Date: Thu, 9 May 2024 09:03:25 -0400
Subject: [PATCH 3/4] Apply description suggestions from code review

Co-authored-by: Bret Mogilefsky <bret.mogilefsky@gsa.gov>
---
 domain/variables.tf | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/domain/variables.tf b/domain/variables.tf
index df8c7b0..ca91bc3 100644
--- a/domain/variables.tf
+++ b/domain/variables.tf
@@ -10,13 +10,13 @@ variable "cf_space_name" {
 
 variable "app_name_or_id" {
   type        = string
-  description = "base application name or id to be accessed at this domain name. Conflicts with var.app_names_or_ids"
+  description = "base application name or id to be accessed at this domain name. Overrides var.app_names_or_ids if used"
   default     = null
 }
 
 variable "app_names_or_ids" {
   type        = list(string)
-  description = "base application names or ids to be accessed at this domain name. Overwritten by var.app_name_or_id"
+  description = "base application names or ids to be accessed at this domain name. Overridden by var.app_name_or_id if used"
   default     = []
 }
 

From 190fb2469164e12d8555f9d024ac768ea3016eba Mon Sep 17 00:00:00 2001
From: Ryan Ahearn <ryan.ahearn@gsa.gov>
Date: Mon, 13 May 2024 12:38:28 -0400
Subject: [PATCH 4/4] Fix variable path for getting target id

---
 domain/main.tf | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/domain/main.tf b/domain/main.tf
index 103f731..9886f80 100644
--- a/domain/main.tf
+++ b/domain/main.tf
@@ -1,7 +1,7 @@
 locals {
-  service_name = (var.name == "" ? "${data.cloudfoundry_app.app[0].name}-${var.domain_name}" : var.name)
   endpoint     = (var.host_name != null ? "${var.host_name}.${var.domain_name}" : var.domain_name)
   target_apps  = (var.app_name_or_id != null ? [var.app_name_or_id] : var.app_names_or_ids)
+  service_name = (var.name == "" ? "${data.cloudfoundry_app.app[local.target_apps[0]].name}-${var.domain_name}" : var.name)
 }
 
 data "cloudfoundry_space" "space" {
@@ -35,7 +35,7 @@ resource "cloudfoundry_route" "origin_route" {
   dynamic "target" {
     for_each = data.cloudfoundry_app.app
     content {
-      app = target.id
+      app = target.value.id
     }
   }
 }