-
Notifications
You must be signed in to change notification settings - Fork 48
/
vpn_user.go
113 lines (96 loc) · 2.86 KB
/
vpn_user.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
package goaviatrix
import (
"errors"
"fmt"
"strings"
)
// VPNUser simple struct to hold vpn_user details
type VPNUser struct {
Action string `form:"action,omitempty" json:"action,omitempty"`
CID string `form:"CID,omitempty" json:"CID,omitempty"`
SamlEndpoint string `form:"saml_endpoint,omitempty" json:"saml_endpoint,omitempty"`
VpcID string `form:"vpc_id,omitempty" json:"vpc_id,omitempty"`
GwName string `form:"lb_name,omitempty" json:"lb_name,omitempty"`
DnsName string `json:"dns,omitempty"`
DnsEnabled bool `json:"dns_enabled,omitempty"`
UserName string `form:"username" json:"_id,omitempty"`
UserEmail string `form:"user_email,omitempty" json:"email,omitempty"`
Profiles []string `json:"profiles,omitempty"`
}
type VPNUserResp struct {
Return bool `json:"return"`
Results VPNUserInfo `json:"results"`
Reason string `json:"reason"`
}
type VPNUserInfo struct {
VpnUser VPNUser `json:"vpn_user"`
}
func (c *Client) CreateVPNUser(vpnUser *VPNUser) error {
form := map[string]string{
"CID": c.CID,
"action": "add_vpn_user",
"username": vpnUser.UserName,
"user_email": vpnUser.UserEmail,
"saml_endpoint": vpnUser.SamlEndpoint,
}
if vpnUser.DnsEnabled {
form["dns"] = "true"
form["lb_name"] = vpnUser.DnsName
} else {
form["vpc_id"] = vpnUser.VpcID
form["lb_name"] = vpnUser.GwName
}
checkFunc := func(act, method, reason string, ret bool) error {
if !ret {
if strings.Contains(reason, "Sending VPN certificates to email") {
return nil
}
return fmt.Errorf("rest API %s %s failed: %s", act, method, reason)
}
return nil
}
return c.PostAPI(form["action"], form, checkFunc)
}
func (c *Client) GetVPNUser(vpnUser *VPNUser) (*VPNUser, error) {
form := map[string]string{
"CID": c.CID,
"action": "get_vpn_user_by_name",
"username": vpnUser.UserName,
}
var data VPNUserResp
checkFunc := func(act, method, reason string, ret bool) error {
if !ret {
if strings.Contains(reason, "Invalid VPN username") {
return ErrNotFound
}
return fmt.Errorf("rest API %s %s failed: %s", act, method, reason)
}
return nil
}
err := c.GetAPI(&data, form["action"], form, checkFunc)
if err != nil {
return nil, err
}
if data.Results.VpnUser.UserName != "" {
if data.Results.VpnUser.UserName == vpnUser.UserName {
return &data.Results.VpnUser, nil
} else {
return nil, errors.New("VPN user name does not match from response")
}
}
return nil, ErrNotFound
}
func (c *Client) DeleteVPNUser(vpnUser *VPNUser) error {
form := map[string]string{
"CID": c.CID,
"action": "delete_vpn_user",
"username": vpnUser.UserName,
}
if vpnUser.DnsEnabled {
form["dns"] = "true"
form["vpc_id"] = vpnUser.DnsName
} else {
form["vpc_id"] = vpnUser.VpcID
}
return c.PostAPI(form["action"], form, BasicCheck)
}