Skip to content

Commit

Permalink
Merge pull request #1228 from andyzhangx/tags-equal
Browse files Browse the repository at this point in the history
fix: add tags matching in storage account search
  • Loading branch information
k8s-ci-robot authored Mar 7, 2022
2 parents c036e6f + 3ca9d40 commit b47f4cb
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/provider/azure_storageaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (az *Cloud) getStorageAccounts(ctx context.Context, accountOptions *Account
isLocationEqual(acct, accountOptions) &&
AreVNetRulesEqual(acct, accountOptions) &&
isLargeFileSharesPropertyEqual(acct, accountOptions) &&
isTagsEqual(acct, accountOptions) &&
isTaggedWithSkip(acct) &&
isHnsPropertyEqual(acct, accountOptions) &&
isEnableNfsV3PropertyEqual(acct, accountOptions) &&
Expand Down Expand Up @@ -496,6 +497,26 @@ func isTaggedWithSkip(account storage.Account) bool {
return true
}

func isTagsEqual(account storage.Account, accountOptions *AccountOptions) bool {
// nil and empty map should be regarded as equal
if len(account.Tags) == 0 && len(accountOptions.Tags) == 0 {
return true
}

for k, v := range account.Tags {
var value string
// nil and empty value should be regarded as equal
if v != nil {
value = *v
}
if accountOptions.Tags[k] != value {
return false
}
}

return true
}

func isHnsPropertyEqual(account storage.Account, accountOptions *AccountOptions) bool {
return to.Bool(account.IsHnsEnabled) == to.Bool(accountOptions.IsHnsEnabled)
}
Expand Down
70 changes: 70 additions & 0 deletions pkg/provider/azure_storageaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-02-01/storage"
"github.com/Azure/go-autorest/autorest/to"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -483,3 +484,72 @@ func TestIsPrivateEndpointAsExpected(t *testing.T) {
assert.Equal(t, result, test.expectedResult)
}
}

func TestIsTagsEqual(t *testing.T) {
tests := []struct {
desc string
account storage.Account
accountOptions *AccountOptions
expectedResult bool
}{
{
desc: "empty tags",
account: storage.Account{
Tags: nil,
},
accountOptions: &AccountOptions{},
expectedResult: true,
},
{
desc: "identitical tags",
account: storage.Account{
Tags: map[string]*string{
"key": to.StringPtr("value"),
"key2": nil,
},
},
accountOptions: &AccountOptions{
Tags: map[string]string{
"key": "value",
"key2": "",
},
},
expectedResult: true,
},
{
desc: "identitical tags",
account: storage.Account{
Tags: map[string]*string{
"key": to.StringPtr("value"),
"key2": to.StringPtr("value2"),
},
},
accountOptions: &AccountOptions{
Tags: map[string]string{
"key2": "value2",
"key": "value",
},
},
expectedResult: true,
},
{
desc: "non-identitical tags",
account: storage.Account{
Tags: map[string]*string{
"key": to.StringPtr("value2"),
},
},
accountOptions: &AccountOptions{
Tags: map[string]string{
"key": "value",
},
},
expectedResult: false,
},
}

for _, test := range tests {
result := isTagsEqual(test.account, test.accountOptions)
assert.Equal(t, result, test.expectedResult)
}
}

0 comments on commit b47f4cb

Please sign in to comment.