|
1 | 1 | from typing import TYPE_CHECKING, Callable, Dict
|
2 | 2 |
|
| 3 | +from cf2tf.terraform.hcl2.complex import ListType, MapType |
3 | 4 | from cf2tf.terraform.hcl2.custom import LiteralType
|
4 | 5 | from cf2tf.terraform.hcl2.primitive import NullType, StringType, TerraformType
|
5 |
| -from cf2tf.terraform.hcl2.complex import ListType, MapType |
6 | 6 |
|
7 | 7 | if TYPE_CHECKING:
|
8 | 8 | from cf2tf.convert import TemplateConverter
|
@@ -50,14 +50,33 @@ def tag_conversion(_tc: "TemplateConverter", params: CFParams) -> CFParams:
|
50 | 50 | if isinstance(params["Tags"], dict):
|
51 | 51 | return params
|
52 | 52 |
|
53 |
| - orginal_tags: ListType = params["Tags"] # type: ignore |
| 53 | + original_tags: ListType = params["Tags"] # type: ignore |
54 | 54 |
|
55 |
| - new_tags = {LiteralType(tag["Key"]): tag["Value"] for tag in orginal_tags} |
| 55 | + first_item = original_tags[0] |
56 | 56 |
|
57 |
| - del params["Tags"] |
58 |
| - params["tags"] = MapType(new_tags) |
| 57 | + # It's possible that the tags might be one or more |
| 58 | + # conditional statements of LiteralType |
| 59 | + # This wont fix every case, but it should fix most |
| 60 | + # and it's better than nothing |
| 61 | + if not isinstance(first_item, (dict, MapType)): |
| 62 | + del params["Tags"] |
| 63 | + params["tags"] = first_item |
| 64 | + return params |
59 | 65 |
|
60 |
| - return params |
| 66 | + try: |
| 67 | + new_tags = {LiteralType(tag["Key"]): tag["Value"] for tag in original_tags} |
| 68 | + |
| 69 | + del params["Tags"] |
| 70 | + params["tags"] = MapType(new_tags) |
| 71 | + return params |
| 72 | + except Exception: |
| 73 | + del params["Tags"] |
| 74 | + params["tags"] = LiteralType( |
| 75 | + f"// Could not convert tags: {original_tags.render(4)}" |
| 76 | + ) |
| 77 | + return params |
| 78 | + |
| 79 | + raise Exception("Could not convert tags") |
61 | 80 |
|
62 | 81 |
|
63 | 82 | OVERRIDE_DISPATCH: ResourceOverride = {
|
|
0 commit comments