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 2.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, cacheUsers map[int64]*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. dbUsersHas := sliceToMap(dbUsers)
  17. addUsers := diffMap(dbUsersHas, cacheUsers)
  18. if err := fdAddUsers(appKey, addUsers); err != nil {
  19. return err
  20. }
  21. delUsers := diffMap(cacheUsers, dbUsersHas)
  22. fdDelUsers(appKey, delUsers)
  23. return nil
  24. }
  25. func sliceToMap(dbUsers []*models.User) map[int64]*models.User {
  26. m := make(map[int64]*models.User, len(dbUsers))
  27. for _, user := range dbUsers {
  28. m[user.Id] = user
  29. }
  30. return m
  31. }
  32. // in m1 and not in m2
  33. func diffMap(m1, m2 map[int64]*models.User) []models.User {
  34. var diff []models.User
  35. for i := range m1 {
  36. if _, ok := m2[i]; !ok {
  37. diff = append(diff, *m1[i])
  38. }
  39. }
  40. return diff
  41. }
  42. type User struct {
  43. Email string `json:"email"`
  44. Phone string `json:"phone"`
  45. CountryCode string `json:"country_code"`
  46. MemberName string `json:"member_name"`
  47. RoleIds []int `json:"role_ids"`
  48. }
  49. func (user *User) delMember(appKey string) error {
  50. if user.Email == "" && user.Phone == "" {
  51. return errors.New("phones and email must be selected one of two")
  52. }
  53. return PostFlashDuty("/member/delete", appKey, user)
  54. }
  55. type Members struct {
  56. Users []User `json:"members"`
  57. }
  58. func (m *Members) addMembers(appKey string) error {
  59. if len(m.Users) == 0 {
  60. return nil
  61. }
  62. validUsers := make([]User, 0, len(m.Users))
  63. for _, user := range m.Users {
  64. if user.Email == "" && (user.Phone == "" || user.MemberName == "") {
  65. 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)
  66. } else {
  67. validUsers = append(validUsers, user)
  68. }
  69. }
  70. m.Users = validUsers
  71. return PostFlashDuty("/member/invite", appKey, m)
  72. }
  73. func fdAddUsers(appKey string, users []models.User) error {
  74. fdUsers := usersToFdUsers(users)
  75. members := &Members{
  76. Users: fdUsers,
  77. }
  78. return members.addMembers(appKey)
  79. }
  80. func fdDelUsers(appKey string, users []models.User) {
  81. fdUsers := usersToFdUsers(users)
  82. for _, fdUser := range fdUsers {
  83. if err := fdUser.delMember(appKey); err != nil {
  84. logger.Errorf("failed to delete user: %v", err)
  85. }
  86. }
  87. }
  88. func usersToFdUsers(users []models.User) []User {
  89. fdUsers := make([]User, 0, len(users))
  90. for i := range users {
  91. fdUsers = append(fdUsers, User{
  92. Email: users[i].Email,
  93. Phone: users[i].Phone,
  94. MemberName: users[i].Username,
  95. })
  96. }
  97. return fdUsers
  98. }