Skip to content

Commit b96c210

Browse files
authored
feat(75): provide access to prior state resources if available (#76)
1 parent 9524892 commit b96c210

5 files changed

Lines changed: 154 additions & 2 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,7 @@ override.tf.json
159159

160160
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
161161
# example: *tfplan*
162+
163+
# Ignore IDE configuration files
164+
**/.idea/*
165+
**/.vscode/*

test/fixtures/plan_output.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,25 @@
9595
},
9696
"prior_state": {
9797
"format_version": "0.1",
98-
"terraform_version": "0.12.6"
98+
"terraform_version": "0.12.6",
99+
"values": {
100+
"root_module": {
101+
"resources": [
102+
{
103+
"address": "data.google_client_config.current",
104+
"mode": "data",
105+
"type": "google_client_config",
106+
"name": "current",
107+
"provider_name": "registry.terraform.io/hashicorp/google",
108+
"schema_version": 0,
109+
"values": {
110+
"description": "foo-value"
111+
},
112+
"sensitive_values": {}
113+
}
114+
]
115+
}
116+
}
99117
},
100118
"configuration": {
101119
"provider_config": {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"format_version": "0.1",
3+
"terraform_version": "0.12.6",
4+
"variables": {
5+
"foo": {
6+
"value": "bar"
7+
}
8+
},
9+
"planned_values": {
10+
"outputs": {
11+
"spam": {
12+
"sensitive": false,
13+
"value": "baz"
14+
}
15+
},
16+
"root_module": {
17+
"resources": [
18+
{
19+
"address": "spam.somespam",
20+
"mode": "managed",
21+
"type": "spam",
22+
"name": "somespam",
23+
"provider_name": "dummy",
24+
"schema_version": 0,
25+
"values": {
26+
"spam-value": "spam"
27+
}
28+
}
29+
],
30+
"child_modules": [
31+
{
32+
"address": "module.parent",
33+
"resources": [
34+
{
35+
"address": "module.parent.foo.somefoo",
36+
"type": "foo",
37+
"name": "somefoo",
38+
"provider_name": "dummy",
39+
"values": {
40+
"foo-value": "foo"
41+
}
42+
}
43+
],
44+
"child_modules": [
45+
{
46+
"address": "module.parent.module.child",
47+
"resources": [
48+
{
49+
"address": "module.parent.module.child.eggs.someeggs",
50+
"type": "eggs",
51+
"name": "someeggs",
52+
"provider_name": "dummy",
53+
"values": {
54+
"eggs-value": "eggs"
55+
}
56+
}
57+
]
58+
}
59+
]
60+
}
61+
]
62+
}
63+
},
64+
"resource_changes": [
65+
{
66+
"address": "module.resource-change.foo_resource.somefoo",
67+
"module_address": "module.resource-change",
68+
"mode": "managed",
69+
"type": "foo-resource",
70+
"name": "somefoo",
71+
"provider_name": "dummy",
72+
"change": {
73+
"actions": [
74+
"create"
75+
],
76+
"before": null,
77+
"after": {
78+
"foo": "foo-value"
79+
},
80+
"after_unknown": {
81+
"eggs": true
82+
}
83+
}
84+
}
85+
],
86+
"output_changes": {
87+
"spam": {
88+
"actions": [
89+
"create"
90+
],
91+
"before": null,
92+
"after": "bar",
93+
"after_unknown": false
94+
}
95+
},
96+
"configuration": {
97+
"provider_config": {
98+
"google": {
99+
"name": "google"
100+
}
101+
}
102+
}
103+
}

test/test_plan.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# https://www.apache.org/licenses/LICENSE-2.0
7+
# https://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -25,6 +25,13 @@ def plan_out(fixtures_dir):
2525
return tftest.TerraformPlanOutput(json.load(fp))
2626

2727

28+
@pytest.fixture(scope="module")
29+
def plan_out_no_prior(fixtures_dir):
30+
import json
31+
with open('%s/plan_output_no_prior.json' % fixtures_dir) as fp:
32+
return tftest.TerraformPlanOutput(json.load(fp))
33+
34+
2835
def test_output_attributes(plan_out):
2936
assert plan_out.format_version == "0.1"
3037
assert plan_out.terraform_version == "0.12.6"
@@ -86,3 +93,15 @@ def test_plan_stdout(fixtures_dir):
8693
tf = tftest.TerraformTest('plan_no_resource_changes', fixtures_dir)
8794
result = tf.plan(output=False)
8895
assert 'just_an_output = "Hello, plan!"' in result
96+
97+
98+
def test_plan_prior_state(plan_out):
99+
prior_resources = plan_out.prior_resources
100+
assert len(prior_resources) == 1
101+
assert prior_resources['data.google_client_config.current']['type'] == 'google_client_config'
102+
assert prior_resources['data.google_client_config.current']['values']['description'] == 'foo-value'
103+
104+
105+
def test_plan_no_prior_state(plan_out_no_prior):
106+
prior_resources = plan_out_no_prior.prior_resources
107+
assert not prior_resources

tftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ def __init__(self, raw):
242242
# there might be no variables defined
243243
self.variables = TerraformValueDict(raw.get('variables', {}))
244244

245+
prior_state = raw.get('prior_state', {})
246+
values = prior_state.get('values', {})
247+
self.prior_root_module = TerraformPlanModule(values.get('root_module', {}))
248+
245249
@property
246250
def resources(self):
247251
return self.root_module.resources
@@ -250,6 +254,10 @@ def resources(self):
250254
def modules(self):
251255
return self.root_module.child_modules
252256

257+
@property
258+
def prior_resources(self):
259+
return self.prior_root_module.resources
260+
253261
def __getattr__(self, name):
254262
return self._raw[name]
255263

0 commit comments

Comments
 (0)