Skip to content

Commit e849f6c

Browse files
committed
add support for workspace argument of terraform_remote_state
1 parent 83087dc commit e849f6c

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

bin/tflocal

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ terraform {
7878
TF_REMOTE_STATE_CONFIG = """
7979
data "terraform_remote_state" "<name>" {
8080
backend = "s3"
81+
<workspace-placeholder>
8182
config = {<configs>
8283
}
8384
}
@@ -324,7 +325,6 @@ def generate_remote_state_config() -> str:
324325
"""
325326

326327
tf_files = parse_tf_files()
327-
328328
result = ""
329329
for filename, obj in tf_files.items():
330330
if LS_PROVIDERS_FILE == filename:
@@ -341,6 +341,13 @@ def generate_remote_state_config() -> str:
341341
backend_config = data_config.get("config", {})
342342
if not backend_config:
343343
continue
344+
workspace = data_config.get("workspace", "")
345+
if workspace:
346+
if workspace[0] == "$":
347+
workspace = workspace.lstrip('${').rstrip('}')
348+
else:
349+
workspace = f'"{workspace}"'
350+
workspace = f"workspace = {workspace}"
344351

345352
# Set up default configs
346353
remote_state_default_config = {
@@ -363,7 +370,9 @@ def generate_remote_state_config() -> str:
363370
# Create the final config
364371
remote_state_config = TF_REMOTE_STATE_CONFIG.replace(
365372
"<name>", data_name
366-
).replace("<configs>", config_options)
373+
) \
374+
.replace("<configs>", config_options) \
375+
.replace("<workspace-placeholder>", workspace)
367376
result += remote_state_config
368377

369378
return result

tests/test_apply.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,53 @@ def test_s3_remote_data_source():
343343
assert get_obj["Body"].read() == b"test"
344344

345345

346+
def test_s3_remote_data_source_with_workspace(monkeypatch):
347+
monkeypatch.setenv("DRY_RUN", "1")
348+
state_bucket = f"tf-data-source-{short_uid()}"
349+
config = """
350+
terraform {
351+
backend "s3" {
352+
bucket = "%s"
353+
key = "terraform.tfstate"
354+
region = "us-east-1"
355+
skip_credentials_validation = true
356+
}
357+
}
358+
359+
data "terraform_remote_state" "terraform_infra" {
360+
backend = "s3"
361+
workspace = terraform.workspace
362+
363+
config = {
364+
bucket = "<state-bucket>"
365+
workspace_key_prefix = "terraform-infrastructure/place"
366+
key = "terraform.tfstate"
367+
}
368+
}
369+
370+
data "terraform_remote_state" "build_infra" {
371+
backend = "s3"
372+
workspace = "build"
373+
374+
config = {
375+
bucket = "<state-bucket>"
376+
workspace_key_prefix = "terraform-infrastructure"
377+
key = "terraform.tfstate"
378+
}
379+
}
380+
381+
""".replace("<state-bucket>", state_bucket)
382+
383+
temp_dir = deploy_tf_script(config, cleanup=False, user_input="yes")
384+
override_file = os.path.join(temp_dir, "localstack_providers_override.tf")
385+
assert check_override_file_exists(override_file)
386+
387+
with open(override_file, "r") as fp:
388+
result = hcl2.load(fp)
389+
assert result["data"][0]["terraform_remote_state"]["terraform_infra"]["workspace"] == "${terraform.workspace}"
390+
assert result["data"][1]["terraform_remote_state"]["build_infra"]["workspace"] == "build"
391+
392+
346393
def test_dry_run(monkeypatch):
347394
monkeypatch.setenv("DRY_RUN", "1")
348395
state_bucket = "tf-state-dry-run"

0 commit comments

Comments
 (0)