From 492dbfa3afc4714a894bbc23a685ca1c006a0150 Mon Sep 17 00:00:00 2001
From: Philippe Caseiro <pcaseiro@cadoles.com>
Date: Wed, 17 Jan 2024 16:09:34 +0100
Subject: [PATCH] feat(tofu): adding information outputs adding enable disable
 support for deployment

---
 tofu/deployments.tf               | 69 +++++++++++++++++++++++++++++++
 tofu/main.tf                      | 37 +++++++++++++++++
 tofu/modules/pycloud/main.tf      | 10 +++--
 tofu/modules/pycloud/variables.tf |  1 +
 tofu/pytofu                       |  4 +-
 5 files changed, 117 insertions(+), 4 deletions(-)

diff --git a/tofu/deployments.tf b/tofu/deployments.tf
index dc156b6..f7e1e91 100644
--- a/tofu/deployments.tf
+++ b/tofu/deployments.tf
@@ -1,7 +1,19 @@
 locals {
    deployments = {
+    "ssh" = {
+      name = "Associer une clef SSH - (2024-01-17)"
+      description = "Adding user ssh key to project deployments"
+      enabled = true
+      catalog_item_name = "Associer une clef SSH"
+      project_name = "GRP-CLOE-TSS-DEV"
+      inputs = {
+        username = var.vra_username
+        sshpubkey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDDph3zh6ojSvH44k13z9B6xj+Hargo3uzkxnYv5s5NI4yagNuBXEc3aS++KdocND+FtVfLK+iVE8qHo2bvmpMmVkqU6WU2apN7DfIP0QGLlSGeo+UOZ9hGeEDlgVO4AOnZKZ5kPGBEPZ84JXuE9CmhKfwEVCK8w3B8XQttA8alFl4A4/4F14x2w4njsSLY1H3b0qah7hgYKU5zHIGLg8Lxx+1BxGIF0l5n5m5rqAskRNaF+aYbs0CcWHv49bPK0sJJ0qPV2r2sq8BlzuZFHExnZRIxpsIXdce4Bm4rdlGi7tBmmurLk4OOtDkwvhD0LMaNJf10k6QLSmRUTVzgsYz/dmGxopbMtwwIXkwi014uSZgi8wAuznXx5I4j2TUGPZHOVf+1iw/yaxWlgTVOSoX7ZxyhDgW5cCgZZGNzU5UWe0vUuVTB+hfSMj50/Q6+Vi92/mDMbPhm4nBoVzD5DT15mB+yGyN45Ej61m0JzVUyZexfvVaffEug1/u5dnwilP0WGKr4i2OXxOXtvSdAs5rlZjvppZk6IxRCwXIcPwEFL97ZrQZAxlVS5Nh+ZnlSwTe3zfQhzHj1ao0AdCAHFPUEdoUPJhSb0OjyCvZ9XZ1KCkXhuhuN/3IUhuoWl4soNCeC3KmU/USx1wda438Exj0hM1mTyBZScDPGyD9nw78DGw== Philippe Caseiro"
+      }
+    },
     "test-pc-vf" = {
       name = "test-pc-vf"
+      enabled = false
       description = "test deployment"
       catalog_item_name = "MonoVM-Dev"
       project_name = "GRP-CLOE-TSS-DEV"
@@ -27,8 +39,65 @@ locals {
         leaseDays = "1"
       }
     },
+    "MonoTest-pc-vf-1" = {
+      name = "MonoTest-1"
+      enabled = true
+      description = "test deployment"
+      catalog_item_name = "MonoVM-Dev"
+      project_name = "GRP-CLOE-TSS-DEV"
+
+      inputs = {
+        MonoVM_cpu = 2
+        MonoVM_memory = 2048
+        MonoVM_securityTag = "DEV-TIER-WEB"
+        MonoVM_service = "app"
+        MonoVM_disks = jsonencode([
+          { 
+            mountpoint = "/toto",
+            size = 10
+          },
+          { 
+            mountpoint = "/titi",
+            size = 20
+          }
+        ])
+        MonoVM_image = "DEB10X"
+        MonoVM_instances = 1
+        MonoVM_startOrder = "1"
+        leaseDays = "1"
+      }
+    },
+    "MonoTest-pc-vf-2" = {
+      name = "MonotTest-2"
+      enabled = true
+      description = "test deployment"
+      catalog_item_name = "MonoVM-Dev"
+      project_name = "GRP-CLOE-TSS-DEV"
+
+      inputs = {
+        MonoVM_cpu = 2
+        MonoVM_memory = 2048
+        MonoVM_securityTag = "DEV-TIER-APP"
+        MonoVM_service = "app"
+        MonoVM_disks = jsonencode([
+          { 
+            mountpoint = "/toto",
+            size = 10
+          },
+          { 
+            mountpoint = "/titi",
+            size = 200
+          }
+        ])
+        MonoVM_image = "DEB10X"
+        MonoVM_instances = 1
+        MonoVM_startOrder = "1"
+        leaseDays = "1"
+      }
+    },
     "test-pc-vf-3-tiers" = {
       name = "test-pc-vf-3-tiers"
+      enabled = true
       description = "test 3 tiers Dev"
       catalog_item_name = "3-tiers-Dev"
       project_name = "GRP-CLOE-TST-DEV"
diff --git a/tofu/main.tf b/tofu/main.tf
index 49320eb..1c0d2b9 100644
--- a/tofu/main.tf
+++ b/tofu/main.tf
@@ -5,6 +5,13 @@ variable "vra_refresh_token" {
   type = string
   sensitive = true
 }
+variable "vra_username" {
+  type = string
+}
+
+data "local_file" "ssh-pub-key" {
+  filename = "/home/pcaseiro/.ssh/id_rsa.pub"
+}
 
 module "deployment" {
   source = "./modules/pycloud"
@@ -14,4 +21,34 @@ module "deployment" {
   vra_insecure_ssl      = true
 
   deployments = local.deployments
+}
+
+/*
+output "flatten_vms" {
+  value = flatten([
+    for k1, deploy in module.deployment.deployments : [
+      for k2, resource in deploy.resources : { 
+        deployment_name = k1
+        vm_name = jsondecode(resource.properties_json).name
+        vm_hostname = jsondecode(resource.properties_json).hostName
+        address = jsondecode(resource.properties_json).address
+        networks = jsondecode(resource.properties_json).networks
+      } if resource.type == "Cloud.vSphere.Machine"
+    ]
+  ])
+}
+*/
+
+output "vms" {
+  value = flatten([
+    for k1, deploy in module.deployment.deployments : {
+      for k2, resource in deploy.resources : jsondecode(resource.properties_json).name => { 
+        deployment_name = k1
+        vm_name = jsondecode(resource.properties_json).name
+        vm_hostname = jsondecode(resource.properties_json).hostName
+        address = jsondecode(resource.properties_json).address
+        networks = jsondecode(resource.properties_json).networks
+      } if resource.type == "Cloud.vSphere.Machine"
+    } 
+  ])
 }
\ No newline at end of file
diff --git a/tofu/modules/pycloud/main.tf b/tofu/modules/pycloud/main.tf
index 8c889ce..23f998e 100644
--- a/tofu/modules/pycloud/main.tf
+++ b/tofu/modules/pycloud/main.tf
@@ -1,10 +1,10 @@
 data "vra_project" "projects" {
-  for_each = { for deployment in var.deployments : deployment.project_name => deployment }
+  for_each = { for deployment in var.deployments : deployment.project_name => deployment... }
   name = each.key
 }
 
 data "vra_catalog_item" "catalog" {
-  for_each = { for deployment in var.deployments : deployment.catalog_item_name => deployment }
+  for_each = { for deployment in var.deployments : deployment.catalog_item_name => deployment... }
 
   name = each.key
   expand_versions = true
@@ -12,7 +12,11 @@ data "vra_catalog_item" "catalog" {
 }
 
 resource "vra_deployment" "deployments" {
-  for_each = var.deployments
+  //for_each = { for deployment in var.deployments: deployment => deployment if deployment.enabled }
+  for_each = {
+    for name, deployment in var.deployments : name => deployment
+    if deployment.enabled
+  }
   name = each.value.name
   description = each.value.description
   
diff --git a/tofu/modules/pycloud/variables.tf b/tofu/modules/pycloud/variables.tf
index c72d968..ec33a99 100644
--- a/tofu/modules/pycloud/variables.tf
+++ b/tofu/modules/pycloud/variables.tf
@@ -39,6 +39,7 @@ variable "deployments" {
     type = map(object({
         name = string
         description = string
+        enabled = bool
         
         catalog_item_name = string
         project_name = string
diff --git a/tofu/pytofu b/tofu/pytofu
index 3e8c6e7..7922990 100755
--- a/tofu/pytofu
+++ b/tofu/pytofu
@@ -64,6 +64,7 @@ else
         if [ ! -f "${TOKENRC}" ]; then
             echo "service_url=${service_url}" >> "${TOKENRC}"
             echo "refresh_token=${refresh_token}" >> "${TOKENRC}"
+            echo "username=${username}" >> "${TOKENRC}"
             chmod 600 ${TOKENRC}
         fi
     fi
@@ -77,5 +78,6 @@ fi
 ARG1=${@:$OPTIND:1}
 
 export TF_VAR_vra_url="${service_url}"
+export TF_VAR_vra_username="${username}"
 export TF_VAR_vra_refresh_token="${refresh_token}"
-tofu ${ARG1}
\ No newline at end of file
+tofu ${ARG1} -no-color
\ No newline at end of file