diff --git a/app/media/terraform.tfvars b/app/media/terraform.tfvars index 4e29150c..07430754 100644 --- a/app/media/terraform.tfvars +++ b/app/media/terraform.tfvars @@ -1,103 +1,32 @@ -# Grafana Connection Variables -grafana_connection = { - "url" = "http://localhost:8080", - "auth" = "" +argocd_instance_info = { + server_addr = "http://argocd.local" + username = "username" + password = "password" + insecure = true } - -# Grafana_Contact_Point Variables -create_contact_point = true -contact_point_name = "My Contact Point" -use_email = false -use_slack = true -email_contact_point = { - addresses = ["one@company.org", "two@company.org"] - message = "{ len .Alerts.Firing } firing." - subject = "{{ template \"default.title\" .}}" - single_email = true - disable_resolve_message = false +repository_create = true +argocd_repository_info = { + repo = "https://your_repo.git" + username = "username" + password = "token" } - -slack_contact_point = { - url = "https://hooks.slack.com/" - text = < Output: diff --git a/app/routes/terraform.py b/app/routes/terraform.py index 19aeaf0a..6894ebf8 100644 --- a/app/routes/terraform.py +++ b/app/routes/terraform.py @@ -1,5 +1,6 @@ from app.app_instance import app from app.gpt_services import gpt_service +from fastapi.responses import FileResponse from app.services import ( edit_directory_generator,execute_pythonfile) @@ -58,74 +59,85 @@ async def IaC_install_generation(request:IaCInstallationInput) -> Output: @app.post("/api/IaC-template/docker") async def IaC_template_generation_docker(request:IaCTemplateGenerationDocker) -> Output: - if os.environ.get("TEST"): - return Output(output='output (nothing special)') - generated_prompt = IaC_template_generator_docker(request) - output = gpt_service(generated_prompt) - edit_directory_generator("terraform_generator",output) - execute_pythonfile("MyTerraform","terraform_generator") - return Output(output='output') + + dir = 'app/media/terraform.tfvars' + + file_response = IaC_template_generator_docker(request) + with open(dir,'w')as f: + f.write(file_response) + + return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") @app.post("/api/IaC-template/aws/ec2") async def IaC_template_generation_aws_ec2(request:IaCTemplateGenerationEC2) -> Output: - if os.environ.get("TEST"): - return Output(output='output (nothing special)') + + dir = 'app/media/terraform.tfvars' + + file_response = IaC_template_generator_ec2(request) + with open(dir,'w')as f: + f.write(file_response) + + return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") - generated_prompt = IaC_template_generator_ec2(request) - output = gpt_service(generated_prompt) - edit_directory_generator("terraform_generator",output) - execute_pythonfile("MyTerraform","terraform_generator") - return Output(output='output') @app.post("/api/IaC-template/aws/s3") async def IaC_template_generation_aws_s3(request:IaCTemplateGenerationS3) -> Output: - if os.environ.get("TEST"): - return Output(output='output (nothing special)') - generated_prompt = IaC_template_generator_s3(request) - output = gpt_service(generated_prompt) - edit_directory_generator("terraform_generator",output) - execute_pythonfile("MyTerraform","terraform_generator") - return Output(output='output') + + dir = 'app/media/terraform.tfvars' + + file_response = IaC_template_generator_s3(request) + with open(dir,'w')as f: + f.write(file_response) + + return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") + @app.post("/api/IaC-template/aws/iam") async def IaC_template_generation_aws_iam(request:IaCTemplateGenerationIAM) -> Output: - if os.environ.get("TEST"): - return Output(output='output (nothing special)') - generated_prompt = IaC_template_generator_iam(request) - output = gpt_service(generated_prompt) - edit_directory_generator("terraform_generator",output) - execute_pythonfile("MyTerraform","terraform_generator") - return Output(output='output') + + dir = 'app/media/terraform.tfvars' + + file_response = IaC_template_generator_iam(request) + with open(dir,'w')as f: + f.write(file_response) + + return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") + @app.post("/api/IaC-template/argocd") async def IaC_template_generation_argocd(request:IaCTemplateGenerationArgoCD) -> Output: - if os.environ.get("TEST"): - return Output(output='output (nothing special)') - generated_prompt = IaC_template_generator_argocd(request) - output = gpt_service(generated_prompt) - edit_directory_generator("terraform_generator",output) - execute_pythonfile("MyTerraform","terraform_generator") - return Output(output='output') + + dir = 'app/media/terraform.tfvars' + + file_response = IaC_template_generator_argocd(request) + with open(dir,'w')as f: + f.write(file_response) + + return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") @app.post("/api/IaC-template/aws/elb") async def IaC_template_generation_aws_elb(request:IaCTemplateGenerationELB) -> Output: - if os.environ.get("TEST"): - return Output(output='output (nothing special)') - generated_prompt = IaC_template_generator_elb(request) - output = gpt_service(generated_prompt) - edit_directory_generator("terraform_generator",output) - execute_pythonfile("MyTerraform","terraform_generator") - return Output(output='output') + + dir = 'app/media/terraform.tfvars' + + file_response = IaC_template_generator_elb(request) + with open(dir,'w')as f: + f.write(file_response) + + return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") + @app.post("/api/IaC-template/aws/efs") async def IaC_template_generation_aws_efs(request:IaCTemplateGenerationEFS) -> Output: - if os.environ.get("TEST"): - return Output(output='output (nothing special)') - generated_prompt = IaC_template_generator_efs(request) - output = gpt_service(generated_prompt) - edit_directory_generator("terraform_generator",output) - execute_pythonfile("MyTerraform","terraform_generator") - return Output(output='output') + + dir = 'app/media/terraform.tfvars' + + file_response = IaC_template_generator_efs(request) + with open(dir,'w')as f: + f.write(file_response) + + return FileResponse(dir, media_type='application/zip', filename=f"terraform.tfvars") + diff --git a/app/template_generators/terraform/argocd.py b/app/template_generators/terraform/argocd.py index ef8f34ec..755ca888 100644 --- a/app/template_generators/terraform/argocd.py +++ b/app/template_generators/terraform/argocd.py @@ -9,208 +9,44 @@ def IaC_template_generator_argocd(input) -> str: argocd_application_selfheal = 'true' if input.argocd_application.sync_policy.self_heal else 'false' else: argocd_create_application = 'false' - argocd_application_auto_prune = "" - argocd_application_selfheal = "" + argocd_application_auto_prune = "false" + argocd_application_selfheal = "false" - depends_on = 'depends_on = []' - if input.application_depends_repository == True: - depends_on = 'depends_on = [argocd_repository.repository]' - - prompt = f""" - Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform) - that dynamically provisions {argocd} resources ensuring a modular, flexible structure to enable users - to configure all essential settings at the root level. Only provide Python code, no explanations or - markdown formatting. The project should be organized as follows: - 1. Root Directory Structure: - - main.tf: - - Define the provider block as follows: - ``` - provider "argocd" {{ - server_addr = var.argocd_instance_info["server_addr"] - username = var.argocd_instance_info["username"] - password = var.argocd_instance_info["password"] - insecure = var.argocd_instance_info["insecure "] - }} - ``` - - Defines a module block that references "argocd" from a subdirectory within modules. - This module block should expose all variables that {argocd} resources require, allowing - configuration at the root level rather than directly within the module. - - Every variable defined in {argocd} resources should be passed through the module block, - ensuring that users can adjust all critical parameters of {argocd} resources by modifying - root main.tf. Avoid using any other parameters. just use the parameters of {argocd} resources with the same keys - - variables.tf: - - Sets this variable name for argocd provider: - argocd_instance_info(object()) as follows: - ``` - type = object({{ - server_addr = string - username = string - password = string - insecure = bool - }}) - ``` - - Sets these variables names for argocd_repository resource: - repository_create(bool), argocd_repository_info(map(string)) - - Sets these variables names for argocd_application resource: - application_create(bool), argocd_application(map(string)), argocd_sync_options(list(string)) - - terraform.tfvars: - - Structure as follows: - argocd_instance_info = {{ - server_addr = "ARGOCD_DOMAIN" - username = "admin" - password = "ARGOCD_ADMIN_PASS" - insecure = true - }} - - repository_create = {argocd_create_repository} - argocd_repository_info = {{ - repo = "https://YOUR_REPO.git" - username = "USERNAME" - password = "CHANGE_ME_WITH_TOKEN" - }} - - application_create = {argocd_create_application} - argocd_application = {{ - name = "APPLICATION_NAME" - destination_server = "https://kubernetes.default.svc" - destination_namespace = "DESTINATION_NAMESPACE" - source_repo_url = "https://YOUR_REPO.git" - source_path = "SOURCE_PATH" - source_target_revision = "SOURCE_TARGET_REVISION" - }} - - argocd_sync_options = ["CreateNamespace=true", "ApplyOutOfSyncOnly=true", "FailOnSharedResource=true"] - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - argocd = {{ - source = "oboukili/argocd" - version = ">= 6.0.2" - }} - }} - }} - 2. Module Directory Structure (modules/argocd): - - main.tf: - - Set the following parameters for argocd_repository resource (name its terraform resource to "repository") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.repository_create ? 1 : 0 - ``` - - 2. repo (type: string): follow the below syntax for repo parameter: - ``` - repo = var.argocd_repository_info["repo"] - ``` - - 3. username (type: string): follow the below syntax for username parameter: - ``` - username = var.argocd_repository_info["username"] - ``` - - 4. password (type: string): follow the below syntax for password parameter: - ``` - password = var.argocd_repository_info["password"] - ``` - - Set the following parameters for argocd_application resource (name its terraform resource to "application") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.application_create ? 1 : 0 - ``` - - 2. add depends_on block following : - ``` - {depends_on} - - ``` - - - 3. metadata (A block): Define a metadata block as follows: - ``` - metadata {{ - name = var.argocd_application["name"] - namespace = argocd - labels = {{ - using_sync_policy_options = "true" - }} - }} - ``` - - 4. spec (A block): Define a spec block as follows: - ``` - spec {{ - destination {{ - server = var.argocd_application["destination_server"] - namespace = var.argocd_application["destination_namespace"] - }} - source {{ - repo_url = var.argocd_application["source_repo_url"] - path = var.argocd_application["source_path"] - target_revision = var.argocd_application["source_target_revision"] - }} - sync_policy {{ - automated {{ - prune = {argocd_application_auto_prune} - self_heal = {argocd_application_selfheal} - }} - sync_options = var.argocd_sync_options - }} - }} - ``` - - variables.tf: - - Sets these variables names for argocd_repository resource: - repository_create(bool), argocd_repository_info(map(string)) - - Sets these variables names for argocd_application resource: - application_create(bool), argocd_application(map(string)), argocd_sync_options(list(string)) - - terraform.tfvars: - - Structure as follows: - repository_create = {argocd_create_repository} - argocd_repository_info = {{ - repo = "https://YOUR_REPO.git" - username = "USERNAME" - password = "CHANGE_ME_WITH_TOKEN" - }} - - application_create = {argocd_create_application} - argocd_application = {{ - name = "APPLICATION_NAME" - destination_server = "https://kubernetes.default.svc" - destination_namespace = "DESTINATION_NAMESPACE" - source_repo_url = "https://YOUR_REPO.git" - source_path = "SOURCE_PATH" - source_target_revision = "SOURCE_TARGET_REVISION" - }} - - argocd_sync_options = ["CreateNamespace=true", "ApplyOutOfSyncOnly=true", "FailOnSharedResource=true"] - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - argocd = {{ - source = "oboukili/argocd" - version = ">= 6.0.2" - }} - }} - }} - Ensure this project structure supports {argocd}’s configurability, extensibility, and - reusability across diverse Terraform providers, empowering users to manage their resources through a - single, customizable root configuration while keeping module internals robustly modular. - - finally just give me a python code without any note that can generate a project folder with the given - schema without ```python entry. and we dont need any base directory in the python code. the final - terraform template must work very well without any error! - - Python code you give me, must have structure like that: + + argocd_instance_info = """{ + server_addr = "http://argocd.local" + username = "username" + password = "password" + insecure = true +} + """ + argocd_repository_info = """{ + repo = "https://your_repo.git" + username = "username" + password = "token" +} + """ + argocd_application = """{ + name = "myapp" + destination_server = "https://kubernetes.default.svc" + destination_namespace = "default" + source_repo_url = "https://your_repo.git" + source_path = "myapp/manifests" + source_target_revision = "master" +} + """ + + tfvars_file = f""" +argocd_instance_info = {argocd_instance_info} - import os - project_name = "app/media/MyTerraform" - modules_dir = os.path.join(project_name, "modules") - argocd_dir = os.path.join(modules_dir, "argocd") +repository_create = {argocd_create_repository} +argocd_repository_info = {argocd_repository_info} - # Create project directories - os.makedirs(argocd_dir, exist_ok=True) +application_create = {argocd_create_application} +argocd_application = {argocd_application} - # Create main.tf - with open(os.path.join(project_name, "main.tf"), "w") as main_file: - # any thing you need +argocd_sync_options = ["CreateNamespace=true", "ApplyOutOfSyncOnly=true", "FailOnSharedResource=true"] - """ - return prompt +auto_prune = {argocd_application_auto_prune} +self_heal = {argocd_application_selfheal} """ + return tfvars_file \ No newline at end of file diff --git a/app/template_generators/terraform/aws/EFS.py b/app/template_generators/terraform/aws/EFS.py index 4f50aa4c..1b9f6f09 100644 --- a/app/template_generators/terraform/aws/EFS.py +++ b/app/template_generators/terraform/aws/EFS.py @@ -5,345 +5,39 @@ def IaC_template_generator_efs(input) -> str: aws_efs_create_file_system = 'true' if input.efs_file_system else 'false' aws_efs_create_mount_target = 'true' if input.efs_mount_target else 'false' aws_efs_create_backup_policy = 'true' if input.efs_backup_policy else 'false' + ingress_rules = """{ + efs_rule = { + description = "EFS Ingress" + from_port = 2049 + to_port = 2049 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } +}""" + egress_ruels = """ { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] +} + """ + efs = """{ + creation_token = "terraform" + encrypted = true + performance_mode = "generalPurpose" + throughput_mode = "elastic" + backup_policy = "ENABLED" +} +""" + + tfvars_file = f"""security_group_name = "efs_rule" +security_group_ingress_rules = {ingress_rules} +security_group_egress_rule = {egress_ruels} - prompt = f""" - Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform) - that dynamically provisions {efs} resources ensuring a modular, flexible structure to enable users - to configure all essential settings at the root level. Only provide Python code, no explanations or - markdown formatting. The project should be organized as follows: - 1. Root Directory Structure: - - main.tf: - - Define the provider block as follows: - ``` - provider "aws" {{ - region = "us-east-1" - }} - ``` - - Defines a module block that references "efs" from a subdirectory within modules. - Don't forget to use source parameter to call efs module as follows: - ``` - source = "./modules/efs" - ``` - This module block should expose all variables that {efs} resources require, allowing - configuration at the root level rather than directly within the module. - - Every variable defined in {efs} resources should be passed through the module block, - ensuring that users can adjust all critical parameters of {efs} resources by modifying - root main.tf. Avoid using any other parameters. just use the parameters of {efs} resources with the same keys - - variables.tf: - - Sets these variables names for aws_efs_file_system resource: - file_system_create(bool), efs(object) - - Sets these variables names for aws_efs_mount_target resource: - mount_target_create(bool) - - Sets these variables names for aws_efs_backup_policy resource: - backup_policy_create(bool) - - Sets these variables names for aws_security_group resource: - security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object()) - Sets security_group_ingress_rules as follows: - ``` - type = map(object({{ - description = string - from_port = number - to_port = number - protocol = string - cidr_blocks = list(string) - }})) - ``` - Sets security_group_egress_rule as follows: - ``` - type = object({{ - from_port = number - to_port = number - protocol = string - cidr_blocks = list(string) - }}) - ``` - Sets efs as follows: - ``` - type = object({{ - creation_token = string - encrypted = bool - performance_mode = string - throughput_mode = string - backup_policy = string - }}) - ``` - - terraform.tfvars: - - Structure as follows: - security_group_name = "efs_rule" - security_group_ingress_rules = {{ - efs_rule = {{ - description = "EFS Ingress" - from_port = 2049 - to_port = 2049 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - }} - }} - security_group_egress_rule = {{ - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - }} - - file_system_create = {aws_efs_create_file_system} - efs = {{ - creation_token = "terraform" - encrypted = true - performance_mode = "generalPurpose" - throughput_mode = "elastic" - backup_policy = "ENABLED" - }} - - mount_target_create = {aws_efs_create_mount_target} - backup_policy_create = {aws_efs_create_backup_policy} - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - aws = {{ - source = "hashicorp/aws" - version = ">= 5.20" - }} - }} - }} - 2. Module Directory Structure (modules/efs): - - main.tf: - - Create a locals block as follows: - ``` - locals {{ - default_efs_lifecycle_policies = {{ - transition_to_ia = "AFTER_14_DAYS", - transition_to_primary_storage_class = "AFTER_1_ACCESS", - }} - }} - ``` - - Create these data blocks as follows: - ``` - data "aws_availability_zones" "available_zones" {{ - state = "available" - }} - - data "aws_vpc" "default_vpc" {{ - default = true - }} - - data "aws_subnets" "subnets_ids" {{ - filter {{ - name = "vpc-id" - values = [data.aws_vpc.default_vpc.id] - }} - }} - ``` - - Set the following parameters for aws_security_group resource (name its terraform resource to "security_group") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.file_system_create && var.mount_target_create ? 1 : 0 - ``` - - 2. name: follow the below syntax for name: - ``` - name = var.security_group_name - ``` - - 3. description: follow the below syntax for description: - ``` - description = "Security group for EFS mount targets" - ``` - - 4. vpc_id: follow the below syntax for vpc_id: - ``` - vpc_id = data.aws_vpc.default_vpc.id - ``` - - 5. create a dynamic block for ingress rules as follows: - ``` - dynamic "ingress" {{ - for_each = var.security_group_ingress_rules - content {{ - description = ingress.value["description"] - from_port = ingress.value["from_port"] - to_port = ingress.value["to_port"] - protocol = ingress.value["protocol"] - cidr_blocks = ingress.value["cidr_blocks"] - }} - }} - ``` - - 6. create a block for egress rule as follows: - ``` - egress {{ - from_port = var.security_group_egress_rule["from_port"] - to_port = var.security_group_egress_rule["to_port"] - protocol = var.security_group_egress_rule["protocol"] - cidr_blocks = var.security_group_egress_rule["cidr_blocks"] - }} - ``` - - Set the following parameters for aws_efs_file_system resource (name its terraform resource to "filesystem") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.file_system_create ? 1 : 0 - ``` - - 2. creation_token (type: string): follow the below syntax for creation_token: - ``` - creation_token = var.efs["creation_token"] - ``` - - 3. encrypted (type: string): follow the below syntax for encrypted: - ``` - encrypted = var.efs["encrypted"] - ``` - - 4. performance_mode: follow the below syntax for performance_mode: - ``` - performance_mode = var.efs["performance_mode"] - ``` - - 5. throughput_mode: follow the below syntax for throughput_mode: - ``` - throughput_mode = var.efs["throughput_mode"] - ``` - - 6. create the below blocks as follows: - ``` - lifecycle_policy {{ - transition_to_ia = lookup(local.default_efs_lifecycle_policies, "transition_to_ia", null) - }} - - lifecycle_policy {{ - transition_to_primary_storage_class = lookup(local.default_efs_lifecycle_policies, "transition_to_primary_storage_class", null) - }} - - tags = {{ - Name = "terraform-efs" - }} - ``` - - Set the following parameters for aws_efs_mount_target resource (name its terraform resource to "mount_target") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.file_system_create && var.mount_target_create ? length(data.aws_availability_zones.available_zones.names) : 0 - ``` - - 2. file_system_id (type: string): follow the below syntax for file_system_id: - ``` - file_system_id = aws_efs_file_system.filesystem[0].id - ``` - - 3. subnet_id: follow the below syntax for subnet_id: - ``` - subnet_id = data.aws_subnets.subnets_ids.ids[count.index] - ``` - - 4. security_groups: follow the below syntax for security_groups: - ``` - security_groups = [aws_security_group.security_group[0].id] - ``` - - Set the following parameters for aws_efs_backup_policy resource (name its terraform resource to "backup_policy") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.file_system_create && var.backup_policy_create ? 1 : 0 - ``` - - 2. file_system_id: follow the below syntax for file_system_id: - ``` - file_system_id = aws_efs_file_system.filesystem[0].id - ``` - - 3. Create the below block as follows: - ``` - backup_policy {{ - status = var.efs["backup_policy"] - }} - ``` - - variables.tf: - - Sets these variables names for aws_efs_file_system resource: - file_system_create(bool), efs(object) - - Sets these variables names for aws_efs_mount_target resource: - mount_target_create(bool) - - Sets these variables names for aws_efs_backup_policy resource: - backup_policy_create(bool) - - Sets these variables names for aws_security_group resource: - security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object()) - Sets security_group_ingress_rules as follows: - ``` - type = map(object({{ - description = string - from_port = number - to_port = number - protocol = string - cidr_blocks = list(string) - }})) - ``` - Sets security_group_egress_rule as follows: - ``` - type = object({{ - from_port = number - to_port = number - protocol = string - cidr_blocks = list(string) - }}) - ``` - Sets efs as follows: - ``` - type = object({{ - creation_token = string - encrypted = bool - performance_mode = string - throughput_mode = string - backup_policy = string - }}) - - terraform.tfvars: - - Structure as follows: - security_group_name = "efs_rule" - security_group_ingress_rules = {{ - efs_rule = {{ - description = "EFS Ingress" - from_port = 2049 - to_port = 2049 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - }} - }} - security_group_egress_rule = {{ - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - }} - - file_system_create = {aws_efs_create_file_system} - efs = {{ - creation_token = "terraform" - encrypted = true - performance_mode = "generalPurpose" - throughput_mode = "elastic" - backup_policy = "ENABLED" - }} - - mount_target_create = {aws_efs_create_mount_target} - backup_policy_create = {aws_efs_create_backup_policy} - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - aws = {{ - source = "hashicorp/aws" - version = ">= 5.20" - }} - }} - }} - Ensure this project structure supports {efs}’s configurability, extensibility, and - reusability across diverse Terraform providers, empowering users to manage their resources through a - single, customizable root configuration while keeping module internals robustly modular. - - finally just give me a python code without any note that can generate a project folder with the given - schema without ```python entry. and we dont need any base directory in the python code. the final - terraform template must work very well without any error! - - Python code you give me, must have structure like that: - - import os - project_name = "app/media/MyTerraform" - modules_dir = os.path.join(project_name, "modules") - efs_dir = os.path.join(modules_dir, "efs") - - # Create project directories - os.makedirs(efs_dir, exist_ok=True) - - # Create main.tf - with open(os.path.join(project_name, "main.tf"), "w") as main_file: - # any thing you need +file_system_create = {aws_efs_create_file_system} +efs = {efs} - """ - return prompt +mount_target_create = {aws_efs_create_mount_target} +backup_policy_create = {aws_efs_create_backup_policy}""" + return tfvars_file \ No newline at end of file diff --git a/app/template_generators/terraform/aws/IAM.py b/app/template_generators/terraform/aws/IAM.py index cc5ebc52..e611f8ac 100644 --- a/app/template_generators/terraform/aws/IAM.py +++ b/app/template_generators/terraform/aws/IAM.py @@ -1,146 +1,26 @@ def IaC_template_generator_iam(input) -> str: - iam = ['aws_iam_user', 'aws_iam_group'] + aws_iam_create_user = 'true' if input.iam_user else 'false' aws_iam_create_group = 'true' if input.iam_group else 'false' - - prompt = f""" - Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform) - that dynamically provisions {iam} resources ensuring a modular, flexible structure to enable users - to configure all essential settings at the root level. Only provide Python code, no explanations or - markdown formatting. The project should be organized as follows: - 1. Root Directory Structure: - - main.tf: - - Define the provider block as follows: - ``` - provider "aws" {{ - region = "us-east-1" - }} - ``` - - Defines a module block that references "iam" from a subdirectory within modules. - This module block should expose all variables that {iam} resources require, allowing - configuration at the root level rather than directly within the module. - - Every variable defined in {iam} resources should be passed through the module block, - ensuring that users can adjust all critical parameters of {iam} resources by modifying - root main.tf. Avoid using any other parameters. just use the parameters of {iam} resources with the same keys - - variables.tf: - - Sets these variables names for aws_iam_user resource: - iam_create_user(bool), iam_users(list(map(string))) - - Sets these variables names for aws_iam_group resource: - iam_create_group(bool), iam_groups(list(map(string))) - - terraform.tfvars: - - Structure as follows: - iam_create_user = {aws_iam_create_user} - iam_users = [ - {{ - name = "devopshobbies" - path = "/" - }} - ] - - iam_create_group = {aws_iam_create_group} - iam_groups = [ - {{ - name = "developers" - path = "/" - }} - ] - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - aws = {{ - source = "hashicorp/aws" - version = ">= 5.20" - }} - }} - }} - 2. Module Directory Structure (modules/iam): - - main.tf: - - Set the following parameters for aws_iam_user resource (name its terraform resource to "users") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.iam_create_user ? length(var.iam_users) : 0 - ``` - - 2. name (type: string): follow the below syntax for name parameter: - ``` - name = var.iam_users[count.index]["name"] - ``` - - 3. path (type: string): follow the below syntax for path parameter: - ``` - path = var.iam_users[count.index]["path"] - ``` - - Set the following parameters for aws_iam_group resource (name its terraform resource to "groups") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.iam_create_group ? length(var.iam_groups) : 0 - ``` - - 2. name (type: string): follow the below syntax for name parameter: - ``` - name = var.iam_groups[count.index]["name"] - ``` - - 3. path (type: string): follow the below syntax for path parameter: - ``` - path = var.iam_groups[count.index]["path"] - ``` - - variables.tf: - - Sets these variables names for aws_iam_user resource: - iam_create_user(bool), iam_users(list(map(string))) - - Sets these variables names for aws_iam_group resource: - iam_create_group(bool), iam_groups(list(map(string))) - - terraform.tfvars: - - Structure as follows: - iam_create_user = {aws_iam_create_user} - iam_users = [ - {{ - name = "devopshobbies" - path = "/" - }} - ] - - iam_create_group = {aws_iam_create_group} - iam_groups = [ - {{ - name = "developers" - path = "/" - }} - ] - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - aws = {{ - source = "hashicorp/aws" - version = ">= 5.20" - }} - }} - }} - Ensure this project structure supports {iam}’s configurability, extensibility, and - reusability across diverse Terraform providers, empowering users to manage their resources through a - single, customizable root configuration while keeping module internals robustly modular. - - finally just give me a python code without any note that can generate a project folder with the given - schema without ```python entry. and we dont need any base directory in the python code. the final - terraform template must work very well without any error! - - Python code you give me, must have structure like that: - - import os - project_name = "app/media/MyTerraform" - modules_dir = os.path.join(project_name, "modules") - iam_dir = os.path.join(modules_dir, "iam") - - # Create project directories - os.makedirs(iam_dir, exist_ok=True) - - # Create main.tf - with open(os.path.join(project_name, "main.tf"), "w") as main_file: - # any thing you need - - """ - return prompt + iam_user = """ { + name = "devopshobbies" + path = "/" + }""" + iam_groups = """{ + name = "developers" + path = "/" + } + """ + + tfvars_file = f"""iam_create_user = {aws_iam_create_user} +iam_users = [ + {iam_user} +] + +iam_create_group = {aws_iam_create_group} +iam_groups = [ + {iam_groups} +]""" + return tfvars_file \ No newline at end of file diff --git a/app/template_generators/terraform/aws/ec2.py b/app/template_generators/terraform/aws/ec2.py index d8e25f82..47d23186 100644 --- a/app/template_generators/terraform/aws/ec2.py +++ b/app/template_generators/terraform/aws/ec2.py @@ -1,310 +1,44 @@ def IaC_template_generator_ec2(input) -> str: - ec2 = ['aws_key_pair', 'aws_security_group', 'aws_instance', 'aws_ami_from_instance'] + aws_ec2_create_key_pair = 'true' if input.key_pair else 'false' aws_ec2_create_security_group = 'true' if input.security_group else 'false' aws_ec2_create_instance = 'true' if input.aws_instance else 'false' aws_ec2_create_ami_from_instance = 'true' if input.ami_from_instance else 'false' - - - prompt = f""" - Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform) - that dynamically provisions {ec2} resources ensuring a modular, flexible structure to enable users - to configure all essential settings at the root level. Only provide Python code, no explanations or - markdown formatting. The project should be organized as follows: - 1. Root Directory Structure: - - main.tf: - - Define the provider block as follows: - ``` - provider "aws" {{ - region = "us-east-1" - }} - ``` - - Defines a module block that references "ec2" from a subdirectory within modules. - Don't forget to use source parameter to call ec2 module as follows: - ``` - source = "./modules/ec2" - ``` - This module block should expose all variables that {ec2} resources require, allowing - configuration at the root level rather than directly within the module. - - Every variable defined in {ec2} resources should be passed through the module block, - ensuring that users can adjust all critical parameters of {ec2} resources by modifying - root main.tf. Avoid using any other parameters. just use the parameters of {ec2} resources with the same keys - - variables.tf: - - Sets these variables names for aws_key_pair resource: - key_pair_create(bool), key_pair_name(string) - - Sets these variables names for aws_security_group resource: - security_group_create(bool), security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object()) - Sets security_group_ingress_rules as follows: - ``` - type = map(object({{ - description = string - from_port = number - to_port = number - protocol = string - cidr_blocks = list(string) - }})) - ``` - Sets security_group_egress_rule as follows: - ``` - type = object({{ - from_port = number - to_port = number - protocol = string - cidr_blocks = list(string) - }}) - ``` - - Sets these variables names for aws_instance resource: - instance_create(bool), instance_type(string) - - Sets these variables names for aws_ami_from_instance resource: - ami_from_instance_create(bool), ami_name(string) - - terraform.tfvars: - - Structure as follows: - key_pair_create = {aws_ec2_create_key_pair} - key_pair_name = "ec2" - - security_group_create = {aws_ec2_create_security_group} - security_group_name = "my_rules" - security_group_ingress_rules = {{ - ssh_rule = {{ - description = "SSH Ingress" - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - }}, - http_rule = {{ - description = "HTTP Ingress" - from_port = 80 - to_port = 80 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - }} - }} - security_group_egress_rule = {{ - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - }} - - instance_create = {aws_ec2_create_instance} - instance_type = "t2.micro" - - ami_from_instance_create = {aws_ec2_create_ami_from_instance} - ami_name = "my-own-ami" - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - aws = {{ - source = "hashicorp/aws" - version = ">= 5.20" - }} - }} - }} - 2. Module Directory Structure (modules/ec2): - - create an empty file called "terraform.pub" to store the public key for key_pair resource - - main.tf: - - Create the below data block: - ``` - data "aws_ami" "linux" {{ - most_recent = true - owners = ["amazon"] - - filter {{ - name = "name" - values = ["al2023-ami-2023*kernel-6.1-x86_64"] - }} - - filter {{ - name = "root-device-type" - values = ["ebs"] - }} - - filter {{ - name = "virtualization-type" - values = ["hvm"] - }} - }} - ``` - - Set the following parameters for aws_key_pair resource (name its terraform resource to "key_pair") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.key_pair_create ? 1 : 0 - ``` - - 2. key_name (type: string): follow the below syntax for key_name: - ``` - key_name = var.key_pair_name - ``` - - 3. public_key (type: string): follow the below syntax for public_key, avoid generating double brackets {{}} for path.module in the below syntax: - ``` - public_key = file("${{path.module}}/terraform.pub") - ``` - - Set the following parameters for aws_security_group resource (name its terraform resource to "security_group") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.security_group_create ? 1 : 0 - ``` - - 2. name: follow the below syntax for name: - ``` - name = var.security_group_name - ``` - - 3. create a dynamic block for ingress rules as follows: - ``` - dynamic "ingress" {{ - for_each = var.security_group_ingress_rules - content {{ - description = ingress.value["description"] - from_port = ingress.value["from_port"] - to_port = ingress.value["to_port"] - protocol = ingress.value["protocol"] - cidr_blocks = ingress.value["cidr_blocks"] - }} - }} - ``` - - 4. create a block for egress rule as follows: - ``` - egress {{ - from_port = var.security_group_egress_rule["from_port"] - to_port = var.security_group_egress_rule["to_port"] - protocol = var.security_group_egress_rule["protocol"] - cidr_blocks = var.security_group_egress_rule["cidr_blocks"] - }} - ``` - - Set the following parameters for aws_instance resource (name its terraform resource to "instance") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.instance_create ? 1 : 0 - ``` - - 2. ami (type: string): follow the below syntax for ami, it uses the data block: - ``` - ami = data.aws_ami.linux.id - ``` - - 3. instance_type (type: string): follow the below syntax for instance_type: - ``` - instance_type = var.instance_type - ``` - - 4. key_name: follow the below syntax for key_name: - ``` - key_name = var.key_pair_create ? aws_key_pair.key_pair[0].key_name : null - ``` - - 5. vpc_security_group_ids: follow the below syntax for vpc_security_group_ids: - ``` - vpc_security_group_ids = var.security_group_create ? [aws_security_group.security_group[0].id] : null - ``` - - Set the following parameters for aws_ami_from_instance resource (name its terraform resource to "ami") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.instance_create && var.ami_from_instance_create ? 1 : 0 - ``` - - 2. name (type: string): follow the below syntax for name: - ``` - name = var.ami_name - ``` - - 3. source_instance_id: follow the below syntax for source_instance_id: - ``` - source_instance_id = aws_instance.instance[0].id - ``` - - variables.tf: - - Sets these variables names for aws_key_pair resource: - key_pair_create(bool), key_pair_name(string) - - Sets these variables names for aws_security_group resource: - security_group_create(bool), security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object()) - Sets security_group_ingress_rules as follows: - ``` - type = map(object({{ - description = string - from_port = number - to_port = number - protocol = string - cidr_blocks = list(string) - }})) - ``` - Sets security_group_egress_rule as follows: - ``` - type = object({{ - from_port = number - to_port = number - protocol = string - cidr_blocks = list(string) - }}) - ``` - - Sets these variables names for aws_instance resource: - instance_create(bool), instance_type(string) - - Sets these variables names for aws_ami_from_instance resource: - ami_from_instance_create(bool), ami_name(string) - - terraform.tfvars: - - Structure as follows: - key_pair_create = {aws_ec2_create_key_pair} - key_pair_name = "ec2" - - security_group_create = {aws_ec2_create_security_group} - security_group_name = "my_rules" - security_group_ingress_rules = {{ - ssh_rule = {{ - description = "SSH Ingress" - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - }}, - http_rule = {{ - description = "HTTP Ingress" - from_port = 80 - to_port = 80 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - }} - }} - security_group_egress_rule = {{ - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - }} - - instance_create = {aws_ec2_create_instance} - instance_type = "t2.micro" - - ami_from_instance_create = {aws_ec2_create_ami_from_instance} - ami_name = "my-own-ami" - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - aws = {{ - source = "hashicorp/aws" - version = ">= 5.20" - }} - }} - }} - Ensure this project structure supports {ec2}’s configurability, extensibility, and - reusability across diverse Terraform providers, empowering users to manage their resources through a - single, customizable root configuration while keeping module internals robustly modular. - - finally just give me a python code without any note that can generate a project folder with the given - schema without ```python entry. and we dont need any base directory in the python code. the final - terraform template must work very well without any error! - - Python code you give me, must have structure like that: - - import os - project_name = "app/media/MyTerraform" - modules_dir = os.path.join(project_name, "modules") - ec2_dir = os.path.join(modules_dir, "ec2") - - # Create project directories - os.makedirs(ec2_dir, exist_ok=True) - - # Create main.tf - with open(os.path.join(project_name, "main.tf"), "w") as main_file: - # any thing you need - - """ - return prompt + ingress_rules = """{ + ssh_rule = { + description = "SSH Ingress" + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + }, + http_rule = { + description = "HTTP Ingress" + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } +}""" + egress_rules = """{ + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] +}""" + + tfvars_file = f"""key_pair_create = {aws_ec2_create_key_pair} +key_pair_name = "ec2" + +security_group_create = {aws_ec2_create_security_group} +security_group_name = "my_rules" +security_group_ingress_rules = {ingress_rules} +security_group_egress_rule = {egress_rules} +instance_create = {aws_ec2_create_instance} +instance_type = "t2.micro" + +ami_from_instance_create = {aws_ec2_create_ami_from_instance} +ami_name = "my-own-ami" """ + return tfvars_file \ No newline at end of file diff --git a/app/template_generators/terraform/aws/s3.py b/app/template_generators/terraform/aws/s3.py index 21ade364..1acbd0f4 100644 --- a/app/template_generators/terraform/aws/s3.py +++ b/app/template_generators/terraform/aws/s3.py @@ -4,130 +4,16 @@ def IaC_template_generator_s3(input) -> str: aws_s3_create_bucket = 'true' if input.s3_bucket else 'false' aws_s3_create_bucket_versioning = 'true' if input.bucket_versioning else 'false' - - prompt = f""" - Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform) - that dynamically provisions {s3} resources ensuring a modular, flexible structure to enable users - to configure all essential settings at the root level. Only provide Python code, no explanations or - markdown formatting. The project should be organized as follows: - 1. Root Directory Structure: - - main.tf: - - Define the provider block as follows: - ``` - provider "aws" {{ - region = "us-east-1" - }} - ``` - - Defines a module block that references "s3" from a subdirectory within modules. - This module block should expose all variables that {s3} resources require, allowing - configuration at the root level rather than directly within the module. - - Every variable defined in {s3} resources should be passed through the module block, - ensuring that users can adjust all critical parameters of {s3} resources by modifying - root main.tf. Avoid using any other parameters. just use the parameters of {s3} resources with the same keys - - variables.tf: - - Sets these variables names for aws_s3_bucket resource: - s3_create_bucket(bool), s3_bucket_name(string), s3_bucket_force_destroy(bool), s3_bucket_tags(map(string)) - - Sets these variables names for aws_s3_bucket_versioning resource: - s3_create_bucket_versioning(bool), s3_bucket_versioning_status(string) - - terraform.tfvars: - - Structure as follows: - s3_create_bucket = {aws_s3_create_bucket} - s3_bucket_name = "UniqueName" - s3_bucket_force_destroy = false - s3_bucket_tags = {{ - Name = "My bucket" - Environment = "Dev" - }} - s3_create_bucket_versioning = {aws_s3_create_bucket_versioning} - s3_bucket_versioning_status = "Enabled" - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - aws = {{ - source = "hashicorp/aws" - version = ">= 5.20" - }} - }} - }} - 2. Module Directory Structure (modules/s3): - - main.tf: - - Set the following parameters for aws_s3_bucket resource (name its terraform resource to "s3_bucket")and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.s3_create_bucket ? 1 : 0 - ``` - - 2. bucket (type: string): Specifies the bucket name. - - 3. force_destroy (type: boolean): Indicates all objects should be deleted from the bucket when the bucket is destroyed - - 4. tags (map(string) type): Includes the following fields: - - Name (type: string): A tag for the bucket. - - Environment (type: string): A tag for the bucket - - Set the following parameters for aws_s3_bucket_versioning resource (name its terraform resource to "s3_bucket_versioning")and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.s3_create_bucket && var.s3_create_bucket_versioning ? 1 : 0 - ``` - - 2. bucket: it must points to the s3_bucket resource like the following syntax: - ``` - bucket = aws_s3_bucket.s3_bucket[0].id - ``` - - 3. versioning_configuration: this is a block which has a key/value pair as follows: - ``` - versioning_configuration {{ - status = var.s3_bucket_versioning_status - }} - ``` - - variables.tf: - - Sets these variables names for aws_s3_bucket resource: - s3_create_bucket(bool), s3_bucket_name(string), s3_bucket_force_destroy(bool), s3_bucket_tags(map(string)) - - Sets these variables names for aws_s3_bucket_versioning resource: - s3_create_bucket_versioning(bool), s3_bucket_versioning_status(string) - - terraform.tfvars: - - Structure as follows: - s3_create_bucket = {aws_s3_create_bucket} - s3_bucket_name = "UniqueName" - s3_bucket_force_destroy = false - s3_bucket_tags = {{ - Name = "My bucket" - Environment = "Dev" - }} - s3_create_bucket_versioning = {aws_s3_create_bucket_versioning} - s3_bucket_versioning_status = "Enabled" - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - aws = {{ - source = "hashicorp/aws" - version = ">= 5.20" - }} - }} - }} - Ensure this project structure supports {s3}’s configurability, extensibility, and - reusability across diverse Terraform providers, empowering users to manage their resources through a - single, customizable root configuration while keeping module internals robustly modular. - - finally just give me a python code without any note that can generate a project folder with the given - schema without ```python entry. and we dont need any base directory in the python code. the final - terraform template must work very well without any error! - - Python code you give me, must have structure like that: - - import os - project_name = "app/media/MyTerraform" - modules_dir = os.path.join(project_name, "modules") - s3_dir = os.path.join(modules_dir, "s3") - - # Create project directories - os.makedirs(s3_dir, exist_ok=True) - - # Create main.tf - with open(os.path.join(project_name, "main.tf"), "w") as main_file: - # any thing you need - - """ - return prompt + bucket_tags = """{ + Name = "My bucket" + Environment = "Dev" +}""" + + tfvars_file = f""" +s3_create_bucket = {aws_s3_create_bucket} +s3_bucket_name = "UniqueName" +s3_bucket_force_destroy = false +s3_bucket_tags = {bucket_tags} +s3_create_bucket_versioning = {aws_s3_create_bucket_versioning} +s3_bucket_versioning_status = "Enabled" """ + return tfvars_file \ No newline at end of file diff --git a/app/template_generators/terraform/docker.py b/app/template_generators/terraform/docker.py index 3bad951f..ac0ae4b2 100644 --- a/app/template_generators/terraform/docker.py +++ b/app/template_generators/terraform/docker.py @@ -1,135 +1,24 @@ def IaC_template_generator_docker(input) -> str: - docker = ['docker_container', 'docker_image'] + create_docker_image = 'true' if input.docker_image else 'false' create_docker_container = 'true' if input.docker_container else 'false' - - prompt = f""" - Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform) - that dynamically provisions {docker} resources ensuring a modular, flexible structure to enable users - to configure all essential settings at the root level. Only provide Python code, no explanations or - markdown formatting. The project should be organized as follows: - 1. Root Directory Structure: - - main.tf: - - Define the provider block as follows: - ``` - provider "docker" {{ - host = "unix:///var/run/docker.sock" - }} - ``` - - Defines a module block that references "docker" from a subdirectory within modules. - This module block should expose all variables that {docker} resources require, allowing - configuration at the root level rather than directly within the module. - - Every variable defined in {docker} resources should be passed through the module block, - ensuring that users can adjust all critical parameters of {docker} resources by modifying - root main.tf. Avoid using any other parameters. just use the parameters of {docker} resources with the same keys - - variables.tf: - - Sets these variables names for docker_image resource: - create_image(bool), image_name(string), image_force_remove(bool), image_build(object) - - Sets these variables names for docker_container resource: - create_container(bool), container_image(string), container_name(string), container_hostname(string), container_restart(string) - - terraform.tfvars: - - Structure as follows: - create_image = {create_docker_image} - image_name = "my-image" - image_force_remove = true - image_build = {{ - context = "./" - tag = ["my-image:latest"] - }} - - create_container = {create_docker_container} - container_image = "my-image" - container_name = "my-container" - container_hostname = "my-host" - container_restart = "always" - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - docker = {{ - source = "kreuzwerker/docker" - version = ">= 2.8.0" - }} - }} - }} - 2. Module Directory Structure (modules/docker): - - main.tf: - - Set the following parameters for docker_image resource (name its terraform resource to "image") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.create_image ? 1 : 0 - ``` - - 2. name (type: string): Specifies the image name. - - 3. force_remove (type: boolean): Determines whether to forcibly remove intermediate containers. - - 4. build (block type): Includes the following required field: - - context (type: string, required): Specifies the build context for the image. - - tag(type: List of Strings, required): Specifices the image tag in the 'name:tag' - format, (e.g., ["NAME:VERSION"]) - - Set the following parameters for docker_container resource (name its terraform resource to "container") and avoid using any other parameters: - - 1. count (type: number): follow the below syntax for count: - ``` - count = var.create_container ? 1 : 0 - ``` - - 2. image (type: string): Specifies the container image. - - 3. name (type: string): Sets the container name. - - 4. hostname (type: string): Configures the container hostname. - - 5. restart (type: string): Defines the container's restart policy (e.g., always, on-failure, no). - - variables.tf: - - Sets these variables names for docker_image resource: - create_image(bool), image_name(string), image_force_remove(bool), image_build(object) - - Sets these variables names for docker_container resource: - create_container(bool), container_image(string), container_name(string), container_hostname(string), container_restart(string) - - terraform.tfvars: - - Structure as follows: - create_image = {create_docker_image} - image_name = "my-image" - image_force_remove = true - image_build = {{ - context = "./" - tag = ["my-image:latest"] - }} - - create_container = {create_docker_container} - container_image = "my-image" - container_name = "my-container" - container_hostname = "my-host" - container_restart = "always" - - versions.tf: - - Structure as follows: - terraform {{ - required_version = ">= 1.0" - - required_providers {{ - docker = {{ - source = "kreuzwerker/docker" - version = ">= 2.8.0" - }} - }} - }} - Ensure this project structure supports {docker}’s configurability, extensibility, and - reusability across diverse Terraform providers, empowering users to manage their resources through a - single, customizable root configuration while keeping module internals robustly modular. - - finally just give me a python code without any note that can generate a project folder with the given - schema without ```python entry. and we dont need any base directory in the python code. the final - terraform template must work very well without any error! - - Python code you give me, must have structure like that: - - import os - project_name = "app/media/MyTerraform" - modules_dir = os.path.join(project_name, "modules") - docker_container_dir = os.path.join(modules_dir, "docker_container") - - # Create project directories - os.makedirs(docker_container_dir, exist_ok=True) - - # Create main.tf - with open(os.path.join(project_name, "main.tf"), "w") as main_file: - # any thing you need - - """ - return prompt + image_build = """{ + context = "./" + tag = ["my-image:latest"] +} +""" + tfvars_file = f"""create_image = {create_docker_image} +image_name = "my-image" +image_force_remove = true +image_build = {image_build} + +create_container = {create_docker_container} +container_image = "my-image" +container_name = "my-container" +container_hostname = "my-host" +container_restart = "always" + + + """ + return tfvars_file \ No newline at end of file