joint.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/util/printutils"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. )
  22. type JointResourceManager struct {
  23. ResourceManager
  24. Master Manager
  25. Slave Manager
  26. }
  27. var _ JointManager = (*JointResourceManager)(nil)
  28. func (this *JointResourceManager) MasterManager() Manager {
  29. return this.Master
  30. }
  31. func (this *JointResourceManager) SlaveManager() Manager {
  32. return this.Slave
  33. }
  34. func (this *JointResourceManager) Get(s *mcclient.ClientSession, mid, sid string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  35. path := fmt.Sprintf("/%s/%s/%s/%s", this.Master.KeyString(), url.PathEscape(mid), this.Slave.KeyString(), url.PathEscape(sid))
  36. if params != nil {
  37. qs := params.QueryString()
  38. if len(qs) > 0 {
  39. path = fmt.Sprintf("%s?%s", path, qs)
  40. }
  41. }
  42. result, err := this._get(s, path, this.Keyword)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return this.filterSingleResult(s, result, params)
  47. }
  48. /*
  49. func (this *JointResourceManager) List(s *mcclient.ClientSession, params jsonutils.JSONObject) (*ListResult, error) {
  50. path := fmt.Sprintf("/%s", this.KeyString())
  51. if params != nil {
  52. qs := params.QueryString()
  53. if len(qs) > 0 {
  54. path = fmt.Sprintf("%s?%s", path, qs)
  55. }
  56. }
  57. return this._list(s, path, this.KeywordPlural)
  58. }
  59. */
  60. func (this *JointResourceManager) ListDescendent(s *mcclient.ClientSession, mid string, params jsonutils.JSONObject) (*printutils.ListResult, error) {
  61. path := fmt.Sprintf("/%s/%s/%s", this.Master.KeyString(), url.PathEscape(mid), this.Slave.KeyString())
  62. if params != nil {
  63. qs := params.QueryString()
  64. if len(qs) > 0 {
  65. path = fmt.Sprintf("%s?%s", path, qs)
  66. }
  67. }
  68. results, err := this._list(s, path, this.KeywordPlural)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return this.filterListResults(s, results, params)
  73. }
  74. func (this *JointResourceManager) ListDescendent2(s *mcclient.ClientSession, sid string, params jsonutils.JSONObject) (*printutils.ListResult, error) {
  75. return this.ListAscendent(s, sid, params)
  76. }
  77. func (this *JointResourceManager) ListAscendent(s *mcclient.ClientSession, mid string, params jsonutils.JSONObject) (*printutils.ListResult, error) {
  78. path := fmt.Sprintf("/%s/%s/%s", this.Slave.KeyString(), url.PathEscape(mid), this.Master.KeyString())
  79. if params != nil {
  80. qs := params.QueryString()
  81. if len(qs) > 0 {
  82. path = fmt.Sprintf("%s?%s", path, qs)
  83. }
  84. }
  85. results, err := this._list(s, path, this.KeywordPlural)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return this.filterListResults(s, results, params)
  90. }
  91. /* func (this *JointResourceManager) Exists(s *mcclient.ClientSession, mid, sid string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  92. path := fmt.Sprintf("/%s/%s/%s/%s", this.Master.KeyString(), url.PathEscape(mid), this.Slave.KeyString(), url.PathEscape(sid))
  93. if params != nil {
  94. qs := params.QueryString()
  95. if len(qs) > 0 {
  96. path = fmt.Sprintf("%s?%s", path, qs)
  97. }
  98. }
  99. return this._head(s, path, this.Keyword)
  100. } */
  101. func (this *JointResourceManager) Attach(s *mcclient.ClientSession, mid, sid string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  102. path := fmt.Sprintf("/%s/%s/%s/%s", this.Master.KeyString(), url.PathEscape(mid), this.Slave.KeyString(), url.PathEscape(sid))
  103. result, err := this._post(s, path, this.params2Body(s, params, this.Keyword), this.Keyword)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return this.filterSingleResult(s, result, nil)
  108. }
  109. func (this *JointResourceManager) BatchAttach(s *mcclient.ClientSession, mid string, sids []string, params jsonutils.JSONObject) []printutils.SubmitResult {
  110. return BatchDo(sids, func(sid string) (jsonutils.JSONObject, error) {
  111. return this.Attach(s, mid, sid, params)
  112. })
  113. }
  114. func (this *JointResourceManager) BatchAttach2(s *mcclient.ClientSession, mid string, sids []string, params jsonutils.JSONObject) []printutils.SubmitResult {
  115. return BatchDo(sids, func(sid string) (jsonutils.JSONObject, error) {
  116. return this.Attach(s, sid, mid, params)
  117. })
  118. }
  119. func (this *JointResourceManager) Detach(s *mcclient.ClientSession, mid, sid string, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  120. path := fmt.Sprintf("/%s/%s/%s/%s", this.Master.KeyString(), url.PathEscape(mid), this.Slave.KeyString(), url.PathEscape(sid))
  121. if query != nil {
  122. qs := query.QueryString()
  123. if len(qs) > 0 {
  124. path = fmt.Sprintf("%s?%s", path, qs)
  125. }
  126. }
  127. result, err := this._delete(s, path, nil, this.Keyword)
  128. if err != nil {
  129. return nil, err
  130. }
  131. return this.filterSingleResult(s, result, nil)
  132. }
  133. func (this *JointResourceManager) BatchDetach(s *mcclient.ClientSession, mid string, sids []string) []printutils.SubmitResult {
  134. return BatchDo(sids, func(sid string) (jsonutils.JSONObject, error) {
  135. return this.Detach(s, mid, sid, nil)
  136. })
  137. }
  138. func (this *JointResourceManager) BatchDetach2(s *mcclient.ClientSession, mid string, sids []string) []printutils.SubmitResult {
  139. return BatchDo(sids, func(sid string) (jsonutils.JSONObject, error) {
  140. return this.Detach(s, sid, mid, nil)
  141. })
  142. }
  143. func (this *JointResourceManager) Update(s *mcclient.ClientSession, mid, sid string, query jsonutils.JSONObject, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  144. path := fmt.Sprintf("/%s/%s/%s/%s", this.Master.KeyString(), url.PathEscape(mid), this.Slave.KeyString(), url.PathEscape(sid))
  145. if query != nil {
  146. queryStr := query.QueryString()
  147. if len(queryStr) > 0 {
  148. path = fmt.Sprintf("%s?%s", path, queryStr)
  149. }
  150. }
  151. result, err := this._put(s, path, this.params2Body(s, params, this.Keyword), this.Keyword)
  152. if err != nil {
  153. return nil, err
  154. }
  155. return this.filterSingleResult(s, result, nil)
  156. }
  157. func (this *JointResourceManager) Patch(s *mcclient.ClientSession, mid, sid string, query jsonutils.JSONObject, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  158. path := fmt.Sprintf("/%s/%s/%s/%s", this.Master.KeyString(), url.PathEscape(mid), this.Slave.KeyString(), url.PathEscape(sid))
  159. if query != nil {
  160. queryStr := query.QueryString()
  161. if len(queryStr) > 0 {
  162. path = fmt.Sprintf("%s?%s", path, queryStr)
  163. }
  164. }
  165. result, err := this._patch(s, path, this.params2Body(s, params, this.Keyword), this.Keyword)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return this.filterSingleResult(s, result, nil)
  170. }
  171. func (this *JointResourceManager) BatchUpdate(s *mcclient.ClientSession, mid string, sids []string, query jsonutils.JSONObject, params jsonutils.JSONObject) []printutils.SubmitResult {
  172. return BatchDo(sids, func(sid string) (jsonutils.JSONObject, error) {
  173. return this.Update(s, mid, sid, query, params)
  174. })
  175. }
  176. func (this *JointResourceManager) BatchPatch(s *mcclient.ClientSession, mid string, sids []string, query jsonutils.JSONObject, params jsonutils.JSONObject) []printutils.SubmitResult {
  177. return BatchDo(sids, func(sid string) (jsonutils.JSONObject, error) {
  178. return this.Patch(s, mid, sid, query, params)
  179. })
  180. }