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.

post.go 3.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package flashduty
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "time"
  10. "github.com/ccfos/nightingale/v6/center/cconf"
  11. "github.com/toolkits/pkg/logger"
  12. )
  13. var (
  14. Api string
  15. Headers map[string]string
  16. Timeout time.Duration
  17. )
  18. func Init(fdConf cconf.FlashDuty) {
  19. Api = fdConf.Api
  20. Headers = make(map[string]string)
  21. Headers = fdConf.Headers
  22. if fdConf.Timeout == 0 {
  23. Timeout = 5 * time.Second
  24. } else {
  25. Timeout = fdConf.Timeout * time.Millisecond
  26. }
  27. }
  28. type dutyResp[T any] struct {
  29. RequestId string `json:"request_id"`
  30. Data T `json:"data"`
  31. Error struct {
  32. Code string `json:"code"`
  33. Message string `json:"message"`
  34. } `json:"error"`
  35. }
  36. type TeamInfo struct {
  37. TeamID int64 `json:"team_id"`
  38. TeamName string `json:"team_name"`
  39. Description string `json:"description"`
  40. CreatedAt int64 `json:"created_at"`
  41. UpdatedAt int64 `json:"updated_at"`
  42. UpdatedBy int64 `json:"updated_by"`
  43. UpdatedByName string `json:"updated_by_name"`
  44. CreatorID int64 `json:"creator_id"`
  45. RefID string `json:"ref_id"`
  46. PersonIDs []int64 `json:"person_ids"`
  47. }
  48. type Data struct {
  49. P int `json:"p"`
  50. Limit int `json:"limit"`
  51. Total int `json:"total"`
  52. Items []Item `json:"items"`
  53. }
  54. type Item struct {
  55. MemberID int `json:"member_id"`
  56. MemberName string `json:"member_name"`
  57. Phone string `json:"phone"`
  58. Email string `json:"email"`
  59. EmailVerified string `json:"email_verified"`
  60. RefID string `json:"ref_id"`
  61. }
  62. func PostFlashDuty(path string, appKey string, body interface{}) error {
  63. _, err := PostFlashDutyWithResp[Data](path, appKey, body)
  64. return err
  65. }
  66. func PostFlashDutyWithResp[T any](path string, appKey string, body interface{}) (T, error) {
  67. urlParams := url.Values{}
  68. urlParams.Add("app_key", appKey)
  69. var url string
  70. if Api != "" {
  71. url = fmt.Sprintf("%s%s?%s", Api, path, urlParams.Encode())
  72. } else {
  73. url = fmt.Sprintf("%s%s?%s", "https://api.flashcat.cloud", path, urlParams.Encode())
  74. }
  75. response, code, err := PostJSON(url, Timeout, Headers, body)
  76. req, _ := json.Marshal(body)
  77. logger.Infof("flashduty post: url=%s, req=%s; response=%s, code=%d", url, string(req), string(response), code)
  78. var resp dutyResp[T]
  79. if err == nil {
  80. e := json.Unmarshal(response, &resp)
  81. if e == nil && resp.Error.Message != "" {
  82. err = fmt.Errorf("flashduty post error: %s", resp.Error.Message)
  83. }
  84. }
  85. return resp.Data, err
  86. }
  87. func PostJSON(url string, timeout time.Duration, headers map[string]string, v interface{}, retries ...int) (response []byte, code int, err error) {
  88. var bs []byte
  89. bs, err = json.Marshal(v)
  90. if err != nil {
  91. return
  92. }
  93. bf := bytes.NewBuffer(bs)
  94. client := http.Client{
  95. Timeout: timeout,
  96. }
  97. req, err := http.NewRequest("POST", url, bf)
  98. if err != nil {
  99. return
  100. }
  101. req.Header.Set("Content-Type", "application/json")
  102. if len(headers) > 0 {
  103. for k, v := range headers {
  104. req.Header.Set(k, v)
  105. }
  106. }
  107. var resp *http.Response
  108. if len(retries) > 0 {
  109. for i := 0; i < retries[0]; i++ {
  110. resp, err = client.Do(req)
  111. if err == nil {
  112. break
  113. }
  114. tryagain := ""
  115. if i+1 < retries[0] {
  116. tryagain = " try again"
  117. }
  118. logger.Warningf("failed to curl %s error: %s"+tryagain, url, err)
  119. if i+1 < retries[0] {
  120. time.Sleep(time.Millisecond * 200)
  121. }
  122. }
  123. } else {
  124. resp, err = client.Do(req)
  125. }
  126. if err != nil {
  127. return
  128. }
  129. code = resp.StatusCode
  130. if resp.Body != nil {
  131. defer resp.Body.Close()
  132. response, err = io.ReadAll(resp.Body)
  133. }
  134. return
  135. }