app.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package http
  2. import (
  3. "go-common/app/admin/main/config/model"
  4. "go-common/library/ecode"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "strconv"
  8. "strings"
  9. )
  10. func updateToken(c *bm.Context) {
  11. v := new(model.UpdateTokenReq)
  12. err := c.Bind(v)
  13. if err != nil {
  14. return
  15. }
  16. if _, err = svr.AuthApp(c, user(c), c.Request.Header.Get("Cookie"), v.TreeID); err != nil {
  17. c.JSON(nil, err)
  18. return
  19. }
  20. if err = svr.UpdateToken(c, v.Env, v.Zone, v.TreeID); err != nil {
  21. c.JSON(nil, err)
  22. return
  23. }
  24. c.JSON(nil, err)
  25. }
  26. func create(c *bm.Context) {
  27. res := map[string]interface{}{}
  28. v := new(model.CreateReq)
  29. err := c.Bind(v)
  30. if err != nil {
  31. return
  32. }
  33. if _, err = svr.AuthApp(c, user(c), c.Request.Header.Get("Cookie"), v.TreeID); err != nil {
  34. res["message"] = "服务树权限不足"
  35. c.JSONMap(res, err)
  36. return
  37. }
  38. creates := []string{"dev", "fat1", "uat", "pre", "prod"}
  39. for _, val := range creates {
  40. if err = svr.CreateApp(v.AppName, val, model.DefaultZone, v.TreeID); err != nil {
  41. res["message"] = "创建app失败"
  42. c.JSONMap(res, err)
  43. return
  44. }
  45. }
  46. c.JSON(nil, err)
  47. }
  48. func appList(c *bm.Context) {
  49. res := map[string]interface{}{}
  50. v := new(model.AppListReq)
  51. err := c.Bind(v)
  52. if err != nil {
  53. return
  54. }
  55. nodes, err := svr.AuthApps(c, user(c), c.Request.Header.Get("Cookie"))
  56. if err != nil {
  57. res["message"] = "服务树权限不足"
  58. c.JSONMap(res, err)
  59. return
  60. }
  61. app, err := svr.AppList(c, v.Bu, v.Team, v.AppName, model.DefaultEnv, model.DefaultZone, v.Ps, v.Pn, nodes, v.Status)
  62. if err != nil {
  63. res["message"] = "数据获取失败"
  64. c.JSONMap(res, err)
  65. return
  66. }
  67. result := app
  68. c.JSON(result, nil)
  69. }
  70. func envsByTeam(c *bm.Context) {
  71. res := map[string]interface{}{}
  72. v := new(model.EnvsByTeamReq)
  73. err := c.Bind(v)
  74. if err != nil {
  75. return
  76. }
  77. nodes, err := svr.AuthApps(c, user(c), c.Request.Header.Get("Cookie"))
  78. if err != nil {
  79. res["message"] = "服务树权限不足"
  80. c.JSONMap(res, err)
  81. return
  82. }
  83. data, err := svr.EnvsByTeam(c, v.AppName, v.Zone, nodes)
  84. if err != nil {
  85. c.JSON(nil, err)
  86. return
  87. }
  88. result := data
  89. c.JSON(result, nil)
  90. }
  91. func envs(c *bm.Context) {
  92. res := map[string]interface{}{}
  93. v := new(model.EnvsReq)
  94. err := c.Bind(v)
  95. if err != nil {
  96. return
  97. }
  98. user := user(c)
  99. nodes, err := svr.AuthApps(c, user, c.Request.Header.Get("Cookie"))
  100. if err != nil {
  101. res["message"] = "服务树权限不足"
  102. c.JSONMap(res, err)
  103. return
  104. }
  105. c.JSON(svr.Envs(c, user, v.AppName, v.Zone, v.TreeID, nodes))
  106. }
  107. func nodeTree(c *bm.Context) {
  108. res := map[string]interface{}{}
  109. v := new(model.NodeTreeReq)
  110. err := c.Bind(v)
  111. if err != nil {
  112. return
  113. }
  114. cookie := c.Request.Header.Get("Cookie")
  115. user := user(c)
  116. nodes, err := svr.AuthApps(c, user, cookie)
  117. if err != nil {
  118. res["message"] = "服务树权限不足"
  119. c.JSONMap(res, err)
  120. return
  121. }
  122. c.JSON(svr.Node(c, user, v.Node, v.Team, cookie, nodes))
  123. }
  124. func zoneCopy(c *bm.Context) {
  125. res := map[string]interface{}{}
  126. v := new(model.ZoneCopyReq)
  127. err := c.Bind(v)
  128. if err != nil {
  129. return
  130. }
  131. if v.From == v.To {
  132. res["message"] = "来源机房和目标机房不能是同一个"
  133. c.JSONMap(res, ecode.RequestErr)
  134. return
  135. }
  136. if _, err = svr.AuthApp(c, user(c), c.Request.Header.Get("Cookie"), v.TreeID); err != nil {
  137. res["message"] = "服务树权限不足"
  138. c.JSONMap(res, err)
  139. return
  140. }
  141. if err = svr.ZoneCopy(c, v.AppName, v.From, v.To, v.TreeID); err != nil {
  142. res["message"] = "拷贝失败"
  143. c.JSONMap(res, err)
  144. return
  145. }
  146. c.JSON(nil, err)
  147. }
  148. func casterEnvs(c *bm.Context) {
  149. v := new(model.CasterEnvsReq)
  150. err := c.Bind(v)
  151. if err != nil {
  152. return
  153. }
  154. if v.Auth != "caster_envs_all" {
  155. c.JSON(nil, ecode.RequestErr)
  156. return
  157. }
  158. c.JSON(svr.CasterEnvs(v.Zone, v.TreeID))
  159. }
  160. func rename(c *bm.Context) {
  161. res := map[string]interface{}{}
  162. v := new(struct {
  163. TreeID int64 `form:"tree_id" validate:"required"`
  164. })
  165. err := c.Bind(v)
  166. if err != nil {
  167. return
  168. }
  169. if _, err = svr.AuthApp(c, user(c), c.Request.Header.Get("Cookie"), v.TreeID); err != nil {
  170. res["message"] = "服务树权限不足"
  171. c.JSONMap(res, err)
  172. return
  173. }
  174. c.JSON(nil, svr.AppRename(v.TreeID, user(c), c.Request.Header.Get("Cookie")))
  175. }
  176. func getApps(c *bm.Context) {
  177. v := new(struct {
  178. Name string `form:"name" validate:"required"`
  179. Env string `form:"env" validate:"required"`
  180. })
  181. err := c.Bind(v)
  182. if err != nil {
  183. return
  184. }
  185. apps, err := svr.GetApps(v.Env)
  186. if err != nil {
  187. c.JSON(nil, err)
  188. return
  189. }
  190. var appIDS []int64
  191. for _, val := range apps {
  192. appIDS = append(appIDS, val.ID)
  193. }
  194. if len(appIDS) == 0 {
  195. c.JSON(nil, ecode.RequestErr)
  196. return
  197. }
  198. builds, err := svr.AllBuilds(appIDS)
  199. if err != nil {
  200. c.JSON(nil, err)
  201. return
  202. }
  203. var tagIDS []int64
  204. for _, val := range builds {
  205. tagIDS = append(tagIDS, val.TagID)
  206. }
  207. tags, err := svr.GetConfigIDS(tagIDS)
  208. if err != nil {
  209. c.JSON(nil, err)
  210. return
  211. }
  212. var configIDS []int64
  213. for _, val := range tags {
  214. tmpIDs := strings.Split(val.ConfigIDs, ",")
  215. for _, vv := range tmpIDs {
  216. id, err := strconv.ParseInt(vv, 10, 64)
  217. if err != nil {
  218. log.Error("strconv.ParseInt() error(%v)", err)
  219. return
  220. }
  221. configIDS = append(configIDS, id)
  222. }
  223. }
  224. var appids []int64
  225. var appslist []*model.App
  226. var names []string
  227. if len(configIDS) > 0 {
  228. configs, err := svr.GetConfigs(configIDS, v.Name)
  229. if err != nil {
  230. c.JSON(nil, err)
  231. return
  232. }
  233. for _, val := range configs {
  234. appids = append(appids, val.AppID)
  235. }
  236. appslist, err = svr.IdsGetApps(appids)
  237. if err != nil {
  238. c.JSON(nil, err)
  239. return
  240. }
  241. for _, val := range appslist {
  242. names = append(names, val.Name)
  243. }
  244. }
  245. c.JSON(names, nil)
  246. }
  247. func upAppStatus(c *bm.Context) {
  248. res := map[string]interface{}{}
  249. v := new(model.AppStatusReq)
  250. err := c.Bind(v)
  251. if err != nil {
  252. return
  253. }
  254. if !(v.Status == model.StatusShow || v.Status == model.StatusHidden) {
  255. c.JSON(nil, ecode.RequestErr)
  256. return
  257. }
  258. _, err = svr.AuthApps(c, user(c), c.Request.Header.Get("Cookie"))
  259. if err != nil {
  260. res["message"] = "服务树权限不足"
  261. c.JSONMap(res, err)
  262. return
  263. }
  264. c.JSON(nil, svr.UpAppStatus(c, v.Status, v.TreeID))
  265. }