Skip to content

Commit 36c9d6f

Browse files
committed
refactor S3 generation to support legacy dynamodb for remote state
1 parent e849f6c commit 36c9d6f

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

bin/tflocal

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import textwrap
1818

1919
from packaging import version
2020
from urllib.parse import urlparse
21-
from typing import Iterable, Optional, Dict
21+
from typing import Iterable, Optional, Dict, Tuple
2222

2323
PARENT_FOLDER = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
2424
if os.path.isdir(os.path.join(PARENT_FOLDER, ".venv")):
@@ -288,33 +288,16 @@ def generate_s3_backend_config() -> str:
288288
if not s3_backend_config:
289289
return ""
290290

291-
backend_default_config = {
292-
# note: default values, updated by `backend_config` further below...
293-
"bucket": "tf-test-state",
294-
"key": "terraform.tfstate",
295-
"region": get_region(),
296-
"skip_credentials_validation": True,
297-
"skip_metadata_api_check": True,
298-
"secret_key": "test",
299-
"endpoints": {
300-
"s3": get_service_endpoint("s3"),
301-
"iam": get_service_endpoint("iam"),
302-
"sso": get_service_endpoint("sso"),
303-
"sts": get_service_endpoint("sts"),
304-
"dynamodb": get_service_endpoint("dynamodb"),
305-
},
306-
}
307-
308-
config_options = _generate_s3_backend_config(s3_backend_config, backend_default_config)
291+
config_values, config_string = _generate_s3_backend_config(s3_backend_config)
309292
if not DRY_RUN:
310-
get_or_create_bucket(backend_default_config["bucket"])
311-
if "dynamodb_table" in backend_default_config:
293+
get_or_create_bucket(config_values["bucket"])
294+
if "dynamodb_table" in config_values:
312295
get_or_create_ddb_table(
313-
backend_default_config["dynamodb_table"],
314-
region=backend_default_config["region"],
296+
config_values["dynamodb_table"],
297+
region=config_values["region"],
315298
)
316299

317-
result = TF_S3_BACKEND_CONFIG.replace("<configs>", config_options)
300+
result = TF_S3_BACKEND_CONFIG.replace("<configs>", config_string)
318301
return result
319302

320303

@@ -349,36 +332,20 @@ def generate_remote_state_config() -> str:
349332
workspace = f'"{workspace}"'
350333
workspace = f"workspace = {workspace}"
351334

352-
# Set up default configs
353-
remote_state_default_config = {
354-
"bucket": "tf-test-state",
355-
"key": "terraform.tfstate",
356-
"region": get_region(),
357-
"skip_credentials_validation": True,
358-
"skip_metadata_api_check": True,
359-
"secret_key": "test",
360-
"endpoints": {
361-
"s3": get_service_endpoint("s3"),
362-
"iam": get_service_endpoint("iam"),
363-
"sso": get_service_endpoint("sso"),
364-
"sts": get_service_endpoint("sts"),
365-
},
366-
}
367-
368-
config_options = _generate_s3_backend_config(backend_config, remote_state_default_config)
335+
_, config_str = _generate_s3_backend_config(backend_config)
369336

370337
# Create the final config
371338
remote_state_config = TF_REMOTE_STATE_CONFIG.replace(
372339
"<name>", data_name
373340
) \
374-
.replace("<configs>", config_options) \
341+
.replace("<configs>", config_str) \
375342
.replace("<workspace-placeholder>", workspace)
376343
result += remote_state_config
377344

378345
return result
379346

380347

381-
def _generate_s3_backend_config(backend_config: Dict, default_config: Dict) -> str:
348+
def _generate_s3_backend_config(backend_config: Dict) -> Tuple[Dict, str]:
382349
is_tf_legacy = TF_VERSION < version.Version("1.6")
383350
legacy_endpoint_mappings = {
384351
"endpoint": "s3",
@@ -387,6 +354,23 @@ def _generate_s3_backend_config(backend_config: Dict, default_config: Dict) -> s
387354
"dynamodb_endpoint": "dynamodb",
388355
}
389356

357+
# Set up default config
358+
default_config = {
359+
"bucket": "tf-test-state",
360+
"key": "terraform.tfstate",
361+
"region": get_region(),
362+
"skip_credentials_validation": True,
363+
"skip_metadata_api_check": True,
364+
"secret_key": "test",
365+
"endpoints": {
366+
"s3": get_service_endpoint("s3"),
367+
"iam": get_service_endpoint("iam"),
368+
"sso": get_service_endpoint("sso"),
369+
"sts": get_service_endpoint("sts"),
370+
"dynamodb": get_service_endpoint("dynamodb"),
371+
},
372+
}
373+
390374
# Merge in legacy endpoint configs if not existing already
391375
if is_tf_legacy and backend_config.get("endpoints"):
392376
print(

0 commit comments

Comments
 (0)