fix: driftignore that ignore type

In some case driftignore filtering engine was ignoring
some types event if some inclusion rules are defined.
This is because we do not handle the case where we use an
inclusion rule on a resource with an ID, the git matching logic
is not exactly what we want here.
main
Elie 2022-05-13 14:12:31 +02:00
parent c5ed5b9c6d
commit 0986e6370e
No known key found for this signature in database
GPG Key ID: 399AF69092C727B6
2 changed files with 56 additions and 1 deletions

View File

@ -90,7 +90,7 @@ func (r *DriftIgnore) parseIgnorePattern(line string, patterns *[]gitignore.Patt
func (r *DriftIgnore) isAnyOfChildrenTypesNotIgnored(ty resource.ResourceType) bool { func (r *DriftIgnore) isAnyOfChildrenTypesNotIgnored(ty resource.ResourceType) bool {
childrenTypes := resource.GetMeta(ty).GetChildrenTypes() childrenTypes := resource.GetMeta(ty).GetChildrenTypes()
for _, childrenType := range childrenTypes { for _, childrenType := range childrenTypes {
if !r.match(fmt.Sprintf("%s.*", childrenType)) { if !r.shouldIgnoreType(childrenType) {
return true return true
} }
if r.isAnyOfChildrenTypesNotIgnored(childrenType) { if r.isAnyOfChildrenTypesNotIgnored(childrenType) {
@ -107,6 +107,17 @@ func (r *DriftIgnore) IsTypeIgnored(ty resource.ResourceType) bool {
return false return false
} }
return r.shouldIgnoreType(ty)
}
func (r *DriftIgnore) shouldIgnoreType(ty resource.ResourceType) bool {
for _, pattern := range r.ignorePatterns {
// If a line start with a `!` and if the type match, we should not ignore it
if strings.HasPrefix(pattern, fmt.Sprintf("!%s.", ty)) {
return false
}
}
return r.match(fmt.Sprintf("%s.*", ty)) return r.match(fmt.Sprintf("%s.*", ty))
} }

View File

@ -712,6 +712,50 @@ func TestDriftIgnore_IsTypeIgnored(t *testing.T) {
path: "testdata/drift_ignore_all/.driftignore", path: "testdata/drift_ignore_all/.driftignore",
ignores: []string{"*", "!aws_s3*", "!aws_route53*"}, ignores: []string{"*", "!aws_s3*", "!aws_route53*"},
}, },
{
name: "do not ignore type when one inclusion rule with resource ID exist",
resources: []*resource.Resource{
// This type should not be ignored because of `!aws_iam_policy_attachment.foo*` expression
{
Type: "aws_iam_policy_attachment",
Id: "foobar",
},
// This type should not be ignored because `azurerm_route` type is not ignored and is a child of `azurerm_route_table`
{
Type: "azurerm_route_table",
Id: "uselessId",
},
// This type should not be ignored because of `!azurerm_route.barfoo` expression
{
Type: "azurerm_route",
Id: "barfoo",
},
},
want: []bool{
false,
false,
false,
},
path: "",
ignores: []string{"*", "!aws_iam_policy_attachment.foobar", "!azurerm_route.barfoo"},
},
{
name: "ignore type wildcard while excluding one",
resources: []*resource.Resource{
{
Type: "type_ignored",
},
{
Type: "type_not_ignored",
},
},
want: []bool{
true,
false,
},
path: "",
ignores: []string{"type_*", "!type_not_ignored"},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {