2
0

emqx_cloud.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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) CreateDefaultDenyRule() error {
  78. return nil
  79. }
  80. func (e *EmqxCloud) CreateHostACL(hostID, serverName string) error {
  81. return nil
  82. }
  83. func (e *EmqxCloud) AppendNodeUpdateACL(hostID, nodeNetwork, nodeID, serverName string) error {
  84. return nil
  85. }
  86. func (e *EmqxCloud) GetUserACL(username string) (*aclObject, error) { return nil, nil } // ununsed on cloud since it doesn't overwrite acls list
  87. func (e *EmqxCloud) DeleteEmqxUser(username string) error {
  88. client := &http.Client{}
  89. req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/api/auth_username/%s", e.URL, username), nil)
  90. if err != nil {
  91. return err
  92. }
  93. req.SetBasicAuth(e.AppID, e.AppSecret)
  94. res, err := client.Do(req)
  95. if err != nil {
  96. return err
  97. }
  98. defer res.Body.Close()
  99. body, err := io.ReadAll(res.Body)
  100. if err != nil {
  101. return err
  102. }
  103. if res.StatusCode != http.StatusOK {
  104. return errors.New("request failed " + string(body))
  105. }
  106. return nil
  107. }