-
Notifications
You must be signed in to change notification settings - Fork 26
/
client.go
300 lines (245 loc) · 10.8 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Copyright 2017 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package remote
import (
"fmt"
"log"
"net/http"
"github.com/IBM/ubiquity/resources"
"reflect"
"github.com/IBM/ubiquity/remote/mounter"
"github.com/IBM/ubiquity/utils"
)
type remoteClient struct {
logger *log.Logger
isActivated bool
isMounted bool
httpClient *http.Client
storageApiURL string
config resources.UbiquityPluginConfig
mounterPerBackend map[string]resources.Mounter
}
func NewRemoteClient(logger *log.Logger, storageApiURL string, config resources.UbiquityPluginConfig) (resources.StorageClient, error) {
return &remoteClient{logger: logger, storageApiURL: storageApiURL, httpClient: &http.Client{}, config: config, mounterPerBackend: make(map[string]resources.Mounter)}, nil
}
func (s *remoteClient) Activate(activateRequest resources.ActivateRequest) error {
s.logger.Println("remoteClient: Activate start")
defer s.logger.Println("remoteClient: Activate end")
if s.isActivated {
return nil
}
// call remote activate
activateURL := utils.FormatURL(s.storageApiURL, "activate")
response, err := utils.HttpExecute(s.httpClient, s.logger, "POST", activateURL, activateRequest)
if err != nil {
s.logger.Printf("Error in activate remote call %#v", err)
return fmt.Errorf("Error in activate remote call")
}
if response.StatusCode != http.StatusOK {
s.logger.Printf("Error in activate remote call %#v\n", response)
return utils.ExtractErrorResponse(response)
}
s.logger.Println("remoteClient: Activate success")
s.isActivated = true
return nil
}
func (s *remoteClient) CreateVolume(createVolumeRequest resources.CreateVolumeRequest) error {
s.logger.Println("remoteClient: create start")
defer s.logger.Println("remoteClient: create end")
createRemoteURL := utils.FormatURL(s.storageApiURL, "volumes")
if reflect.DeepEqual(s.config.SpectrumNfsRemoteConfig, resources.SpectrumNfsRemoteConfig{}) == false {
createVolumeRequest.Opts["nfsClientConfig"] = s.config.SpectrumNfsRemoteConfig.ClientConfig
}
response, err := utils.HttpExecute(s.httpClient, s.logger, "POST", createRemoteURL, createVolumeRequest)
if err != nil {
s.logger.Printf("Error in create volume remote call %s", err.Error())
return fmt.Errorf("Error in create volume remote call(http error)")
}
if response.StatusCode != http.StatusOK {
s.logger.Printf("Error in create volume remote call %#v", response)
return utils.ExtractErrorResponse(response)
}
return nil
}
func (s *remoteClient) RemoveVolume(removeVolumeRequest resources.RemoveVolumeRequest) error {
s.logger.Println("remoteClient: remove start")
defer s.logger.Println("remoteClient: remove end")
removeRemoteURL := utils.FormatURL(s.storageApiURL, "volumes", removeVolumeRequest.Name)
response, err := utils.HttpExecute(s.httpClient, s.logger, "DELETE", removeRemoteURL, removeVolumeRequest)
if err != nil {
s.logger.Printf("Error in remove volume remote call %#v", err)
return fmt.Errorf("Error in remove volume remote call")
}
if response.StatusCode != http.StatusOK {
s.logger.Printf("Error in remove volume remote call %#v", response)
return utils.ExtractErrorResponse(response)
}
return nil
}
func (s *remoteClient) GetVolume(getVolumeRequest resources.GetVolumeRequest) (resources.Volume, error) {
s.logger.Println("remoteClient: get start")
defer s.logger.Println("remoteClient: get finish")
getRemoteURL := utils.FormatURL(s.storageApiURL, "volumes", getVolumeRequest.Name)
response, err := utils.HttpExecute(s.httpClient, s.logger, "GET", getRemoteURL, getVolumeRequest)
if err != nil {
s.logger.Printf("Error in get volume remote call %#v", err)
return resources.Volume{}, fmt.Errorf("Error in get volume remote call")
}
if response.StatusCode != http.StatusOK {
s.logger.Printf("Error in get volume remote call %#v", response)
return resources.Volume{}, utils.ExtractErrorResponse(response)
}
getResponse := resources.GetResponse{}
err = utils.UnmarshalResponse(response, &getResponse)
if err != nil {
s.logger.Printf("Error in unmarshalling response for get remote call %#v for response %#v", err, response)
return resources.Volume{}, fmt.Errorf("Error in unmarshalling response for get remote call")
}
return getResponse.Volume, nil
}
func (s *remoteClient) GetVolumeConfig(getVolumeConfigRequest resources.GetVolumeConfigRequest) (map[string]interface{}, error) {
s.logger.Println("remoteClient: GetVolumeConfig start")
defer s.logger.Println("remoteClient: GetVolumeConfig finish")
getRemoteURL := utils.FormatURL(s.storageApiURL, "volumes", getVolumeConfigRequest.Name, "config")
response, err := utils.HttpExecute(s.httpClient, s.logger, "GET", getRemoteURL, getVolumeConfigRequest)
if err != nil {
s.logger.Printf("Error in get volume remote call %#v", err)
return nil, fmt.Errorf("Error in get volume remote call")
}
if response.StatusCode != http.StatusOK {
s.logger.Printf("Error in get volume remote call %#v", response)
return nil, utils.ExtractErrorResponse(response)
}
getResponse := resources.GetConfigResponse{}
err = utils.UnmarshalResponse(response, &getResponse)
if err != nil {
s.logger.Printf("Error in unmarshalling response for get remote call %#v for response %#v", err, response)
return nil, fmt.Errorf("Error in unmarshalling response for get remote call")
}
return getResponse.VolumeConfig, nil
}
func (s *remoteClient) Attach(attachRequest resources.AttachRequest) (string, error) {
s.logger.Println("remoteClient: attach start")
defer s.logger.Println("remoteClient: attach end")
attachRemoteURL := utils.FormatURL(s.storageApiURL, "volumes", attachRequest.Name, "attach")
response, err := utils.HttpExecute(s.httpClient, s.logger, "PUT", attachRemoteURL, attachRequest)
if err != nil {
s.logger.Printf("Error in attach volume remote call %#v", err)
return "", fmt.Errorf("Error in attach volume remote call")
}
if response.StatusCode != http.StatusOK {
s.logger.Printf("Error in attach volume remote call %#v", response)
return "", utils.ExtractErrorResponse(response)
}
attachResponse := resources.AttachResponse{}
err = utils.UnmarshalResponse(response, &attachResponse)
if err != nil {
return "", fmt.Errorf("Error in unmarshalling response for attach remote call")
}
getVolumeConfigRequest := resources.GetVolumeConfigRequest{Name: attachRequest.Name}
volumeConfig, err := s.GetVolumeConfig(getVolumeConfigRequest)
if err != nil {
return "", err
}
getVolumeRequest := resources.GetVolumeRequest{Name: attachRequest.Name}
volume, err := s.GetVolume(getVolumeRequest)
mounter, err := s.getMounterForBackend(volume.Backend)
if err != nil {
return "", fmt.Errorf("Error determining mounter for volume: %s", err.Error())
}
mountRequest := resources.MountRequest{Mountpoint: attachResponse.Mountpoint, VolumeConfig: volumeConfig}
mountpoint, err := mounter.Mount(mountRequest)
if err != nil {
return "", err
}
return mountpoint, nil
}
func (s *remoteClient) Detach(detachRequest resources.DetachRequest) error {
s.logger.Println("remoteClient: detach start")
defer s.logger.Println("remoteClient: detach end")
getVolumeRequest := resources.GetVolumeRequest{Name: detachRequest.Name}
volume, err := s.GetVolume(getVolumeRequest)
mounter, err := s.getMounterForBackend(volume.Backend)
if err != nil {
return fmt.Errorf("Volume not found")
}
getVolumeConfigRequest := resources.GetVolumeConfigRequest{Name: detachRequest.Name}
volumeConfig, err := s.GetVolumeConfig(getVolumeConfigRequest)
if err != nil {
return err
}
unmountRequest := resources.UnmountRequest{VolumeConfig: volumeConfig}
err = mounter.Unmount(unmountRequest)
if err != nil {
return err
}
detachRemoteURL := utils.FormatURL(s.storageApiURL, "volumes", detachRequest.Name, "detach")
response, err := utils.HttpExecute(s.httpClient, s.logger, "PUT", detachRemoteURL, detachRequest)
if err != nil {
s.logger.Printf("Error in detach volume remote call %#v", err)
return fmt.Errorf("Error in detach volume remote call")
}
if response.StatusCode != http.StatusOK {
s.logger.Printf("Error in detach volume remote call %#v", response)
return utils.ExtractErrorResponse(response)
}
afterDetachRequest := resources.AfterDetachRequest{VolumeConfig: volumeConfig}
if err := mounter.ActionAfterDetach(afterDetachRequest); err != nil {
s.logger.Printf(fmt.Sprintf("Error execute action after detaching the volume : %#v", err))
return err
}
return nil
}
func (s *remoteClient) ListVolumes(listVolumesRequest resources.ListVolumesRequest) ([]resources.Volume, error) {
s.logger.Println("remoteClient: list start")
defer s.logger.Println("remoteClient: list end")
listRemoteURL := utils.FormatURL(s.storageApiURL, "volumes")
response, err := utils.HttpExecute(s.httpClient, s.logger, "GET", listRemoteURL, listVolumesRequest)
if err != nil {
s.logger.Printf("Error in list volume remote call %#v", err)
return nil, fmt.Errorf("Error in list volume remote call")
}
if response.StatusCode != http.StatusOK {
s.logger.Printf("Error in list volume remote call %#v", err)
return nil, utils.ExtractErrorResponse(response)
}
listResponse := resources.ListResponse{}
err = utils.UnmarshalResponse(response, &listResponse)
if err != nil {
s.logger.Printf("Error in unmarshalling response for get remote call %#v for response %#v", err, response)
return []resources.Volume{}, nil
}
return listResponse.Volumes, nil
}
// Return the mounter object. If mounter object already used(in the map mounterPerBackend) then just reuse it
func (s *remoteClient) getMounterForBackend(backend string) (resources.Mounter, error) {
s.logger.Println("remoteClient: getMounterForVolume start")
defer s.logger.Println("remoteClient: getMounterForVolume end")
mounterInst, ok := s.mounterPerBackend[backend]
if ok {
s.logger.Printf("getMounterForVolume reuse existing mounter for backend " + backend)
return mounterInst, nil
} else if backend == resources.SpectrumScale {
s.mounterPerBackend[backend] = mounter.NewSpectrumScaleMounter(s.logger)
} else if backend == resources.SoftlayerNFS || backend == resources.SpectrumScaleNFS {
s.mounterPerBackend[backend] = mounter.NewNfsMounter(s.logger)
} else if backend == resources.SCBE {
s.mounterPerBackend[backend] = mounter.NewScbeMounter(s.config.ScbeRemoteConfig)
} else {
return nil, fmt.Errorf("Mounter not found for backend: %s", backend)
}
return s.mounterPerBackend[backend], nil
}