forked from Telmate/terraform-provider-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Telmate#1007 from Tinyblargon/qemu-tags
fix: `tags` incorrect diff
- Loading branch information
Showing
6 changed files
with
189 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package tags | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
pxapi "github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// Returns an unordered list of unique tags | ||
func RemoveDuplicates(tags *[]pxapi.Tag) *[]pxapi.Tag { | ||
if tags == nil || len(*tags) == 0 { | ||
return nil | ||
} | ||
tagMap := make(map[pxapi.Tag]struct{}) | ||
for _, tag := range *tags { | ||
tagMap[tag] = struct{}{} | ||
} | ||
uniqueTags := make([]pxapi.Tag, len(tagMap)) | ||
var index uint | ||
for tag := range tagMap { | ||
uniqueTags[index] = tag | ||
index++ | ||
} | ||
return &uniqueTags | ||
} | ||
|
||
func Schema() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics { | ||
v, ok := i.(string) | ||
if !ok { | ||
return diag.Errorf("expected a string, got: %s", i) | ||
} | ||
for _, e := range *Split(v) { | ||
if err := e.Validate(); err != nil { | ||
return diag.Errorf("tag validation failed: %s", err) | ||
} | ||
} | ||
return nil | ||
}, | ||
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { | ||
return String(sortArray(RemoveDuplicates(Split(old)))) == String(sortArray(RemoveDuplicates(Split(new)))) | ||
}, | ||
} | ||
} | ||
|
||
func sortArray(tags *[]pxapi.Tag) *[]pxapi.Tag { | ||
if tags == nil || len(*tags) == 0 { | ||
return nil | ||
} | ||
sort.SliceStable(*tags, func(i, j int) bool { | ||
return (*tags)[i] < (*tags)[j] | ||
}) | ||
return tags | ||
} | ||
|
||
func Split(rawTags string) *[]pxapi.Tag { | ||
tags := make([]pxapi.Tag, 0) | ||
if rawTags == "" { | ||
return &tags | ||
} | ||
tagArrays := strings.Split(rawTags, ";") | ||
for _, tag := range tagArrays { | ||
tagSubArrays := strings.Split(tag, ",") | ||
if len(tagSubArrays) > 1 { | ||
tmpTags := make([]pxapi.Tag, len(tagSubArrays)) | ||
for i, e := range tagSubArrays { | ||
tmpTags[i] = pxapi.Tag(e) | ||
} | ||
tags = append(tags, tmpTags...) | ||
} else { | ||
tags = append(tags, pxapi.Tag(tag)) | ||
} | ||
} | ||
return &tags | ||
} | ||
|
||
func String(tags *[]pxapi.Tag) (tagList string) { | ||
if tags == nil || len(*tags) == 0 { | ||
return "" | ||
} | ||
for _, tag := range *tags { | ||
tagList += ";" + string(tag) | ||
} | ||
return tagList[1:] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package tags | ||
|
||
import ( | ||
"testing" | ||
|
||
pxapi "github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_RemoveDuplicates(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input *[]pxapi.Tag | ||
output *[]pxapi.Tag | ||
}{ | ||
{name: `nil`}, | ||
{name: `empty`, input: &[]pxapi.Tag{}}, | ||
{name: `single`, input: &[]pxapi.Tag{"a"}, output: &[]pxapi.Tag{"a"}}, | ||
{name: `multiple`, input: &[]pxapi.Tag{"b", "a", "c"}, output: &[]pxapi.Tag{"a", "b", "c"}}, | ||
{name: `duplicate`, input: &[]pxapi.Tag{"b", "a", "c", "b", "a"}, output: &[]pxapi.Tag{"a", "b", "c"}}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, sortArray(RemoveDuplicates(test.input))) | ||
}) | ||
} | ||
} | ||
|
||
func Test_sort(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input *[]pxapi.Tag | ||
output *[]pxapi.Tag | ||
}{ | ||
{name: `nil`}, | ||
{name: `empty`, input: &[]pxapi.Tag{}}, | ||
{name: `single`, input: &[]pxapi.Tag{"a"}, output: &[]pxapi.Tag{"a"}}, | ||
{name: `multiple`, input: &[]pxapi.Tag{"b", "a", "c"}, output: &[]pxapi.Tag{"a", "b", "c"}}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, sortArray(test.input)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_Split(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
output *[]pxapi.Tag | ||
}{ | ||
{name: `empty`, output: &[]pxapi.Tag{}}, | ||
{name: `single`, input: "a", output: &[]pxapi.Tag{"a"}}, | ||
{name: `multiple ,`, input: "b,a,c", output: &[]pxapi.Tag{"b", "a", "c"}}, | ||
{name: `multiple ;`, input: "b;a;c", output: &[]pxapi.Tag{"b", "a", "c"}}, | ||
{name: `multiple mixed`, input: "b,a;c,d;e", output: &[]pxapi.Tag{"b", "a", "c", "d", "e"}}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, Split(test.input)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_String(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input *[]pxapi.Tag | ||
output string | ||
}{ | ||
{name: `nil`}, | ||
{name: `empty`, input: &[]pxapi.Tag{}}, | ||
{name: `single`, input: &[]pxapi.Tag{"a"}, output: "a"}, | ||
{name: `multiple`, input: &[]pxapi.Tag{"b", "a", "c"}, output: "b;a;c"}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, String(test.input)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters