1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-02 08:43:27 +00:00
Beego/logs/alils/machine_group.go

92 lines
2.0 KiB
Go
Raw Normal View History

2016-12-27 01:58:42 +00:00
package alils
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
)
2017-04-30 14:41:23 +00:00
// MachineGroupAttribute define the Attribute
type MachineGroupAttribute struct {
2016-12-27 01:58:42 +00:00
ExternalName string `json:"externalName"`
TopicName string `json:"groupTopic"`
}
2017-04-30 14:41:23 +00:00
// MachineGroup define the machine Group
2016-12-27 01:58:42 +00:00
type MachineGroup struct {
Name string `json:"groupName"`
Type string `json:"groupType"`
2017-04-30 14:41:23 +00:00
MachineIDType string `json:"machineIdentifyType"`
MachineIDList []string `json:"machineList"`
2016-12-27 01:58:42 +00:00
2017-04-30 14:41:23 +00:00
Attribute MachineGroupAttribute `json:"groupAttribute"`
2016-12-27 01:58:42 +00:00
CreateTime uint32
LastModifyTime uint32
project *LogProject
}
2017-04-30 14:41:23 +00:00
// Machine define the Machine
2016-12-27 01:58:42 +00:00
type Machine struct {
IP string
2017-04-30 14:41:23 +00:00
UniqueID string `json:"machine-uniqueid"`
UserdefinedID string `json:"userdefined-id"`
2016-12-27 01:58:42 +00:00
}
2017-04-30 14:41:23 +00:00
// MachineList define the Machine List
2016-12-27 01:58:42 +00:00
type MachineList struct {
Total int
Machines []*Machine
}
// ListMachines returns machine list of this machine group.
func (m *MachineGroup) ListMachines() (ms []*Machine, total int, err error) {
h := map[string]string{
"x-sls-bodyrawsize": "0",
}
uri := fmt.Sprintf("/machinegroups/%v/machines", m.Name)
r, err := request(m.project, "GET", uri, h, nil)
if err != nil {
return
}
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
if r.StatusCode != http.StatusOK {
errMsg := &errorMessage{}
err = json.Unmarshal(buf, errMsg)
if err != nil {
err = fmt.Errorf("failed to remove config from machine group")
dump, _ := httputil.DumpResponse(r, true)
fmt.Println(dump)
return
}
err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
return
}
body := &MachineList{}
err = json.Unmarshal(buf, body)
if err != nil {
return
}
ms = body.Machines
total = body.Total
return
}
// GetAppliedConfigs returns applied configs of this machine group.
func (m *MachineGroup) GetAppliedConfigs() (confNames []string, err error) {
confNames, err = m.project.GetAppliedConfigs(m.Name)
return
}