Skip to content

Commit

Permalink
feat: add new utils function to filter map tag (#6416)
Browse files Browse the repository at this point in the history
  • Loading branch information
deer-hang authored Feb 28, 2025
1 parent edf90d9 commit 9c7f924
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
8 changes: 8 additions & 0 deletions huaweicloud/utils/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func FlattenTagsToMap(tags interface{}) map[string]interface{} {
return nil
}

// FlattenSameKeyTagsToMap using to flatten remote tag list and filter tag map with same key.
// Parameters:
// + d : It is required that there is a `tags` field of map type in the parameter.
// + remoteTags: It is required that the parameter is a tag list, for example: [{"key":"key1","value":"value1"}].
func FlattenSameKeyTagsToMap(d *schema.ResourceData, remoteTags interface{}) map[string]interface{} {
return FilterMapWithSameKey(d.Get("tags").(map[string]interface{}), FlattenTagsToMap(remoteTags))
}

// ExpandResourceTags returns the tags for the given map of data.
func ExpandResourceTags(tagmap map[string]interface{}) []tags.ResourceTag {
var taglist []tags.ResourceTag
Expand Down
82 changes: 82 additions & 0 deletions huaweicloud/utils/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"reflect"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

Expand Down Expand Up @@ -50,3 +52,83 @@ func TestTagsFunc_ExpandResourceTagsMap(t *testing.T) {

t.Logf("All processing results of the ExpandResourceTagsMap method meets expectation")
}

func TestTagsFunc_FlattenSameKeyTagsToMap(t *testing.T) {
resource := &schema.Resource{
Schema: map[string]*schema.Schema{
"tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}

d := resource.TestResourceData()
var (
rawArray = []map[string]interface{}{
{"a": "b"},
{"a": "b"},
{"a": "b", "c": "d"},
{"a": "b", "c": "d"},
{"a": "b"},
{},
{},
}

remoteTagArray = []interface{}{
[]interface{}{
map[string]interface{}{"key": "a", "value": "d"},
},
[]interface{}{
map[string]interface{}{"key": "a", "value": "d"},
map[string]interface{}{"key": "m", "value": "n"},
},
[]interface{}{
map[string]interface{}{"key": "a", "value": "d"},
map[string]interface{}{"key": "m", "value": "n"},
},
[]interface{}{
map[string]interface{}{"key": "a", "value": "d"},
map[string]interface{}{"key": "c", "value": "a"},
map[string]interface{}{"key": "m", "value": "n"},
},
make([]interface{}, 0),
[]interface{}{
map[string]interface{}{"key": "m", "value": "n"},
},
make([]interface{}, 0),
}

expectedArray = []map[string]interface{}{
{"a": "d"},
{"a": "d"},
{"a": "d"},
{"a": "d", "c": "a"},
{},
{},
{},
}
)

for i := 0; i < 7; i++ {
if err := d.Set("tags", rawArray[i]); err != nil {
t.Fatalf("error setting tags attribute: %s", utils.Yellow(err))
}

if !reflect.DeepEqual(d.Get("tags"), rawArray[i]) {
t.Fatalf("error setting tags attribute, want '%v', but got '%v'", utils.Green(rawArray[i]),
utils.Yellow(d.Get("tags")))
}

remoteTags := remoteTagArray[i]
expectedMap := expectedArray[i]
result := utils.FlattenSameKeyTagsToMap(d, remoteTags)

if !reflect.DeepEqual(result, expectedMap) {
t.Fatalf("The processing result of the function 'FlattenSameKeyTagsToMap' is not as expected, want '%v', "+
"but got '%v'", utils.Green(expectedMap), utils.Yellow(result))
}
t.Logf("The processing result of `FlattenSameKeyTagsToMap` method meets expectation: %s", utils.Green(expectedMap))
}
}
20 changes: 20 additions & 0 deletions huaweicloud/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,23 @@ func IsUUID(uuid string) bool {
match, _ := regexp.MatchString(pattern, uuid)
return match
}

// FilterMapWithSameKey using to filter the value of `filterMap` by the key of `rawMap`, and return the filtered map.
// Example:
// Parameters rawMap = {"a":"b"}, filterMap = {"a":"d"}; Return {"a":"d"}
// Parameters rawMap = {"a":"b"}, filterMap = {"a":"d", "m":"n"}; Return {"a":"d"}
// Parameters rawMap = {"a":"b", "c":"d"}, filterMap = {"a":"d", "m":"n"}; Return {"a":"d"}
// Parameters rawMap = {"a":"b", "c":"d"}, filterMap = {"a":"d", "c":"a", "m":"n"}; Return {"a":"d", "c":"a"}
// Parameters rawMap = {"a":"b"}, filterMap = {}; Return {}
// Parameters rawMap = {}, filterMap = {"m":"n"}; Return {}
// Parameters rawMap = {}, filterMap = {}; Return {}
func FilterMapWithSameKey(rawMap, filterMap map[string]interface{}) map[string]interface{} {
rst := make(map[string]interface{})
for rawKey := range rawMap {
if filterValue, ok := filterMap[rawKey]; ok {
rst[rawKey] = filterValue
}
}

return rst
}
47 changes: 47 additions & 0 deletions huaweicloud/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,50 @@ func TestAccFunction_IsUUID(t *testing.T) {
t.Logf("The processing result of IsUUID method meets expectation: %s", Green(expected[i]))
}
}

func TestAccFunction_FilterMapWithSameKey(t *testing.T) {
var (
rawArray = []map[string]interface{}{
{"a": "b"},
{"a": "b"},
{"a": "b", "c": "d"},
{"a": "b", "c": "d"},
{"a": "b"},
{},
{},
}

filterArray = []map[string]interface{}{
{"a": "d"},
{"a": "d", "m": "n"},
{"a": "d", "m": "n"},
{"a": "d", "c": "a", "m": "n"},
{},
{"m": "n"},
{},
}

expectedArray = []map[string]interface{}{
{"a": "d"},
{"a": "d"},
{"a": "d"},
{"a": "d", "c": "a"},
{},
{},
{},
}
)

for i := 0; i < 7; i++ {
rawMap := rawArray[i]
filterMap := filterArray[i]
expectedMap := expectedArray[i]
result := FilterMapWithSameKey(rawMap, filterMap)

if !reflect.DeepEqual(result, expectedMap) {
t.Fatalf("The processing result of the function 'FilterMapWithSameKey' is not as expected, want '%v', "+
"but got '%v'", Green(expectedMap), Yellow(result))
}
t.Logf("The processing result of `FilterMapWithSameKey` method meets expectation: %s", Green(expectedMap))
}
}

0 comments on commit 9c7f924

Please sign in to comment.