From 3e7bd072ea3ced40a3e6c00762a7a94a59892b83 Mon Sep 17 00:00:00 2001 From: Oskar Date: Mon, 31 Jan 2022 05:58:42 +0100 Subject: [PATCH] Omit empty NetworkID in account resource (#59) * Omit empty NetworkID in account * Run `go generate` * Fix tests Co-authored-by: Joshua Spence --- fields/main.go | 3 +- unifi/account.generated.go | 2 +- unifi/account_test.go | 4 +- unifi/device.generated.go | 8 +-- unifi/network.generated.go | 1 + unifi/setting_teleport.generated.go | 87 +++++++++++++++++++++++++++++ unifi/version.generated.go | 2 +- unifi/wlan.generated.go | 8 ++- 8 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 unifi/setting_teleport.generated.go diff --git a/fields/main.go b/fields/main.go index cb0b7c1..86001e3 100644 --- a/fields/main.go +++ b/fields/main.go @@ -304,7 +304,8 @@ func main() { switch resource.StructName { case "Account": resource.FieldProcessor = func(name string, f *FieldInfo) error { - if name == "IP" { + switch name { + case "IP", "NetworkID": f.OmitEmpty = true } return nil diff --git a/unifi/account.generated.go b/unifi/account.generated.go index 8fee6a0..24051a7 100644 --- a/unifi/account.generated.go +++ b/unifi/account.generated.go @@ -27,7 +27,7 @@ type Account struct { IP string `json:"ip,omitempty"` // ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^$ Name string `json:"name,omitempty"` // ^[^"' ]+$ - NetworkID string `json:"networkconf_id"` + NetworkID string `json:"networkconf_id,omitempty"` TunnelConfigType string `json:"tunnel_config_type,omitempty"` // vpn|802.1x|custom TunnelMediumType int `json:"tunnel_medium_type,omitempty"` // [1-9]|1[0-5]|^$ TunnelType int `json:"tunnel_type,omitempty"` // [1-9]|1[0-3]|^$ diff --git a/unifi/account_test.go b/unifi/account_test.go index 225d7cc..6c1b4d6 100644 --- a/unifi/account_test.go +++ b/unifi/account_test.go @@ -14,11 +14,11 @@ func TestAccountMarshalJSON(t *testing.T) { acc unifi.Account }{ "empty strings": { - `{"vlan":"","tunnel_type":"","tunnel_medium_type":"","networkconf_id":""}`, + `{"vlan":"","tunnel_type":"","tunnel_medium_type":""}`, unifi.Account{}, }, "response": { - `{"vlan":10,"tunnel_type":1,"tunnel_medium_type":1,"networkconf_id":""}`, + `{"vlan":10,"tunnel_type":1,"tunnel_medium_type":1}`, unifi.Account{ VLAN: 10, TunnelType: 1, diff --git a/unifi/device.generated.go b/unifi/device.generated.go index aed4320..4386b57 100644 --- a/unifi/device.generated.go +++ b/unifi/device.generated.go @@ -204,7 +204,7 @@ func (dst *DeviceOutletOverrides) UnmarshalJSON(b []byte) error { } type DevicePortOverrides struct { - AggregateNumPorts int `json:"aggregate_num_ports,omitempty"` // [2-6] + AggregateNumPorts int `json:"aggregate_num_ports,omitempty"` // [2-8] Autoneg bool `json:"autoneg,omitempty"` Dot1XCtrl string `json:"dot1x_ctrl,omitempty"` // auto|force_authorized|force_unauthorized|mac_based|multi_host Dot1XIDleTimeout int `json:"dot1x_idle_timeout,omitempty"` // [0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5] @@ -292,8 +292,8 @@ func (dst *DevicePortOverrides) UnmarshalJSON(b []byte) error { type DeviceRadioTable struct { AntennaGain int `json:"antenna_gain,omitempty"` // ^-?([0-9]|[1-9][0-9]) AntennaID int `json:"antenna_id,omitempty"` // -1|[0-9] - BackupChannel string `json:"backup_channel,omitempty"` // [0-9]|[1][0-4]|16|34|36|38|40|42|44|46|48|52|56|60|64|100|104|108|112|116|120|124|128|132|136|140|144|149|153|157|161|165|183|184|185|187|188|189|192|196|auto - Channel string `json:"channel,omitempty"` // [0-9]|[1][0-4]|4.5|16|34|36|38|40|42|44|46|48|52|56|60|64|100|104|108|112|116|120|124|128|132|136|140|144|149|153|157|161|165|183|184|185|187|188|189|192|196|auto + BackupChannel string `json:"backup_channel,omitempty"` // [0-9]|[1][0-4]|4.5|5|16|21|34|36|37|38|40|42|44|46|48|52|53|56|60|64|69|85|100|101|104|108|112|117|116|120|124|128|132|133|136|140|144|149|153|157|161|165|181|183|184|185|187|188|189|192|196|197|213|229|auto + Channel string `json:"channel,omitempty"` // [0-9]|[1][0-4]|4.5|5|16|21|34|36|37|38|40|42|44|46|48|52|53|56|60|64|69|85|100|101|104|108|112|117|116|120|124|128|132|133|136|140|144|149|153|157|161|165|181|183|184|185|187|188|189|192|196|197|213|229|auto HardNoiseFloorEnabled bool `json:"hard_noise_floor_enabled,omitempty"` Ht int `json:"ht,omitempty"` // 20|40|80|160|1080|2160 LoadbalanceEnabled bool `json:"loadbalance_enabled,omitempty"` @@ -301,7 +301,7 @@ type DeviceRadioTable struct { MinRssi int `json:"min_rssi,omitempty"` // ^-(6[7-9]|[7-8][0-9]|90)$ MinRssiEnabled bool `json:"min_rssi_enabled,omitempty"` Name string `json:"name,omitempty"` - Radio string `json:"radio,omitempty"` // ng|na|ad + Radio string `json:"radio,omitempty"` // ng|na|ad|6e SensLevel int `json:"sens_level,omitempty"` // ^-([5-8][0-9]|90)$ SensLevelEnabled bool `json:"sens_level_enabled,omitempty"` TxPower string `json:"tx_power,omitempty"` // [\d]+|auto diff --git a/unifi/network.generated.go b/unifi/network.generated.go index 14b321e..815f4fa 100644 --- a/unifi/network.generated.go +++ b/unifi/network.generated.go @@ -137,6 +137,7 @@ type Network struct { ReportWANEvent bool `json:"report_wan_event"` RequireMschapv2 bool `json:"require_mschapv2"` RouteDistance int `json:"route_distance,omitempty"` // ^[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]$|^$ + TeleportEnabled bool `json:"teleport_enabled"` UpnpLanEnabled bool `json:"upnp_lan_enabled"` UserGroupID string `json:"usergroup_id"` VLAN int `json:"vlan,omitempty"` // [2-9]|[1-9][0-9]{1,2}|[1-3][0-9]{3}|400[0-9]|^$ diff --git a/unifi/setting_teleport.generated.go b/unifi/setting_teleport.generated.go new file mode 100644 index 0000000..70555eb --- /dev/null +++ b/unifi/setting_teleport.generated.go @@ -0,0 +1,87 @@ +// Code generated from ace.jar fields *.json files +// DO NOT EDIT. + +package unifi + +import ( + "context" + "encoding/json" + "fmt" +) + +// just to fix compile issues with the import +var ( + _ context.Context + _ fmt.Formatter + _ json.Marshaler +) + +type SettingTeleport struct { + ID string `json:"_id,omitempty"` + SiteID string `json:"site_id,omitempty"` + + Hidden bool `json:"attr_hidden,omitempty"` + HiddenID string `json:"attr_hidden_id,omitempty"` + NoDelete bool `json:"attr_no_delete,omitempty"` + NoEdit bool `json:"attr_no_edit,omitempty"` + + Key string `json:"key"` + + SubnetCidr string `json:"subnet_cidr"` // ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\/([8-9]|[1-2][0-9]|3[0-2])$|^$ +} + +func (dst *SettingTeleport) UnmarshalJSON(b []byte) error { + type Alias SettingTeleport + aux := &struct { + *Alias + }{ + Alias: (*Alias)(dst), + } + + err := json.Unmarshal(b, &aux) + if err != nil { + return fmt.Errorf("unable to unmarshal alias: %w", err) + } + + return nil +} + +func (c *Client) getSettingTeleport(ctx context.Context, site string) (*SettingTeleport, error) { + var respBody struct { + Meta meta `json:"meta"` + Data []SettingTeleport `json:"data"` + } + + err := c.do(ctx, "GET", fmt.Sprintf("s/%s/get/setting/teleport", site), nil, &respBody) + if err != nil { + return nil, err + } + + if len(respBody.Data) != 1 { + return nil, &NotFoundError{} + } + + d := respBody.Data[0] + return &d, nil +} + +func (c *Client) updateSettingTeleport(ctx context.Context, site string, d *SettingTeleport) (*SettingTeleport, error) { + var respBody struct { + Meta meta `json:"meta"` + Data []SettingTeleport `json:"data"` + } + + d.Key = "teleport" + err := c.do(ctx, "PUT", fmt.Sprintf("s/%s/set/setting/teleport", site), d, &respBody) + if err != nil { + return nil, err + } + + if len(respBody.Data) != 1 { + return nil, &NotFoundError{} + } + + new := respBody.Data[0] + + return &new, nil +} diff --git a/unifi/version.generated.go b/unifi/version.generated.go index 117a8c9..e0a0d43 100644 --- a/unifi/version.generated.go +++ b/unifi/version.generated.go @@ -2,4 +2,4 @@ package unifi -const UnifiVersion = "6.4.54" +const UnifiVersion = "6.5.55" diff --git a/unifi/wlan.generated.go b/unifi/wlan.generated.go index 95dc8db..1579724 100644 --- a/unifi/wlan.generated.go +++ b/unifi/wlan.generated.go @@ -34,6 +34,7 @@ type WLAN struct { CountryBeacon bool `json:"country_beacon"` DPIEnabled bool `json:"dpi_enabled"` DPIgroupID string `json:"dpigroup_id"` // [\d\w]+|^$ + DTIM6E int `json:"dtim_6e,omitempty"` // ^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^$ DTIMMode string `json:"dtim_mode,omitempty"` // default|custom DTIMNa int `json:"dtim_na,omitempty"` // ^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^$ DTIMNg int `json:"dtim_ng,omitempty"` // ^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^$ @@ -93,8 +94,9 @@ type WLAN struct { UserGroupID string `json:"usergroup_id"` VLAN int `json:"vlan,omitempty"` // [2-9]|[1-9][0-9]{1,2}|[1-3][0-9]{3}|40[0-8][0-9]|409[0-5]|^$ VLANEnabled bool `json:"vlan_enabled"` - WEPIDX int `json:"wep_idx,omitempty"` // [1-4] - WLANBand string `json:"wlan_band,omitempty"` // 2g|5g|both + WEPIDX int `json:"wep_idx,omitempty"` // [1-4] + WLANBand string `json:"wlan_band,omitempty"` // 2g|5g|both + WLANBands []string `json:"wlan_bands,omitempty"` // 2g|5g|6g WLANGroupID string `json:"wlangroup_id"` WPA3Enhanced192 bool `json:"wpa3_enhanced_192"` WPA3FastRoaming bool `json:"wpa3_fast_roaming"` @@ -111,6 +113,7 @@ type WLAN struct { func (dst *WLAN) UnmarshalJSON(b []byte) error { type Alias WLAN aux := &struct { + DTIM6E emptyStringInt `json:"dtim_6e"` DTIMNa emptyStringInt `json:"dtim_na"` DTIMNg emptyStringInt `json:"dtim_ng"` GroupRekey emptyStringInt `json:"group_rekey"` @@ -132,6 +135,7 @@ func (dst *WLAN) UnmarshalJSON(b []byte) error { if err != nil { return fmt.Errorf("unable to unmarshal alias: %w", err) } + dst.DTIM6E = int(aux.DTIM6E) dst.DTIMNa = int(aux.DTIMNa) dst.DTIMNg = int(aux.DTIMNg) dst.GroupRekey = int(aux.GroupRekey)