Skip to content

Commit 2832426

Browse files
authored
Merge pull request #66 from mohammadll/terraform
Modify docker prompt as well as adding S3 prompt
2 parents 0fa4008 + 5a8c8b9 commit 2832426

2 files changed

Lines changed: 150 additions & 19 deletions

File tree

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,133 @@
11
def IaC_template_generator_s3(input) -> str:
22

3-
3+
s3 = ['aws_s3_bucket', 'aws_s3_bucket_versioning']
44

5-
prompt = f""" """
6-
return prompt
5+
aws_s3_create_bucket = 'true' if input.s3_bucket else 'false'
6+
aws_s3_create_bucket_versioning = 'true' if input.bucket_versioning else 'false'
7+
8+
prompt = f"""
9+
Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
10+
that dynamically provisions {s3} resources ensuring a modular, flexible structure to enable users
11+
to configure all essential settings at the root level. Only provide Python code, no explanations or
12+
markdown formatting. The project should be organized as follows:
13+
1. Root Directory Structure:
14+
- main.tf:
15+
- Define the provider block as follows:
16+
```
17+
provider "aws" {{
18+
region = "us-east-1"
19+
}}
20+
```
21+
- Defines a module block that references "s3" from a subdirectory within modules.
22+
This module block should expose all variables that {s3} resources require, allowing
23+
configuration at the root level rather than directly within the module.
24+
- Every variable defined in {s3} resources should be passed through the module block,
25+
ensuring that users can adjust all critical parameters of {s3} resources by modifying
26+
root main.tf. Avoid using any other parameters. just use the parameters of {s3} resources with the same keys
27+
- variables.tf:
28+
- Sets these variables names for aws_s3_bucket resource:
29+
s3_create_bucket(bool), s3_bucket_name(string), s3_bucket_force_destroy(bool), s3_bucket_tags(map(string))
30+
- Sets these variables names for aws_s3_bucket_versioning resource:
31+
s3_create_bucket_versioning(bool), s3_bucket_versioning_status(string)
32+
- terraform.tfvars:
33+
- Structure as follows:
34+
s3_create_bucket = {aws_s3_create_bucket}
35+
s3_bucket_name = "UniqueName"
36+
s3_bucket_force_destroy = false
37+
s3_bucket_tags = {{
38+
Name = "My bucket"
39+
Environment = "Dev"
40+
}}
41+
s3_create_bucket_versioning = {aws_s3_create_bucket_versioning}
42+
s3_bucket_versioning_status = "Enabled"
43+
- versions.tf:
44+
- Structure as follows:
45+
terraform {{
46+
required_version = ">= 1.0"
47+
48+
required_providers {{
49+
aws = {{
50+
source = "hashicorp/aws"
51+
version = ">= 5.20"
52+
}}
53+
}}
54+
}}
55+
2. Module Directory Structure (modules/s3):
56+
- main.tf:
57+
- Set the following parameters for aws_s3_bucket resource (name its terraform resource to "s3_bucket")and avoid using any other parameters:
58+
- 1. count (type: number): follow the below syntax for count:
59+
```
60+
count = var.s3_create_bucket ? 1 : 0
61+
```
62+
- 2. bucket (type: string): Specifies the bucket name.
63+
- 3. force_destroy (type: boolean): Indicates all objects should be deleted from the bucket when the bucket is destroyed
64+
- 4. tags (map(string) type): Includes the following fields:
65+
- Name (type: string): A tag for the bucket.
66+
- Environment (type: string): A tag for the bucket
67+
- Set the following parameters for aws_s3_bucket_versioning resource (name its terraform resource to "s3_bucket_versioning")and avoid using any other parameters:
68+
- 1. count (type: number): follow the below syntax for count:
69+
```
70+
count = var.s3_create_bucket && var.s3_create_bucket_versioning ? 1 : 0
71+
```
72+
- 2. bucket: it must points to the s3_bucket resource like the following syntax:
73+
```
74+
bucket = aws_s3_bucket.s3_bucket[0].id
75+
```
76+
- 3. versioning_configuration: this is a block which has a key/value pair as follows:
77+
```
78+
versioning_configuration {{
79+
status = var.s3_bucket_versioning_status
80+
}}
81+
```
82+
- variables.tf:
83+
- Sets these variables names for aws_s3_bucket resource:
84+
s3_create_bucket(bool), s3_bucket_name(string), s3_bucket_force_destroy(bool), s3_bucket_tags(map(string))
85+
- Sets these variables names for aws_s3_bucket_versioning resource:
86+
s3_create_bucket_versioning(bool), s3_bucket_versioning_status(string)
87+
- terraform.tfvars:
88+
- Structure as follows:
89+
s3_create_bucket = {aws_s3_create_bucket}
90+
s3_bucket_name = "UniqueName"
91+
s3_bucket_force_destroy = false
92+
s3_bucket_tags = {{
93+
Name = "My bucket"
94+
Environment = "Dev"
95+
}}
96+
s3_create_bucket_versioning = {aws_s3_create_bucket_versioning}
97+
s3_bucket_versioning_status = "Enabled"
98+
- versions.tf:
99+
- Structure as follows:
100+
terraform {{
101+
required_version = ">= 1.0"
102+
103+
required_providers {{
104+
aws = {{
105+
source = "hashicorp/aws"
106+
version = ">= 5.20"
107+
}}
108+
}}
109+
}}
110+
Ensure this project structure supports {s3}’s configurability, extensibility, and
111+
reusability across diverse Terraform providers, empowering users to manage their resources through a
112+
single, customizable root configuration while keeping module internals robustly modular.
113+
114+
finally just give me a python code without any note that can generate a project folder with the given
115+
schema without ```python entry. and we dont need any base directory in the python code. the final
116+
terraform template must work very well without any error!
117+
118+
Python code you give me, must have structure like that:
119+
120+
import os
121+
project_name = "app/media/MyTerraform"
122+
modules_dir = os.path.join(project_name, "modules")
123+
s3_dir = os.path.join(modules_dir, "s3")
124+
125+
# Create project directories
126+
os.makedirs(s3_dir, exist_ok=True)
127+
128+
# Create main.tf
129+
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
130+
# any thing you need
131+
132+
"""
133+
return prompt

app/template_generators/terraform/docker.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
def IaC_template_generator_docker(input) -> str:
22

33
docker = ['docker_container', 'docker_image']
4-
docker_image_count = 1 if input.docker_image else 0
5-
docker_container_count = 1 if input.docker_container else 0
4+
create_docker_image = 'true' if input.docker_image else 'false'
5+
create_docker_container = 'true' if input.docker_container else 'false'
66

77
prompt = f"""
88
Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
@@ -25,25 +25,24 @@ def IaC_template_generator_docker(input) -> str:
2525
root main.tf. Avoid using any other parameters. just use the parameters of {docker} resources with the same keys
2626
- variables.tf:
2727
- Sets these variables names for docker_image resource:
28-
image_name(string), image_force_remove(bool), image_build(object), image_count(number)
28+
create_image(bool), image_name(string), image_force_remove(bool), image_build(object)
2929
- Sets these variables names for docker_container resource:
30-
container_image(string), container_name(string), container_hostname(string),
31-
container_restart(string), container_count(number)
30+
create_container(bool), container_image(string), container_name(string), container_hostname(string), container_restart(string)
3231
- terraform.tfvars:
3332
- Structure as follows:
33+
create_image = {create_docker_image}
3434
image_name = "my-image"
3535
image_force_remove = true
3636
image_build = {{
3737
context = "./"
3838
tag = ["my-image:latest"]
3939
}}
40-
image_count = {docker_image_count}
4140
41+
create_container = {create_docker_container}
4242
container_image = "my-image"
4343
container_name = "my-container"
4444
container_hostname = "my-host"
4545
container_restart = "always"
46-
container_count = {docker_container_count}
4746
- versions.tf:
4847
- Structure as follows:
4948
terraform {{
@@ -58,41 +57,46 @@ def IaC_template_generator_docker(input) -> str:
5857
}}
5958
2. Module Directory Structure (modules/docker):
6059
- main.tf:
61-
- Set the following parameters for docker_image resource and avoid using any other parameters:
62-
- 1. count (type: number) Specifies the number of images
60+
- Set the following parameters for docker_image resource (name its terraform resource to "image") and avoid using any other parameters:
61+
- 1. count (type: number): follow the below syntax for count:
62+
```
63+
count = var.create_image ? 1 : 0
64+
```
6365
- 2. name (type: string): Specifies the image name.
6466
- 3. force_remove (type: boolean): Determines whether to forcibly remove intermediate containers.
6567
- 4. build (block type): Includes the following required field:
6668
- context (type: string, required): Specifies the build context for the image.
6769
- tag(type: List of Strings, required): Specifices the image tag in the 'name:tag'
6870
format, (e.g., ["NAME:VERSION"])
69-
- Set the following parameters for docker_container resource and avoid using any other parameters:
70-
- 1. count (type: number): Specifies the number of containers
71+
- Set the following parameters for docker_container resource (name its terraform resource to "container") and avoid using any other parameters:
72+
- 1. count (type: number): follow the below syntax for count:
73+
```
74+
count = var.create_container ? 1 : 0
75+
```
7176
- 2. image (type: string): Specifies the container image.
7277
- 3. name (type: string): Sets the container name.
7378
- 4. hostname (type: string): Configures the container hostname.
7479
- 5. restart (type: string): Defines the container's restart policy (e.g., always, on-failure, no).
7580
- variables.tf:
7681
- Sets these variables names for docker_image resource:
77-
image_name(string), image_force_remove(bool), image_build(object), image_count(number)
82+
create_image(bool), image_name(string), image_force_remove(bool), image_build(object)
7883
- Sets these variables names for docker_container resource:
79-
container_image(string), container_name(string), container_hostname(string),
80-
container_restart(string), container_count(number)
84+
create_container(bool), container_image(string), container_name(string), container_hostname(string), container_restart(string)
8185
- terraform.tfvars:
8286
- Structure as follows:
87+
create_image = {create_docker_image}
8388
image_name = "my-image"
8489
image_force_remove = true
8590
image_build = {{
8691
context = "./"
8792
tag = ["my-image:latest"]
8893
}}
89-
image_count = {docker_image_count}
9094
95+
create_container = {create_docker_container}
9196
container_image = "my-image"
9297
container_name = "my-container"
9398
container_hostname = "my-host"
9499
container_restart = "always"
95-
container_count = {docker_container_count}
96100
- versions.tf:
97101
- Structure as follows:
98102
terraform {{

0 commit comments

Comments
 (0)