Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing resource changes in plan #152

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions terraform_compliance/extensions/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ def _parse_resources(self):
:return: none
'''

#TODO: Consider about using 'resource_changes' instead of 'resources'

# Resources ( exists in Plan )
for findings in seek_key_in_dict(self.raw.get('planned_values', {}).get('root_module', {}), 'resources'):
for resource in findings.get('resources', []):
Expand Down Expand Up @@ -110,6 +108,21 @@ def _parse_resources(self):
else:
self.resources[resource['address']] = resource

# Resource Changes ( exists in Plan )
for finding in self.raw.get('resource_changes', {}):
resource = deepcopy(finding)
change = resource.get('change', {})
actions = change.get('actions', [])
if actions != ['delete']:
resource['values'] = change.get('after', {})
if 'change' in resource:
del resource['change']

if resource['address'].startswith('data'):
self.data[resource['address']] = resource
else:
self.resources[resource['address']] = resource

def _parse_configurations(self):
'''
Assigns all configuration related data defined in the terraform plan. This is mostly used for
Expand Down
59 changes: 59 additions & 0 deletions tests/terraform_compliance/extensions/test_terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,65 @@ def test_parse_resources_child_resources_exists_in_the_state_resource(self, *arg
obj._parse_resources()
self.assertEqual(obj.resources['something'], {'address': 'something'})

@patch.object(TerraformParser, '_read_file', return_value={})
def test_parse_resources_resources_exists_in_the_resource_changes_data(self, *args):
obj = TerraformParser('somefile', parse_it=False)
obj.raw['resource_changes'] = [
{
'address': 'data_something',
'change': {
'actions': ['update'],
'before': {
'key': 'foo'
},
'after': {
'key': 'bar'
}
}
}
]
obj._parse_resources()
self.assertEqual(obj.data['data_something'], {'address': 'data_something', 'values': {'key': 'bar'}})

@patch.object(TerraformParser, '_read_file', return_value={})
def test_parse_resources_resources_exists_in_the_resource_changes_resource(self, *args):
obj = TerraformParser('somefile', parse_it=False)
obj.raw['resource_changes'] = [
{
'address': 'something',
'change': {
'actions': ['update'],
'before': {
'key': 'foo'
},
'after': {
'key': 'bar'
}
}
}
]
obj._parse_resources()
self.assertEqual(obj.resources['something'], {'address': 'something', 'values': {'key': 'bar'}})

@patch.object(TerraformParser, '_read_file', return_value={})
def test_parse_resources_resources_exists_in_the_resource_changes_deleted(self, *args):
obj = TerraformParser('somefile', parse_it=False)
obj.raw['resource_changes'] = [
{
'address': 'something',
'change': {
'actions': ['delete'],
'before': {
'key': 'foo'
},
'after': {
'key': 'bar'
}
}
}
]
obj._parse_resources()
self.assertEqual(obj.resources, {})

@patch.object(TerraformParser, '_read_file', return_value={})
def test_parse_configurations_resources(self, *args):
Expand Down