Skip to content

Commit

Permalink
🔥feat: Add hclv2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
D10S0VSkY-OSS committed Nov 12, 2023
1 parent f4fd1f1 commit 91154df
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 19 deletions.
2 changes: 2 additions & 0 deletions sld-api-backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ iniconfig==2.0.0
Jinja2==3.1.2
jmespath==1.0.1
kombu==5.3.2
lark==1.1.8
MarkupSafe==2.1.3
mysqlclient==2.2.0
packaging==23.2
Expand All @@ -52,6 +53,7 @@ pyparsing==3.1.1
pytest==7.4.2
python-dateutil==2.8.2
python-dotenv==1.0.0
python-hcl2==4.3.2
python-jose==3.3.0
python-multipart==0.0.6
python-usernames==1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ class ActivityLogs(BaseModel):
action: constr(strip_whitespace=True)

class Config:
orm_mode = True
from_attributes = True
2 changes: 1 addition & 1 deletion sld-api-backend/src/aws/domain/entities/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ class Aws(AwsBase):
id: int

class Config:
orm_mode = True
from_attributes = True
2 changes: 1 addition & 1 deletion sld-api-backend/src/azure/domain/entities/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class Azure(AzureBase):
id: int

class Config:
orm_mode = True
from_attributes = True
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class CustomProvider(CustomProviderBase):
id: int

class Config:
orm_mode = True
from_attributes = True
2 changes: 1 addition & 1 deletion sld-api-backend/src/deploy/domain/entities/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ class Deploy(DeployBase):
user_id: int

class Config:
orm_mode = True
from_attributes = True
2 changes: 1 addition & 1 deletion sld-api-backend/src/gcp/domain/entities/gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class Gcloud(GcloudBase):
id: int

class Config:
orm_mode = True
from_attributes = True
6 changes: 3 additions & 3 deletions sld-api-backend/src/stacks/domain/entities/stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class StackBase(BaseModel):
class Config:
"""Extra configuration options"""

anystr_strip_whitespace = True # remove trailing whitespace
str_strip_whitespace = True # remove trailing whitespace


class StackCreate(StackBase):
Expand All @@ -24,7 +24,7 @@ class StackCreate(StackBase):
class Config:
"""Extra configuration options"""

anystr_strip_whitespace = True # remove trailing whitespace
str_strip_whitespace = True # remove trailing whitespace


class Stack(StackBase):
Expand All @@ -33,4 +33,4 @@ class Stack(StackBase):
user_id: int

class Config:
orm_mode = True
from_attributes = True
4 changes: 2 additions & 2 deletions sld-api-backend/src/tasks/domain/entities/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class PasswordReset(BaseModel):
passwd: str

class Config:
orm_mode = True
from_attributes = True


class User(UserBase):
id: int

class Config:
orm_mode = True
from_attributes = True


class Token(BaseModel):
Expand Down
4 changes: 2 additions & 2 deletions sld-api-backend/src/users/domain/entities/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class PasswordReset(BaseModel):
passwd: str

class Config:
orm_mode = True
from_attributes = True


class User(UserBase):
id: int

class Config:
orm_mode = True
from_attributes = True


class Token(BaseModel):
Expand Down
29 changes: 29 additions & 0 deletions sld-api-backend/src/worker/helpers/hcl2_to_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import hcl2


def convert_to_json(file_path):
with open(file_path, "r") as f:
hcl_data = hcl2.load(f)
clean_data = remove_interpolations(hcl_data)
json_data = list_to_dict(clean_data["variable"])
return json_data


def remove_interpolations(data):
if isinstance(data, dict):
return {k: remove_interpolations(v) for k, v in data.items()}
elif isinstance(data, list):
return [remove_interpolations(elem) for elem in data]
elif isinstance(data, str):
return data.replace("${", "").replace("}", "")
else:
return data


def is_interpolation(value):
return isinstance(value, str) and "${" in value


def list_to_dict(variables_list: list) -> dict:
variables_dict = {k: v for d in variables_list for k, v in d.items()}
return variables_dict
52 changes: 46 additions & 6 deletions sld-api-backend/src/worker/providers/hashicorp/templates.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import logging
from dataclasses import dataclass

import hcl
from jinja2 import Template
from src.worker.helpers.hcl2_to_json import convert_to_json


@dataclass
Expand All @@ -13,6 +15,11 @@ class StructBase:
squad: str


@dataclass
class StructProject(StructBase):
project_path: str


@dataclass
class Backend(StructBase):
project_path: str
Expand Down Expand Up @@ -62,8 +69,7 @@ def save(self) -> dict:


@dataclass
class GetVars(StructBase):
project_path: str
class GetVars(StructProject):
"""
In this class are the methods to obtain information from the terraform variables
"""
Expand All @@ -73,18 +79,52 @@ def __set_path(self):
return f"/tmp/{self.stack_name}/{self.environment}/{self.squad}/{self.name}/variables.tf"
return f"/tmp/{self.stack_name}/{self.environment}/{self.squad}/{self.name}/{self.project_path}/variables.tf"

def process_dict(self, d):
return {
key: (
value.replace("${", "").replace("}", "")
if isinstance(value, str)
else (
self.process_dict(value)
if isinstance(value, dict)
else [self.process_dict(item) for item in value]
if isinstance(value, list)
else value
)
)
for key, value in d.items()
}

def get_vars_json(self) -> dict:
try:
file_hcl = self.__set_path()
with open(file_hcl, "r") as fp:
obj = hcl.load(fp)
if obj.get("variable"):
return {"command": "get_vars_json", "rc": 0, "stdout": json.dumps(obj)}
hcl_data = hcl.load(fp)
if hcl_data.get("variable"):
return {
"command": "get_vars_json",
"rc": 0,
"stdout": json.dumps(hcl_data),
}
else:
error_msg = "Variable file is empty, not iterable"
return {"command": "get_vars_json", "rc": 1, "stdout": error_msg}
except IOError:
error_msg = "Variable file not accessible"
return {"command": "get_vars_json", "rc": 1, "stdout": error_msg}
except ValueError as err:
logging.warn(err)
logging.warn("hclv1 cannot read variables.tf trying to read with hcl2")
try:
result = {"variable": convert_to_json(file_hcl)}
return {
"command": "get_vars_json",
"rc": 0,
"stdout": json.dumps(result),
}
except Exception as err:
error_msg = f"Syntax error in variable file(check with terraform validate): {err}"
logging.error(error_msg)
return {"command": "get_vars_json", "rc": 1, "stdout": error_msg}
except Exception as err:
return {"command": "get_vars_json", "rc": 1, "stdout": err}
return {"command": "get_vars_json", "rc": 1, "stdout": err}

0 comments on commit 91154df

Please sign in to comment.