-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhost.go
254 lines (236 loc) · 6.04 KB
/
host.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
/*
*
* Copyright © 2020 Dell Inc. or its subsidiaries. All Rights Reserved.
*
* 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 gopowerstore
import (
"context"
"fmt"
"github.com/dell/gopowerstore/api"
)
const (
hostURL = "host"
hostMappingURL = "host_volume_mapping"
)
func getHostDefaultQueryParams(c Client) api.QueryParamsEncoder {
host := Host{}
return c.APIClient().QueryParamsWithFields(&host)
}
func getHostVolumeMappingQueryParams(c Client) api.QueryParamsEncoder {
hostMapping := HostVolumeMapping{}
return c.APIClient().QueryParamsWithFields(&hostMapping)
}
// GetHosts returns hosts list
func (c *ClientIMPL) GetHosts(ctx context.Context) (resp []Host, err error) {
err = c.readPaginatedData(func(offset int) (api.RespMeta, error) {
var page []Host
qp := getHostDefaultQueryParams(c)
qp.Limit(paginationDefaultPageSize)
qp.Offset(offset)
qp.Order("name")
meta, err := c.APIClient().Query(
ctx,
RequestConfig{
Method: "GET",
Endpoint: hostURL,
QueryParams: qp,
},
&page)
err = WrapErr(err)
if err == nil {
resp = append(resp, page...)
}
return meta, err
})
return resp, err
}
// GetHost get host by id
func (c *ClientIMPL) GetHost(ctx context.Context, id string) (resp Host, err error) {
_, err = c.APIClient().Query(
ctx,
RequestConfig{
Method: "GET",
Endpoint: hostURL,
ID: id,
QueryParams: getHostDefaultQueryParams(c),
},
&resp)
return resp, WrapErr(err)
}
// GetHostByName get host by name
func (c *ClientIMPL) GetHostByName(ctx context.Context, name string) (resp Host, err error) {
var hostList []Host
qp := getHostDefaultQueryParams(c)
qp.RawArg("name", fmt.Sprintf("eq.%s", name))
_, err = c.APIClient().Query(
ctx,
RequestConfig{
Method: "GET",
Endpoint: hostURL,
QueryParams: qp,
},
&hostList)
err = WrapErr(err)
if err != nil {
return resp, err
}
if len(hostList) != 1 {
return resp, NewHostIsNotExistError()
}
return hostList[0], err
}
// CreateHost register new host
func (c *ClientIMPL) CreateHost(ctx context.Context, createParams *HostCreate) (resp CreateResponse, err error) {
_, err = c.APIClient().Query(
ctx,
RequestConfig{
Method: "POST",
Endpoint: hostURL,
Body: createParams,
},
&resp)
return resp, WrapErr(err)
}
// DeleteHost removes host registration
func (c *ClientIMPL) DeleteHost(ctx context.Context,
deleteParams *HostDelete, id string,
) (resp EmptyResponse, err error) {
_, err = c.APIClient().Query(
ctx,
RequestConfig{
Method: "DELETE",
Endpoint: hostURL,
ID: id,
Body: deleteParams,
},
&resp)
return resp, WrapErr(err)
}
// ModifyHost update host info
func (c *ClientIMPL) ModifyHost(ctx context.Context,
modifyParams *HostModify, id string,
) (resp CreateResponse, err error) {
_, err = c.APIClient().Query(
ctx,
RequestConfig{
Method: "PATCH",
Endpoint: hostURL,
ID: id,
Body: modifyParams,
},
&resp)
return resp, WrapErr(err)
}
// GetHostVolumeMappings returns volume mapping
func (c *ClientIMPL) GetHostVolumeMappings(ctx context.Context) (resp []HostVolumeMapping, err error) {
err = c.readPaginatedData(func(offset int) (api.RespMeta, error) {
var page []HostVolumeMapping
qp := getHostVolumeMappingQueryParams(c)
qp.Limit(paginationDefaultPageSize)
qp.Offset(offset)
qp.Order("id")
meta, err := c.APIClient().Query(
ctx,
RequestConfig{
Method: "GET",
Endpoint: hostMappingURL,
QueryParams: qp,
},
&page)
err = WrapErr(err)
if err == nil {
resp = append(resp, page...)
}
return meta, err
})
return resp, WrapErr(err)
}
// GetHostVolumeMapping returns volume mapping by id
func (c *ClientIMPL) GetHostVolumeMapping(ctx context.Context, id string) (resp HostVolumeMapping, err error) {
_, err = c.APIClient().Query(
ctx,
RequestConfig{
Method: "GET",
Endpoint: hostMappingURL,
ID: id,
QueryParams: getHostVolumeMappingQueryParams(c),
},
&resp)
return resp, WrapErr(err)
}
// GetHostVolumeMappingByVolumeID returns volume mapping by volumeID
func (c *ClientIMPL) GetHostVolumeMappingByVolumeID(
ctx context.Context, volumeID string,
) (resp []HostVolumeMapping, err error) {
err = c.readPaginatedData(func(offset int) (api.RespMeta, error) {
var page []HostVolumeMapping
qp := getHostVolumeMappingQueryParams(c)
qp.RawArg("volume_id", fmt.Sprintf("eq.%s", volumeID))
qp.Order("id")
qp.Limit(paginationDefaultPageSize)
qp.Offset(offset)
meta, err := c.APIClient().Query(
ctx,
RequestConfig{
Method: "GET",
Endpoint: hostMappingURL,
QueryParams: qp,
},
&page)
err = WrapErr(err)
if err == nil {
resp = append(resp, page...)
}
return meta, err
})
return resp, WrapErr(err)
}
// AttachVolumeToHost attaches volume to host
func (c *ClientIMPL) AttachVolumeToHost(
ctx context.Context,
hostID string,
attachParams *HostVolumeAttach,
) (resp EmptyResponse, err error) {
_, err = c.APIClient().Query(
ctx,
RequestConfig{
Method: "POST",
Endpoint: hostURL,
ID: hostID,
Action: "attach",
Body: attachParams,
},
&resp)
return resp, WrapErr(err)
}
// DetachVolumeFromHost detaches volume to host
func (c *ClientIMPL) DetachVolumeFromHost(
ctx context.Context,
hostID string,
detachParams *HostVolumeDetach,
) (resp EmptyResponse, err error) {
_, err = c.APIClient().Query(
ctx,
RequestConfig{
Method: "POST",
Endpoint: hostURL,
ID: hostID,
Action: "detach",
Body: detachParams,
},
&resp)
return resp, WrapErr(err)
}