-
Notifications
You must be signed in to change notification settings - Fork 0
/
machines.go
159 lines (128 loc) · 4.3 KB
/
machines.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package go_esri
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"github.com/go-resty/resty/v2"
)
type machineJSON struct {
MachineName string `json:"machineName"`
MachineAdminUrl string `json:"adminURL"`
}
type machinesJSON struct {
MachinesList []machineJSON `json:"machines"`
}
// GetMachineNames gets a list of all machines federated in serverName.
//
// returns machinesJSON struct containing information.
// getMachinesNames(token, serverName string) (*machinesJSON, error)
func GetMachinesNames(token, serverName string) (*machinesJSON, error) {
baseUrl, err := url.Parse(serverName)
if err != nil {
return nil, err
}
baseUrl.Path += "/admin/machines/"
// ----------------------------------------- build url encode string to be included in the header body
v := url.Values{}
v.Set("token", token)
v.Add("f", "json")
// ----------------------------------------- request
req := resty.New()
resp, err := req.R().
SetHeader("Content-type", "application/x-www-form-urlencoded").
SetBody(string(v.Encode())).
Post(baseUrl.String())
if err != nil {
return nil, err
}
// ----------------------------------------- decode json response
var obj machinesJSON
err = json.Unmarshal(resp.Body(), &obj)
if err != nil {
return nil, err
}
return &obj, nil
}
type MachineInfoJSON struct {
MachineNameDomain string `json:"machineName"`
AdminURL string `json:"adminURL"`
Platform string `json:"platform"`
ServerStartTime int64 `json:"ServerStartTime"`
WebServerMaxHeapSize int32 `json:"webServerMaxHeapSize"`
WebServerSSLEnabled bool `json:"webServerSSLEnabled"`
WebServerCertificateAlias string `json:"webServerCertificateAlias"`
SocMaxHeapSize int32 `json:"socMaxHeapSize"`
AppServerMaxHeapSize int32 `json:"appServerMaxHeapSize"`
ConfiguredState string `json:"configuredState"` // <"STARTED"|"STOPPED">
Synchronize bool `json:"synchronize"`
UnderMaintenance bool `json:"underMaintenance"`
}
// GetMachineInformation returns machineInfoJSON struct for given machineName
func GetMachineInformation(token, serverName, machineName string) (*MachineInfoJSON, error) {
baseUrl, err := url.Parse(serverName)
if err != nil {
return nil, err
}
baseUrl.Path += "/admin/machines/" + machineName
// ----------------------------------------- build url encode string to be included in the header body
v := url.Values{}
v.Set("token", token)
v.Add("f", "json")
// ----------------------------------------- request
req := resty.New()
resp, err := req.R().
SetHeader("Content-type", "application/x-www-form-urlencoded").
SetBody(string(v.Encode())).
Post(baseUrl.String())
if err != nil {
return nil, err
}
// ----------------------------------------- decode json response
var obj MachineInfoJSON
err = json.Unmarshal(resp.Body(), &obj)
if err != nil {
return nil, err
}
return &obj, nil
}
// editMachineInformation returns machineInfoJSON struct for given machineName
func EditMachineInformation(token, serverName, machineName string, m MachineInfoJSON) error {
type statusJSON struct {
Status string `json:"status"`
}
baseUrl, err := url.Parse(serverName)
if err != nil {
return err
}
baseUrl.Path += "/admin/machines/" + machineName + "/edit"
// ----------------------------------------- build url encode string to be included in the header body
v := url.Values{}
v.Set("token", token)
v.Add("machineName", machineName) // not documented
v.Add("adminURL", m.AdminURL)
v.Add("webServerMaxHeapSize", fmt.Sprintf("%d", m.WebServerMaxHeapSize))
v.Add("webServerCertificateAlias", m.WebServerCertificateAlias)
v.Add("socMaxHeapSize", fmt.Sprintf("%d", m.SocMaxHeapSize))
v.Add("underMaintenance", fmt.Sprintf("%t", m.UnderMaintenance))
v.Add("f", "json")
// ----------------------------------------- request
req := resty.New()
resp, err := req.SetDebug(true).R().
SetHeader("Content-type", "application/x-www-form-urlencoded").
SetBody(string(v.Encode())).
Post(baseUrl.String())
if err != nil {
return err
}
// ----------------------------------------- decode json response
var obj statusJSON
err = json.Unmarshal(resp.Body(), &obj)
if err != nil {
return err
}
if obj.Status != "success" {
return errors.New("rest/api query error")
}
return nil
}