forked from mindjiver/gopherstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
162 lines (134 loc) · 4.2 KB
/
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
package gopherstack
import (
"crypto/hmac"
"crypto/sha1"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
type CloudstackClient struct {
// The http client for communicating
client *http.Client
// The base URL of the API
BaseURL string
// Credentials
APIKey string
Secret string
}
// Creates a new client for communicating with Cloudstack
func (cloudstack CloudstackClient) New(apiurl string, apikey string, secret string, insecureskipverify bool) *CloudstackClient {
c := &CloudstackClient{
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureskipverify},
Proxy: http.ProxyFromEnvironment,
},
},
BaseURL: apiurl,
APIKey: apikey,
Secret: secret,
}
return c
}
func NewRequest(c CloudstackClient, request string, params url.Values) (interface{}, error) {
client := c.client
params.Set("apikey", c.APIKey)
params.Set("command", request)
params.Set("response", "json")
// Generate signature for API call
// * Serialize parameters and sort them by key, done by Encode()
// * Use byte sequence for '+' character as Cloudstack requires this
// * Convert the entire argument string to lowercase
// * Calculate HMAC SHA1 of argument string with Cloudstack secret
// * URL encode the string and convert to base64
s := params.Encode()
s2 := strings.Replace(s, "+", "%20", -1)
s3 := strings.ToLower(s2)
mac := hmac.New(sha1.New, []byte(c.Secret))
mac.Write([]byte(s3))
signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))
signature = url.QueryEscape(signature)
// Create the final URL before we issue the request
// For some reason Cloudstack refuses to accept '+' as a space character so we byte escape it instead.
url := c.BaseURL + "?" + s2 + "&signature=" + signature
log.Printf("Calling %s ", url)
resp, err := client.Get(url)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
log.Printf("Response from Cloudstack: %d - %s", resp.StatusCode, body)
if resp.StatusCode != 200 {
err = errors.New(fmt.Sprintf("Received HTTP client/server error from Cloudstack: %d", resp.StatusCode))
return nil, err
}
switch request {
default:
log.Printf("Unknown request %s", request)
case "createSSHKeyPair":
var decodedResponse CreateSshKeyPairResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "deleteSSHKeyPair":
var decodedResponse DeleteSshKeyPairResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "deployVirtualMachine":
var decodedResponse DeployVirtualMachineResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "destroyVirtualMachine":
var decodedResponse DestroyVirtualMachineResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "stopVirtualMachine":
var decodedResponse StopVirtualMachineResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "listVirtualMachines":
var decodedResponse ListVirtualMachinesResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "listProjects":
var decodedResponse ListProjectsResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "listVolumes":
var decodedResponse ListVolumesResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "createTemplate":
var decodedResponse CreateTemplateResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "listTemplates":
var decodedResponse ListTemplatesResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "listDiskOfferings":
var decodedResponse ListDiskOfferingsResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "detachIso":
var decodedResponse DetachIsoResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
case "queryAsyncJobResult":
var decodedResponse QueryAsyncJobResultResponse
json.Unmarshal(body, &decodedResponse)
return decodedResponse, nil
}
// only reached with unknown request
return "", nil
}