2021-08-21 18:34:45 +00:00
|
|
|
package webapi
|
2021-08-15 19:37:11 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-21 18:34:45 +00:00
|
|
|
"encoding/json"
|
2021-08-15 19:37:11 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-08-21 18:34:45 +00:00
|
|
|
"log"
|
2021-08-15 19:37:11 +00:00
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2021-08-21 18:34:45 +00:00
|
|
|
type UpdateRequest struct {
|
2021-08-23 18:41:32 +00:00
|
|
|
IPv4 net.IP
|
|
|
|
IPv6 net.IP
|
|
|
|
UserName string
|
|
|
|
Password string
|
|
|
|
IPv6Net *net.IPNet
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 18:34:45 +00:00
|
|
|
func (ur *UpdateRequest) String() string {
|
2021-08-23 18:41:32 +00:00
|
|
|
return fmt.Sprintf("IPv4: %v, IPv6: %v, UserName: %v, Password: %v, IPv6Net: %v",
|
|
|
|
ur.IPv4, ur.IPv6, ur.UserName, ur.Password, ur.IPv6Net)
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 18:34:45 +00:00
|
|
|
func (ur *UpdateRequest) PrettyPrint() string {
|
|
|
|
s, err := json.MarshalIndent(ur, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to pretty print UpdateRequest via JSON: %v", err)
|
|
|
|
}
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createUpdateRequestFromForm(form url.Values) (*UpdateRequest, error) {
|
|
|
|
ur := &UpdateRequest{}
|
2021-08-15 19:37:11 +00:00
|
|
|
|
|
|
|
if form.Get("IPv4") != "" {
|
2021-08-21 18:34:45 +00:00
|
|
|
if ur.IPv4 = net.ParseIP(form.Get("IPv4")); ur.IPv4 == nil {
|
2021-08-15 19:37:11 +00:00
|
|
|
return nil, errors.New("could not parse IPv4 address")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Get("IPv6") != "" {
|
2021-08-21 18:34:45 +00:00
|
|
|
if ur.IPv6 = net.ParseIP(form.Get("IPv6")); ur.IPv6 == nil {
|
2021-08-15 19:37:11 +00:00
|
|
|
return nil, errors.New("could not parse IPv6 address")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:34:45 +00:00
|
|
|
ur.UserName = form.Get("UserName")
|
|
|
|
if ur.UserName == "" {
|
2021-08-15 19:37:11 +00:00
|
|
|
return nil, errors.New("a UserName must be specified")
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:34:45 +00:00
|
|
|
ur.Password = form.Get("Password")
|
|
|
|
if ur.Password == "" {
|
2021-08-15 19:37:11 +00:00
|
|
|
return nil, errors.New("a Password must be specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip6net := form.Get("IPv6Net"); ip6net != "" {
|
|
|
|
_, ipnet, err := net.ParseCIDR(ip6net)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("could not parse IPv6Net")
|
|
|
|
}
|
2021-08-21 18:34:45 +00:00
|
|
|
ur.IPv6Net = ipnet
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 18:34:45 +00:00
|
|
|
return ur, nil
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|