From 094f45d3a342f048a1cb18fde0f8c08ebccda5b8 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 5 Jun 2019 17:12:59 -0700 Subject: [PATCH] Add site name to all assets. Include Travis build. --- core/unifi/.travis.yml | 14 ++++++++++++++ core/unifi/clients_influx.go | 1 + core/unifi/clients_type.go | 1 + core/unifi/parsers.go | 5 ++++- core/unifi/uap_influx.go | 1 + core/unifi/uap_type.go | 1 + core/unifi/unifi.go | 26 +++++++++++++++++++++----- core/unifi/unifi_test.go | 3 ++- core/unifi/usg_influx.go | 1 + core/unifi/usg_type.go | 1 + core/unifi/usw_influx.go | 1 + core/unifi/usw_type.go | 1 + 12 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 core/unifi/.travis.yml diff --git a/core/unifi/.travis.yml b/core/unifi/.travis.yml new file mode 100644 index 00000000..c95dc3ca --- /dev/null +++ b/core/unifi/.travis.yml @@ -0,0 +1,14 @@ +language: go +go: +- 1.12.x +before_install: +- mkdir -p $GOPATH/bin + # Download the `dep` binary to bin folder in $GOPATH +- curl -sLo $GOPATH/bin/dep https://github.com/golang/dep/releases/download/v0.5.3/dep-darwin-amd64 +- chmod +x $GOPATH/bin/dep + # 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: +- dep ensure +script: +- golangci-lint run --enable-all -e G402 diff --git a/core/unifi/clients_influx.go b/core/unifi/clients_influx.go index 402bb428..9219c785 100644 --- a/core/unifi/clients_influx.go +++ b/core/unifi/clients_influx.go @@ -25,6 +25,7 @@ func (c UCL) Points() ([]*influx.Point, error) { "mac": c.Mac, "user_id": c.UserID, "site_id": c.SiteID, + "site_name": c.SiteName, "network_id": c.NetworkID, "usergroup_id": c.UserGroupID, "ap_mac": c.ApMac, diff --git a/core/unifi/clients_type.go b/core/unifi/clients_type.go index 2ad29ccf..d0a21c04 100644 --- a/core/unifi/clients_type.go +++ b/core/unifi/clients_type.go @@ -66,6 +66,7 @@ type UCL struct { RxRate int64 `json:"rx_rate"` Signal int64 `json:"signal"` SiteID string `json:"site_id"` + SiteName string `json:"-"` SwDepth int `json:"sw_depth"` SwMac string `json:"sw_mac"` SwPort int `json:"sw_port"` diff --git a/core/unifi/parsers.go b/core/unifi/parsers.go index 70c2d193..672187ea 100644 --- a/core/unifi/parsers.go +++ b/core/unifi/parsers.go @@ -3,7 +3,7 @@ package unifi import "encoding/json" // parseDevices parses the raw JSON from the Unifi Controller into device structures. -func (u *Unifi) parseDevices(data []json.RawMessage) *Devices { +func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices { devices := new(Devices) for _, r := range data { // Loop each item in the raw JSON message, detect its type and unmarshal it. @@ -18,14 +18,17 @@ func (u *Unifi) parseDevices(data []json.RawMessage) *Devices { switch assetType { // Unmarshal again into the correct type.. case "uap": if uap := (UAP{}); u.unmarshalDevice(assetType, r, &uap) == nil { + uap.SiteName = siteName devices.UAPs = append(devices.UAPs, uap) } case "ugw", "usg": // in case they ever fix the name in the api. if usg := (USG{}); u.unmarshalDevice(assetType, r, &usg) == nil { + usg.SiteName = siteName devices.USGs = append(devices.USGs, usg) } case "usw": if usw := (USW{}); u.unmarshalDevice(assetType, r, &usw) == nil { + usw.SiteName = siteName devices.USWs = append(devices.USWs, usw) } default: diff --git a/core/unifi/uap_influx.go b/core/unifi/uap_influx.go index 3b5072f2..53a369f3 100644 --- a/core/unifi/uap_influx.go +++ b/core/unifi/uap_influx.go @@ -20,6 +20,7 @@ func (u UAP) Points() ([]*influx.Point, error) { "device_oid": u.Stat.Oid, "device_ap": u.Stat.Ap, "site_id": u.SiteID, + "site_name": u.SiteName, "name": u.Name, "adopted": u.Adopted.Txt, "bandsteering_mode": u.BandsteeringMode, diff --git a/core/unifi/uap_type.go b/core/unifi/uap_type.go index 5ceae3f4..2250f4e5 100644 --- a/core/unifi/uap_type.go +++ b/core/unifi/uap_type.go @@ -163,6 +163,7 @@ type UAP struct { Scanning FlexBool `json:"scanning"` Serial string `json:"serial"` SiteID string `json:"site_id"` + SiteName string `json:"-"` SpectrumScanning FlexBool `json:"spectrum_scanning"` SSHSessionTable []interface{} `json:"ssh_session_table"` Stat struct { diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 0422a529..02ee58e6 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -60,7 +60,7 @@ func (u *Unifi) getController(user, pass string) error { // GetClients returns a response full of clients' data from the Unifi Controller. func (u *Unifi) GetClients(sites []Site) (*Clients, error) { - var data []UCL + data := make([]UCL, 0) for _, site := range sites { var response struct { Data []UCL `json:"data"` @@ -71,6 +71,9 @@ func (u *Unifi) GetClients(sites []Site) (*Clients, error) { if err := u.GetData(clientPath, &response); err != nil { return nil, err } + for i := range response.Data { + response.Data[i].SiteName = site.Name + } data = append(data, response.Data...) } return &Clients{UCLs: data}, nil @@ -78,7 +81,7 @@ func (u *Unifi) GetClients(sites []Site) (*Clients, error) { // GetDevices returns a response full of devices' data from the Unifi Controller. func (u *Unifi) GetDevices(sites []Site) (*Devices, error) { - var data []json.RawMessage + devices := new(Devices) for _, site := range sites { u.dLogf("Polling Site '%s' (%s) Devices", site.Name, site.Desc) var response struct { @@ -88,9 +91,22 @@ func (u *Unifi) GetDevices(sites []Site) (*Devices, error) { if err := u.GetData(devicePath, &response); err != nil { return nil, err } - data = append(data, response.Data...) + loopDevices := u.parseDevices(response.Data, site.Name) + // Add SiteName to each device asset. + for i := range loopDevices.UAPs { + loopDevices.UAPs[i].SiteName = site.Name + } + for i := range loopDevices.USGs { + loopDevices.USGs[i].SiteName = site.Name + } + for i := range loopDevices.USWs { + loopDevices.USWs[i].SiteName = site.Name + } + devices.UAPs = append(devices.UAPs, loopDevices.UAPs...) + devices.USGs = append(devices.USGs, loopDevices.USGs...) + devices.USWs = append(devices.USWs, loopDevices.USWs...) } - return u.parseDevices(data), nil + return devices, nil } // GetSites returns a list of configured sites on the Unifi controller. @@ -101,7 +117,7 @@ func (u *Unifi) GetSites() ([]Site, error) { if err := u.GetData(SiteList, &response); err != nil { return nil, err } - var sites []string + sites := make([]string, 0) for i := range response.Data { sites = append(sites, response.Data[i].Name) } diff --git a/core/unifi/unifi_test.go b/core/unifi/unifi_test.go index dff7687d..7ab1df31 100644 --- a/core/unifi/unifi_test.go +++ b/core/unifi/unifi_test.go @@ -17,7 +17,8 @@ func TestNewUnifi(t *testing.T) { a.EqualValues(url, authReq.baseURL) a.Contains(err.Error(), "authReq.Do(req):", "an invalid destination should product a .Do(req) error.") /* TODO: OPEN web server, check parameters posted, more. This test is incomplete. - a.EqualValues(`{"username": "user1","password": "pass2"}`, string(post_params), "user/pass json parameters improperly encoded") + a.EqualValues(`{"username": "user1","password": "pass2"}`, string(post_params), + "user/pass json parameters improperly encoded") */ } diff --git a/core/unifi/usg_influx.go b/core/unifi/usg_influx.go index c6225788..1ab176ae 100644 --- a/core/unifi/usg_influx.go +++ b/core/unifi/usg_influx.go @@ -16,6 +16,7 @@ func (u USG) Points() ([]*influx.Point, error) { "device_type": u.Stat.O, "device_oid": u.Stat.Oid, "site_id": u.SiteID, + "site_name": u.SiteName, "adopted": u.Adopted.Txt, "name": u.Name, "adopt_ip": u.AdoptIP, diff --git a/core/unifi/usg_type.go b/core/unifi/usg_type.go index 014fdad1..b7762c73 100644 --- a/core/unifi/usg_type.go +++ b/core/unifi/usg_type.go @@ -116,6 +116,7 @@ type USG struct { RxBytes FlexInt `json:"rx_bytes"` Serial string `json:"serial"` SiteID string `json:"site_id"` + SiteName string `json:"-"` SpeedtestStatus struct { Latency float64 `json:"latency"` Rundate float64 `json:"rundate"` diff --git a/core/unifi/usw_influx.go b/core/unifi/usw_influx.go index 8435c53e..fa77d2aa 100644 --- a/core/unifi/usw_influx.go +++ b/core/unifi/usw_influx.go @@ -15,6 +15,7 @@ func (u USW) Points() ([]*influx.Point, error) { "device_type": u.Stat.O, "device_oid": u.Stat.Oid, "site_id": u.SiteID, + "site_name": u.SiteName, "name": u.Name, "adopted": u.Adopted.Txt, "adopt_ip": u.AdoptIP, diff --git a/core/unifi/usw_type.go b/core/unifi/usw_type.go index ef0081ff..c234eba7 100644 --- a/core/unifi/usw_type.go +++ b/core/unifi/usw_type.go @@ -117,6 +117,7 @@ type USW struct { RxBytes float64 `json:"rx_bytes"` Serial string `json:"serial"` SiteID string `json:"site_id"` + SiteName string `json:"-"` SSHSessionTable []interface{} `json:"ssh_session_table"` Stat struct {