resource.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package modulebase
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/util/httputils"
  22. "yunion.io/x/pkg/util/printutils"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. type TResourceFilter func(*mcclient.ClientSession, jsonutils.JSONObject, jsonutils.JSONObject) (jsonutils.JSONObject, error)
  27. const (
  28. DEFAULT_NAME_FIELD_NAME = "name"
  29. DEFAULT_ID_FIELD_NAME = "id"
  30. )
  31. type ResourceManager struct {
  32. BaseManager
  33. context string
  34. Keyword string
  35. KeywordPlural string
  36. readFilter TResourceFilter
  37. writeFilter TResourceFilter
  38. enableFilter bool
  39. nameFieldName string
  40. idFieldName string
  41. }
  42. func (this ResourceManager) GetKeyword() string {
  43. return this.Keyword
  44. }
  45. func (this ResourceManager) KeyString() string {
  46. return this.KeywordPlural
  47. }
  48. func (this ResourceManager) Version() string {
  49. return this.version
  50. }
  51. func (this ResourceManager) ServiceType() string {
  52. return this.serviceType
  53. }
  54. func (this ResourceManager) EndpointType() string {
  55. return this.endpointType
  56. }
  57. func (this ResourceManager) URLPath() string {
  58. return strings.Replace(this.KeywordPlural, ":", "/", -1)
  59. }
  60. func (this *ResourceManager) SetReadFilter(filter TResourceFilter) *ResourceManager {
  61. this.readFilter = filter
  62. this.enableFilter = true
  63. return this
  64. }
  65. func (this *ResourceManager) SetWriteFilter(filter TResourceFilter) *ResourceManager {
  66. this.writeFilter = filter
  67. this.enableFilter = true
  68. return this
  69. }
  70. func (this *ResourceManager) SetEnableFilter(enable bool) *ResourceManager {
  71. this.enableFilter = enable
  72. return this
  73. }
  74. func (this *ResourceManager) SetNameField(fn string) *ResourceManager {
  75. this.nameFieldName = fn
  76. return this
  77. }
  78. func (this *ResourceManager) getNameFieldName() string {
  79. if len(this.nameFieldName) > 0 {
  80. return this.nameFieldName
  81. }
  82. return DEFAULT_NAME_FIELD_NAME
  83. }
  84. func (this *ResourceManager) getIdFieldName() string {
  85. if len(this.idFieldName) > 0 {
  86. return this.idFieldName
  87. }
  88. return DEFAULT_ID_FIELD_NAME
  89. }
  90. func (this *ResourceManager) ContextPath(ctxs []ManagerContext) string {
  91. segs := make([]string, 0)
  92. if len(this.context) > 0 {
  93. segs = append(segs, this.context)
  94. }
  95. if ctxs != nil && len(ctxs) > 0 {
  96. for _, ctx := range ctxs {
  97. segs = append(segs, ctx.InstanceManager.KeyString())
  98. if len(ctx.InstanceId) > 0 {
  99. segs = append(segs, url.PathEscape(ctx.InstanceId))
  100. }
  101. }
  102. }
  103. segs = append(segs, this.URLPath())
  104. return strings.Join(segs, "/")
  105. }
  106. func (this *ResourceManager) GetById(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  107. return this.GetByIdInContexts(session, id, params, nil)
  108. }
  109. func (this *ResourceManager) GetByIdInContext(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  110. return this.GetByIdInContexts(session, id, params, []ManagerContext{{ctx, ctxid}})
  111. }
  112. func (this *ResourceManager) GetByIdInContexts(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  113. path := fmt.Sprintf("/%s/%s", this.ContextPath(ctxs), url.PathEscape(id))
  114. if params != nil {
  115. qs := params.QueryString()
  116. if len(qs) > 0 {
  117. path = fmt.Sprintf("%s?%s", path, qs)
  118. }
  119. }
  120. obj, err := this._get(session, path, this.Keyword)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return this.filterSingleResult(session, obj, params)
  125. }
  126. func (this *ResourceManager) GetByName(session *mcclient.ClientSession, name string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  127. return this.GetByNameInContexts(session, name, params, nil)
  128. }
  129. func (this *ResourceManager) GetByNameInContext(session *mcclient.ClientSession, name string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  130. return this.GetByNameInContexts(session, name, params, []ManagerContext{{ctx, ctxid}})
  131. }
  132. func (this *ResourceManager) GetByNameInContexts(session *mcclient.ClientSession, name string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  133. var paramsDict *jsonutils.JSONDict
  134. if params != nil {
  135. paramsDict = params.(*jsonutils.JSONDict)
  136. paramsDict = paramsDict.Copy()
  137. } else {
  138. paramsDict = jsonutils.NewDict()
  139. }
  140. paramsDict.Add(jsonutils.NewString(name), this.getNameFieldName())
  141. results, e := this.ListInContexts(session, paramsDict, ctxs)
  142. if e != nil {
  143. return nil, e
  144. }
  145. if len(results.Data) == 0 {
  146. return nil, httperrors.NewNotFoundError("Name %s not found", name)
  147. } else if len(results.Data) == 1 {
  148. oname, _ := results.Data[0].GetString(this.getNameFieldName())
  149. if oname == name {
  150. return results.Data[0], nil
  151. } else {
  152. return nil, httperrors.NewNotFoundError("Name %s not found", name)
  153. }
  154. } else {
  155. return nil, httperrors.NewDuplicateNameError("name", name)
  156. }
  157. }
  158. func (this *ResourceManager) Get(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  159. return this.GetInContexts(session, id, params, nil)
  160. }
  161. func (this *ResourceManager) GetInContext(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  162. return this.GetInContexts(session, id, params, []ManagerContext{{ctx, ctxid}})
  163. }
  164. func (this *ResourceManager) GetInContexts(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  165. obj, e := this.GetByIdInContexts(session, id, params, ctxs)
  166. if e != nil {
  167. je, ok := e.(*httputils.JSONClientError)
  168. if ok && je.Code == 404 {
  169. return this.GetByNameInContexts(session, id, params, ctxs)
  170. } else {
  171. return nil, e
  172. }
  173. } else {
  174. return obj, e
  175. }
  176. }
  177. func (this *ResourceManager) GetId(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (string, error) {
  178. return this.GetIdInContexts(session, id, params, nil)
  179. }
  180. func (this *ResourceManager) GetIdInContext(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctx Manager, ctxid string) (string, error) {
  181. return this.GetIdInContexts(session, id, params, []ManagerContext{{ctx, ctxid}})
  182. }
  183. func (this *ResourceManager) GetIdInContexts(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctxs []ManagerContext) (string, error) {
  184. obj, e := this.GetInContexts(session, id, params, ctxs)
  185. if e != nil {
  186. return "", e
  187. }
  188. return obj.GetString(this.getIdFieldName())
  189. }
  190. func (this *ResourceManager) GetSpecific(session *mcclient.ClientSession, id string, spec string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  191. return this.GetSpecificInContexts(session, id, spec, params, nil)
  192. }
  193. func (this *ResourceManager) GetSpecificInContext(session *mcclient.ClientSession, id string, spec string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  194. return this.GetSpecificInContexts(session, id, spec, params, []ManagerContext{{ctx, ctxid}})
  195. }
  196. func (this *ResourceManager) GetSpecificInContexts(session *mcclient.ClientSession, id string, spec string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  197. path := fmt.Sprintf("/%s/%s/%s", this.ContextPath(ctxs), url.PathEscape(id), url.PathEscape(spec))
  198. if params != nil {
  199. qs := params.QueryString()
  200. if len(qs) > 0 {
  201. path = fmt.Sprintf("%s?%s", path, qs)
  202. }
  203. }
  204. return this._get(session, path, this.Keyword)
  205. }
  206. func (this *ResourceManager) BatchGet(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []printutils.SubmitResult {
  207. return this.BatchGetInContexts(session, idlist, params, nil)
  208. }
  209. func (this *ResourceManager) BatchGetInContext(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject, ctx Manager, ctxid string) []printutils.SubmitResult {
  210. return this.BatchGetInContexts(session, idlist, params, []ManagerContext{{ctx, ctxid}})
  211. }
  212. func (this *ResourceManager) BatchGetInContexts(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject, ctxs []ManagerContext) []printutils.SubmitResult {
  213. return BatchDo(idlist, func(id string) (jsonutils.JSONObject, error) {
  214. return this.GetInContexts(session, id, params, ctxs)
  215. })
  216. }
  217. func (this *ResourceManager) List(session *mcclient.ClientSession, params jsonutils.JSONObject) (*printutils.ListResult, error) {
  218. return this.ListInContexts(session, params, nil)
  219. }
  220. func (this *ResourceManager) ListInContext(session *mcclient.ClientSession, params jsonutils.JSONObject, ctx Manager, ctxid string) (*printutils.ListResult, error) {
  221. return this.ListInContexts(session, params, []ManagerContext{{ctx, ctxid}})
  222. }
  223. func (this *ResourceManager) ListInContexts(session *mcclient.ClientSession, params jsonutils.JSONObject, ctxs []ManagerContext) (*printutils.ListResult, error) {
  224. path := fmt.Sprintf("/%s", this.ContextPath(ctxs))
  225. if params != nil {
  226. qs := params.QueryString()
  227. if len(qs) > 0 {
  228. path = fmt.Sprintf("%s?%s", path, qs)
  229. }
  230. }
  231. results, err := this._list(session, path, this.KeywordPlural)
  232. if err != nil {
  233. return nil, err
  234. }
  235. return this.filterListResults(session, results, params)
  236. }
  237. func (this *ResourceManager) Head(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  238. return this.HeadInContexts(session, id, params, nil)
  239. }
  240. func (this *ResourceManager) HeadInContext(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  241. return this.HeadInContexts(session, id, params, []ManagerContext{{ctx, ctxid}})
  242. }
  243. func (this *ResourceManager) HeadInContexts(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  244. path := fmt.Sprintf("/%s/%s", this.ContextPath(ctxs), url.PathEscape(id))
  245. if params != nil {
  246. qs := params.QueryString()
  247. if len(qs) > 0 {
  248. path = fmt.Sprintf("%s?%s", path, qs)
  249. }
  250. }
  251. result, err := this._head(session, path, this.Keyword)
  252. if err != nil {
  253. return nil, err
  254. }
  255. return this.filterSingleResult(session, result, params)
  256. }
  257. func (this *ResourceManager) params2Body(s *mcclient.ClientSession, params jsonutils.JSONObject, key string) *jsonutils.JSONDict {
  258. body := jsonutils.NewDict()
  259. if params != nil {
  260. if this.enableFilter && this.writeFilter != nil {
  261. val, err := this.writeFilter(s, params, nil)
  262. if err == nil {
  263. params = val
  264. } else {
  265. log.Warningf("writeFilter fail %s: %s", params, err)
  266. }
  267. }
  268. body.Add(params, key)
  269. }
  270. return body
  271. }
  272. func (this *ResourceManager) Create(session *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  273. return this.CreateInContexts(session, params, nil)
  274. }
  275. func (this *ResourceManager) CreateInContext(session *mcclient.ClientSession, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  276. return this.CreateInContexts(session, params, []ManagerContext{{ctx, ctxid}})
  277. }
  278. func (this *ResourceManager) CreateInContexts(session *mcclient.ClientSession, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  279. path := fmt.Sprintf("/%s", this.ContextPath(ctxs))
  280. result, err := this._post(session, path, this.params2Body(session, params, this.Keyword), this.Keyword)
  281. if err != nil {
  282. return nil, err
  283. }
  284. return this.filterSingleResult(session, result, nil)
  285. }
  286. func (this *ResourceManager) BatchCreate(session *mcclient.ClientSession, params jsonutils.JSONObject, count int) []printutils.SubmitResult {
  287. return this.BatchCreateInContexts(session, params, count, nil)
  288. }
  289. func (this *ResourceManager) BatchCreateInContext(session *mcclient.ClientSession, params jsonutils.JSONObject, count int, ctx Manager, ctxid string) []printutils.SubmitResult {
  290. return this.BatchCreateInContexts(session, params, count, []ManagerContext{{ctx, ctxid}})
  291. }
  292. func (this *ResourceManager) BatchCreateInContexts(session *mcclient.ClientSession, params jsonutils.JSONObject, count int, ctxs []ManagerContext) []printutils.SubmitResult {
  293. path := fmt.Sprintf("/%s", this.ContextPath(ctxs))
  294. body := this.params2Body(session, params, this.Keyword)
  295. body.Add(jsonutils.NewInt(int64(count)), "count")
  296. ret := make([]printutils.SubmitResult, count)
  297. respbody, err := this._post(session, path, body, this.KeywordPlural)
  298. if err != nil {
  299. jsonErr, ok := err.(*httputils.JSONClientError)
  300. if ok {
  301. for i := 0; i < count; i++ {
  302. ret[i] = printutils.SubmitResult{Status: jsonErr.Code, Data: jsonutils.Marshal(jsonErr)}
  303. }
  304. } else {
  305. for i := 0; i < count; i++ {
  306. ret[i] = printutils.SubmitResult{Status: 500, Data: jsonutils.NewString(err.Error())}
  307. }
  308. }
  309. return ret
  310. }
  311. respArray, ok := respbody.(*jsonutils.JSONArray)
  312. if !ok {
  313. for i := 0; i < count; i++ {
  314. ret[i] = printutils.SubmitResult{Status: 500, Data: jsonutils.NewString("Invalid response")}
  315. }
  316. return ret
  317. }
  318. for i := 0; i < respArray.Size(); i++ {
  319. json, e := respArray.GetAt(i)
  320. if e != nil {
  321. ret[i] = printutils.SubmitResult{Status: 500, Data: jsonutils.NewString(e.Error())}
  322. } else {
  323. code, _ := json.Int("status")
  324. dat, _ := json.Get("body")
  325. if this.enableFilter && this.readFilter != nil {
  326. val, err := this.readFilter(session, dat, nil)
  327. if err != nil {
  328. log.Warningf("readFilter fail for %s: %s", dat, err)
  329. } else {
  330. dat = val
  331. }
  332. }
  333. idstr, _ := dat.GetString(this.getIdFieldName())
  334. ret[i] = printutils.SubmitResult{Status: int(code), Id: idstr, Data: dat}
  335. }
  336. }
  337. return ret
  338. }
  339. func (this *ResourceManager) Update(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  340. return this.PutInContexts(session, id, params, nil)
  341. }
  342. func (this *ResourceManager) Put(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  343. return this.PutInContexts(session, id, params, nil)
  344. }
  345. func (this *ResourceManager) PutInContext(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  346. return this.PutInContexts(session, id, params, []ManagerContext{{ctx, ctxid}})
  347. }
  348. func (this *ResourceManager) PutInContexts(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  349. path := fmt.Sprintf("/%s/%s", this.ContextPath(ctxs), url.PathEscape(id))
  350. result, err := this._put(session, path, this.params2Body(session, params, this.Keyword), this.Keyword)
  351. if err != nil {
  352. return nil, err
  353. }
  354. return this.filterSingleResult(session, result, nil)
  355. }
  356. func (this *ResourceManager) PutSpecific(session *mcclient.ClientSession, id string, spec string, query, body jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  357. return this.PutSpecificInContexts(session, id, spec, query, body, nil)
  358. }
  359. func (this *ResourceManager) PutSpecificInContext(session *mcclient.ClientSession, id string, spec string, query, body jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  360. return this.PutSpecificInContexts(session, id, spec, query, body, []ManagerContext{{ctx, ctxid}})
  361. }
  362. func (this *ResourceManager) PutSpecificInContexts(session *mcclient.ClientSession, id string, spec string, query, body jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  363. path := fmt.Sprintf("/%s/%s/%s", this.ContextPath(ctxs), url.PathEscape(id), url.PathEscape(spec))
  364. if query != nil {
  365. qs := query.QueryString()
  366. if len(qs) > 0 {
  367. path = fmt.Sprintf("%s?%s", path, qs)
  368. }
  369. }
  370. if body != nil {
  371. body = this.params2Body(session, body, this.Keyword)
  372. }
  373. result, err := this._put(session, path, body, this.Keyword)
  374. if err != nil {
  375. return nil, err
  376. }
  377. return this.filterSingleResult(session, result, nil)
  378. }
  379. func (this *ResourceManager) BatchUpdate(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []printutils.SubmitResult {
  380. return this.BatchPutInContexts(session, idlist, params, nil)
  381. }
  382. func (this *ResourceManager) BatchPut(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []printutils.SubmitResult {
  383. return this.BatchPutInContexts(session, idlist, params, nil)
  384. }
  385. func (this *ResourceManager) BatchPutInContext(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject, ctx Manager, ctxid string) []printutils.SubmitResult {
  386. return this.BatchPutInContexts(session, idlist, params, []ManagerContext{{ctx, ctxid}})
  387. }
  388. func (this *ResourceManager) BatchPutInContexts(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject, ctxs []ManagerContext) []printutils.SubmitResult {
  389. return BatchDo(idlist, func(id string) (jsonutils.JSONObject, error) {
  390. return this.PutInContexts(session, id, params, ctxs)
  391. })
  392. }
  393. func (this *ResourceManager) BatchParamsUpdate(session *mcclient.ClientSession, idlist []string, params []jsonutils.JSONObject) []printutils.SubmitResult {
  394. return this.BatchParamsPutInContexts(session, idlist, params, nil)
  395. }
  396. func (this *ResourceManager) BatchParamsPutInContexts(session *mcclient.ClientSession, idlist []string, params []jsonutils.JSONObject, ctxs []ManagerContext) []printutils.SubmitResult {
  397. return BatchParamsDo(idlist, params, func(id string, param jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  398. return this.PutInContexts(session, id, param, ctxs)
  399. })
  400. }
  401. func (this *ResourceManager) Patch(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  402. return this.PatchInContexts(session, id, params, nil)
  403. }
  404. func (this *ResourceManager) PatchInContext(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  405. return this.PatchInContexts(session, id, params, []ManagerContext{{ctx, ctxid}})
  406. }
  407. func (this *ResourceManager) PatchInContexts(session *mcclient.ClientSession, id string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  408. path := fmt.Sprintf("/%s/%s", this.ContextPath(ctxs), url.PathEscape(id))
  409. result, err := this._patch(session, path, this.params2Body(session, params, this.Keyword), this.Keyword)
  410. if err != nil {
  411. return nil, err
  412. }
  413. return this.filterSingleResult(session, result, nil)
  414. }
  415. func (this *ResourceManager) BatchPatch(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject) []printutils.SubmitResult {
  416. return this.BatchPatchInContexts(session, idlist, params, nil)
  417. }
  418. func (this *ResourceManager) BatchPatchInContext(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject, ctx Manager, ctxid string) []printutils.SubmitResult {
  419. return this.BatchPatchInContexts(session, idlist, params, []ManagerContext{{ctx, ctxid}})
  420. }
  421. func (this *ResourceManager) BatchPatchInContexts(session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject, ctxs []ManagerContext) []printutils.SubmitResult {
  422. return BatchDo(idlist, func(id string) (jsonutils.JSONObject, error) {
  423. return this.PatchInContexts(session, id, params, ctxs)
  424. })
  425. }
  426. func (this *ResourceManager) PerformAction(session *mcclient.ClientSession, id string, action string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  427. return this.PerformActionInContexts(session, id, action, params, nil)
  428. }
  429. func (this *ResourceManager) PerformActionInContext(session *mcclient.ClientSession, id string, action string, params jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  430. return this.PerformActionInContexts(session, id, action, params, []ManagerContext{{ctx, ctxid}})
  431. }
  432. func (this *ResourceManager) PerformActionInContexts(session *mcclient.ClientSession, id string, action string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  433. path := fmt.Sprintf("/%s/%s/%s", this.ContextPath(ctxs), url.PathEscape(id), url.PathEscape(action))
  434. result, err := this._post(session, path, this.params2Body(session, params, this.Keyword), this.Keyword)
  435. if err != nil {
  436. return nil, err
  437. }
  438. return this.filterSingleResult(session, result, nil)
  439. }
  440. func (this *ResourceManager) PerformClassAction(session *mcclient.ClientSession, action string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  441. return this.PerformClassActionInContexts(session, action, params, nil)
  442. }
  443. func (this *ResourceManager) PerformClassActionInContexts(session *mcclient.ClientSession, action string, params jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  444. path := fmt.Sprintf("/%s/%s", this.ContextPath(ctxs), url.PathEscape(action))
  445. return this._post(session, path, this.params2Body(session, params, this.KeywordPlural), this.KeywordPlural)
  446. }
  447. func (this *ResourceManager) BatchPerformClassAction(session *mcclient.ClientSession, action string, batchParams []jsonutils.JSONObject) []printutils.SubmitResult {
  448. return this.BatchPerformClassActionInContexts(session, action, batchParams, nil)
  449. }
  450. func (this *ResourceManager) BatchPerformClassActionInContexts(session *mcclient.ClientSession, action string, batchParams []jsonutils.JSONObject, ctxs []ManagerContext) []printutils.SubmitResult {
  451. return BatchDoClassAction(batchParams, func(params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  452. return this.PerformClassActionInContexts(session, action, params, ctxs)
  453. })
  454. }
  455. func (this *ResourceManager) BatchPerformAction(session *mcclient.ClientSession, idlist []string, action string, params jsonutils.JSONObject) []printutils.SubmitResult {
  456. return this.BatchPerformActionInContexts(session, idlist, action, params, nil)
  457. }
  458. func (this *ResourceManager) BatchPerformActionInContext(session *mcclient.ClientSession, idlist []string, action string, params jsonutils.JSONObject, ctx Manager, ctxid string) []printutils.SubmitResult {
  459. return this.BatchPerformActionInContexts(session, idlist, action, params, []ManagerContext{{ctx, ctxid}})
  460. }
  461. func (this *ResourceManager) BatchPerformActionInContexts(session *mcclient.ClientSession, idlist []string, action string, params jsonutils.JSONObject, ctxs []ManagerContext) []printutils.SubmitResult {
  462. return BatchDo(idlist, func(id string) (jsonutils.JSONObject, error) {
  463. return this.PerformActionInContexts(session, id, action, params, ctxs)
  464. })
  465. }
  466. func (this *ResourceManager) Delete(session *mcclient.ClientSession, id string, body jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  467. return this.DeleteInContexts(session, id, body, nil)
  468. }
  469. func (this *ResourceManager) DeleteWithParam(session *mcclient.ClientSession, id string, params, body jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  470. return this.DeleteInContextsWithParam(session, id, params, body, nil)
  471. }
  472. func (this *ResourceManager) DeleteInContext(session *mcclient.ClientSession, id string, body jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  473. return this.DeleteInContexts(session, id, body, []ManagerContext{{ctx, ctxid}})
  474. }
  475. func (this *ResourceManager) DeleteInContextWithParam(session *mcclient.ClientSession, id string, query jsonutils.JSONObject, body jsonutils.JSONObject, ctx Manager, ctxid string) (jsonutils.JSONObject, error) {
  476. return this.DeleteInContextsWithParam(session, id, query, body, []ManagerContext{{ctx, ctxid}})
  477. }
  478. func (this *ResourceManager) DeleteInContexts(session *mcclient.ClientSession, id string, body jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  479. return this.deleteInContexts(session, id, nil, body, ctxs)
  480. }
  481. func (this *ResourceManager) DeleteInContextsWithParam(session *mcclient.ClientSession, id string, params, body jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  482. return this.deleteInContexts(session, id, params, body, ctxs)
  483. }
  484. func (this *ResourceManager) deleteInContexts(session *mcclient.ClientSession, id string, params, body jsonutils.JSONObject, ctxs []ManagerContext) (jsonutils.JSONObject, error) {
  485. path := fmt.Sprintf("/%s/%s", this.ContextPath(ctxs), url.PathEscape(id))
  486. if params != nil {
  487. qs := params.QueryString()
  488. if len(qs) > 0 {
  489. path = fmt.Sprintf("%s?%s", path, qs)
  490. }
  491. }
  492. if body != nil {
  493. body = this.params2Body(session, body, this.Keyword)
  494. }
  495. result, err := this._delete(session, path, body, this.Keyword)
  496. if err != nil {
  497. return nil, err
  498. }
  499. return this.filterSingleResult(session, result, nil)
  500. }
  501. func (this *ResourceManager) BatchDelete(session *mcclient.ClientSession, idlist []string, body jsonutils.JSONObject) []printutils.SubmitResult {
  502. return this.BatchDeleteInContexts(session, idlist, body, nil)
  503. }
  504. func (this *ResourceManager) BatchDeleteWithParam(session *mcclient.ClientSession, idlist []string, params, body jsonutils.JSONObject) []printutils.SubmitResult {
  505. return this.BatchDeleteInContextsWithParam(session, idlist, params, body, nil)
  506. }
  507. func (this *ResourceManager) BatchDeleteInContext(session *mcclient.ClientSession, idlist []string, body jsonutils.JSONObject, ctx Manager, ctxid string) []printutils.SubmitResult {
  508. return this.BatchDeleteInContexts(session, idlist, body, []ManagerContext{{ctx, ctxid}})
  509. }
  510. func (this *ResourceManager) BatchDeleteInContextWithParam(session *mcclient.ClientSession, idlist []string, params, body jsonutils.JSONObject, ctx Manager, ctxid string) []printutils.SubmitResult {
  511. return this.BatchDeleteInContextsWithParam(session, idlist, params, body, []ManagerContext{{ctx, ctxid}})
  512. }
  513. func (this *ResourceManager) BatchDeleteInContexts(session *mcclient.ClientSession, idlist []string, body jsonutils.JSONObject, ctxs []ManagerContext) []printutils.SubmitResult {
  514. return BatchDo(idlist, func(id string) (jsonutils.JSONObject, error) {
  515. return this.DeleteInContexts(session, id, body, ctxs)
  516. })
  517. }
  518. func (this *ResourceManager) BatchDeleteInContextsWithParam(session *mcclient.ClientSession, idlist []string, params, body jsonutils.JSONObject, ctxs []ManagerContext) []printutils.SubmitResult {
  519. return BatchDo(idlist, func(id string) (jsonutils.JSONObject, error) {
  520. return this.deleteInContexts(session, id, params, body, ctxs)
  521. })
  522. }
  523. func (this *ResourceManager) GetMetadata(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  524. return this.GetSpecific(session, id, "metadata", params)
  525. }
  526. func (this *ResourceManager) SetMetadata(session *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  527. return this.PerformAction(session, id, "metadata", params)
  528. }