Fix duplicated IPs address suggestions

Use Go 1.21+ stdlib's package slices to filter duplicated suggestions

Signed-off-by: Angel Iglesias <angel.iglesias@merlinsoftware.es>
This commit is contained in:
Angel Iglesias 2023-12-26 22:54:09 +01:00
parent fd5f8fe68b
commit 38fc0868a8
No known key found for this signature in database

View file

@ -9,6 +9,7 @@ import (
"net/http"
"os"
"regexp"
"slices"
"sort"
"strings"
"time"
@ -1018,10 +1019,14 @@ func SuggestIPAllocation(db store.IStore) echo.HandlerFunc {
fmt.Sprintf("Cannot suggest ip allocation: failed to get available ip from network %s", cidr),
})
}
var newAddr string
if strings.Contains(ip, ":") {
suggestedIPs = append(suggestedIPs, fmt.Sprintf("%s/128", ip))
newAddr = fmt.Sprintf("%s/128", ip)
} else {
suggestedIPs = append(suggestedIPs, fmt.Sprintf("%s/32", ip))
newAddr = fmt.Sprintf("%s/32", ip)
}
if !slices.Contains(suggestedIPs, newAddr) {
suggestedIPs = slices.Insert(suggestedIPs, len(suggestedIPs), newAddr)
}
}