File: vpn.go

package info (click to toggle)
golang-github-packethost-packngo 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 440 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,231 bytes parent folder | download | duplicates (2)
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
package packngo

import "fmt"

const vpnBasePath = "/user/vpn"

// VPNConfig struct
type VPNConfig struct {
	Config string `json:"config,omitempty"`
}

// VPNService interface defines available VPN functions
type VPNService interface {
	Enable() (*Response, error)
	Disable() (*Response, error)
	Get(code string, getOpt *GetOptions) (*VPNConfig, *Response, error)
}

// VPNServiceOp implements VPNService
type VPNServiceOp struct {
	client *Client
}

// Enable VPN for current user
func (s *VPNServiceOp) Enable() (resp *Response, err error) {
	return s.client.DoRequest("POST", vpnBasePath, nil, nil)
}

// Disable VPN for current user
func (s *VPNServiceOp) Disable() (resp *Response, err error) {
	return s.client.DoRequest("DELETE", vpnBasePath, nil, nil)

}

// Get returns the client vpn config for the currently logged-in user.
func (s *VPNServiceOp) Get(code string, getOpt *GetOptions) (config *VPNConfig, resp *Response, err error) {
	params := createGetOptionsURL(getOpt)
	config = &VPNConfig{}
	path := fmt.Sprintf("%s?code=%s", vpnBasePath, code)
	if params != "" {
		path += params
	}

	resp, err = s.client.DoRequest("GET", path, nil, config)
	if err != nil {
		return nil, resp, err
	}

	return config, resp, err
}