diff --git a/core/unifi/.travis.yml b/core/unifi/.travis.yml index 46ed9dd0..654efb02 100644 --- a/core/unifi/.travis.yml +++ b/core/unifi/.travis.yml @@ -1,11 +1,9 @@ language: go go: -- 1.13.x +- 1.14.x before_install: # download super-linter: golangci-lint - curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest -install: -- go mod download script: -- golangci-lint run --enable-all -e G402 -D gochecknoglobals +- golangci-lint run --enable-all - go test ./... diff --git a/core/unifi/clients.go b/core/unifi/clients.go index 5956c5ff..a5a76019 100644 --- a/core/unifi/clients.go +++ b/core/unifi/clients.go @@ -2,6 +2,7 @@ package unifi import ( "fmt" + "strings" ) // GetClients returns a response full of clients' data from the UniFi Controller. @@ -26,8 +27,8 @@ func (u *Unifi) GetClients(sites Sites) (Clients, error) { // Add the special "Site Name" to each client. This becomes a Grafana filter somewhere. response.Data[i].SiteName = site.Desc + " (" + site.Name + ")" // Fix name and hostname fields. Sometimes one or the other is blank. - response.Data[i].Hostname = pick(d.Hostname, d.Name, d.Mac) - response.Data[i].Name = pick(d.Name, d.Hostname) + response.Data[i].Hostname = strings.TrimSpace(pick(d.Hostname, d.Name, d.Mac)) + response.Data[i].Name = strings.TrimSpace(pick(d.Name, d.Hostname)) } data = append(data, response.Data...) diff --git a/core/unifi/devices.go b/core/unifi/devices.go index febcec37..9ddfefe6 100644 --- a/core/unifi/devices.go +++ b/core/unifi/devices.go @@ -3,6 +3,7 @@ package unifi import ( "encoding/json" "fmt" + "strings" ) // GetDevices returns a response full of devices' data from the UniFi Controller. @@ -50,25 +51,25 @@ func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices { case "uap": dev := &UAP{SiteName: siteName, SourceName: u.URL} if u.unmarshalDevice(assetType, r, dev) == nil { - dev.Name = pick(dev.Name, dev.Mac) + dev.Name = strings.TrimSpace(pick(dev.Name, dev.Mac)) devices.UAPs = append(devices.UAPs, dev) } case "ugw", "usg": // in case they ever fix the name in the api. dev := &USG{SiteName: siteName, SourceName: u.URL} if u.unmarshalDevice(assetType, r, dev) == nil { - dev.Name = pick(dev.Name, dev.Mac) + dev.Name = strings.TrimSpace(pick(dev.Name, dev.Mac)) devices.USGs = append(devices.USGs, dev) } case "usw": dev := &USW{SiteName: siteName, SourceName: u.URL} if u.unmarshalDevice(assetType, r, dev) == nil { - dev.Name = pick(dev.Name, dev.Mac) + dev.Name = strings.TrimSpace(pick(dev.Name, dev.Mac)) devices.USWs = append(devices.USWs, dev) } case "udm": dev := &UDM{SiteName: siteName, SourceName: u.URL} if u.unmarshalDevice(assetType, r, dev) == nil { - dev.Name = pick(dev.Name, dev.Mac) + dev.Name = strings.TrimSpace(pick(dev.Name, dev.Mac)) devices.UDMs = append(devices.UDMs, dev) } default: diff --git a/core/unifi/dpi.go b/core/unifi/dpi.go index 86170d03..6dc3d843 100644 --- a/core/unifi/dpi.go +++ b/core/unifi/dpi.go @@ -56,7 +56,7 @@ func (d DPIMap) Keys() []string { // DPICats maps the categories to descriptions. // From: https://fw-download.ubnt.com/data/usg-dpi/1628-debian-v1.442.0-05f5a57eaef344358bd5a8e84a184c18.tar -var DPICats = DPIMap{ +var DPICats = DPIMap{ // nolint: gochecknoglobals 0: "Instant Messengers", 1: "Peer-to-Peer Networks", 3: "File Sharing", @@ -83,7 +83,7 @@ var DPICats = DPIMap{ // DPIApps maps the applications to names. // From: https://fw-download.ubnt.com/data/usg-dpi/1628-debian-v1.442.0-05f5a57eaef344358bd5a8e84a184c18.tar -var DPIApps = DPIMap{ +var DPIApps = DPIMap{ // nolint: gochecknoglobals 1: "MSN", 2: "Yahoo Messenger", 3: "AIM/ICQ/iIM", diff --git a/core/unifi/go.mod b/core/unifi/go.mod index f72667b3..9e2e4827 100644 --- a/core/unifi/go.mod +++ b/core/unifi/go.mod @@ -1,10 +1,11 @@ module github.com/unifi-poller/unifi -go 1.13 +go 1.14 require ( github.com/davecgh/go-spew v1.1.1 + github.com/pkg/errors v0.9.1 github.com/pmezard/go-difflib v1.0.0 github.com/stretchr/testify v1.4.0 - golang.org/x/net v0.0.0-20200202094626-16171245cfb2 + golang.org/x/net v0.0.0-20200602114024-627f9648deb9 ) diff --git a/core/unifi/go.sum b/core/unifi/go.sum index 35df3bcb..c8eb7c3d 100644 --- a/core/unifi/go.sum +++ b/core/unifi/go.sum @@ -1,6 +1,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -9,7 +11,10 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM= +golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/core/unifi/ids.go b/core/unifi/ids.go index 6af0c8ab..1fc707aa 100644 --- a/core/unifi/ids.go +++ b/core/unifi/ids.go @@ -6,6 +6,8 @@ import ( "io/ioutil" "net/http" "time" + + "github.com/pkg/errors" ) // IDSList contains a list that contains all of the IDS Events on a controller. @@ -135,7 +137,7 @@ func (u *Unifi) GetSiteIDS(site *Site, from, to time.Time) ([]*IDS, error) { } if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("invalid status code from server %s", resp.Status) + return nil, errors.Wrap(errInvalidStatusCode, resp.Status) } if err := json.Unmarshal(body, &response); err != nil { diff --git a/core/unifi/site.go b/core/unifi/site.go index 03207ae1..f521a77e 100644 --- a/core/unifi/site.go +++ b/core/unifi/site.go @@ -5,8 +5,9 @@ import ( "strings" ) -// Satisfy gomnd -const oneItem = 1 +var ( + errDPIDataBug = fmt.Errorf("dpi data table contains more than 1 item; please open a bug report") +) // GetSites returns a list of configured sites on the UniFi controller. func (u *Unifi) GetSites() (Sites, error) { @@ -24,7 +25,7 @@ func (u *Unifi) GetSites() (Sites, error) { // Add special SourceName value. response.Data[i].SourceName = u.URL // If the human name is missing (description), set it to the cryptic name. - response.Data[i].Desc = pick(d.Desc, d.Name) + response.Data[i].Desc = strings.TrimSpace(pick(d.Desc, d.Name)) // Add the custom site name to each site. used as a Grafana filter somewhere. response.Data[i].SiteName = d.Desc + " (" + d.Name + ")" sites = append(sites, d.Name) // used for debug log only @@ -51,9 +52,9 @@ func (u *Unifi) GetSiteDPI(sites Sites) ([]*DPITable, error) { return nil, err } - if l := len(response.Data); l > oneItem { - return nil, fmt.Errorf("dpi data table contains more than 1 item; please open a bug report") - } else if l != oneItem { + if l := len(response.Data); l > 1 { + return nil, errDPIDataBug + } else if l != 1 { continue } diff --git a/core/unifi/types.go b/core/unifi/types.go index d8c56a76..a5a5c504 100644 --- a/core/unifi/types.go +++ b/core/unifi/types.go @@ -6,6 +6,12 @@ import ( "net/http" "strconv" "strings" + + "github.com/pkg/errors" +) + +var ( + errCannotUnmarshalFlexInt = fmt.Errorf("cannot unmarshal to FlexInt") ) // This is a list of unifi API paths. @@ -124,7 +130,7 @@ func (f *FlexInt) UnmarshalJSON(b []byte) error { f.Txt = "0" f.Val = 0 default: - return fmt.Errorf("cannot unmarshal to FlexInt: %s", b) + return errors.Wrapf(errCannotUnmarshalFlexInt, "%v", b) } return nil diff --git a/core/unifi/types_test.go b/core/unifi/types_test.go index a7eeef06..118662e1 100644 --- a/core/unifi/types_test.go +++ b/core/unifi/types_test.go @@ -1,4 +1,4 @@ -package unifi +package unifi // nolint: testpackage import ( "encoding/json" diff --git a/core/unifi/uap.go b/core/unifi/uap.go index 23f20c29..0d3b3172 100644 --- a/core/unifi/uap.go +++ b/core/unifi/uap.go @@ -399,7 +399,7 @@ type RadioTable []struct { WlangroupID string `json:"wlangroup_id"` } -// RadioTableStats is part of the data shared between UAP and UDM +// RadioTableStats is part of the data shared between UAP and UDM. type RadioTableStats []struct { Name string `json:"name"` Channel FlexInt `json:"channel"` diff --git a/core/unifi/uap_test.go b/core/unifi/uap_test.go index 155b3539..3c6c28a1 100644 --- a/core/unifi/uap_test.go +++ b/core/unifi/uap_test.go @@ -1,4 +1,4 @@ -package unifi +package unifi // nolint: testpackage import ( "testing" diff --git a/core/unifi/udm.go b/core/unifi/udm.go index 16842809..49727256 100644 --- a/core/unifi/udm.go +++ b/core/unifi/udm.go @@ -175,7 +175,7 @@ type NetworkTable []struct { } // UDMStat holds the "stat" data for a dream machine. -// A dream machine is a USG + USW + Controller +// A dream machine is a USG + USW + Controller. type UDMStat struct { *Gw `json:"gw"` *Sw `json:"sw"` diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 7614ce5e..5b3ddd5d 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -18,9 +18,15 @@ import ( "strings" "time" + "github.com/pkg/errors" "golang.org/x/net/publicsuffix" ) +var ( + errAuthenticationFailed = fmt.Errorf("authentication failed") + errInvalidStatusCode = fmt.Errorf("invalid status code from server") +) + // NewUnifi creates a http.Client with authenticated cookies. // Used to make additional, authenticated requests to the APIs. // Start here. @@ -44,7 +50,7 @@ func NewUnifi(config *Config) (*Unifi, error) { Client: &http.Client{ Jar: jar, Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: !config.VerifySSL}, + TLSClientConfig: &tls.Config{InsecureSkipVerify: !config.VerifySSL}, // nolint: gosec }, }, } @@ -58,7 +64,7 @@ func NewUnifi(config *Config) (*Unifi, error) { } if err := u.GetServerData(); err != nil { - return u, fmt.Errorf("unable to get server version: %v", err) + return u, errors.Wrap(err, "unable to get server version") } return u, nil @@ -85,7 +91,7 @@ func (u *Unifi) Login() error { req.URL, time.Since(start).Round(time.Millisecond), resp.ContentLength) if resp.StatusCode != http.StatusOK { - return fmt.Errorf("authentication failed (user: %s): %s (status: %s)", + return errors.Wrapf(errAuthenticationFailed, "(user: %s): %s (status: %s)", u.User, req.URL, resp.Status) } @@ -111,7 +117,7 @@ func (u *Unifi) checkNewStyleAPI() error { return http.ErrUseLastResponse }, Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: !u.VerifySSL}, + TLSClientConfig: &tls.Config{InsecureSkipVerify: !u.VerifySSL}, // nolint: gosec }, } @@ -216,7 +222,7 @@ func (u *Unifi) GetJSON(apiPath string, params ...string) ([]byte, error) { } if resp.StatusCode != http.StatusOK { - err = fmt.Errorf("invalid status code from server for %s: %s", req.URL, resp.Status) + err = errors.Wrapf(errInvalidStatusCode, "%s: %s", req.URL, resp.Status) } return body, err diff --git a/core/unifi/unifi_test.go b/core/unifi/unifi_test.go index 9c9015fb..28204201 100644 --- a/core/unifi/unifi_test.go +++ b/core/unifi/unifi_test.go @@ -1,4 +1,4 @@ -package unifi +package unifi // nolint: testpackage import ( "io/ioutil" diff --git a/core/unifi/usg_test.go b/core/unifi/usg_test.go index 16bb6a18..2905d066 100644 --- a/core/unifi/usg_test.go +++ b/core/unifi/usg_test.go @@ -1,4 +1,4 @@ -package unifi +package unifi // nolint: testpackage import ( "testing" diff --git a/core/unifi/usw_test.go b/core/unifi/usw_test.go index 66bfac63..4ba1eb5e 100644 --- a/core/unifi/usw_test.go +++ b/core/unifi/usw_test.go @@ -1,4 +1,4 @@ -package unifi +package unifi // nolint: testpackage import ( "testing"