2
0

emqx_cloud.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package mq
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "github.com/gravitl/netmaker/servercfg"
  10. )
  11. type EmqxCloud struct {
  12. URL string
  13. AppID string
  14. AppSecret string
  15. }
  16. type userCreateReq struct {
  17. UserName string `json:"username"`
  18. Password string `json:"password"`
  19. }
  20. func (e *EmqxCloud) GetType() servercfg.Emqxdeploy { return servercfg.EmqxCloudDeploy }
  21. func (e *EmqxCloud) CreateEmqxUser(username, pass string) error {
  22. payload := userCreateReq{
  23. UserName: username,
  24. Password: pass,
  25. }
  26. data, _ := json.Marshal(payload)
  27. client := &http.Client{}
  28. req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/auth_username", e.URL), strings.NewReader(string(data)))
  29. if err != nil {
  30. return err
  31. }
  32. req.SetBasicAuth(e.AppID, e.AppSecret)
  33. req.Header.Add("Content-Type", "application/json")
  34. res, err := client.Do(req)
  35. if err != nil {
  36. return err
  37. }
  38. defer res.Body.Close()
  39. body, err := io.ReadAll(res.Body)
  40. if err != nil {
  41. return err
  42. }
  43. if res.StatusCode != http.StatusOK {
  44. return errors.New("request failed " + string(body))
  45. }
  46. return nil
  47. }
  48. func (e *EmqxCloud) CreateEmqxUserforServer() error {
  49. payload := userCreateReq{
  50. UserName: servercfg.GetMqUserName(),
  51. Password: servercfg.GetMqPassword(),
  52. }
  53. data, _ := json.Marshal(payload)
  54. client := &http.Client{}
  55. req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/auth_username", e.URL), strings.NewReader(string(data)))
  56. if err != nil {
  57. return err
  58. }
  59. req.SetBasicAuth(e.AppID, e.AppSecret)
  60. req.Header.Add("Content-Type", "application/json")
  61. res, err := client.Do(req)
  62. if err != nil {
  63. return err
  64. }
  65. defer res.Body.Close()
  66. body, err := io.ReadAll(res.Body)
  67. if err != nil {
  68. return err
  69. }
  70. if res.StatusCode != http.StatusOK {
  71. return errors.New("request failed " + string(body))
  72. }
  73. return nil
  74. }
  75. func (e *EmqxCloud) CreateEmqxDefaultAuthenticator() error { return nil } // ignore
  76. func (e *EmqxCloud) CreateEmqxDefaultAuthorizer() error { return nil } // ignore
  77. func (e *EmqxCloud) CreateDefaultAllowRule() error {
  78. return nil
  79. }
  80. func (e *EmqxCloud) DeleteEmqxUser(username string) error {
  81. client := &http.Client{}
  82. req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/api/auth_username/%s", e.URL, username), nil)
  83. if err != nil {
  84. return err
  85. }
  86. req.SetBasicAuth(e.AppID, e.AppSecret)
  87. res, err := client.Do(req)
  88. if err != nil {
  89. return err
  90. }
  91. defer res.Body.Close()
  92. body, err := io.ReadAll(res.Body)
  93. if err != nil {
  94. return err
  95. }
  96. if res.StatusCode != http.StatusOK {
  97. return errors.New("request failed " + string(body))
  98. }
  99. return nil
  100. }