Skip to content

Commit

Permalink
fix(terraform_prompt): modify prompt to generate tf resources based o…
Browse files Browse the repository at this point in the history
…n a standard structure
  • Loading branch information
mohammadll committed Nov 10, 2024
1 parent c4941ad commit 9ef9a5a
Showing 1 changed file with 109 additions and 22 deletions.
131 changes: 109 additions & 22 deletions app/prompt_generators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .models import (IaCBasicInput,
from .models import (IaCBasicInput,
IaCBugfixInput,
IaCInstallationInput, IaCTemplateGeneration,HelmTemplateGeneration)

Expand Down Expand Up @@ -38,21 +38,110 @@ def IaC_installation_generator(input : IaCInstallationInput) -> str:
def IaC_template_generator(input : IaCTemplateGeneration) -> str:

prompt = f"""
generate a complex template for {input.service} base on standard template with project structure focus on
these important points, Only provide Python code, no explanations or markdown formatting.:
Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
that dynamically provisions resources for {input.base_config} 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:
- Contains a provider block, configured with flexible variables where required to allow
users to specify provider settings without hardcoding.
- Defines a module block that references {input.base_config} from a subdirectory within
modules. This module block should expose all variables that {input.base_config} requires,
allowing configuration at the root level rather than directly within the module.
- Every variable defined in {input.base_config} should be passed through the module block,
ensuring that users can adjust all critical parameters of {input.base_config} by
modifying root main.tf.
- variables.tf:
- Declares all variables that users might need to configure for {input.base_config}.
These should include any inputs required by the provider or the {input.base_config}
resource, as well as optional parameters that users may want to customize.
- Variable descriptions should clearly indicate their purpose, and default values should
be avoided unless there are reasonable, common defaults, to maintain flexibility and
encourage explicit configuration.
- All types of variables can be used such as (number, string, bool, list(string),
map(string), list(map(string)), map(map(string)), object(), any)
- terraform.tfvars:
- Provides default values for variables declared in the root `variables.tf`, making it easy
for users to define common configurations.
- This file should be structured to include any typical default settings without hardcoding
sensitive values.
- versions.tf:
- Contains the `terraform` and `provider` blocks, specifying required versions.
- Structure the `terraform` block as:
terraform {{
required_version = ">= 1.0"
required_providers {{
<provider_name> = {{
source = "<source>"
version = ">= <version>"
}}
}}
}}
- outputs.tf:
- Exposes relevant outputs from {input.base_config}, retrieving them from the module output
and making them accessible at the root level. Each output should have a clear description,
making it easy for users to understand the value and purpose of the output data.
2. Module Directory Structure (modules/{input.base_config}):
- main.tf:
- Defines the {input.base_config} resource, fully utilizing required and also some optional
parameters. Avoid any hardcoded values within the module to support full configurability
from the root level.
- Required parameters should cover the essentials for creating {input.base_config}, while
optional parameters should provide a range of additional settings that enable more
granular configuration if desired.
- variables.tf:
- Lists all variables necessary for configuring {input.base_config}, with descriptions and
types specified to improve usability. No default values should be set here unless
necessary for required fields.
- Variable names should be clear and consistent with naming conventions in the root
variables file, ensuring consistency in usage.
- terraform.tfvars:
- Includes default values for module-level variables to ensure that common parameters have
defaults. This `terraform.tfvars` file within the module should be structured to provide
typical configuration values, making it easier to set up and reducing the need for hardcoded values.
- versions.tf:
- Contains the `terraform` and `provider` blocks, specifying required versions.
- Structure the `terraform` block as:
terraform {{
required_version = ">= 1.0"
required_providers {{
<provider_name> = {{
source = "<source>"
version = ">= <version>"
}}
}}
}}
- outputs.tf:
- Specifies outputs for the {input.base_config} resource, selecting relevant data that users
might need to access from the root level. Each output should have an informative
description, so users can understand its purpose and utility.
Ensure this project structure supports {input.base_config}’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"
moduels_dir = os.path.join(project_name, "modules")
# Create project directories
os.makedirs(moduels_dir, exist_ok=True)
# Create main.tf (for example)
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
# any thing you need
1 - CI pipeline integration = {input.CI_integration}
2 - base config is {input.base_config}
3 - project name is app/media/MyTerraform
4 - number of modules = 1
if CI integration is true, generate a pipeline based on github actions.
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!
"""
return prompt

Expand All @@ -67,12 +156,12 @@ def helm_template_generator(input : HelmTemplateGeneration) -> str:
status = [{i.name:i.stateless} for i in input.pods]
ingress_ = [{i.name:i.ingress} for i in input.pods]

prompt = f"""
generate a correct python code to generate a helm project structure (project name: app/media/MyHelm)
prompt = f"""
generate a correct python code to generate a helm project structure (project name: app/media/MyHelm)
based on the latest version of helm chart. Only provide Python code, no explanations or markdown formatting.
just generate a code to generate a folder as project template. don't consider base_dir
consider these directories : [charts/,templates/]
consider these files : Chart.yaml & values.yaml
in the templates/ directory create these directories: {templates}.
Expand All @@ -89,19 +178,17 @@ def helm_template_generator(input : HelmTemplateGeneration) -> str:
name=value
)
initialize ingress with a default host for pod if the pod ingress is true in here {ingress_}.
set stateless in pod based on {status}.
set stateless in pod based on {status}.
Based on values.yaml, create all necessary Kubernetes templates in the templates directory:
if stateless.enabled is true, create deployment.yaml; if stateless.enabled is false, create statefulset.yaml.
If a persistence block exists, include pvc.yaml. If the ingress block is defined and ingress.enabled
is true, create ingress.yaml. if ingress.enabled is false, do not create ingress.yaml. Always create
is true, create ingress.yaml. if ingress.enabled is false, do not create ingress.yaml. Always create
secrets.yaml for secure data storage.
Ensure each template is fully parameterized to match values from values.yaml for flexible configuration.
in the final stage, put helpers.tpl in all templates and set the content based on information given.
"""
return prompt


0 comments on commit 9ef9a5a

Please sign in to comment.