You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sync_user.go 4.9 kB

feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
feat: sync user and user_group to flashduty (#1842) * fix build event * fix: append labels * Add the function of batch subscription alert rules (#1825) * add: docker-compose files for logs processing * update: set restart:always * fix: compose-host-network-metric-log * update: regularize * add: batch subscription * add: sql columns for rule_ids and rule_names * add: add migrate of AlertSubscribe * update: Remove redundant codes * fix: The question of 1821 * fix: Optimized for getting rule_ids and rule_names * fix: error handle * fix: add rule_ids for update api * fix: Clear the rule_id to zero when updating * refactor: Compatible with old rule_id * refactor: rename * fix: set rule_id=0 when updating subscription rules --------- Co-authored-by: wdk <wdk_cc@163.com> * feat: sync user and team to flashduty * fix: sync to flashduty * fix: failed to update team change to flashduty * fix: sync default user when create team * chore: delete the generated binary file * refactor: user_group refact * fix: func AddUsers(fdConf *cconf.FlashDuty, appKey string, users []User) error { * fix: remove sync for user in router * fix: user_grroup no change in n9e when put user_group * chore: set default api_url=https://api.flashcat.cloud * chore: refactor user_group * chore: refact codes * chore: set api=https://jira.flashcat.cloud/api for test * chore: set api=https://api.flashcat.cloud * chore: adjust the import order * chore: remove excess code * chore: refact codes * chore: remove excess codes * chore: adjust import order * chore: adjust import order * chore: adjust import order * chore: refact code * chore: optimized codes * code refactor * chore: remove excess code --------- Co-authored-by: ning <710leo@gmail.com> Co-authored-by: wdk <wdk_cc@163.com>
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package flashduty
  2. import (
  3. "errors"
  4. "github.com/ccfos/nightingale/v6/models"
  5. "github.com/ccfos/nightingale/v6/pkg/ctx"
  6. "github.com/toolkits/pkg/logger"
  7. )
  8. func SyncUsersChange(ctx *ctx.Context, dbUsers []*models.User) error {
  9. if !ctx.IsCenter {
  10. return nil
  11. }
  12. appKey, err := models.ConfigsGetFlashDutyAppKey(ctx)
  13. if err != nil {
  14. return err
  15. }
  16. req := make(map[string]interface{})
  17. req["limit"] = 100
  18. userList, err := PostFlashDutyWithResp("/member/list", appKey, req)
  19. if err != nil {
  20. return err
  21. }
  22. total := userList.Total
  23. items := []Item{}
  24. for i := 0; i < total/100+1; i++ {
  25. req["p"] = i
  26. req["limit"] = 100
  27. resp, err := PostFlashDutyWithResp("/member/list", appKey, req)
  28. if err != nil {
  29. return err
  30. }
  31. items = append(items, resp.Items...)
  32. }
  33. dutyUsers := make(map[string]*models.User, len(items))
  34. for i := range items {
  35. user := &models.User{
  36. Username: items[i].MemberName,
  37. Email: items[i].Email,
  38. Phone: items[i].Phone,
  39. }
  40. dutyUsers[user.Username+user.Email] = user
  41. }
  42. dbUsersHas := sliceToMap(dbUsers)
  43. delUsers := diffMap(dutyUsers, dbUsersHas)
  44. fdDelUsers(appKey, delUsers)
  45. addUsers := diffMap(dbUsersHas, dutyUsers)
  46. if err := fdAddUsers(appKey, addUsers); err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func sliceToMap(dbUsers []*models.User) map[string]*models.User {
  52. m := make(map[string]*models.User, len(dbUsers))
  53. for _, user := range dbUsers {
  54. m[user.Username+user.Email] = user
  55. }
  56. return m
  57. }
  58. // in m1 and not in m2
  59. func diffMap(m1, m2 map[string]*models.User) []models.User {
  60. var diff []models.User
  61. for i := range m1 {
  62. if _, ok := m2[i]; !ok {
  63. diff = append(diff, *m1[i])
  64. }
  65. }
  66. return diff
  67. }
  68. type User struct {
  69. Email string `json:"email,omitempty"`
  70. Phone string `json:"phone,omitempty"`
  71. MemberName string `json:"member_name,omitempty"`
  72. Updates Updates `json:"updates,omitempty"`
  73. }
  74. type Updates struct {
  75. Email string `json:"email,omitempty"`
  76. Phone string `json:"phone,omitempty"`
  77. MemberName string `json:"member_name,omitempty"`
  78. CountryCode string `json:"country_code,omitempty"`
  79. }
  80. func (user *User) delMember(appKey string) error {
  81. if user.Email == "" && user.Phone == "" {
  82. return errors.New("phones and email must be selected one of two")
  83. }
  84. return PostFlashDuty("/member/delete", appKey, user)
  85. }
  86. func (user *User) UpdateMember(ctx *ctx.Context) error {
  87. appKey, err := models.ConfigsGetFlashDutyAppKey(ctx)
  88. if err != nil {
  89. return err
  90. }
  91. return PostFlashDuty("/member/info/reset", appKey, user)
  92. }
  93. type Members struct {
  94. Users []User `json:"members"`
  95. }
  96. func (m *Members) addMembers(appKey string) error {
  97. if len(m.Users) == 0 {
  98. return nil
  99. }
  100. validUsers := make([]User, 0, len(m.Users))
  101. for _, user := range m.Users {
  102. if user.Email == "" && (user.Phone != "" && user.MemberName == "" || user.Phone == "") {
  103. logger.Errorf("user(%+v) phone and email must be selected one of two, and the member_name must be added when selecting the phone", user)
  104. } else {
  105. validUsers = append(validUsers, user)
  106. }
  107. }
  108. m.Users = validUsers
  109. return PostFlashDuty("/member/invite", appKey, m)
  110. }
  111. func fdAddUsers(appKey string, users []models.User) error {
  112. fdUsers := usersToFdUsers(users)
  113. members := &Members{
  114. Users: fdUsers,
  115. }
  116. return members.addMembers(appKey)
  117. }
  118. func fdDelUsers(appKey string, users []models.User) {
  119. fdUsers := usersToFdUsers(users)
  120. for _, fdUser := range fdUsers {
  121. if err := fdUser.delMember(appKey); err != nil {
  122. logger.Errorf("failed to delete user: %v", err)
  123. }
  124. }
  125. }
  126. func usersToFdUsers(users []models.User) []User {
  127. fdUsers := make([]User, 0, len(users))
  128. for i := range users {
  129. fdUsers = append(fdUsers, User{
  130. Email: users[i].Email,
  131. Phone: users[i].Phone,
  132. MemberName: users[i].Username,
  133. })
  134. }
  135. return fdUsers
  136. }
  137. func UpdateUser(ctx *ctx.Context, target models.User, email, phone string) {
  138. contact := target.FindSameContact(email, phone)
  139. var flashdutyUser User
  140. var needSync bool
  141. switch contact {
  142. case "email":
  143. flashdutyUser = User{
  144. Email: target.Email,
  145. }
  146. if target.Phone != phone {
  147. needSync = true
  148. flashdutyUser.Updates = Updates{
  149. Phone: phone,
  150. MemberName: target.Username,
  151. CountryCode: "CN",
  152. }
  153. }
  154. case "phone":
  155. flashdutyUser = User{
  156. Phone: target.Phone,
  157. }
  158. if target.Email != email {
  159. needSync = true
  160. flashdutyUser.Updates = Updates{
  161. Email: email,
  162. MemberName: target.Username,
  163. }
  164. }
  165. default:
  166. flashdutyUser = User{
  167. MemberName: target.Username,
  168. }
  169. if target.Email != email {
  170. needSync = true
  171. flashdutyUser.Updates.Email = email
  172. }
  173. if target.Phone != phone {
  174. needSync = true
  175. flashdutyUser.Updates.Phone = phone
  176. flashdutyUser.Updates.CountryCode = "CN"
  177. }
  178. }
  179. if needSync {
  180. err := flashdutyUser.UpdateMember(ctx)
  181. if err != nil {
  182. logger.Errorf("failed to update user: %v", err)
  183. }
  184. }
  185. }