request.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 handler
  15. import (
  16. "context"
  17. "net/http"
  18. "github.com/pkg/errors"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/appctx"
  21. "yunion.io/x/pkg/utils"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/mcclient/auth"
  26. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  27. )
  28. type Request struct {
  29. ctx context.Context
  30. w http.ResponseWriter
  31. r *http.Request
  32. err error
  33. session *mcclient.ClientSession
  34. params map[string]string
  35. query jsonutils.JSONObject
  36. body jsonutils.JSONObject
  37. mod1 modulebase.Manager
  38. mod2 modulebase.Manager
  39. mod3 modulebase.Manager
  40. }
  41. func newRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) *Request {
  42. params := appctx.AppContextParams(ctx)
  43. token := AppContextToken(ctx)
  44. session := auth.GetSession(ctx, token, FetchRegion(r))
  45. query, err := jsonutils.ParseQueryString(r.URL.RawQuery)
  46. req := &Request{
  47. ctx: ctx,
  48. w: w,
  49. r: r,
  50. session: session,
  51. params: params,
  52. query: query,
  53. }
  54. if err != nil {
  55. req.err = errors.Errorf("Parse query string %s failed: %v", r.URL.RawQuery, err)
  56. return req
  57. }
  58. var body jsonutils.JSONObject = nil
  59. if utils.IsInStringArray(r.Method, []string{PUT, POST, DELETE, PATCH}) {
  60. body, err = appsrv.FetchJSON(r)
  61. if err != nil {
  62. req.err = errors.Errorf("failed to decode JSON request body: %v", err)
  63. return req
  64. }
  65. req.body = body
  66. }
  67. return req
  68. }
  69. func (req *Request) Error() error {
  70. return req.err
  71. }
  72. func (req *Request) Session() *mcclient.ClientSession {
  73. return req.session
  74. }
  75. func (req *Request) Query() jsonutils.JSONObject {
  76. return req.query
  77. }
  78. func (req *Request) Body() jsonutils.JSONObject {
  79. return req.body
  80. }
  81. func (req *Request) Params() map[string]string {
  82. return req.params
  83. }
  84. func (req *Request) ResName() string {
  85. return req.params[ResName]
  86. }
  87. func (req *Request) ResID() string {
  88. return req.params[ResID]
  89. }
  90. func (req *Request) ResName2() string {
  91. return req.params[ResName2]
  92. }
  93. func (req *Request) ResID2() string {
  94. return req.params[ResID2]
  95. }
  96. func (req *Request) ResName3() string {
  97. return req.params[ResName3]
  98. }
  99. func (req *Request) ResID3() string {
  100. return req.params[ResID3]
  101. }
  102. func (req *Request) Action() string {
  103. return req.params[Action]
  104. }
  105. func (req *Request) Spec() string {
  106. return req.params[Spec]
  107. }
  108. func (req *Request) Mod1() modulebase.Manager {
  109. return req.mod1
  110. }
  111. func (req *Request) Mod2() modulebase.Manager {
  112. return req.mod2
  113. }
  114. func (req *Request) Mod3() modulebase.Manager {
  115. return req.mod3
  116. }
  117. func (req Request) findMod(resKey string) (modulebase.Manager, error) {
  118. resName := req.params[resKey]
  119. module, err := modulebase.GetModule(req.session, resName)
  120. if err != nil {
  121. return nil, errors.Errorf("found module by %s: %v", resName, err)
  122. }
  123. if module == nil {
  124. return nil, httperrors.NewNotFoundError("resource %s module not exists", resName)
  125. }
  126. return module, nil
  127. }
  128. func (req *Request) WithMod1() *Request {
  129. if req.err != nil {
  130. return req
  131. }
  132. mod, err := req.findMod(ResName)
  133. if err != nil {
  134. req.err = err
  135. return req
  136. }
  137. req.mod1 = mod
  138. return req
  139. }
  140. func (req *Request) WithMod2() *Request {
  141. if req.err != nil {
  142. return req
  143. }
  144. mod, err := req.findMod(ResName2)
  145. if err != nil {
  146. req.err = err
  147. return req
  148. }
  149. req.mod2 = mod
  150. return req
  151. }
  152. func (req *Request) WithMod3() *Request {
  153. if req.err != nil {
  154. return req
  155. }
  156. mod, err := req.findMod(ResName3)
  157. if err != nil {
  158. req.err = err
  159. return req
  160. }
  161. req.mod3 = mod
  162. return req
  163. }