Repository: iterative/tpi (language: python)

Issue:
Add rename method to TerraformBackend
related to https://github.com/iterative/dvc/pull/6633


Discussion / hints:

Fix patch:
diff --git a/tpi/terraform.py b/tpi/terraform.py
index d630358..c2a64df 100644
--- a/tpi/terraform.py
+++ b/tpi/terraform.py
@@ -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)


Test patch:
diff --git a/tests/test_terraform.py b/tests/test_terraform.py
index 9a344a5..1879520 100644
--- a/tests/test_terraform.py
+++ b/tests/test_terraform.py
@@ -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