commit
						9674b1a85d
					
				|  | @ -1 +1,2 @@ | ||||||
| /vendor | /vendor | ||||||
|  | *.swp | ||||||
|  |  | ||||||
|  | @ -0,0 +1,62 @@ | ||||||
|  | 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, err := u.parseNetwork(data, site.SiteName) | ||||||
|  | 			if err != nil { | ||||||
|  | 				return networks, err | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			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, error) { | ||||||
|  | 	network := new(Network) | ||||||
|  | 	return network, u.unmarshalDevice(data, network) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // 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"` | ||||||
|  | } | ||||||
|  | @ -29,6 +29,8 @@ const ( | ||||||
| 	APIClientDPI string = "/api/s/%s/stat/stadpi" | 	APIClientDPI string = "/api/s/%s/stat/stadpi" | ||||||
| 	// APIClientPath is Unifi Clients API Path
 | 	// APIClientPath is Unifi Clients API Path
 | ||||||
| 	APIClientPath string = "/api/s/%s/stat/sta" | 	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 is where we get data about Unifi devices.
 | ||||||
| 	APIDevicePath string = "/api/s/%s/stat/device" | 	APIDevicePath string = "/api/s/%s/stat/device" | ||||||
| 	// APILoginPath is Unifi Controller Login API Path
 | 	// APILoginPath is Unifi Controller Login API Path
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue