-
Notifications
You must be signed in to change notification settings - Fork 3
/
vpnconfigupdate.go
63 lines (53 loc) · 1.19 KB
/
vpnconfigupdate.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
package daemon
import (
"encoding/json"
"github.com/telekom-mms/oc-daemon/pkg/vpnconfig"
)
// VPNConfigUpdate is a VPN configuration update.
type VPNConfigUpdate struct {
Reason string
Token string
Config *vpnconfig.Config
}
// Valid returns whether the config update is valid.
func (c *VPNConfigUpdate) Valid() bool {
switch c.Reason {
case "disconnect":
// token must be valid and config nil
if c.Token == "" || c.Config != nil {
return false
}
case "connect":
// token and config must be valid
if c.Token == "" || c.Config == nil {
return false
}
if !c.Config.Valid() {
return false
}
default:
return false
}
return true
}
// JSON returns the Config as JSON.
func (c *VPNConfigUpdate) JSON() ([]byte, error) {
b, err := json.Marshal(c)
if err != nil {
return nil, err
}
return b, nil
}
// VPNConfigUpdateFromJSON parses and returns the VPNConfigUpdate in b.
func VPNConfigUpdateFromJSON(b []byte) (*VPNConfigUpdate, error) {
c := NewVPNConfigUpdate()
err := json.Unmarshal(b, c)
if err != nil {
return nil, err
}
return c, nil
}
// NewVPNConfigUpdate returns a new VPNConfigUpdate.
func NewVPNConfigUpdate() *VPNConfigUpdate {
return &VPNConfigUpdate{}
}