-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathargocd.py
More file actions
206 lines (190 loc) · 11.5 KB
/
argocd.py
File metadata and controls
206 lines (190 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def IaC_template_generator_argocd(input) -> str:
argocd = ['argocd_repository', 'argocd_application']
argocd_create_repository = 'true' if input.argocd_repository else 'false'
if input.argocd_application != None:
argocd_create_application = 'true'
argocd_application_auto_prune = 'true' if input.argocd_application.sync_policy.auto_prune else 'false'
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 = ""
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. metadata (A block): Define a metadata block as follows:
```
metadata {{
name = var.argocd_application["name"]
namespace = argocd
labels = {{
using_sync_policy_options = "true"
}}
}}
```
- 3. 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:
import os
project_name = "app/media/MyTerraform"
modules_dir = os.path.join(project_name, "modules")
argocd_dir = os.path.join(modules_dir, "argocd")
# Create project directories
os.makedirs(argocd_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