forked from paultyng/go-unifi
/
setting.go
120 lines (110 loc) · 2.63 KB
/
setting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package unifi
import (
"context"
"encoding/json"
"fmt"
)
type Setting struct {
ID string `json:"_id,omitempty"`
SiteID string `json:"site_id,omitempty"`
Key string `json:"key"`
}
func (s *Setting) newFields() (interface{}, error) {
switch s.Key {
case "auto_speedtest":
return &SettingAutoSpeedtest{}, nil
case "baresip":
return &SettingBaresip{}, nil
case "broadcast":
return &SettingBroadcast{}, nil
case "connectivity":
return &SettingConnectivity{}, nil
case "country":
return &SettingCountry{}, nil
case "dpi":
return &SettingDpi{}, nil
case "element_adopt":
return &SettingElementAdopt{}, nil
case "guest_access":
return &SettingGuestAccess{}, nil
// case "ips":
// return &SettingI
case "lcm":
return &SettingLcm{}, nil
case "locale":
return &SettingLocale{}, nil
case "mgmt":
return &SettingMgmt{}, nil
case "network_optimization":
return &SettingNetworkOptimization{}, nil
case "ntp":
return &SettingNtp{}, nil
case "porta":
return &SettingPorta{}, nil
case "provider_capabilities":
return &SettingProviderCapabilities{}, nil
case "radio_ai":
return &SettingRadioAi{}, nil
case "radius":
return &SettingRadius{}, nil
case "rsyslogd":
return &SettingRsyslogd{}, nil
case "snmp":
return &SettingSnmp{}, nil
case "super_cloudaccess":
return &SettingSuperCloudaccess{}, nil
case "super_events":
return &SettingSuperEvents{}, nil
case "super_fwupdate":
return &SettingSuperFwupdate{}, nil
case "super_identity":
return &SettingSuperIdentity{}, nil
case "super_mail":
return &SettingSuperMail{}, nil
case "super_mgmt":
return &SettingSuperMgmt{}, nil
case "super_sdn":
return &SettingSuperSdn{}, nil
case "super_smtp":
return &SettingSuperSmtp{}, nil
case "usg":
return &SettingUsg{}, nil
case "usw":
return &SettingUsw{}, nil
}
return nil, fmt.Errorf("unexpected key %q", s.Key)
}
func (c *Client) GetSetting(ctx context.Context, site, key string) (*Setting, interface{}, error) {
var respBody struct {
Meta meta `json:"meta"`
Data []json.RawMessage `json:"data"`
}
err := c.do(ctx, "GET", fmt.Sprintf("s/%s/get/setting", site), nil, &respBody)
if err != nil {
return nil, nil, err
}
var raw json.RawMessage
var setting *Setting
for _, d := range respBody.Data {
err = json.Unmarshal(d, &setting)
if err != nil {
return nil, nil, err
}
if setting.Key == key {
raw = d
break
}
}
if setting == nil {
return nil, nil, &NotFoundError{}
}
fields, err := setting.newFields()
if err != nil {
return nil, nil, err
}
err = json.Unmarshal(raw, &fields)
if err != nil {
return nil, nil, err
}
return setting, fields, nil
}