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

azurerm_storage_account - fix hanging destroy caused by multiple network rules #5565

Merged
merged 4 commits into from
Feb 7, 2020
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
16 changes: 16 additions & 0 deletions azurerm/helpers/common/arrays.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package common

// Remove duplicates from the input array and return unify array (without duplicated elements)
func RemoveDuplicatesFromStringArray(elements []string) []string {
visited := map[string]bool{}
result := []string{}

for v := range elements {
if !visited[elements[v]] {
visited[elements[v]] = true // Mark the element as visited.
result = append(result, elements[v]) // Add it to the result.
}
}

return result
}
38 changes: 38 additions & 0 deletions azurerm/helpers/common/arrays_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package common

import (
"reflect"
"testing"
)

func TestRemoveDuplicatesInStringArray(t *testing.T) {
cases := []struct {
Name string
Input []string
Result []string
}{
{
Name: "contain duplicates",
Input: []string{"string1", "string2", "string1", "string3", ""},
Result: []string{"string1", "string2", "string3", ""},
},
{
Name: "does not contain duplicates",
Input: []string{"string1", "string2", "string3", ""},
Result: []string{"string1", "string2", "string3", ""},
},
{
Name: "empty array",
Input: []string{},
Result: []string{},
},
}

for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
if !reflect.DeepEqual(RemoveDuplicatesFromStringArray(tc.Input), tc.Result) {
t.Fatalf("Expected TestRemoveDuplicatesInStringArray to return %v", tc.Result)
}
})
}
}
13 changes: 10 additions & 3 deletions azurerm/internal/locks/lock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package locks

import "github.com/hashicorp/terraform-plugin-sdk/helper/mutexkv"
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/mutexkv"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/common"
)

// armMutexKV is the instance of MutexKV for ARM resources
var armMutexKV = mutexkv.NewMutexKV()
Expand All @@ -16,7 +19,9 @@ func ByName(name string, resourceType string) {
}

func MultipleByName(names *[]string, resourceType string) {
for _, name := range *names {
newSlice := common.RemoveDuplicatesFromStringArray(*names)

for _, name := range newSlice {
ByName(name, resourceType)
}
}
Expand All @@ -31,7 +36,9 @@ func UnlockByName(name string, resourceType string) {
}

func UnlockMultipleByName(names *[]string, resourceType string) {
for _, name := range *names {
newSlice := common.RemoveDuplicatesFromStringArray(*names)

for _, name := range newSlice {
UnlockByName(name, resourceType)
}
}