Skip to content

Commit 99e231c

Browse files
authored
Parse state flag (#70)
Adds support for specifying the state file (https://developer.hashicorp.com/terraform/language/settings/backends/local#command-line-arguments) during a plan. This is very useful for setting up test cases for, "what if I modified this attribute on an already provisioned resource?". Whilst an equivalent is possible today by specifying init_vars during `setup`, that requires a unique `terraform init` and hence cache miss for each test case. Using the `state` flag during `plan` allows for a cache hit for the init/setup phase.
1 parent e11c6a9 commit 99e231c

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

tftest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def parse_args(init_vars=None, tf_vars=None, targets=None, **kw):
135135
cmd_args += ['-plugin-dir', kw['plugin_dir']]
136136
if kw.get('refresh') is False:
137137
cmd_args.append('-refresh=false')
138+
if kw.get('state'):
139+
cmd_args += ['-state', kw['state']]
138140
if kw.get('upgrade'):
139141
cmd_args.append('-upgrade')
140142
if isinstance(init_vars, dict):
@@ -595,7 +597,7 @@ def workspace(self, name=None):
595597

596598
@_cache
597599
def plan(self, input=False, color=False, refresh=True, tf_vars=None,
598-
targets=None, output=False, tf_var_file=None, use_cache=False, **kw):
600+
targets=None, output=False, tf_var_file=None, state=None, use_cache=False, **kw):
599601
"""
600602
Run Terraform plan command, optionally returning parsed plan output.
601603
@@ -608,10 +610,11 @@ def plan(self, input=False, color=False, refresh=True, tf_vars=None,
608610
and its dependencies
609611
output: Determines if output will be returned.
610612
tf_var_file: Path to terraform variable configuration file relative to `self.tfdir`.
613+
state: Path to state file to use when reading the prior state snapshot.
611614
"""
612615
cmd_args = parse_args(input=input, color=color, refresh=refresh,
613616
tf_vars=tf_vars, targets=targets,
614-
tf_var_file=tf_var_file, **kw)
617+
tf_var_file=tf_var_file, state=state, **kw)
615618
if not output:
616619
return self.execute_command('plan', *cmd_args).out
617620
with tempfile.NamedTemporaryFile() as fp:

0 commit comments

Comments
 (0)