From 8d9af25d472b8309ff0d122b6697ef92b831fb64 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Sun, 7 Mar 2021 17:57:16 -0800 Subject: [PATCH] a fe wmore tweaks to align outputs.a --- core/unifi/unifi_test.go | 13 +++- core/unifi/usg.go | 70 ++++++++------------- core/unifi/usw.go | 129 ++++++++++++++++++--------------------- core/unifi/uxg.go | 52 ++++++++-------- 4 files changed, 121 insertions(+), 143 deletions(-) diff --git a/core/unifi/unifi_test.go b/core/unifi/unifi_test.go index 1ef3725f..f466a950 100644 --- a/core/unifi/unifi_test.go +++ b/core/unifi/unifi_test.go @@ -30,9 +30,11 @@ func TestUniReq(t *testing.T) { a := assert.New(t) p := "/test/path" u := "http://some.url:8443" + // Test empty parameters. authReq := &Unifi{Client: &http.Client{}, Config: &Config{URL: u, DebugLog: discardLogs}} r, err := authReq.UniReq(p, "") + a.Nil(err, "newrequest must not produce an error") a.EqualValues(p, r.URL.Path, "the provided apiPath was not added to http request") @@ -45,11 +47,13 @@ func TestUniReq(t *testing.T) { authReq = &Unifi{Client: &http.Client{}, Config: &Config{URL: "http://some.url:8443", DebugLog: discardLogs}} r, err = authReq.UniReq(p, k) a.Nil(err, "newrequest must not produce an error") + a.EqualValues(p, r.URL.Path, "the provided apiPath was not added to http request") a.EqualValues(u, r.URL.Scheme+"://"+r.URL.Host, "URL improperly encoded") a.EqualValues("POST", r.Method, "with parameters the method must be POST") a.EqualValues("application/json", r.Header.Get("Accept"), "Accept header must be set to application/json") + // Check the parameters. d, err := ioutil.ReadAll(r.Body) a.Nil(err, "problem reading request body, POST parameters may be malformed") @@ -61,28 +65,31 @@ func TestUniReqPut(t *testing.T) { a := assert.New(t) p := "/test/path" u := "http://some.url:8443" + // Test empty parameters. authReq := &Unifi{Client: &http.Client{}, Config: &Config{URL: u, DebugLog: discardLogs}} - r, err := authReq.UniReqPut(p, "") + _, err := authReq.UniReqPut(p, "") a.NotNil(err, "empty params must produce an error") // Test with parameters k := "key1=value9&key2=value7" authReq = &Unifi{Client: &http.Client{}, Config: &Config{URL: "http://some.url:8443", DebugLog: discardLogs}} - r, err = authReq.UniReqPut(p, k) + r, err := authReq.UniReqPut(p, k) a.Nil(err, "newrequest must not produce an error") + a.EqualValues(p, r.URL.Path, "the provided apiPath was not added to http request") a.EqualValues(u, r.URL.Scheme+"://"+r.URL.Host, "URL improperly encoded") a.EqualValues("PUT", r.Method, "with parameters the method must be POST") a.EqualValues("application/json", r.Header.Get("Accept"), "Accept header must be set to application/json") + // Check the parameters. d, err := ioutil.ReadAll(r.Body) a.Nil(err, "problem reading request body, PUT parameters may be malformed") a.EqualValues(k, string(d), "PUT parameters improperly encoded") } -/* NOT DONE: OPEN web server, check parameters posted, more. This test is incomplete. +/* NOT DONE: OPEN web server, check parameters posted, more. These tests are incomplete. a.EqualValues(`{"username": "user1","password": "pass2"}`, string(post_params), "user/pass json parameters improperly encoded") */ diff --git a/core/unifi/usg.go b/core/unifi/usg.go index 60815209..751b76f8 100644 --- a/core/unifi/usg.go +++ b/core/unifi/usg.go @@ -53,40 +53,19 @@ type USG struct { SpeedtestStatusSaved FlexBool `json:"speedtest-status-saved"` Wan1 Wan `json:"wan1"` Wan2 Wan `json:"wan2"` - PortTable []struct { - Name string `json:"name"` - Ifname string `json:"ifname"` - IP string `json:"ip"` - Netmask string `json:"netmask"` - Mac string `json:"mac"` - Up FlexBool `json:"up"` - Speed FlexInt `json:"speed"` - FullDuplex FlexBool `json:"full_duplex"` - RxBytes FlexInt `json:"rx_bytes"` - RxDropped FlexInt `json:"rx_dropped"` - RxErrors FlexInt `json:"rx_errors"` - RxPackets FlexInt `json:"rx_packets"` - TxBytes FlexInt `json:"tx_bytes"` - TxDropped FlexInt `json:"tx_dropped"` - TxErrors FlexInt `json:"tx_errors"` - TxPackets FlexInt `json:"tx_packets"` - RxMulticast FlexInt `json:"rx_multicast"` - Enable FlexBool `json:"enable"` - DNS []string `json:"dns,omitempty"` - Gateway string `json:"gateway,omitempty"` - } `json:"port_table"` - NetworkTable NetworkTable `json:"network_table"` - Uplink Uplink `json:"uplink"` - Stat USGStat `json:"stat"` - TxBytes FlexInt `json:"tx_bytes"` - RxBytes FlexInt `json:"rx_bytes"` - Bytes FlexInt `json:"bytes"` - NumSta FlexInt `json:"num_sta"` - UserNumSta FlexInt `json:"user-num_sta"` - GuestNumSta FlexInt `json:"guest-num_sta"` - NumDesktop FlexInt `json:"num_desktop"` - NumMobile FlexInt `json:"num_mobile"` - NumHandheld FlexInt `json:"num_handheld"` + PortTable []*Port `json:"port_table"` + NetworkTable NetworkTable `json:"network_table"` + Uplink Uplink `json:"uplink"` + Stat USGStat `json:"stat"` + TxBytes FlexInt `json:"tx_bytes"` + RxBytes FlexInt `json:"rx_bytes"` + Bytes FlexInt `json:"bytes"` + NumSta FlexInt `json:"num_sta"` + UserNumSta FlexInt `json:"user-num_sta"` + GuestNumSta FlexInt `json:"guest-num_sta"` + NumDesktop FlexInt `json:"num_desktop"` + NumMobile FlexInt `json:"num_mobile"` + NumHandheld FlexInt `json:"num_handheld"` } // Uplink is the Internet connection (or uplink) on a UniFi device. @@ -175,17 +154,18 @@ type Wan struct { // SpeedtestStatus is the speed test info on a USG or UDM. type SpeedtestStatus struct { - Latency FlexInt `json:"latency"` - Rundate FlexInt `json:"rundate"` - Runtime FlexInt `json:"runtime"` - ServerDesc string `json:"server_desc,omitempty"` - Server *SpeedtestServer `json:"server"` - StatusDownload FlexInt `json:"status_download"` - StatusPing FlexInt `json:"status_ping"` - StatusSummary FlexInt `json:"status_summary"` - StatusUpload FlexInt `json:"status_upload"` - XputDownload FlexInt `json:"xput_download"` - XputUpload FlexInt `json:"xput_upload"` + Latency FlexInt `json:"latency"` + Rundate FlexInt `json:"rundate"` + Runtime FlexInt `json:"runtime"` + ServerDesc string `json:"server_desc,omitempty"` + Server *SpeedtestServer `json:"server"` + SourceInterface string `json:"source_interface"` + StatusDownload FlexInt `json:"status_download"` + StatusPing FlexInt `json:"status_ping"` + StatusSummary FlexInt `json:"status_summary"` + StatusUpload FlexInt `json:"status_upload"` + XputDownload FlexInt `json:"xput_download"` + XputUpload FlexInt `json:"xput_upload"` } type SpeedtestServer struct { diff --git a/core/unifi/usw.go b/core/unifi/usw.go index fcd75c9c..444577f2 100644 --- a/core/unifi/usw.go +++ b/core/unifi/usw.go @@ -7,80 +7,68 @@ import ( // USW represents all the data from the Ubiquiti Controller for a Unifi Switch. type USW struct { - SourceName string `json:"-"` - SiteName string `json:"-"` - ID string `json:"_id"` - Adopted FlexBool `json:"adopted"` - BoardRev FlexInt `json:"board_rev"` - Cfgversion string `json:"cfgversion"` - ConfigNetwork struct { - Type string `json:"type"` - IP string `json:"ip"` - } `json:"config_network"` - Dot1XPortctrlEnabled FlexBool `json:"dot1x_portctrl_enabled"` - EthernetTable []struct { - Mac string `json:"mac"` - NumPort FlexInt `json:"num_port,omitempty"` - Name string `json:"name"` - } `json:"ethernet_table"` - FlowctrlEnabled FlexBool `json:"flowctrl_enabled"` - FwCaps FlexInt `json:"fw_caps"` - HasFan FlexBool `json:"has_fan"` - HasTemperature FlexBool `json:"has_temperature"` - InformIP string `json:"inform_ip"` - InformURL string `json:"inform_url"` - IP string `json:"ip"` - JumboframeEnabled FlexBool `json:"jumboframe_enabled"` - LedOverride string `json:"led_override"` - LicenseState string `json:"license_state"` - Mac string `json:"mac"` - Model string `json:"model"` - Name string `json:"name"` - OutdoorModeOverride string `json:"outdoor_mode_override"` - PortOverrides []struct { + SourceName string `json:"-"` + SiteName string `json:"-"` + ID string `json:"_id"` + Adopted FlexBool `json:"adopted"` + BoardRev FlexInt `json:"board_rev"` + Cfgversion string `json:"cfgversion"` + ConfigNetwork *ConfigNetwork `json:"config_network"` + Dot1XPortctrlEnabled FlexBool `json:"dot1x_portctrl_enabled"` + EthernetTable []*EthernetTable `json:"ethernet_table"` + FlowctrlEnabled FlexBool `json:"flowctrl_enabled"` + FwCaps FlexInt `json:"fw_caps"` + HasFan FlexBool `json:"has_fan"` + HasTemperature FlexBool `json:"has_temperature"` + InformIP string `json:"inform_ip"` + InformURL string `json:"inform_url"` + IP string `json:"ip"` + JumboframeEnabled FlexBool `json:"jumboframe_enabled"` + LedOverride string `json:"led_override"` + LicenseState string `json:"license_state"` + Mac string `json:"mac"` + Model string `json:"model"` + Name string `json:"name"` + OutdoorModeOverride string `json:"outdoor_mode_override"` + PortOverrides []struct { Name string `json:"name,omitempty"` PoeMode string `json:"poe_mode,omitempty"` PortIdx FlexInt `json:"port_idx"` PortconfID string `json:"portconf_id"` } `json:"port_overrides"` - PortTable []Port `json:"port_table"` - Serial string `json:"serial"` - SiteID string `json:"site_id"` - StpPriority FlexInt `json:"stp_priority"` - StpVersion string `json:"stp_version"` - Type string `json:"type"` - Version string `json:"version"` - RequiredVersion string `json:"required_version"` - SwitchCaps *SwitchCaps `json:"switch_caps"` - HwCaps FlexInt `json:"hw_caps"` - Unsupported FlexBool `json:"unsupported"` - UnsupportedReason FlexInt `json:"unsupported_reason"` - SysErrorCaps FlexInt `json:"sys_error_caps"` - DeviceID string `json:"device_id"` - State FlexInt `json:"state"` - LastSeen FlexInt `json:"last_seen"` - Upgradable FlexBool `json:"upgradable,omitempty"` - AdoptableWhenUpgraded FlexBool `json:"adoptable_when_upgraded,omitempty"` - Rollupgrade FlexBool `json:"rollupgrade,omitempty"` - KnownCfgversion string `json:"known_cfgversion"` - Uptime FlexInt `json:"uptime"` - Locating FlexBool `json:"locating"` - ConnectRequestIP string `json:"connect_request_ip"` - ConnectRequestPort string `json:"connect_request_port"` - SysStats SysStats `json:"sys_stats"` - SystemStats SystemStats `json:"system-stats"` - FanLevel FlexInt `json:"fan_level"` - GeneralTemperature FlexInt `json:"general_temperature"` - Overheating FlexBool `json:"overheating"` - TotalMaxPower FlexInt `json:"total_max_power"` - DownlinkTable []struct { - PortIdx FlexInt `json:"port_idx"` - Speed FlexInt `json:"speed"` - FullDuplex FlexBool `json:"full_duplex"` - Mac string `json:"mac"` - } `json:"downlink_table"` - Uplink Uplink `json:"uplink"` - LastUplink struct { + PortTable []Port `json:"port_table"` + Serial string `json:"serial"` + SiteID string `json:"site_id"` + StpPriority FlexInt `json:"stp_priority"` + StpVersion string `json:"stp_version"` + Type string `json:"type"` + Version string `json:"version"` + RequiredVersion string `json:"required_version"` + SwitchCaps *SwitchCaps `json:"switch_caps"` + HwCaps FlexInt `json:"hw_caps"` + Unsupported FlexBool `json:"unsupported"` + UnsupportedReason FlexInt `json:"unsupported_reason"` + SysErrorCaps FlexInt `json:"sys_error_caps"` + DeviceID string `json:"device_id"` + State FlexInt `json:"state"` + LastSeen FlexInt `json:"last_seen"` + Upgradable FlexBool `json:"upgradable,omitempty"` + AdoptableWhenUpgraded FlexBool `json:"adoptable_when_upgraded,omitempty"` + Rollupgrade FlexBool `json:"rollupgrade,omitempty"` + KnownCfgversion string `json:"known_cfgversion"` + Uptime FlexInt `json:"uptime"` + Locating FlexBool `json:"locating"` + ConnectRequestIP string `json:"connect_request_ip"` + ConnectRequestPort string `json:"connect_request_port"` + SysStats SysStats `json:"sys_stats"` + SystemStats SystemStats `json:"system-stats"` + FanLevel FlexInt `json:"fan_level"` + GeneralTemperature FlexInt `json:"general_temperature"` + Overheating FlexBool `json:"overheating"` + TotalMaxPower FlexInt `json:"total_max_power"` + DownlinkTable []*DownlinkTable `json:"downlink_table"` + Uplink Uplink `json:"uplink"` + LastUplink struct { UplinkMac string `json:"uplink_mac"` } `json:"last_uplink"` UplinkDepth FlexInt `json:"uplink_depth"` @@ -109,7 +97,8 @@ type MacTable struct { Mac string `json:"mac"` } -// Port is a physical connection on a USW or UDM. +// Port is a physical connection on a USW or Gateway. +// Not every port has the same capabilities. type Port struct { AggregatedBy FlexBool `json:"aggregated_by"` Autoneg FlexBool `json:"autoneg,omitempty"` diff --git a/core/unifi/uxg.go b/core/unifi/uxg.go index 41604087..41ee2046 100644 --- a/core/unifi/uxg.go +++ b/core/unifi/uxg.go @@ -9,15 +9,15 @@ type UXG struct { IP string `json:"ip"` Mac string `json:"mac"` Model string `json:"model"` - ModelInLts bool `json:"model_in_lts"` - ModelInEol bool `json:"model_in_eol"` + ModelInLts FlexBool `json:"model_in_lts"` + ModelInEol FlexBool `json:"model_in_eol"` Type string `json:"type"` Version string `json:"version"` - Adopted bool `json:"adopted"` + Adopted FlexBool `json:"adopted"` SiteID string `json:"site_id"` Cfgversion string `json:"cfgversion"` SyslogKey string `json:"syslog_key"` - ConfigNetwork ConfigNetwork `json:"config_network"` + ConfigNetwork *ConfigNetwork `json:"config_network"` SetupID string `json:"setup_id"` LicenseState string `json:"license_state"` ConfigNetworkLan *ConfigNetworkLan `json:"config_network_lan"` @@ -28,22 +28,22 @@ type UXG struct { Architecture string `json:"architecture"` BoardRev FlexInt `json:"board_rev"` ManufacturerID FlexInt `json:"manufacturer_id"` - Internet bool `json:"internet"` - ModelIncompatible bool `json:"model_incompatible"` + Internet FlexBool `json:"internet"` + ModelIncompatible FlexBool `json:"model_incompatible"` EthernetTable []*EthernetTable `json:"ethernet_table"` PortTable []*Port `json:"port_table"` EthernetOverrides []*EthernetOverrides `json:"ethernet_overrides"` UsgCaps FlexInt `json:"usg_caps"` - HasSpeaker bool `json:"has_speaker"` - HasEth1 bool `json:"has_eth1"` + HasSpeaker FlexBool `json:"has_speaker"` + HasEth1 FlexBool `json:"has_eth1"` FwCaps FlexInt `json:"fw_caps"` HwCaps FlexInt `json:"hw_caps"` WifiCaps FlexInt `json:"wifi_caps"` SwitchCaps *SwitchCaps `json:"switch_caps"` - HasFan bool `json:"has_fan"` - HasTemperature bool `json:"has_temperature"` + HasFan FlexBool `json:"has_fan"` + HasTemperature FlexBool `json:"has_temperature"` Temperatures []*Temperature `json:"temperatures"` - Storage []Storage `json:"storage"` + Storage []*Storage `json:"storage"` RulesetInterfaces interface{} `json:"ruleset_interfaces"` ConnectedAt FlexInt `json:"connected_at"` ProvisionedAt FlexInt `json:"provisioned_at"` @@ -51,36 +51,36 @@ type UXG struct { LedOverrideColor string `json:"led_override_color"` LedOverrideColorBrightness FlexInt `json:"led_override_color_brightness"` OutdoorModeOverride string `json:"outdoor_mode_override"` - LcmBrightnessOverride bool `json:"lcm_brightness_override"` - LcmIdleTimeoutOverride bool `json:"lcm_idle_timeout_override"` + LcmBrightnessOverride FlexBool `json:"lcm_brightness_override"` + LcmIdleTimeoutOverride FlexBool `json:"lcm_idle_timeout_override"` Name string `json:"name"` - Unsupported bool `json:"unsupported"` + Unsupported FlexBool `json:"unsupported"` UnsupportedReason FlexInt `json:"unsupported_reason"` Serial string `json:"serial"` HashID string `json:"hash_id"` - TwoPhaseAdopt bool `json:"two_phase_adopt"` + TwoPhaseAdopt FlexBool `json:"two_phase_adopt"` DeviceID string `json:"device_id"` State FlexInt `json:"state"` - StartDisconnectedMillis int64 `json:"start_disconnected_millis"` + StartDisconnectedMillis FlexInt `json:"start_disconnected_millis"` UpgradeState FlexInt `json:"upgrade_state"` - StartConnectedMillis int64 `json:"start_connected_millis"` + StartConnectedMillis FlexInt `json:"start_connected_millis"` LastSeen FlexInt `json:"last_seen"` Uptime FlexInt `json:"uptime"` UnderscoreUptime FlexInt `json:"_uptime"` - Locating bool `json:"locating"` + Locating FlexBool `json:"locating"` SysStats *SysStats `json:"sys_stats"` SystemStats *SystemStats `json:"system-stats"` GuestKicks FlexInt `json:"guest_kicks"` GuestToken string `json:"guest_token"` UptimeStats map[string]*UptimeStats `json:"uptime_stats"` - Overheating bool `json:"overheating"` + Overheating FlexBool `json:"overheating"` GeoInfo map[string]*GeoInfo `json:"geo_info"` LedState *LedState `json:"led_state"` SpeedtestStatus *SpeedtestStatus `json:"speedtest-status"` - SpeedtestStatusSaved bool `json:"speedtest-status-saved"` + SpeedtestStatusSaved FlexBool `json:"speedtest-status-saved"` Wan1 *Wan `json:"wan1"` Wan2 *Wan `json:"wan2"` - Uplink Uplink `json:"uplink"` + Uplink *Uplink `json:"uplink"` DownlinkTable []*DownlinkTable `json:"downlink_table"` NetworkTable []*NetworkTable `json:"network_table"` KnownCfgversion string `json:"known_cfgversion"` @@ -91,8 +91,8 @@ type UXG struct { ConsideredLostAt FlexInt `json:"considered_lost_at"` Stat *UXGStat `json:"stat"` TxBytes FlexInt `json:"tx_bytes"` - RxBytes int64 `json:"rx_bytes"` - Bytes int64 `json:"bytes"` + RxBytes FlexInt `json:"rx_bytes"` + Bytes FlexInt `json:"bytes"` NumSta FlexInt `json:"num_sta"` WlanNumSta FlexInt `json:"wlan-num_sta"` LanNumSta FlexInt `json:"lan-num_sta"` @@ -121,11 +121,13 @@ type DownlinkTable struct { Mac string `json:"mac"` } +// LedState is incuded with newer devices. type LedState struct { Pattern string `json:"pattern"` Tempo FlexInt `json:"tempo"` } +// GeoInfo is incuded with certain devices. type GeoInfo struct { Accuracy FlexInt `json:"accuracy"` Address string `json:"address"` @@ -141,14 +143,14 @@ type GeoInfo struct { Timezone string `json:"timezone"` } +// UptimeStats is incuded with certain devices. type UptimeStats struct { Availability FlexInt `json:"availability"` LatencyAverage FlexInt `json:"latency_average"` TimePeriod FlexInt `json:"time_period"` } -// UDMStat holds the "stat" data for a dream machine. -// A dream machine is a USG + USW + Controller. +// UXGStat holds the "stat" data for a 10Gb gateway. type UXGStat struct { *Gw `json:"gw"` *Sw `json:"sw"`