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_group.go 3.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. type UserGroupSyncer struct {
  9. ctx *ctx.Context
  10. ug *models.UserGroup
  11. appKey string
  12. }
  13. func NewUserGroupSyncer(ctx *ctx.Context, ug *models.UserGroup) (*UserGroupSyncer, error) {
  14. appKey, err := models.ConfigsGetFlashDutyAppKey(ctx)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return &UserGroupSyncer{
  19. ctx: ctx,
  20. ug: ug,
  21. appKey: appKey,
  22. }, nil
  23. }
  24. func (ugs *UserGroupSyncer) SyncUGAdd() error {
  25. return ugs.syncTeamMember()
  26. }
  27. func (ugs *UserGroupSyncer) SyncUGPut(oldUGName string) error {
  28. if err := ugs.syncTeamMember(); err != nil {
  29. return err
  30. }
  31. if oldUGName != ugs.ug.Name {
  32. if err := ugs.SyncUGDel(oldUGName); err != nil {
  33. return err
  34. }
  35. }
  36. return nil
  37. }
  38. func (ugs *UserGroupSyncer) SyncUGDel(ugName string) error {
  39. fdt := Team{
  40. TeamName: ugName,
  41. }
  42. err := fdt.DelTeam(ugs.appKey)
  43. return err
  44. }
  45. func (ugs *UserGroupSyncer) SyncMembersAdd() error {
  46. return ugs.syncTeamMember()
  47. }
  48. func (ugs *UserGroupSyncer) SyncMembersDel() error {
  49. return ugs.syncTeamMember()
  50. }
  51. func (ugs *UserGroupSyncer) syncTeamMember() error {
  52. uids, err := models.MemberIds(ugs.ctx, ugs.ug.Id)
  53. if err != nil {
  54. return err
  55. }
  56. users, err := models.UserGetsByIds(ugs.ctx, uids)
  57. if err != nil {
  58. return err
  59. }
  60. toDutyErr := ugs.addMemberToFDTeam(users)
  61. if toDutyErr != nil {
  62. logger.Warningf("failed to sync user group %s %v to flashduty's team: %v", ugs.ug.Name, users, toDutyErr)
  63. }
  64. return err
  65. }
  66. func (ugs *UserGroupSyncer) addMemberToFDTeam(users []models.User) error {
  67. if err := fdAddUsers(ugs.appKey, users); err != nil {
  68. return err
  69. }
  70. emails := make([]string, 0)
  71. phones := make([]string, 0)
  72. for _, user := range users {
  73. if user.Email != "" {
  74. emails = append(emails, user.Email)
  75. } else if user.Phone != "" {
  76. phones = append(phones, user.Phone)
  77. } else {
  78. logger.Warningf("The user %s has no email and phone, and failed to sync to flashduty's team", user.Username)
  79. }
  80. }
  81. fdt := Team{
  82. TeamName: ugs.ug.Name,
  83. Emails: emails,
  84. Phones: phones,
  85. }
  86. err := fdt.UpdateTeam(ugs.appKey)
  87. return err
  88. }
  89. type Team struct {
  90. TeamName string `json:"team_name"`
  91. ResetIfNameExist bool `json:"reset_if_name_exist"`
  92. Description string `json:"description"`
  93. Emails []string `json:"emails"`
  94. Phones []string `json:"phones"`
  95. }
  96. func (t *Team) AddTeam(appKey string) error {
  97. if t.TeamName == "" {
  98. return errors.New("team_name must be set")
  99. }
  100. return PostFlashDuty("/team/upsert", appKey, t)
  101. }
  102. func (t *Team) UpdateTeam(appKey string) error {
  103. t.ResetIfNameExist = true
  104. err := t.AddTeam(appKey)
  105. return err
  106. }
  107. func (t *Team) DelTeam(appKey string) error {
  108. if t.TeamName == "" {
  109. return errors.New("team_name must be set")
  110. }
  111. return PostFlashDuty("/team/delete", appKey, t)
  112. }
  113. func NeedSyncTeam(ctx *ctx.Context) bool {
  114. configs, err := models.ConfigsSelectByCkey(ctx, "flashduty_sync_team")
  115. if err != nil {
  116. logger.Warningf("failed to query flashduty_sync_team: %v", err)
  117. return false
  118. }
  119. if len(configs) == 0 || configs[0].Cval == "" {
  120. return false
  121. }
  122. return configs[0].Cval == "true"
  123. }
  124. func NeedSyncUser(ctx *ctx.Context) bool {
  125. configs, err := models.ConfigsSelectByCkey(ctx, "flashduty_app_key")
  126. if err != nil {
  127. logger.Warningf("failed to query flashduty_app_key: %v", err)
  128. return false
  129. }
  130. if len(configs) == 0 || configs[0].Cval == "" {
  131. return false
  132. }
  133. return true
  134. }