Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(terraform): add outputs.tf endpoint value generator #228

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions terraform/scripts/generate_integrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import yaml
import sys


def parse_yaml(file_path):
"""Parse the YAML file and return the data"""
with open(file_path, "r") as file:
return yaml.safe_load(file)


def format_keys(section):
"""
Format the keys by replacing hyphens with underscores
Although HCL allows dashes in keys, this does not allow for dot notation
"""
return {key.replace("-", "_"): key for key in section}


def generate_output(data):
"""Generate the output string based on the parsed YAML data"""
requires = data.get("requires", {})
provides = data.get("provides", {})

# Format the keys in the 'requires' and 'provides' sections
requires_formatted = format_keys(requires)
provides_formatted = format_keys(provides)

# Prepare the output string
output = 'output "endpoints" {\n value = {\n'

# Add requires
output += " # Requires\n"
for key, value in requires_formatted.items():
output += f' {key:<20} = "{value}",\n'

# Add provides
output += " # Provides\n"
for key, value in provides_formatted.items():
output += f' {key:<20} = "{value}",\n'

output += " }\n}"
# TODO: Create a function for `terraform format` of the output
return output


def main():
"""Main function to handle script execution"""
if len(sys.argv) < 2:
print("Usage: python script.py <path_to_yaml>")
sys.exit(1)

yaml_file_path = sys.argv[1]

# Parse the YAML file
try:
data = parse_yaml(yaml_file_path)
except Exception as e:
print(f"Error reading YAML file: {e}")
sys.exit(1)

# Generate the output and print it
output = generate_output(data)
print(output)


if __name__ == "__main__":
main()
116 changes: 116 additions & 0 deletions terraform/scripts/hcl-generator/generate_integrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import yaml
import sys
import hcl2
import os
import argparse
from pathlib import Path

OUTPUTS_TF_FILE = "outputs.tf"


def build_parser():
"""Create and configure the argument parser for the script."""

parser = argparse.ArgumentParser(description="Process YAML and generate Terraform output.")

parser.add_argument(
'--input-yaml',
required=True,
help='Path to the input YAML file (charmcraft.yaml or metadata.yaml)'
)

parser.add_argument(
'--outputs-tf',
default=OUTPUTS_TF_FILE,
help=f'Path to the output Terraform file (default: {OUTPUTS_TF_FILE})'
)

return parser


class HCLGenerator(object):
def __init__(self, input_yaml: str, outputs_tf: str):
self.input_yaml = Path(input_yaml)
if outputs_tf == OUTPUTS_TF_FILE:
self.outputs_tf = Path(os.getcwd()) / outputs_tf
else:
self.outputs_tf = Path(outputs_tf)

def parse_yaml(self):
"""Parse the YAML file and return the data"""
try:
with open(self.input_yaml, "r") as file:
return yaml.safe_load(file)
except Exception as e:
print(f"Error reading YAML file: {e}")
sys.exit(1)

@staticmethod
def format_keys(section):
"""
Format the keys by replacing hyphens with underscores
Although HCL allows dashes in keys, this does not allow for dot notation
"""
return {key.replace("-", "_"): key for key in section}

def create_file_with_parents(self):
"""Create the file and parent directories if they don't exist"""
self.outputs_tf.parent.mkdir(parents=True, exist_ok=True)
self.outputs_tf.touch(exist_ok=True)

def generate_output(self, data):
"""Generate the output string based on the parsed YAML data"""
requires = data.get("requires", {})
provides = data.get("provides", {})

# Format the keys in the 'requires' and 'provides' sections
requires_formatted = self.format_keys(requires)
provides_formatted = self.format_keys(provides)

# Prepare the output string
output = 'output "endpoints" {\n value = {\n'

# Add requires
output += " # Requires\n"
for key, value in requires_formatted.items():
output += f' {key:<20} = "{value}",\n'

# Add provides
output += " # Provides\n"
for key, value in provides_formatted.items():
output += f' {key:<20} = "{value}",\n'

output += " }\n}"
# TODO: Create a function for `terraform format` of the output
return output

def write_hcl_file(self, hcl_content):
"""Parse the YAML file and return the data"""

self.create_file_with_parents()

with open(self.outputs_tf, "r") as file:
read_obj = hcl2.load(file)

if read_obj:
print("WE HAD CONTENTS")

with open(self.outputs_tf, "w") as file:
file.write(hcl_content)


def main():
"""Main function to handle script execution"""

parser = build_parser()
args = parser.parse_args()

gen = HCLGenerator(args.input_yaml, args.outputs_tf)
data = gen.parse_yaml()
output_raw = gen.generate_output(data)
print(output_raw)
gen.write_hcl_file(output_raw)


if __name__ == "__main__":
main()