common.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package http
  2. import (
  3. "go-common/app/admin/main/config/model"
  4. bm "go-common/library/net/http/blademaster"
  5. "go-common/library/time"
  6. "strconv"
  7. "strings"
  8. )
  9. func createComConfig(c *bm.Context) {
  10. res := map[string]interface{}{}
  11. v := new(model.CreateComConfigReq)
  12. err := c.Bind(v)
  13. if err != nil {
  14. return
  15. }
  16. name := user(c)
  17. if _, err = svr.AuthApps(c, name, c.Request.Header.Get("Cookie")); err != nil {
  18. res["message"] = "服务树权限不足"
  19. c.JSONMap(res, err)
  20. return
  21. }
  22. conf := &model.CommonConf{}
  23. conf.Operator = name
  24. conf.State = v.State
  25. conf.Comment = v.Comment
  26. conf.Mark = v.Mark
  27. conf.Name = v.Name
  28. c.JSON(nil, svr.CreateComConf(conf, v.Team, v.Env, v.Zone, v.SkipLint))
  29. }
  30. func comValue(c *bm.Context) {
  31. res := map[string]interface{}{}
  32. v := new(model.ComValueReq)
  33. err := c.Bind(v)
  34. if err != nil {
  35. return
  36. }
  37. if _, err = svr.AuthApps(c, user(c), c.Request.Header.Get("Cookie")); err != nil {
  38. res["message"] = "服务树权限不足"
  39. c.JSONMap(res, err)
  40. return
  41. }
  42. c.JSON(svr.ComConfig(v.ConfigID))
  43. }
  44. func configsByTeam(c *bm.Context) {
  45. res := map[string]interface{}{}
  46. v := new(model.ConfigsByTeamReq)
  47. err := c.Bind(v)
  48. if err != nil {
  49. return
  50. }
  51. if _, err = svr.AuthApps(c, user(c), c.Request.Header.Get("Cookie")); err != nil {
  52. res["message"] = "服务树权限不足"
  53. c.JSONMap(res, err)
  54. return
  55. }
  56. c.JSON(svr.ComConfigsByTeam(v.Team, v.Env, v.Zone, v.Ps, v.Pn))
  57. }
  58. func comConfigsByName(c *bm.Context) {
  59. res := map[string]interface{}{}
  60. v := new(model.ComConfigsByNameReq)
  61. err := c.Bind(v)
  62. if err != nil {
  63. return
  64. }
  65. if _, err = svr.AuthApps(c, user(c), c.Request.Header.Get("Cookie")); err != nil {
  66. res["message"] = "服务树权限不足"
  67. c.JSONMap(res, err)
  68. return
  69. }
  70. c.JSON(svr.ComConfigsByName(v.Team, v.Env, v.Zone, v.Name))
  71. }
  72. func updateComConfValue(c *bm.Context) {
  73. res := map[string]interface{}{}
  74. v := new(model.UpdateComConfValueReq)
  75. err := c.Bind(v)
  76. if err != nil {
  77. return
  78. }
  79. user := user(c)
  80. if _, err = svr.AuthApps(c, user, c.Request.Header.Get("Cookie")); err != nil {
  81. res["message"] = "服务树权限不足"
  82. c.JSONMap(res, err)
  83. return
  84. }
  85. conf := &model.CommonConf{}
  86. conf.Mtime = time.Time(v.Mtime)
  87. conf.Mark = v.Mark
  88. conf.ID = v.ID
  89. conf.State = v.State
  90. conf.Comment = v.Comment
  91. conf.Operator = user
  92. conf.Name = v.Name
  93. c.JSON(nil, svr.UpdateComConfValue(conf, v.SkipLint))
  94. }
  95. func namesByTeam(c *bm.Context) {
  96. res := map[string]interface{}{}
  97. v := new(model.NamesByTeamReq)
  98. err := c.Bind(v)
  99. if err != nil {
  100. return
  101. }
  102. if _, err = svr.AuthApps(c, user(c), c.Request.Header.Get("Cookie")); err != nil {
  103. res["message"] = "服务树权限不足"
  104. c.JSONMap(res, err)
  105. return
  106. }
  107. c.JSON(svr.NamesByTeam(v.Team, v.Env, v.Zone))
  108. }
  109. func appByTeam(c *bm.Context) {
  110. v := new(struct {
  111. CommonConfigID int64 `form:"common_config_id" validate:"required"`
  112. })
  113. err := c.Bind(v)
  114. if err != nil {
  115. return
  116. }
  117. tagMap, err := svr.AppByTeam(v.CommonConfigID)
  118. if err != nil {
  119. c.JSON(nil, err)
  120. return
  121. }
  122. c.JSON(tagMap, nil)
  123. }
  124. func tagPush(c *bm.Context) {
  125. v := new(struct {
  126. CommonConfigID int64 `form:"common_config_id" validate:"required"`
  127. Tags string `form:"tags" validate:"required"`
  128. })
  129. err := c.Bind(v)
  130. if err != nil {
  131. return
  132. }
  133. user := user(c)
  134. tagMap, err := svr.AppByTeam(v.CommonConfigID)
  135. if err != nil {
  136. c.JSON(nil, err)
  137. return
  138. }
  139. res := make(map[int64]interface{})
  140. tagIDS := strings.Split(v.Tags, ",")
  141. for _, val := range tagIDS {
  142. val, _ := strconv.ParseInt(val, 10, 64)
  143. if _, ok := tagMap[val]; ok {
  144. err = svr.CommonPush(c, val, v.CommonConfigID, user)
  145. if err == nil {
  146. res[val] = "success"
  147. } else {
  148. res[val] = "fail"
  149. }
  150. } else {
  151. res[val] = "data error"
  152. }
  153. }
  154. c.JSON(res, nil)
  155. }