-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxapi.go
100 lines (77 loc) · 2.68 KB
/
proxapi.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
package proxapi
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"github.com/tidwall/gjson"
)
type Cluster struct {
ApiUrl string
AuthTocken string
CSRFPreventionToken string
}
func (proxCluster *Cluster) GetAuthTocken(client *http.Client, username, password *string) error {
apiPath := "/access/ticket"
endpoint, err := url.ParseRequestURI(proxCluster.ApiUrl) // parse URI and enforce encoding
if err != nil {
return err
}
endpoint.Path = path.Join(endpoint.Path, apiPath) // construct request location
loginPayload := url.Values{} // construct body
loginPayload.Set("username", *username)
loginPayload.Set("password", *password)
request, err := http.NewRequest("POST", endpoint.String(), strings.NewReader(loginPayload.Encode())) // construct request
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded") // assign required headers
request.Header.Add("Content-Length", strconv.Itoa(len(loginPayload.Encode())))
response, err := client.Do(request) // exec request
if err != nil {
return err
}
if response.StatusCode != 200 {
return errors.New("Status Code " + strconv.Itoa(response.StatusCode))
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body) // read the body
if err != nil {
return err
}
proxCluster.AuthTocken = gjson.Get(string(body), "data.ticket").String() // assing authTocken to cluster
proxCluster.CSRFPreventionToken = gjson.Get(string(body), "data.CSRFPreventionToken").String() // assign csrfPreventionToken to cluster
return nil
}
func (proxCluster *Cluster) GetClusterStatus(client *http.Client) (string, error) {
apiPath := "/cluster/status"
endpoint, err := url.ParseRequestURI(proxCluster.ApiUrl) // parse URI and enforce encoding
if err != nil {
return "", err
}
endpoint.Path = path.Join(endpoint.Path, apiPath) // construct request location
request, err := http.NewRequest("GET", endpoint.String(), nil) // construct request
if err != nil {
return "", err
}
request.Header.Add("Cookie", "PVEAuthCookie="+proxCluster.AuthTocken) // assign authTocken header
request.Header.Add("CSRFPreventionToken", proxCluster.CSRFPreventionToken) // assign csrf header
response, err := client.Do(request) // exec request
if err != nil {
return "", err
}
if response.StatusCode != 200 {
return "", errors.New("Status Code " + strconv.Itoa(response.StatusCode))
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body) // read the body
if err != nil {
return "", err
}
// statusJson := gjson.Get(string(body), "data").String()
statusJson := string(body)
return statusJson, nil
}