Skip to content

Commit 13ddcb8

Browse files
authored
Merge pull request #53 from mohammadll/terraform_prompt
fix(terraform_prompt): modify prompt to generate tf resources based o…
2 parents c4941ad + 9ef9a5a commit 13ddcb8

File tree

1 file changed

+109
-22
lines changed

1 file changed

+109
-22
lines changed

app/prompt_generators.py

Lines changed: 109 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .models import (IaCBasicInput,
1+
from .models import (IaCBasicInput,
22
IaCBugfixInput,
33
IaCInstallationInput, IaCTemplateGeneration,HelmTemplateGeneration)
44

@@ -38,21 +38,110 @@ def IaC_installation_generator(input : IaCInstallationInput) -> str:
3838
def IaC_template_generator(input : IaCTemplateGeneration) -> str:
3939

4040
prompt = f"""
41-
generate a complex template for {input.service} base on standard template with project structure focus on
42-
these important points, Only provide Python code, no explanations or markdown formatting.:
41+
Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
42+
that dynamically provisions resources for {input.base_config} ensuring a modular, flexible structure
43+
to enable users to configure all essential settings at the root level. Only provide Python code,
44+
no explanations or markdown formatting. The project should be organized as follows:
45+
1. Root Directory Structure:
46+
- main.tf:
47+
- Contains a provider block, configured with flexible variables where required to allow
48+
users to specify provider settings without hardcoding.
49+
- Defines a module block that references {input.base_config} from a subdirectory within
50+
modules. This module block should expose all variables that {input.base_config} requires,
51+
allowing configuration at the root level rather than directly within the module.
52+
- Every variable defined in {input.base_config} should be passed through the module block,
53+
ensuring that users can adjust all critical parameters of {input.base_config} by
54+
modifying root main.tf.
55+
- variables.tf:
56+
- Declares all variables that users might need to configure for {input.base_config}.
57+
These should include any inputs required by the provider or the {input.base_config}
58+
resource, as well as optional parameters that users may want to customize.
59+
- Variable descriptions should clearly indicate their purpose, and default values should
60+
be avoided unless there are reasonable, common defaults, to maintain flexibility and
61+
encourage explicit configuration.
62+
- All types of variables can be used such as (number, string, bool, list(string),
63+
map(string), list(map(string)), map(map(string)), object(), any)
64+
- terraform.tfvars:
65+
- Provides default values for variables declared in the root `variables.tf`, making it easy
66+
for users to define common configurations.
67+
- This file should be structured to include any typical default settings without hardcoding
68+
sensitive values.
69+
- versions.tf:
70+
- Contains the `terraform` and `provider` blocks, specifying required versions.
71+
- Structure the `terraform` block as:
72+
terraform {{
73+
required_version = ">= 1.0"
74+
75+
required_providers {{
76+
<provider_name> = {{
77+
source = "<source>"
78+
version = ">= <version>"
79+
}}
80+
}}
81+
}}
82+
- outputs.tf:
83+
- Exposes relevant outputs from {input.base_config}, retrieving them from the module output
84+
and making them accessible at the root level. Each output should have a clear description,
85+
making it easy for users to understand the value and purpose of the output data.
86+
2. Module Directory Structure (modules/{input.base_config}):
87+
- main.tf:
88+
- Defines the {input.base_config} resource, fully utilizing required and also some optional
89+
parameters. Avoid any hardcoded values within the module to support full configurability
90+
from the root level.
91+
- Required parameters should cover the essentials for creating {input.base_config}, while
92+
optional parameters should provide a range of additional settings that enable more
93+
granular configuration if desired.
94+
- variables.tf:
95+
- Lists all variables necessary for configuring {input.base_config}, with descriptions and
96+
types specified to improve usability. No default values should be set here unless
97+
necessary for required fields.
98+
- Variable names should be clear and consistent with naming conventions in the root
99+
variables file, ensuring consistency in usage.
100+
- terraform.tfvars:
101+
- Includes default values for module-level variables to ensure that common parameters have
102+
defaults. This `terraform.tfvars` file within the module should be structured to provide
103+
typical configuration values, making it easier to set up and reducing the need for hardcoded values.
104+
- versions.tf:
105+
- Contains the `terraform` and `provider` blocks, specifying required versions.
106+
- Structure the `terraform` block as:
107+
terraform {{
108+
required_version = ">= 1.0"
109+
110+
required_providers {{
111+
<provider_name> = {{
112+
source = "<source>"
113+
version = ">= <version>"
114+
}}
115+
}}
116+
}}
117+
- outputs.tf:
118+
- Specifies outputs for the {input.base_config} resource, selecting relevant data that users
119+
might need to access from the root level. Each output should have an informative
120+
description, so users can understand its purpose and utility.
121+
Ensure this project structure supports {input.base_config}’s configurability, extensibility, and
122+
reusability across diverse Terraform providers, empowering users to manage their resources through a
123+
single, customizable root configuration while keeping module internals robustly modular.
124+
125+
finally just give me a python code without any note that can generate a project folder with the given
126+
schema without ```python entry. and we dont need any base directory in the python code. the final
127+
terraform template must work very well without any error!
128+
129+
Python code you give me, must have structure like that:
130+
131+
import os
132+
project_name = "app/media/MyTerraform"
133+
moduels_dir = os.path.join(project_name, "modules")
134+
135+
# Create project directories
136+
137+
os.makedirs(moduels_dir, exist_ok=True)
138+
139+
# Create main.tf (for example)
140+
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
141+
# any thing you need
43142
44-
1 - CI pipeline integration = {input.CI_integration}
45-
2 - base config is {input.base_config}
46-
3 - project name is app/media/MyTerraform
47-
4 - number of modules = 1
48-
49-
if CI integration is true, generate a pipeline based on github actions.
50143
51-
finally just give me a python code without any note that can generate a project folder with the given schema
52-
without ```python entry. and we dont need any base directory in the python code.
53-
the final terraform template must work very well without any error!
54144
55-
56145
"""
57146
return prompt
58147

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

70-
prompt = f"""
71-
72-
generate a correct python code to generate a helm project structure (project name: app/media/MyHelm)
159+
prompt = f"""
160+
161+
generate a correct python code to generate a helm project structure (project name: app/media/MyHelm)
73162
based on the latest version of helm chart. Only provide Python code, no explanations or markdown formatting.
74163
just generate a code to generate a folder as project template. don't consider base_dir
75-
164+
76165
consider these directories : [charts/,templates/]
77166
consider these files : Chart.yaml & values.yaml
78167
in the templates/ directory create these directories: {templates}.
@@ -89,19 +178,17 @@ def helm_template_generator(input : HelmTemplateGeneration) -> str:
89178
name=value
90179
)
91180
initialize ingress with a default host for pod if the pod ingress is true in here {ingress_}.
92-
set stateless in pod based on {status}.
181+
set stateless in pod based on {status}.
182+
93183
94-
95184
Based on values.yaml, create all necessary Kubernetes templates in the templates directory:
96185
if stateless.enabled is true, create deployment.yaml; if stateless.enabled is false, create statefulset.yaml.
97186
If a persistence block exists, include pvc.yaml. If the ingress block is defined and ingress.enabled
98-
is true, create ingress.yaml. if ingress.enabled is false, do not create ingress.yaml. Always create
187+
is true, create ingress.yaml. if ingress.enabled is false, do not create ingress.yaml. Always create
99188
secrets.yaml for secure data storage.
100189
101190
Ensure each template is fully parameterized to match values from values.yaml for flexible configuration.
102191
103192
in the final stage, put helpers.tpl in all templates and set the content based on information given.
104193
"""
105194
return prompt
106-
107-

0 commit comments

Comments
 (0)