Skip to content
This repository was archived by the owner on Sep 13, 2023. It is now read-only.
15 changes: 15 additions & 0 deletions tests/test_terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,18 @@ def test_shell_default(terraform, resource, mocker):
f"ubuntu@{expected['instance_ip']}",
]
)


@pytest.mark.timeout(10)
def test_state_mv(tmp_path, resource):
from tpi.terraform import TerraformBackend

new_name = "new-resource"
mtype = "iterative_machine"

tf = TerraformBackend(tmp_path)
tf.state_mv(source=f"{mtype}.{resource}", destination=f"{mtype}.{new_name}")

with open(tmp_path / "terraform.tfstate") as fobj:
tfstate = json.load(fobj)
assert tfstate["resources"][0]["name"] == new_name
7 changes: 7 additions & 0 deletions tpi/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,10 @@ def _shell_default(
port = f":{port}" if port is not None else ""
cmd.append(f"{user}{host}{port}")
run(cmd)

def state_mv(self, source, destination, **kwargs):
"""Retain an existing remote object but track it as a
different resource instance address.
"""
assert source and destination
self.tf.cmd("state mv", source, destination)
Comment on lines +134 to +140
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great 😅 thanks to #17 doesn't this function now become unnecessary?

@pmrowla WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll still need the wrapper for terraform state mv in DVC