Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crash when converting tags to Terraform format #294

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/cf2tf/conversion/overrides.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import TYPE_CHECKING, Callable, Dict

from cf2tf.terraform.hcl2.complex import ListType, MapType
from cf2tf.terraform.hcl2.custom import LiteralType
from cf2tf.terraform.hcl2.primitive import NullType, StringType, TerraformType
from cf2tf.terraform.hcl2.complex import ListType, MapType

if TYPE_CHECKING:
from cf2tf.convert import TemplateConverter
Expand Down Expand Up @@ -50,14 +50,33 @@
if isinstance(params["Tags"], dict):
return params

orginal_tags: ListType = params["Tags"] # type: ignore
original_tags: ListType = params["Tags"] # type: ignore

new_tags = {LiteralType(tag["Key"]): tag["Value"] for tag in orginal_tags}
first_item = original_tags[0]

del params["Tags"]
params["tags"] = MapType(new_tags)
# It's possible that the tags might be one or more
# conditional statements of LiteralType
# This wont fix every case, but it should fix most
# and it's better than nothing
if not isinstance(first_item, (dict, MapType)):
del params["Tags"]
params["tags"] = first_item
return params

Check warning on line 64 in src/cf2tf/conversion/overrides.py

View check run for this annotation

Codecov / codecov/patch

src/cf2tf/conversion/overrides.py#L62-L64

Added lines #L62 - L64 were not covered by tests

return params
try:
new_tags = {LiteralType(tag["Key"]): tag["Value"] for tag in original_tags}

del params["Tags"]
params["tags"] = MapType(new_tags)
return params
except Exception:
del params["Tags"]
params["tags"] = LiteralType(

Check warning on line 74 in src/cf2tf/conversion/overrides.py

View check run for this annotation

Codecov / codecov/patch

src/cf2tf/conversion/overrides.py#L72-L74

Added lines #L72 - L74 were not covered by tests
f"// Could not convert tags: {original_tags.render(4)}"
)
return params

Check warning on line 77 in src/cf2tf/conversion/overrides.py

View check run for this annotation

Codecov / codecov/patch

src/cf2tf/conversion/overrides.py#L77

Added line #L77 was not covered by tests

raise Exception("Could not convert tags")


OVERRIDE_DISPATCH: ResourceOverride = {
Expand Down
4 changes: 2 additions & 2 deletions src/cf2tf/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def add_space():
def perform_resource_overrides(
tf_type: str, params: Dict[str, TerraformType], tc: TemplateConverter
):
log.debug("Overiding params for {tf_type}")
log.debug(f"Overiding params for {tf_type}")
if tf_type not in OVERRIDE_DISPATCH:
return params

Expand All @@ -638,7 +638,7 @@ def perform_resource_overrides(
def perform_global_overrides(
tf_type: str, params: Dict[str, TerraformType], tc: TemplateConverter
):
log.debug("Performing global overrides for {tf_type}")
log.debug(f"Performing global overrides for {tf_type}")

for param, override in GLOBAL_OVERRIDES.items():
if param in params:
Expand Down
Loading