Add site name to all assets. Include Travis build.
This commit is contained in:
parent
6b25037aa4
commit
094f45d3a3
|
|
@ -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
|
||||||
|
|
@ -25,6 +25,7 @@ func (c UCL) Points() ([]*influx.Point, error) {
|
||||||
"mac": c.Mac,
|
"mac": c.Mac,
|
||||||
"user_id": c.UserID,
|
"user_id": c.UserID,
|
||||||
"site_id": c.SiteID,
|
"site_id": c.SiteID,
|
||||||
|
"site_name": c.SiteName,
|
||||||
"network_id": c.NetworkID,
|
"network_id": c.NetworkID,
|
||||||
"usergroup_id": c.UserGroupID,
|
"usergroup_id": c.UserGroupID,
|
||||||
"ap_mac": c.ApMac,
|
"ap_mac": c.ApMac,
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,7 @@ type UCL struct {
|
||||||
RxRate int64 `json:"rx_rate"`
|
RxRate int64 `json:"rx_rate"`
|
||||||
Signal int64 `json:"signal"`
|
Signal int64 `json:"signal"`
|
||||||
SiteID string `json:"site_id"`
|
SiteID string `json:"site_id"`
|
||||||
|
SiteName string `json:"-"`
|
||||||
SwDepth int `json:"sw_depth"`
|
SwDepth int `json:"sw_depth"`
|
||||||
SwMac string `json:"sw_mac"`
|
SwMac string `json:"sw_mac"`
|
||||||
SwPort int `json:"sw_port"`
|
SwPort int `json:"sw_port"`
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package unifi
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
|
|
||||||
// parseDevices parses the raw JSON from the Unifi Controller into device structures.
|
// 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)
|
devices := new(Devices)
|
||||||
for _, r := range data {
|
for _, r := range data {
|
||||||
// Loop each item in the raw JSON message, detect its type and unmarshal it.
|
// 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..
|
switch assetType { // Unmarshal again into the correct type..
|
||||||
case "uap":
|
case "uap":
|
||||||
if uap := (UAP{}); u.unmarshalDevice(assetType, r, &uap) == nil {
|
if uap := (UAP{}); u.unmarshalDevice(assetType, r, &uap) == nil {
|
||||||
|
uap.SiteName = siteName
|
||||||
devices.UAPs = append(devices.UAPs, uap)
|
devices.UAPs = append(devices.UAPs, uap)
|
||||||
}
|
}
|
||||||
case "ugw", "usg": // in case they ever fix the name in the api.
|
case "ugw", "usg": // in case they ever fix the name in the api.
|
||||||
if usg := (USG{}); u.unmarshalDevice(assetType, r, &usg) == nil {
|
if usg := (USG{}); u.unmarshalDevice(assetType, r, &usg) == nil {
|
||||||
|
usg.SiteName = siteName
|
||||||
devices.USGs = append(devices.USGs, usg)
|
devices.USGs = append(devices.USGs, usg)
|
||||||
}
|
}
|
||||||
case "usw":
|
case "usw":
|
||||||
if usw := (USW{}); u.unmarshalDevice(assetType, r, &usw) == nil {
|
if usw := (USW{}); u.unmarshalDevice(assetType, r, &usw) == nil {
|
||||||
|
usw.SiteName = siteName
|
||||||
devices.USWs = append(devices.USWs, usw)
|
devices.USWs = append(devices.USWs, usw)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ func (u UAP) Points() ([]*influx.Point, error) {
|
||||||
"device_oid": u.Stat.Oid,
|
"device_oid": u.Stat.Oid,
|
||||||
"device_ap": u.Stat.Ap,
|
"device_ap": u.Stat.Ap,
|
||||||
"site_id": u.SiteID,
|
"site_id": u.SiteID,
|
||||||
|
"site_name": u.SiteName,
|
||||||
"name": u.Name,
|
"name": u.Name,
|
||||||
"adopted": u.Adopted.Txt,
|
"adopted": u.Adopted.Txt,
|
||||||
"bandsteering_mode": u.BandsteeringMode,
|
"bandsteering_mode": u.BandsteeringMode,
|
||||||
|
|
|
||||||
|
|
@ -163,6 +163,7 @@ type UAP struct {
|
||||||
Scanning FlexBool `json:"scanning"`
|
Scanning FlexBool `json:"scanning"`
|
||||||
Serial string `json:"serial"`
|
Serial string `json:"serial"`
|
||||||
SiteID string `json:"site_id"`
|
SiteID string `json:"site_id"`
|
||||||
|
SiteName string `json:"-"`
|
||||||
SpectrumScanning FlexBool `json:"spectrum_scanning"`
|
SpectrumScanning FlexBool `json:"spectrum_scanning"`
|
||||||
SSHSessionTable []interface{} `json:"ssh_session_table"`
|
SSHSessionTable []interface{} `json:"ssh_session_table"`
|
||||||
Stat struct {
|
Stat struct {
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ func (u *Unifi) getController(user, pass string) error {
|
||||||
|
|
||||||
// GetClients returns a response full of clients' data from the Unifi Controller.
|
// GetClients returns a response full of clients' data from the Unifi Controller.
|
||||||
func (u *Unifi) GetClients(sites []Site) (*Clients, error) {
|
func (u *Unifi) GetClients(sites []Site) (*Clients, error) {
|
||||||
var data []UCL
|
data := make([]UCL, 0)
|
||||||
for _, site := range sites {
|
for _, site := range sites {
|
||||||
var response struct {
|
var response struct {
|
||||||
Data []UCL `json:"data"`
|
Data []UCL `json:"data"`
|
||||||
|
|
@ -71,6 +71,9 @@ func (u *Unifi) GetClients(sites []Site) (*Clients, error) {
|
||||||
if err := u.GetData(clientPath, &response); err != nil {
|
if err := u.GetData(clientPath, &response); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
for i := range response.Data {
|
||||||
|
response.Data[i].SiteName = site.Name
|
||||||
|
}
|
||||||
data = append(data, response.Data...)
|
data = append(data, response.Data...)
|
||||||
}
|
}
|
||||||
return &Clients{UCLs: data}, nil
|
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.
|
// GetDevices returns a response full of devices' data from the Unifi Controller.
|
||||||
func (u *Unifi) GetDevices(sites []Site) (*Devices, error) {
|
func (u *Unifi) GetDevices(sites []Site) (*Devices, error) {
|
||||||
var data []json.RawMessage
|
devices := new(Devices)
|
||||||
for _, site := range sites {
|
for _, site := range sites {
|
||||||
u.dLogf("Polling Site '%s' (%s) Devices", site.Name, site.Desc)
|
u.dLogf("Polling Site '%s' (%s) Devices", site.Name, site.Desc)
|
||||||
var response struct {
|
var response struct {
|
||||||
|
|
@ -88,9 +91,22 @@ func (u *Unifi) GetDevices(sites []Site) (*Devices, error) {
|
||||||
if err := u.GetData(devicePath, &response); err != nil {
|
if err := u.GetData(devicePath, &response); err != nil {
|
||||||
return nil, err
|
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.
|
// 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 {
|
if err := u.GetData(SiteList, &response); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var sites []string
|
sites := make([]string, 0)
|
||||||
for i := range response.Data {
|
for i := range response.Data {
|
||||||
sites = append(sites, response.Data[i].Name)
|
sites = append(sites, response.Data[i].Name)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,8 @@ func TestNewUnifi(t *testing.T) {
|
||||||
a.EqualValues(url, authReq.baseURL)
|
a.EqualValues(url, authReq.baseURL)
|
||||||
a.Contains(err.Error(), "authReq.Do(req):", "an invalid destination should product a .Do(req) error.")
|
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.
|
/* 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")
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ func (u USG) Points() ([]*influx.Point, error) {
|
||||||
"device_type": u.Stat.O,
|
"device_type": u.Stat.O,
|
||||||
"device_oid": u.Stat.Oid,
|
"device_oid": u.Stat.Oid,
|
||||||
"site_id": u.SiteID,
|
"site_id": u.SiteID,
|
||||||
|
"site_name": u.SiteName,
|
||||||
"adopted": u.Adopted.Txt,
|
"adopted": u.Adopted.Txt,
|
||||||
"name": u.Name,
|
"name": u.Name,
|
||||||
"adopt_ip": u.AdoptIP,
|
"adopt_ip": u.AdoptIP,
|
||||||
|
|
|
||||||
|
|
@ -116,6 +116,7 @@ type USG struct {
|
||||||
RxBytes FlexInt `json:"rx_bytes"`
|
RxBytes FlexInt `json:"rx_bytes"`
|
||||||
Serial string `json:"serial"`
|
Serial string `json:"serial"`
|
||||||
SiteID string `json:"site_id"`
|
SiteID string `json:"site_id"`
|
||||||
|
SiteName string `json:"-"`
|
||||||
SpeedtestStatus struct {
|
SpeedtestStatus struct {
|
||||||
Latency float64 `json:"latency"`
|
Latency float64 `json:"latency"`
|
||||||
Rundate float64 `json:"rundate"`
|
Rundate float64 `json:"rundate"`
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ func (u USW) Points() ([]*influx.Point, error) {
|
||||||
"device_type": u.Stat.O,
|
"device_type": u.Stat.O,
|
||||||
"device_oid": u.Stat.Oid,
|
"device_oid": u.Stat.Oid,
|
||||||
"site_id": u.SiteID,
|
"site_id": u.SiteID,
|
||||||
|
"site_name": u.SiteName,
|
||||||
"name": u.Name,
|
"name": u.Name,
|
||||||
"adopted": u.Adopted.Txt,
|
"adopted": u.Adopted.Txt,
|
||||||
"adopt_ip": u.AdoptIP,
|
"adopt_ip": u.AdoptIP,
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,7 @@ type USW struct {
|
||||||
RxBytes float64 `json:"rx_bytes"`
|
RxBytes float64 `json:"rx_bytes"`
|
||||||
Serial string `json:"serial"`
|
Serial string `json:"serial"`
|
||||||
SiteID string `json:"site_id"`
|
SiteID string `json:"site_id"`
|
||||||
|
SiteName string `json:"-"`
|
||||||
SSHSessionTable []interface{} `json:"ssh_session_table"`
|
SSHSessionTable []interface{} `json:"ssh_session_table"`
|
||||||
|
|
||||||
Stat struct {
|
Stat struct {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue