Skip to content

Commit

Permalink
sc-12773 use a more rigid regex for tfvars templating (#27)
Browse files Browse the repository at this point in the history
* sc-12773 use a more rigid regex for tfvars templating

---------

Co-authored-by: Matthew Warren <[email protected]>
  • Loading branch information
mattwwarren and mattwwarren authored May 30, 2024
1 parent 4b67bc6 commit a3822bd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Config Catalyst automatically converts static config files in your repos into parameterized templates.
It's the easiest way to "pay down config tech debt" with a single command.

We :heart: feedback, [bugs](https://github.com/cloudtruth/config-catalyst/issues/new), and [enhancement suggestions](https://github.com/cloudtruth/config-catalyst/issues/new).
We :heart: feedback, [bugs](https://github.com/cloudtruth/config-catalyst/issues/new), and [enhancement suggestions](https://github.com/cloudtruth/config-catalyst/issues/new).

We also have a #config-catalyst channel [on our Discord](https://discord.com/invite/eBZXm9Tzr7).

Expand All @@ -19,7 +19,7 @@ Config Catalyst exists to solve this problem:

>"I need to take this weird network config that Bob (who left five years ago) roughed out by hand and turn it into a crisp little YAML template with parameterized variables."
Static, hard-coded config is a form of "tech debt" many teams want to eliminate, but the "pay it down" process is tedious and time-consuming. Until now.
Static, hard-coded config is a form of "tech debt" many teams want to eliminate, but the "pay it down" process is tedious and time-consuming. Until now.

# How it works
Config Catalyst works off of a local copy of your repo folder structure.
Expand Down
4 changes: 2 additions & 2 deletions src/dynamic_importer/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def create_value(
environment_id = self.create_environment(environment_name)["id"]
parameter_id = self.create_parameter(project_name, parameter_name)["id"]

value = str(value) if isinstance(value, bool) else value
value = str(value).lower() if isinstance(value, bool) else value
resp = self._make_request(
f"projects/{project_id}/parameters/{parameter_id}/values",
"POST",
Expand All @@ -329,7 +329,7 @@ def update_value(
environment_id = self.get_environment_id(environment_name)
parameter_id = self.get_parameter_id(project_name, parameter_name)

value = str(value) if isinstance(value, bool) else value
value = str(value).lower() if isinstance(value, bool) else value
return self._make_request(
f"projects/{project_id}/parameters/{parameter_id}/values/{value_id}",
"PATCH",
Expand Down
3 changes: 2 additions & 1 deletion src/dynamic_importer/processors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ def _traverse_data(
if not hints:
obj_type = self.guess_type(obj)
param_name = self.path_to_param_name(path)
value = str(obj).lower() if obj_type == "boolean" else obj
return f"{{{{ cloudtruth.parameters.{param_name} }}}}", {
path: {
"values": {env: obj},
"values": {env: value},
"param_name": param_name,
"type": obj_type,
"secret": self.is_param_secret(param_name),
Expand Down
8 changes: 6 additions & 2 deletions src/dynamic_importer/processors/tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ def _traverse_data(
if set(obj.keys()) >= self.data_keys:
if not hints:
param_name = self.path_to_param_name(path)
value = (
str(obj["default"]).lower() if obj["type"] == "boolean" else obj
)
return f"{{{{ cloudtruth.parameters.{param_name} }}}}", {
path: {
"values": {env: obj["default"]},
"values": {env: value},
"param_name": param_name,
"description": obj.get("description", ""),
"type": obj["type"],
Expand All @@ -111,9 +114,10 @@ def _traverse_data(
if not hints:
obj_type = self.guess_type(obj)
param_name = self.path_to_param_name(path)
value = str(obj).lower() if obj_type == "boolean" else obj
return f"{{{{ cloudtruth.parameters.{param_name} }}}}", {
path: {
"values": {env: obj},
"values": {env: value},
"param_name": param_name,
"type": obj_type,
"secret": False,
Expand Down
9 changes: 7 additions & 2 deletions src/dynamic_importer/processors/tfvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ def encode_template_references(
environment = "default"
if config_data:
for _, data in config_data.items():
value = str(data["values"][environment])
reference = rf'{{{{ cloudtruth.parameters.{data["param_name"]} }}}}'
source_name = sub(r"_\d+", "", data["param_name"])
value = (
rf"({source_name})(\s*=\s*.*){str(data['values'][environment])}(.*)"
)
reference = (
rf'\1\2{{{{ cloudtruth.parameters.{data["param_name"]} }}}}\3'
)
template_body = sub(value, reference, template_body)

return template_body

0 comments on commit a3822bd

Please sign in to comment.