Skip to content

Commit 3e8f905

Browse files
HarshCasperHoffs
andauthored
fix: pass strip_string_quotes to hcl2.load() for python-hcl2 v8.x compat (#96)
Co-authored-by: Ignas Maslinskas <ignas@maslinskas.lt>
1 parent d6715c8 commit 3e8f905

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ ADDITIONAL_TF_OVERRIDE_LOCATIONS=/path/to/module1,path/to/module2 tflocal plan
117117

118118
## Change Log
119119

120+
* v0.26.0: Fix compatibility with `python-hcl2` v8+ by using `SerializationOptions` to handle quoted dict keys, block metadata, and comments
120121
* v0.25.0: Improve `s3control` local endpoint override and respect `AWS_ENDPOINT_URL` configuration for `mwaa`
121122
* v0.24.1: Exclude broken `python-hcl2` version from requirements
122123
* v0.24.0: Add support to return `terraform-local` version when calling `tflocal -version` and fix AWS provider detection

bin/tflocal

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ if os.path.isdir(os.path.join(PARENT_FOLDER, ".venv")):
2626

2727
from localstack_client import config # noqa: E402
2828
import hcl2 # noqa: E402
29+
from hcl2 import SerializationOptions # noqa: E402
30+
31+
HCL2_SERIALIZATION_OPTIONS = SerializationOptions(
32+
strip_string_quotes=True, explicit_blocks=False, with_comments=False
33+
)
2934

3035
DRY_RUN = str(os.environ.get("DRY_RUN")).strip().lower() in ["1", "true"]
3136
DEFAULT_REGION = "us-east-1"
@@ -611,7 +616,7 @@ def parse_tf_files() -> dict:
611616
for _file in glob.glob("*.tf"):
612617
try:
613618
with open(_file, "r") as fp:
614-
result[_file] = hcl2.load(fp)
619+
result[_file] = hcl2.load(fp, serialization_options=HCL2_SERIALIZATION_OPTIONS)
615620
except Exception as e:
616621
print(f'Unable to parse "{_file}" as HCL file: {e}')
617622
return result
@@ -634,7 +639,7 @@ def get_provider_version_from_lock_file() -> Optional[version.Version]:
634639

635640
provider_version = None
636641
with open(lock_file, "r") as fp:
637-
result = hcl2.load(fp)
642+
result = hcl2.load(fp, serialization_options=HCL2_SERIALIZATION_OPTIONS)
638643
for provider in result.get("provider", []):
639644
for provider_name, provider_config in provider.items():
640645
if provider_name.endswith(AWS_PROVIDER_NAME_SUFFIX):

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = terraform-local
3-
version = 0.25.0
3+
version = 0.26.0
44
url = https://github.com/localstack/terraform-local
55
author = LocalStack Team
66
author_email = info@localstack.cloud
@@ -28,7 +28,7 @@ packages = find:
2828

2929
install_requires =
3030
localstack-client
31-
python-hcl2!=7.3.0
31+
python-hcl2>=8
3232
packaging
3333

3434
[options.extras_require]

tests/test_apply.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
import boto3
1313
import pytest
1414
import hcl2
15+
from hcl2 import SerializationOptions
16+
17+
HCL2_SERIALIZATION_OPTIONS = SerializationOptions(
18+
strip_string_quotes=True, explicit_blocks=False, with_comments=False
19+
)
1520

1621
# TODO set up the tests to run with tox so we can run the tests with different python versions
1722

@@ -385,7 +390,7 @@ def test_s3_remote_data_source_with_workspace(monkeypatch):
385390
assert check_override_file_exists(override_file)
386391

387392
with open(override_file, "r") as fp:
388-
result = hcl2.load(fp)
393+
result = hcl2.load(fp, serialization_options=HCL2_SERIALIZATION_OPTIONS)
389394
assert result["data"][0]["terraform_remote_state"]["terraform_infra"]["workspace"] == "${terraform.workspace}"
390395
assert result["data"][1]["terraform_remote_state"]["build_infra"]["workspace"] == "build"
391396

@@ -432,7 +437,7 @@ def test_versioned_endpoints(monkeypatch, provider_version):
432437
assert check_override_file_exists(override_file)
433438

434439
with open(override_file, "r") as fp:
435-
result = hcl2.load(fp)
440+
result = hcl2.load(fp, serialization_options=HCL2_SERIALIZATION_OPTIONS)
436441
endpoints = result["provider"][0]["aws"]["endpoints"][0]
437442
if provider_version == "5.99.1":
438443
assert "iotanalytics" in endpoints
@@ -486,7 +491,7 @@ def test_subdomain_endpoints(monkeypatch, endpoint_host):
486491
assert check_override_file_exists(override_file)
487492

488493
with open(override_file, "r") as fp:
489-
result = hcl2.load(fp)
494+
result = hcl2.load(fp, serialization_options=HCL2_SERIALIZATION_OPTIONS)
490495
endpoints = result["provider"][0]["aws"]["endpoints"][0]
491496
assert "s3control" in endpoints
492497
assert "mwaa" in endpoints
@@ -570,7 +575,7 @@ def test_service_endpoint_alias_replacements(monkeypatch):
570575
def check_override_file_content(override_file):
571576
try:
572577
with open(override_file, "r") as fp:
573-
result = hcl2.load(fp)
578+
result = hcl2.load(fp, serialization_options=HCL2_SERIALIZATION_OPTIONS)
574579
result = result["provider"][0]["aws"]
575580
except Exception as e:
576581
raise Exception(f'Unable to parse "{override_file}" as HCL file: {e}')
@@ -616,7 +621,7 @@ def test_s3_backend_configs_merge(monkeypatch):
616621
def check_override_file_backend_extra_content(override_file):
617622
try:
618623
with open(override_file, "r") as fp:
619-
result = hcl2.load(fp)
624+
result = hcl2.load(fp, serialization_options=HCL2_SERIALIZATION_OPTIONS)
620625
result = result["terraform"][0]["backend"][0]["s3"]
621626
except Exception as e:
622627
raise Exception(f'Unable to parse "{override_file}" as HCL file: {e}')
@@ -690,7 +695,7 @@ def check_override_file_backend_endpoints_content(override_file, is_legacy: bool
690695
}
691696
try:
692697
with open(override_file, "r") as fp:
693-
result = hcl2.load(fp)
698+
result = hcl2.load(fp, serialization_options=HCL2_SERIALIZATION_OPTIONS)
694699
result = result["terraform"][0]["backend"][0]["s3"]
695700
except Exception as e:
696701
print(f'Unable to parse "{override_file}" as HCL file: {e}')
@@ -727,7 +732,7 @@ def test_provider_aliases_ignored(monkeypatch):
727732
def check_override_file_content_for_alias(override_file):
728733
try:
729734
with open(override_file, "r") as fp:
730-
result = hcl2.load(fp)
735+
result = hcl2.load(fp, serialization_options=HCL2_SERIALIZATION_OPTIONS)
731736
result = result["provider"]
732737
except Exception as e:
733738
raise Exception(f'Unable to parse "{override_file}" as HCL file: {e}')

0 commit comments

Comments
 (0)