[agent-operator] - message pusher

This commit is contained in:
zeaslity
2024-04-23 16:27:12 +08:00
parent 7b9e471c00
commit 4e5af5856a
6 changed files with 106 additions and 14 deletions

View File

@@ -0,0 +1,80 @@
package pusher
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"wdd.io/agent-common/logger"
)
var log = logger.Log
// CmiiUpdateMessage message_pusher/cmii/CmiiMessage.go
type CmiiUpdateMessage struct {
Namespace string
AppName string
FromTag string
ToTag string
Replicas string
DeployStatus bool
}
type Message struct { // TODO combine with server.message
ID string
Event string
Time int64
Topic string
Message string
Title string
Priority int
Tags []string
Click string
Icon string
Attachment *Attachment
// Additional fields
TopicURL string
SubscriptionID string
Raw string
}
func (c *CmiiUpdateMessage) SendMessage() (message Message) {
// 将结构体转换为JSON字符串
requestBytes, err := json.Marshal(c)
if err != nil {
fmt.Println("Error encoding request body to JSON:", err)
return
}
url := "http://192.168.35.71:8080/cmii/update" // 替换为实际的API地址
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBytes))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// 添加请求头
req.Header.Set("Content-Type", "application/json")
// 发送请求并获取响应
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
}
defer resp.Body.Close()
bodyBytes, _ := io.ReadAll(resp.Body)
var m Message
err = json.Unmarshal(bodyBytes, &m)
if err != nil {
log.ErrorF("Error unmarshaling response body to JSON:", err)
return message
}
return m
}

View File

@@ -0,0 +1,16 @@
package pusher
import "testing"
func TestCmiiUpdateMessage_SendMessage(t *testing.T) {
c := &CmiiUpdateMessage{
Namespace: "dev",
AppName: "cmii-uav-gateway",
FromTag: "5.1.0",
ToTag: "5.5.0",
Replicas: "2",
DeployStatus: false,
}
c.SendMessage()
}

View File

@@ -1,11 +0,0 @@
package cmii
// CmiiUpdateMessage message_pusher/cmii/CmiiMessage.go
type CmiiUpdateMessage struct {
Namespace string
AppName string
FromTag string
ToTag string
Replicas string
DeployStatus bool
}