package webapi import ( "encoding/json" "errors" "fmt" "log" "net" "net/url" ) type UpdateRequest struct { IPv4 net.IP IPv6 net.IP UserName string Password string IPv6Net *net.IPNet } func (ur *UpdateRequest) String() string { return fmt.Sprintf("IPv4: %v, IPv6: %v, UserName: %v, Password: %v, IPv6Net: %v", ur.IPv4, ur.IPv6, ur.UserName, ur.Password, ur.IPv6Net) } 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{} if form.Get("IPv4") != "" { if ur.IPv4 = net.ParseIP(form.Get("IPv4")); ur.IPv4 == nil { return nil, errors.New("could not parse IPv4 address") } } if form.Get("IPv6") != "" { if ur.IPv6 = net.ParseIP(form.Get("IPv6")); ur.IPv6 == nil { return nil, errors.New("could not parse IPv6 address") } } ur.UserName = form.Get("UserName") if ur.UserName == "" { return nil, errors.New("a UserName must be specified") } ur.Password = form.Get("Password") if ur.Password == "" { 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") } ur.IPv6Net = ipnet } return ur, nil }