Add site name to all assets. Include Travis build.

This commit is contained in:
David Newhall II 2019-06-05 17:12:59 -07:00
parent 6b25037aa4
commit 094f45d3a3
12 changed files with 49 additions and 7 deletions

14
core/unifi/.travis.yml Normal file
View File

@ -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

View File

@ -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,

View File

@ -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"`

View File

@ -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:

View File

@ -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,

View File

@ -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 {

View File

@ -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
}
return u.parseDevices(data), nil
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 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)
}

View File

@ -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")
*/
}

View File

@ -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,

View File

@ -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"`

View File

@ -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,

View File

@ -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 {