From eb47bf768011f420de60e42948a76795debeeaa8 Mon Sep 17 00:00:00 2001
From: Debora Benedetto <debora.benedetto@hpe.com>
Date: Mon, 11 Jul 2022 12:18:43 +0200
Subject: [PATCH] added vms output terraform file template and generation

---
 plugin/TerraformPlugin.py                     | 38 ++++++++++++++-----
 template-location.properties                  |  1 +
 .../open_stack/virtual_machine_out.tpl        | 11 ++++++
 3 files changed, 41 insertions(+), 9 deletions(-)
 create mode 100644 templates/terraform/open_stack/virtual_machine_out.tpl

diff --git a/plugin/TerraformPlugin.py b/plugin/TerraformPlugin.py
index db19c21..6b7facf 100644
--- a/plugin/TerraformPlugin.py
+++ b/plugin/TerraformPlugin.py
@@ -6,19 +6,34 @@ def create_files(parameters, output_path):
     language = "terraform"
     provider = parameters["provider"]
     resources = parameters.keys()
-    terraform_file = create_init_file(language, provider)
+    terraform_main_file = create_init_file(language, provider)
+    terraform_out_file = ""
     for resource_name in resources:
-        logging.info("Creating template for resource '%s'", resource_name)
-        template_path = TemplateUtils.find_template_path(language, provider, resource_name)
-        if template_path:
+        logging.info("Creating output and main terraform template for resource '%s'", resource_name)
+
+        template_for_main_path = TemplateUtils.find_template_path(language, provider, resource_name)
+        template_for_output_path = TemplateUtils.find_template_path(language, provider,
+                                                                    get_resource_out_path(resource_name))
+        if template_for_main_path:
             for resource_params in parameters[resource_name]:
-                template = TemplateUtils.read_template(template_path)
+                template = TemplateUtils.read_template(template_for_main_path)
                 # resource = parameters[resource_name]
                 template_filled = TemplateUtils.edit_template(template, resource_params)
-                terraform_file = terraform_file + template_filled + "\n"
-    output_file_path = output_path + "/main.tf"
-    TemplateUtils.write_template(terraform_file, output_file_path)
-    logging.info("File available at: {}".format(output_path))
+                terraform_main_file = terraform_main_file + template_filled + "\n"
+
+        if template_for_output_path:
+            for resource_params in parameters[resource_name]:
+                template_out = TemplateUtils.read_template(template_for_output_path)
+                # resource = parameters[resource_name]
+                template_out_filled = TemplateUtils.edit_template(template_out, resource_params)
+                terraform_out_file = terraform_out_file + template_out_filled + "\n"
+
+    main_file_stored_path = output_path + "/main.tf"
+    TemplateUtils.write_template(terraform_main_file, main_file_stored_path)
+    output_file_stored_path = output_path + "/output.tf"
+    TemplateUtils.write_template(terraform_out_file, output_file_stored_path)
+    logging.info("Terraform main file available at: {}".format(main_file_stored_path))
+    logging.info(f"Terraform output file available at {output_file_stored_path}")
 
 
 def create_init_file(language, provider):
@@ -26,3 +41,8 @@ def create_init_file(language, provider):
     template_path = TemplateUtils.find_template_path(language, provider, "init")
     template = TemplateUtils.read_template(template_path)
     return template.render() + "\n"
+
+## TODO spostare i template di out in una cartella?? es. cartella vms&vms_out? altrimenti come prendo nome di out?
+## non รจ nel doml
+def get_resource_out_path(resource_name):
+    return resource_name + "_out"
diff --git a/template-location.properties b/template-location.properties
index b644572..2a462fc 100644
--- a/template-location.properties
+++ b/template-location.properties
@@ -1,6 +1,7 @@
 [terraform.openstack]
 init = templates/terraform/open_stack/init.tpl
 vms = templates/terraform/open_stack/virtual_machine.tpl
+vms_out = templates/terraform/open_stack/virtual_machine_out.tpl
 networks = templates/terraform/open_stack/network.tpl
 computingGroup = templates/terraform/open_stack/port_rule.tpl
 
diff --git a/templates/terraform/open_stack/virtual_machine_out.tpl b/templates/terraform/open_stack/virtual_machine_out.tpl
new file mode 100644
index 0000000..95fa933
--- /dev/null
+++ b/templates/terraform/open_stack/virtual_machine_out.tpl
@@ -0,0 +1,11 @@
+output "instance_server_public_key_{{ vm_key_name }}" {
+  value = openstack_compute_keypair_v2.{{ vm_key_name }}.public_key
+}
+
+output "instance_server_private_key_{{ vm_key_name }}" {
+  value = openstack_compute_keypair_v2.{{ vm_key_name }}.private_key
+}
+
+output "instance_ip" {
+  value = openstack_compute_floatingip_associate_v2.{{ infra_element_name ~ "_floating_ip_association" }}.floating_ip
+}
\ No newline at end of file
-- 
GitLab