This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
softlayer_client.go
253 lines (199 loc) · 7.35 KB
/
softlayer_client.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"text/template"
services "github.com/maximilien/softlayer-go/services"
softlayer "github.com/maximilien/softlayer-go/softlayer"
)
const (
SOFTLAYER_API_URL = "api.softlayer.com/rest/v3"
TEMPLATE_ROOT_PATH = "templates"
)
type softLayerClient struct {
username string
apiKey string
templatePath string
httpClient *http.Client
softLayerServices map[string]softlayer.Service
}
func NewSoftLayerClient(username, apiKey string) *softLayerClient {
pwd, _ := os.Getwd()
slc := &softLayerClient{
username: username,
apiKey: apiKey,
templatePath: filepath.Join(pwd, TEMPLATE_ROOT_PATH),
httpClient: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
},
softLayerServices: map[string]softlayer.Service{},
}
slc.initSoftLayerServices()
return slc
}
//softlayer.Client interface methods
func (slc *softLayerClient) GetService(serviceName string) (softlayer.Service, error) {
slService, ok := slc.softLayerServices[serviceName]
if !ok {
return nil, errors.New(fmt.Sprintf("softlayer-go does not support service '%s'", serviceName))
}
return slService, nil
}
func (slc *softLayerClient) GetSoftLayer_Account_Service() (softlayer.SoftLayer_Account_Service, error) {
slService, err := slc.GetService("SoftLayer_Account")
if err != nil {
return nil, err
}
return slService.(softlayer.SoftLayer_Account_Service), nil
}
func (slc *softLayerClient) GetSoftLayer_Virtual_Guest_Service() (softlayer.SoftLayer_Virtual_Guest_Service, error) {
slService, err := slc.GetService("SoftLayer_Virtual_Guest")
if err != nil {
return nil, err
}
return slService.(softlayer.SoftLayer_Virtual_Guest_Service), nil
}
func (slc *softLayerClient) GetSoftLayer_Virtual_Disk_Image_Service() (softlayer.SoftLayer_Virtual_Disk_Image_Service, error) {
slService, err := slc.GetService("SoftLayer_Virtual_Disk_Image")
if err != nil {
return nil, err
}
return slService.(softlayer.SoftLayer_Virtual_Disk_Image_Service), nil
}
func (slc *softLayerClient) GetSoftLayer_Security_Ssh_Key_Service() (softlayer.SoftLayer_Security_Ssh_Key_Service, error) {
slService, err := slc.GetService("SoftLayer_Security_Ssh_Key")
if err != nil {
return nil, err
}
return slService.(softlayer.SoftLayer_Security_Ssh_Key_Service), nil
}
func (slc *softLayerClient) GetSoftLayer_Product_Package_Service() (softlayer.SoftLayer_Product_Package_Service, error) {
slService, err := slc.GetService("SoftLayer_Product_Package")
if err != nil {
return nil, err
}
return slService.(softlayer.SoftLayer_Product_Package_Service), nil
}
func (slc *softLayerClient) GetSoftLayer_Virtual_Guest_Block_Device_Template_Group_Service() (softlayer.SoftLayer_Virtual_Guest_Block_Device_Template_Group_Service, error) {
slService, err := slc.GetService("SoftLayer_Virtual_Guest_Block_Device_Template_Group_Service")
if err != nil {
return nil, err
}
return slService.(softlayer.SoftLayer_Virtual_Guest_Block_Device_Template_Group_Service), nil
}
func (slc *softLayerClient) GetSoftLayer_Network_Storage_Service() (softlayer.SoftLayer_Network_Storage_Service, error) {
slService, err := slc.GetService("SoftLayer_Network_Storage")
if err != nil {
return nil, err
}
return slService.(softlayer.SoftLayer_Network_Storage_Service), nil
}
func (slc *softLayerClient) GetSoftLayer_Product_Order_Service() (softlayer.SoftLayer_Product_Order_Service, error) {
slService, err := slc.GetService("SoftLayer_Product_Order")
if err != nil {
return nil, err
}
return slService.(softlayer.SoftLayer_Product_Order_Service), nil
}
func (slc *softLayerClient) GetSoftLayer_Billing_Item_Cancellation_Request_Service() (softlayer.SoftLayer_Billing_Item_Cancellation_Request_Service, error) {
slService, err := slc.GetService("SoftLayer_Billing_Item_Cancellation_Request")
if err != nil {
return nil, err
}
return slService.(softlayer.SoftLayer_Billing_Item_Cancellation_Request_Service), nil
}
//Public methods
func (slc *softLayerClient) DoRawHttpRequestWithObjectMask(path string, masks []string, requestType string, requestBody *bytes.Buffer) ([]byte, error) {
url := fmt.Sprintf("https://%s:%s@%s/%s", slc.username, slc.apiKey, SOFTLAYER_API_URL, path)
url += "?objectMask="
for i := 0; i < len(masks); i++ {
url += masks[i]
if i != len(masks)-1 {
url += ";"
}
}
return slc.makeHttpRequest(url, requestType, requestBody)
}
func (slc *softLayerClient) DoRawHttpRequest(path string, requestType string, requestBody *bytes.Buffer) ([]byte, error) {
url := fmt.Sprintf("https://%s:%s@%s/%s", slc.username, slc.apiKey, SOFTLAYER_API_URL, path)
return slc.makeHttpRequest(url, requestType, requestBody)
}
func (slc *softLayerClient) GenerateRequestBody(templateData interface{}) (*bytes.Buffer, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
bodyTemplate := template.Must(template.ParseFiles(filepath.Join(cwd, slc.templatePath)))
body := new(bytes.Buffer)
bodyTemplate.Execute(body, templateData)
return body, nil
}
func (slc *softLayerClient) HasErrors(body map[string]interface{}) error {
if errString, ok := body["error"]; !ok {
return nil
} else {
return errors.New(errString.(string))
}
}
func (slc *softLayerClient) CheckForHttpResponseErrors(data []byte) error {
var decodedResponse map[string]interface{}
err := json.Unmarshal(data, &decodedResponse)
if err != nil {
return err
}
if err := slc.HasErrors(decodedResponse); err != nil {
return err
}
return nil
}
//Private methods
func (slc *softLayerClient) initSoftLayerServices() {
slc.softLayerServices["SoftLayer_Account"] = services.NewSoftLayer_Account_Service(slc)
slc.softLayerServices["SoftLayer_Virtual_Guest"] = services.NewSoftLayer_Virtual_Guest_Service(slc)
slc.softLayerServices["SoftLayer_Virtual_Disk_Image"] = services.NewSoftLayer_Virtual_Disk_Image_Service(slc)
slc.softLayerServices["SoftLayer_Security_Ssh_Key"] = services.NewSoftLayer_Security_Ssh_Key_Service(slc)
slc.softLayerServices["SoftLayer_Product_Package"] = services.NewSoftLayer_Product_Package_Service(slc)
slc.softLayerServices["SoftLayer_Network_Storage"] = services.NewSoftLayer_Network_Storage_Service(slc)
slc.softLayerServices["SoftLayer_Product_Order"] = services.NewSoftLayer_Product_Order_Service(slc)
slc.softLayerServices["SoftLayer_Billing_Item_Cancellation_Request"] = services.NewSoftLayer_Billing_Item_Cancellation_Request_Service(slc)
slc.softLayerServices["SoftLayer_Virtual_Guest_Block_Device_Template_Group"] = services.NewSoftLayer_Virtual_Guest_Block_Device_Template_Group_Service(slc)
}
func (slc *softLayerClient) makeHttpRequest(url string, requestType string, requestBody *bytes.Buffer) ([]byte, error) {
var lastResponse http.Response
switch requestType {
case "POST", "DELETE":
req, err := http.NewRequest(requestType, url, requestBody)
if err != nil {
return nil, err
}
resp, err := slc.httpClient.Do(req)
if err != nil {
return nil, err
} else {
lastResponse = *resp
}
case "GET":
resp, err := http.Get(url)
if err != nil {
return nil, err
} else {
lastResponse = *resp
}
default:
return nil, errors.New(fmt.Sprintf("Undefined request type '%s', only GET/POST/DELETE are available!", requestType))
}
responseBody, err := ioutil.ReadAll(lastResponse.Body)
lastResponse.Body.Close()
if err != nil {
return nil, err
}
return responseBody, nil
}