paas.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "strconv"
  7. "go-common/app/admin/ep/merlin/model"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _genPaasMachines = "/api/merlin/machine/create"
  14. _delPaasMachine = "/api/merlin/machine/free"
  15. _queryPaasMachineStatus = "/api/merlin/machine/status"
  16. _queryPaasMachine = "/api/merlin/machine/detail"
  17. _updatePaasMachineNode = "/api/merlin/machine/update"
  18. _updatePaasMachineSnapshot = "/api/merlin/machine/snapshot"
  19. _queryPaasClusters = "/api/merlin/clusters"
  20. _queryPaasClusterByNetwork = "/api/merlin/clusters/network/"
  21. _auth = "/api/v1/auth"
  22. _authHeader = "X-Authorization-Token"
  23. )
  24. // GenPaasMachines create machine in paas.
  25. func (d *Dao) GenPaasMachines(c context.Context, mc *model.PaasGenMachineRequest) (instances []*model.CreateInstance, err error) {
  26. var (
  27. req *http.Request
  28. res = &model.PaasGenMachineResponse{}
  29. )
  30. if req, err = d.newPaasRequest(c, http.MethodPost, _genPaasMachines, mc); err != nil {
  31. return
  32. }
  33. if err = d.httpClient.Do(c, req, &res); err != nil {
  34. log.Error("gen paas machine url(%s) err(%v)", _genPaasMachines, err)
  35. err = ecode.MerlinPaasRequestErr
  36. return
  37. }
  38. if err = res.CheckStatus(); err != nil {
  39. return
  40. }
  41. instances = res.Data
  42. return
  43. }
  44. // DelPaasMachine delete machine in paas.
  45. func (d *Dao) DelPaasMachine(c context.Context, pqadmr *model.PaasQueryAndDelMachineRequest) (instance *model.ReleaseInstance, err error) {
  46. var (
  47. req *http.Request
  48. res = &model.PaasDelMachineResponse{}
  49. )
  50. if req, err = d.newPaasRequest(c, http.MethodPost, _delPaasMachine, pqadmr); err != nil {
  51. return
  52. }
  53. if err = d.httpClient.Do(c, req, &res); err != nil {
  54. log.Error("delete paas machine url(%s) err(%v)", _delPaasMachine, err)
  55. err = ecode.MerlinPaasRequestErr
  56. return
  57. }
  58. if err = res.CheckStatus(); err != nil {
  59. return
  60. }
  61. instance = &res.Data
  62. return
  63. }
  64. // QueryPaasMachineStatus query status of machine in paas.
  65. func (d *Dao) QueryPaasMachineStatus(c context.Context, pqadmr *model.PaasQueryAndDelMachineRequest) (machineStatus *model.MachineStatus, err error) {
  66. var (
  67. req *http.Request
  68. res = &model.PaasQueryMachineStatusResponse{}
  69. )
  70. if req, err = d.newPaasRequest(c, http.MethodPost, _queryPaasMachineStatus, pqadmr); err != nil {
  71. return
  72. }
  73. if err = d.httpClient.Do(c, req, &res); err != nil {
  74. log.Error("query paas machine status url(%s) err(%v)", _queryPaasMachineStatus, err)
  75. err = ecode.MerlinPaasRequestErr
  76. return
  77. }
  78. if err = res.CheckStatus(); err != nil {
  79. return
  80. }
  81. machineStatus = &res.Data
  82. return
  83. }
  84. // SnapshotPaasMachineStatus Snapshot Paas Machine Status.
  85. func (d *Dao) SnapshotPaasMachineStatus(c context.Context, pqadmr *model.PaasQueryAndDelMachineRequest) (status int, err error) {
  86. var (
  87. req *http.Request
  88. res = &model.PaasSnapshotMachineResponse{}
  89. )
  90. if req, err = d.newPaasRequest(c, http.MethodPost, _updatePaasMachineSnapshot, pqadmr); err != nil {
  91. return
  92. }
  93. if err = d.httpClient.Do(c, req, &res); err != nil {
  94. log.Error("snapshot machine status url(%s) err(%v)", _updatePaasMachineSnapshot, err)
  95. err = ecode.MerlinPaasRequestErr
  96. return
  97. }
  98. if err = res.CheckStatus(); err != nil {
  99. return
  100. }
  101. status = res.Status
  102. return
  103. }
  104. // QueryPaasMachine query detail information of machine in paas.
  105. func (d *Dao) QueryPaasMachine(c context.Context, pqadmr *model.PaasQueryAndDelMachineRequest) (md *model.PaasMachineDetail, err error) {
  106. var (
  107. req *http.Request
  108. res = &model.PaasQueryMachineResponse{}
  109. )
  110. if req, err = d.newPaasRequest(c, http.MethodPost, _queryPaasMachine, pqadmr); err != nil {
  111. return
  112. }
  113. if err = d.httpClient.Do(c, req, &res); err != nil {
  114. log.Error("query paas machine url(%s) err(%v)", _queryPaasMachine, err)
  115. err = ecode.MerlinPaasRequestErr
  116. return
  117. }
  118. if err = res.CheckStatus(); err != nil {
  119. return
  120. }
  121. md = &res.Data
  122. return
  123. }
  124. // QueryClusters query cluster information in paas.
  125. func (d *Dao) QueryClusters(c context.Context) (clusters []*model.Cluster, err error) {
  126. var (
  127. req *http.Request
  128. res = &model.PaasQueryClustersResponse{}
  129. )
  130. if req, err = d.newPaasRequest(c, http.MethodGet, _queryPaasClusters, nil); err != nil {
  131. return
  132. }
  133. if err = d.httpClient.Do(c, req, &res); err != nil {
  134. log.Error("d.httpSearch url(%s) error(%v)", d.c.Paas.Host+"?"+_queryPaasClusters, err)
  135. err = ecode.MerlinPaasRequestErr
  136. return
  137. }
  138. if err = res.CheckStatus(); err != nil {
  139. return
  140. }
  141. clusters = res.Data.Items
  142. return
  143. }
  144. // QueryCluster query cluster information in paas by giving network.
  145. func (d *Dao) QueryCluster(c context.Context, netWordID int64) (cluster *model.Cluster, err error) {
  146. var (
  147. req *http.Request
  148. res = &model.PaasQueryClusterResponse{}
  149. )
  150. if req, err = d.newPaasRequest(c, http.MethodGet, _queryPaasClusterByNetwork+strconv.FormatInt(netWordID, 10), nil); err != nil {
  151. log.Error("http new request err(%v)", err)
  152. return
  153. }
  154. if err = d.httpClient.Do(c, req, &res); err != nil {
  155. log.Error("d.httpSearch url(%s) error(%v)", d.c.Paas.Host+"?"+_queryPaasClusters, err)
  156. err = ecode.MerlinPaasRequestErr
  157. return
  158. }
  159. if err = res.CheckStatus(); err != nil {
  160. return
  161. }
  162. cluster = res.Data
  163. return
  164. }
  165. // UpdatePaasMachineNode update paas machine node.
  166. func (d *Dao) UpdatePaasMachineNode(c context.Context, pumnr *model.PaasUpdateMachineNodeRequest) (data string, err error) {
  167. var (
  168. req *http.Request
  169. res = &model.PaasUpdateMachineNodeResponse{}
  170. )
  171. if req, err = d.newPaasRequest(c, http.MethodPost, _updatePaasMachineNode, pumnr); err != nil {
  172. log.Error("http new request err(%v)", err)
  173. return
  174. }
  175. if err = d.httpClient.Do(c, req, &res); err != nil {
  176. log.Error("d.httpSearch url(%s) error(%v)", d.c.Paas.Host+_updatePaasMachineNode, err)
  177. err = ecode.MerlinPaasRequestErr
  178. return
  179. }
  180. if err = res.CheckStatus(); err != nil {
  181. return
  182. }
  183. data = res.Data
  184. return
  185. }
  186. func (d *Dao) authPaas(c context.Context) (token string, err error) {
  187. var (
  188. req *http.Request
  189. res = &model.PaasAuthResponse{}
  190. authRequest = model.PaasAuthRequest{
  191. APIToken: d.c.Paas.Token,
  192. PlatformID: "merlin",
  193. }
  194. )
  195. if req, err = d.newRequest(http.MethodPost, d.c.Paas.Host+_auth, authRequest); err != nil {
  196. return
  197. }
  198. if err = d.httpClient.Do(c, req, &res); err != nil {
  199. log.Error("query paas machine url(%s) err(%v)", _auth, err)
  200. err = ecode.MerlinPaasRequestErr
  201. return
  202. }
  203. if err = res.CheckStatus(); err != nil {
  204. return
  205. }
  206. token = res.Data.Token
  207. return
  208. }
  209. // paasToken TODO:当前放在dao层有点不规范,放在service层,封装上又不如这样更好,后续再考虑一下.
  210. func (d *Dao) paasToken(c context.Context) (authToken string, err error) {
  211. var (
  212. item *memcache.Item
  213. conn = d.mc.Get(c)
  214. )
  215. defer conn.Close()
  216. if item, err = conn.Get(d.c.Paas.Token); err == nil {
  217. if err = json.Unmarshal(item.Value, &authToken); err != nil {
  218. log.Error("Json unmarshal err(%v)", err)
  219. }
  220. return
  221. }
  222. if authToken, err = d.authPaas(c); err != nil {
  223. return
  224. }
  225. item = &memcache.Item{Key: d.c.Paas.Token, Object: authToken, Flags: memcache.FlagJSON, Expiration: d.expire}
  226. d.tokenCacheSave(c, item)
  227. return
  228. }
  229. func (d *Dao) newPaasRequest(c context.Context, method, uri string, v interface{}) (req *http.Request, err error) {
  230. var authToken string
  231. if authToken, err = d.paasToken(c); err != nil {
  232. return
  233. }
  234. if req, err = d.newRequest(method, d.c.Paas.Host+uri, v); err != nil {
  235. return
  236. }
  237. req.Header.Set(_authHeader, authToken)
  238. return
  239. }