netmaker/mq/emqx_cloud.go
Max Ma 65faf73fe9
NET-1226: Scalability Improvements (#2987)
* add api to check if failover node existed

* remove 5 minute peerUpdate

* update peerUpdate to trigger pull

* update Action name to SignalPull

* revert the peerUpdate from SignalPull

* fix getfailover error issue

* rm acls creation for on-prem emqx

* remove use of acls

* add additional broker status field on status api

* NET-1165: Remove creation of acls on emqx (#2996)

* rm acls creation for on-prem emqx

* remove use of acls

* add additional broker status field on status api

* comment out mq reconnect logic

* configure mq conn params

* add metric_interval in ENV for publishing metrics

* add metric_interval in ENV for publishing metrics

* update PUBLISH_METRIC_INTERVAL env name

* revert the mq setttings back

* fix error nil issue

---------

Co-authored-by: abhishek9686 <abhi281342@gmail.com>
Co-authored-by: Abhishek K <32607604+abhishek9686@users.noreply.github.com>
2024-07-09 18:56:55 +05:30

119 lines
2.6 KiB
Go

package mq
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/gravitl/netmaker/servercfg"
)
type EmqxCloud struct {
URL string
AppID string
AppSecret string
}
type userCreateReq struct {
UserName string `json:"username"`
Password string `json:"password"`
}
func (e *EmqxCloud) GetType() servercfg.Emqxdeploy { return servercfg.EmqxCloudDeploy }
func (e *EmqxCloud) CreateEmqxUser(username, pass string) error {
payload := userCreateReq{
UserName: username,
Password: pass,
}
data, _ := json.Marshal(payload)
client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/auth_username", e.URL), strings.NewReader(string(data)))
if err != nil {
return err
}
req.SetBasicAuth(e.AppID, e.AppSecret)
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return errors.New("request failed " + string(body))
}
return nil
}
func (e *EmqxCloud) CreateEmqxUserforServer() error {
payload := userCreateReq{
UserName: servercfg.GetMqUserName(),
Password: servercfg.GetMqPassword(),
}
data, _ := json.Marshal(payload)
client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/auth_username", e.URL), strings.NewReader(string(data)))
if err != nil {
return err
}
req.SetBasicAuth(e.AppID, e.AppSecret)
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return errors.New("request failed " + string(body))
}
return nil
}
func (e *EmqxCloud) CreateEmqxDefaultAuthenticator() error { return nil } // ignore
func (e *EmqxCloud) CreateEmqxDefaultAuthorizer() error { return nil } // ignore
func (e *EmqxCloud) CreateDefaultAllowRule() error {
return nil
}
func (e *EmqxCloud) DeleteEmqxUser(username string) error {
client := &http.Client{}
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/api/auth_username/%s", e.URL, username), nil)
if err != nil {
return err
}
req.SetBasicAuth(e.AppID, e.AppSecret)
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return errors.New("request failed " + string(body))
}
return nil
}