From b7f8e3a98e71384fddddce7cd0efab5b25ce113b Mon Sep 17 00:00:00 2001 From: Andrew Regner Date: Tue, 8 Sep 2020 00:16:26 -0700 Subject: [PATCH 1/4] add networks --- core/unifi/.gitignore | 1 + core/unifi/networks.go | 74 ++++++++++++++++++++++++++++++++++++++++++ core/unifi/types.go | 2 ++ 3 files changed, 77 insertions(+) create mode 100644 core/unifi/networks.go diff --git a/core/unifi/.gitignore b/core/unifi/.gitignore index 61ead866..6d292b19 100644 --- a/core/unifi/.gitignore +++ b/core/unifi/.gitignore @@ -1 +1,2 @@ /vendor +*.swp diff --git a/core/unifi/networks.go b/core/unifi/networks.go new file mode 100644 index 00000000..23237c19 --- /dev/null +++ b/core/unifi/networks.go @@ -0,0 +1,74 @@ +package unifi + +import ( + "encoding/json" + "fmt" +) + +// GetNetworks returns a response full of network data from the UniFi Controller. +func (u *Unifi) GetNetworks(sites []*Site) ([]Network, error) { + networks := make([]Network, 0) + + for _, site := range sites { + var response struct { + Data []json.RawMessage `json:"data"` + } + + networkPath := fmt.Sprintf(APINetworkPath, site.Name) + if err := u.GetData(networkPath, &response); err != nil { + return nil, err + } + + for _, data := range response.Data { + network := u.parseNetwork(data, site.SiteName) + networks = append(networks, *network) + } + } + + return networks, nil +} + +// parseNetwork parses the raw JSON from the Unifi Controller into network structures. +func (u *Unifi) parseNetwork(data json.RawMessage, siteName string) *Network { + network := new(Network) + u.unmarshalNetwork(data, &network) + return network +} + +// unmarshalNetwork handles logging for the unmarshal operations in parseNetwork(). +func (u *Unifi) unmarshalNetwork(data json.RawMessage, v interface{}) (err error) { + if err = json.Unmarshal(data, v); err != nil { + u.ErrorLog("json.Unmarshal(): %v", err) + u.ErrorLog("Enable Debug Logging to output the failed payload.") + + json, err := data.MarshalJSON() + u.DebugLog("Failed Payload: %s (marshal err: %v)", json, err) + u.DebugLog("The above payload can prove useful during torubleshooting when you open an Issue:") + u.DebugLog("==- https://github.com/unifi-poller/unifi/issues/new -==") + } + + return err +} + +// Network is metadata about a network managed by a UniFi controller +type Network struct { + DhcpdDNSEnabled FlexBool `json:"dhcpd_dns_enabled"` + DhcpdEnabled FlexBool `json:"dhcpd_enabled"` + DhcpdGatewayEnabled FlexBool `json:"dhcpd_gateway_enabled"` + DhcpdIP1 string `json:"dhcpd_ip_1"` + DhcpdLeasetime FlexInt `json:"dhcpd_leasetime"` + DhcpRelayEnabled FlexBool `json:"dhcp_relay_enabled"` + DhcpdTimeOffsetEnabled FlexBool `json:"dhcpd_time_offset_enabled"` + DhcpGuardEnabled FlexBool `json:"dhcpguard_enabled"` + DomainName string `json:"domain_name"` + Enabled FlexBool `json:"enabled"` + ID string `json:"_id"` + IPSubnet string `json:"ip_subnet"` + IsNat FlexBool `json:"is_nat"` + Name string `json:"name"` + Networkgroup string `json:"networkgroup"` + Purpose string `json:"purpose"` + SiteID string `json:"site_id"` + Vlan FlexInt `json:"vlan"` + VlanEnabled FlexBool `json:"vlan_enabled"` +} diff --git a/core/unifi/types.go b/core/unifi/types.go index 77c46b15..626ce333 100644 --- a/core/unifi/types.go +++ b/core/unifi/types.go @@ -29,6 +29,8 @@ const ( APIClientDPI string = "/api/s/%s/stat/stadpi" // APIClientPath is Unifi Clients API Path APIClientPath string = "/api/s/%s/stat/sta" + // APINetworkPath is where we get data about Unifi networks. + APINetworkPath string = "/api/s/%s/rest/networkconf" // APIDevicePath is where we get data about Unifi devices. APIDevicePath string = "/api/s/%s/stat/device" // APILoginPath is Unifi Controller Login API Path From 422c3f220fb936ad8c2d7c73e7b953993a084b92 Mon Sep 17 00:00:00 2001 From: Andrew Regner Date: Fri, 11 Sep 2020 14:52:25 -0700 Subject: [PATCH 2/4] Update networks.go Co-authored-by: David Newhall II --- core/unifi/networks.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/core/unifi/networks.go b/core/unifi/networks.go index 23237c19..ac392eb3 100644 --- a/core/unifi/networks.go +++ b/core/unifi/networks.go @@ -35,21 +35,6 @@ func (u *Unifi) parseNetwork(data json.RawMessage, siteName string) *Network { return network } -// unmarshalNetwork handles logging for the unmarshal operations in parseNetwork(). -func (u *Unifi) unmarshalNetwork(data json.RawMessage, v interface{}) (err error) { - if err = json.Unmarshal(data, v); err != nil { - u.ErrorLog("json.Unmarshal(): %v", err) - u.ErrorLog("Enable Debug Logging to output the failed payload.") - - json, err := data.MarshalJSON() - u.DebugLog("Failed Payload: %s (marshal err: %v)", json, err) - u.DebugLog("The above payload can prove useful during torubleshooting when you open an Issue:") - u.DebugLog("==- https://github.com/unifi-poller/unifi/issues/new -==") - } - - return err -} - // Network is metadata about a network managed by a UniFi controller type Network struct { DhcpdDNSEnabled FlexBool `json:"dhcpd_dns_enabled"` From 95f017cae6d7ce7a72a99a76a7ee31983d46ac27 Mon Sep 17 00:00:00 2001 From: Andrew Regner Date: Fri, 11 Sep 2020 14:52:40 -0700 Subject: [PATCH 3/4] Update networks.go Co-authored-by: David Newhall II --- core/unifi/networks.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/unifi/networks.go b/core/unifi/networks.go index ac392eb3..3a8b7560 100644 --- a/core/unifi/networks.go +++ b/core/unifi/networks.go @@ -29,10 +29,9 @@ func (u *Unifi) GetNetworks(sites []*Site) ([]Network, error) { } // parseNetwork parses the raw JSON from the Unifi Controller into network structures. -func (u *Unifi) parseNetwork(data json.RawMessage, siteName string) *Network { +func (u *Unifi) parseNetwork(data json.RawMessage, siteName string) (*Network, error) { network := new(Network) - u.unmarshalNetwork(data, &network) - return network + return network, u.unmarshalDevice(data, network) } // Network is metadata about a network managed by a UniFi controller From 23dc92dbfa446ac6f72219cbac11bbfc024a696f Mon Sep 17 00:00:00 2001 From: Andrew Regner Date: Fri, 11 Sep 2020 14:52:46 -0700 Subject: [PATCH 4/4] Update networks.go Co-authored-by: David Newhall II --- core/unifi/networks.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/unifi/networks.go b/core/unifi/networks.go index 3a8b7560..ec64efcd 100644 --- a/core/unifi/networks.go +++ b/core/unifi/networks.go @@ -20,7 +20,11 @@ func (u *Unifi) GetNetworks(sites []*Site) ([]Network, error) { } for _, data := range response.Data { - network := u.parseNetwork(data, site.SiteName) + network, err := u.parseNetwork(data, site.SiteName) + if err != nil { + return networks, err + } + networks = append(networks, *network) } }