Skip to content

Commit

Permalink
Merge pull request #287 from terrateamio/main
Browse files Browse the repository at this point in the history
Release v1
  • Loading branch information
orbitz authored Jun 26, 2024
2 parents 8152ba5 + a02bf1b commit b1fef12
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions terrat_runner/workflow_step_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ def _store_plan(state, plan_storage, work_token, api_base_url, dir_path, workspa
raise Exception('Unknown method')


def run(state, config):
def run_plan(state, config, targets=None):
config = config.copy()
config['args'] = ['plan', '-detailed-exitcode', '-out', '$TERRATEAM_PLAN_FILE']
config['output_key'] = 'plan'

outputs = {}
if targets:
config['args'].extend(['-target=' + addr for addr in targets])

result = workflow_step_terraform.run(state, config)

Expand All @@ -171,9 +172,50 @@ def run(state, config):
or result.workflow_step['exit_code'] == 1)):
return result._replace(workflow_step={'type': 'plan'})

return result


def plan_fast_and_loose(state, config):
config = config.copy()
config['args'] = ['plan', '-detailed-exitcode', '-json', '-refresh=false']
config['output_key'] = 'plan'

result = workflow_step_terraform.run(state, config)

if (result.failed and (
'exit_code' not in result.workflow_step
or result.workflow_step['exit_code'] == 1)):
return result._replace(workflow_step={'type': 'plan'})

output = result.outputs['text']

targets = []

for line in output.splitlines():
line = json.loads(line)

if line.get('type') in ['planned_change', 'resource_drift']:
targets.append(line['change']['resource']['addr'])

return run_plan(state, config, targets)


def run(state, config):
if config.get('mode') == 'fast-and-loose':
result = plan_fast_and_loose(state, config)
else:
result = run_plan(state, config)

if (result.failed and (
'exit_code' not in result.workflow_step
or result.workflow_step['exit_code'] == 1)):
return result

# The terraform CLI has an exit code of 2 if the plan contains changes.
has_changes = result.workflow_step.get('exit_code') == 2

outputs = {}

outputs['plan'] = result.outputs['text']

# Grab just the plan output. If running the plan succeded, we want to just
Expand Down

0 comments on commit b1fef12

Please sign in to comment.