forked from yunionio/cloudmux
/
utils.go
270 lines (238 loc) · 8.34 KB
/
utils.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
// Copyright 2019 Yunion
//
// 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 hcso
import (
"fmt"
"reflect"
"strings"
"github.com/nyl1001/pkg/jsonutils"
"github.com/nyl1001/pkg/errors"
"github.com/nyl1001/pkg/util/httputils"
"github.com/nyl1001/pkg/utils"
"yunion.io/x/log"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
"github.com/nyl1001/cloudmux/pkg/multicloud/hcso/client/manager"
"github.com/nyl1001/cloudmux/pkg/multicloud/hcso/client/responses"
)
// 常用的方法
type listFunc func(querys map[string]string) (*responses.ListResult, error)
type getFunc func(id string, querys map[string]string) (jsonutils.JSONObject, error)
type createFunc func(params jsonutils.JSONObject) (jsonutils.JSONObject, error)
type updateFunc func(id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error)
type updateFunc2 func(ctx manager.IManagerContext, id string, spec string, params jsonutils.JSONObject, responseKey string) (jsonutils.JSONObject, error)
type deleteFunc func(id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error)
type deleteFunc2 func(ctx manager.IManagerContext, id string, spec string, queries map[string]string, params jsonutils.JSONObject, responseKey string) (jsonutils.JSONObject, error)
type listInCtxFunc func(ctx manager.IManagerContext, querys map[string]string) (*responses.ListResult, error)
type listInCtxWithSpecFunc func(ctx manager.IManagerContext, spec string, querys map[string]string, responseKey string) (*responses.ListResult, error)
func unmarshalResult(resp jsonutils.JSONObject, respErr error, result interface{}, method string) error {
if respErr != nil {
switch e := respErr.(type) {
case *httputils.JSONClientError:
if (e.Code == 404 || utils.IsInStringArray(e.Class, NOT_FOUND_CODES)) && method != "POST" {
return cloudprovider.ErrNotFound
}
return e
default:
return e
}
}
if result == nil {
return nil
}
err := resp.Unmarshal(result)
if err != nil {
log.Errorf("unmarshal json error %s", err)
}
return err
}
var pageLimit = 100
// offset 表示的是页码
func doListAllWithPagerOffset(doList listFunc, queries map[string]string, result interface{}) error {
startIndex := 0
resultValue := reflect.Indirect(reflect.ValueOf(result))
queries["limit"] = fmt.Sprintf("%d", pageLimit)
queries["offset"] = fmt.Sprintf("%d", startIndex)
for {
total, part, err := doListPart(doList, queries, result)
if err != nil {
return err
}
if (total > 0 && resultValue.Len() >= total) || (total == 0 && pageLimit > part) {
break
}
startIndex++
queries["offset"] = fmt.Sprintf("%d", startIndex)
}
return nil
}
func doListAllWithNextLink(doList listFunc, querys map[string]string, result interface{}) error {
values := []jsonutils.JSONObject{}
for {
ret, err := doList(querys)
if err != nil {
return errors.Wrap(err, "doList")
}
values = append(values, ret.Data...)
if len(ret.NextLink) == 0 || ret.NextLink == "null" {
break
}
}
if result != nil {
return jsonutils.Update(result, values)
}
return nil
}
func doListAllWithOffset(doList listFunc, queries map[string]string, result interface{}) error {
startIndex := 0
resultValue := reflect.Indirect(reflect.ValueOf(result))
queries["limit"] = fmt.Sprintf("%d", pageLimit)
queries["offset"] = fmt.Sprintf("%d", startIndex)
for {
total, part, err := doListPart(doList, queries, result)
if err != nil {
return err
}
if (total > 0 && resultValue.Len() >= total) || (total == 0 && pageLimit > part) {
break
}
queries["offset"] = fmt.Sprintf("%d", startIndex+resultValue.Len())
}
return nil
}
func doListAllWithPage(doList listFunc, queries map[string]string, result interface{}) error {
startIndex := 1
resultValue := reflect.Indirect(reflect.ValueOf(result))
queries["limit"] = fmt.Sprintf("%d", pageLimit)
queries["page"] = fmt.Sprintf("%d", startIndex)
for {
total, part, err := doListPart(doList, queries, result)
if err != nil {
return err
}
if (total > 0 && resultValue.Len() >= total) || (total == 0 && pageLimit > part) {
break
}
queries["page"] = fmt.Sprintf("%d", startIndex+resultValue.Len())
}
return nil
}
func doListAllWithMarker(doList listFunc, queries map[string]string, result interface{}) error {
resultValue := reflect.Indirect(reflect.ValueOf(result))
queries["limit"] = fmt.Sprintf("%d", pageLimit)
for {
total, part, err := doListPart(doList, queries, result)
if err != nil {
return err
}
if (total > 0 && resultValue.Len() >= total) || (total == 0 && pageLimit > part) {
break
}
lastValue := resultValue.Index(resultValue.Len() - 1)
markerValue := lastValue.FieldByNameFunc(func(key string) bool {
if strings.ToLower(key) == "id" {
return true
}
return false
})
queries["marker"] = markerValue.String()
}
return nil
}
func doListAll(doList listFunc, queries map[string]string, result interface{}) error {
total, _, err := doListPart(doList, queries, result)
if err != nil {
return err
}
resultValue := reflect.Indirect(reflect.ValueOf(result))
if total > 0 && resultValue.Len() < total {
log.Warningf("INCOMPLETE QUERY, total %d queried %d", total, resultValue.Len())
}
return nil
}
func doListPart(doList listFunc, queries map[string]string, result interface{}) (int, int, error) {
ret, err := doList(queries)
if err != nil {
return 0, 0, err
}
resultValue := reflect.Indirect(reflect.ValueOf(result))
elemType := resultValue.Type().Elem()
for i := range ret.Data {
elemPtr := reflect.New(elemType)
err = ret.Data[i].Unmarshal(elemPtr.Interface())
if err != nil {
return 0, 0, err
}
resultValue.Set(reflect.Append(resultValue, elemPtr.Elem()))
}
return ret.Total, len(ret.Data), nil
}
func DoGet(doGet getFunc, id string, queries map[string]string, result interface{}) error {
if len(id) == 0 {
resultType := reflect.Indirect(reflect.ValueOf(result)).Type()
return errors.Wrap(cloudprovider.ErrNotFound, fmt.Sprintf(" Get %s id should not be empty", resultType.Name()))
}
ret, err := doGet(id, queries)
return unmarshalResult(ret, err, result, "GET")
}
func DoListInContext(listFunc listInCtxFunc, ctx manager.IManagerContext, querys map[string]string, result interface{}) error {
ret, err := listFunc(ctx, querys)
if err != nil {
return err
}
obj := responses.ListResult2JSON(ret)
err = obj.Unmarshal(result, "data")
if err != nil {
log.Errorf("unmarshal json error %s", err)
return err
}
return nil
}
func DoCreate(createFunc createFunc, params jsonutils.JSONObject, result interface{}) error {
ret, err := createFunc(params)
return unmarshalResult(ret, err, result, "POST")
}
func DoUpdate(updateFunc updateFunc, id string, params jsonutils.JSONObject, result interface{}) error {
ret, err := updateFunc(id, params)
return unmarshalResult(ret, err, result, "PUT")
}
func DoUpdateWithSpec(updateFunc updateFunc2, id string, spec string, params jsonutils.JSONObject) error {
_, err := updateFunc(nil, id, spec, params, "")
return err
}
func DoUpdateWithSpec2(updateFunc updateFunc2, id string, spec string, params jsonutils.JSONObject, result interface{}) error {
ret, err := updateFunc(nil, id, spec, params, "")
return unmarshalResult(ret, err, result, "PUT")
}
func DoDelete(deleteFunc deleteFunc, id string, params jsonutils.JSONObject, result interface{}) error {
if len(id) == 0 {
return fmt.Errorf(" id should not be empty")
}
ret, err := deleteFunc(id, params)
return unmarshalResult(ret, err, result, "DELETE")
}
func DoDeleteWithSpec(deleteFunc deleteFunc2, ctx manager.IManagerContext, id string, spec string, queries map[string]string, params jsonutils.JSONObject) error {
if len(id) == 0 {
return fmt.Errorf(" id should not be empty")
}
_, err := deleteFunc(ctx, id, spec, queries, params, "")
return err
}
func ErrMessage(err error) string {
switch v := err.(type) {
case *httputils.JSONClientError:
return fmt.Sprintf("%d(%s):%s", v.Code, v.Class, v.Details)
default:
return err.Error()
}
}