Skip to content

Commit

Permalink
Workaround regarding issue hashicorp/terraform-plugin-sdk#477
Browse files Browse the repository at this point in the history
For handling Application GSLB members
  • Loading branch information
alexissavin committed Jun 17, 2021
1 parent 1c6d8f5 commit ce920de
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
43 changes: 5 additions & 38 deletions solidserver/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
"sort"
"net/url"
"strings"
)
Expand Down Expand Up @@ -286,47 +285,15 @@ func resourceapplicationRead(d *schema.ResourceData, meta interface{}) error {
d.Set("class", buf[0]["appapplication_class_name"].(string))

// Updating gslb_members information
// Suspended because of issue https://github.com/hashicorp/terraform-plugin-sdk/issues/477
// Removed because of issue https://github.com/hashicorp/terraform-plugin-sdk/issues/477
// Doesn't make sense to read this information until this issue is fixed
//if buf[0]["appapplication_gslbserver_list"].(string) != "" {
// d.Set("gslb_members", toStringArrayInterface(strings.Split(strings.TrimSuffix(buf[0]["appapplication_gslbserver_list"].(string), ","), ",")))
//}

if buf[0]["appapplication_gslbserver_list"].(string) != "" {
// Retrieve the list of the remotely configured members
remote_members := toStringArrayInterface(strings.Split(strings.TrimSuffix(buf[0]["appapplication_gslbserver_list"].(string), ","), ","))

// Build local list of member indexed by their offset
local_members := toStringArray(d.Get("gslb_members").([]interface{}))
local_members_offsets := make(map[int]string, len(local_members))
diff := make([]string, 0, len(remote_members))
res := make([]interface{}, 0, len(remote_members))

for _, remote_member := range remote_members {
offset := stringOffsetInSlice(remote_member.(string), local_members)

if offset != -1 {
local_members_offsets[offset] = remote_member.(string)
} else {
diff = append(diff, remote_member.(string))
}
}

// Concat sorted members from by their offset and the diff into d.Set("gslb_members")
keys := make([]int, 0, len(local_members))
for k := range local_members_offsets {
keys = append(keys, k)
}
sort.Ints(keys)
for _, k := range keys {
res = append(res, local_members_offsets[k])
}
for _, v := range diff {
res = append(res, v)
}

d.Set("gslb_members", res)
}
// Workaround
remote_members := strings.Split(strings.TrimSuffix(buf[0]["appapplication_gslbserver_list"].(string), ","), ",")
local_members := toStringArray(d.Get("gslb_members").([]interface{}))
d.Set("gslb_members", typeListConsistentMerge(local_members, remote_members))

// Updating local class_parameters
currentClassParameters := d.Get("class_parameters").(map[string]interface{})
Expand Down
40 changes: 40 additions & 0 deletions solidserver/solidserver-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"math/big"
"net/url"
"sort"
"strconv"
"strings"
)
Expand Down Expand Up @@ -56,6 +57,45 @@ func toStringArrayInterface(in []string) []interface{} {
return out
}

// Consistent merge of TypeList elements, maintaining entries position within the list
// Workaround to TF Plugin SDK issue https://github.com/hashicorp/terraform-plugin-sdk/issues/477
func typeListConsistentMerge(old []string, new []string) []interface{} {
// Step 1 Build local list of member indexed by their offset
old_offsets := make(map[int]string, len(old))
diff := make([]string, 0, len(new))
res := make([]interface{}, 0, len(new))

for _, n := range new {
if n != "" {
offset := stringOffsetInSlice(n, old)

if offset != -1 {
old_offsets[offset] = n
} else {
diff = append(diff, n)
}
}
}

// Merge sorted entries ordered by their offset with the diff array that contain the new ones
// Step 2 Sort the index
keys := make([]int, 0, len(old))
for k := range old_offsets {
keys = append(keys, k)
}
sort.Ints(keys)

// Step 3 build the result
for _, k := range keys {
res = append(res, old_offsets[k])
}
for _, v := range diff {
res = append(res, v)
}

return res
}

// BigIntToHexStr convert a Big Integer into an Hexa String
func BigIntToHexStr(bigInt *big.Int) string {
return fmt.Sprintf("%x", bigInt)
Expand Down

0 comments on commit ce920de

Please sign in to comment.