github.com/astaxie/beego@v1.12.3/logs/alils/machine_group.go (about)

     1  package alils
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httputil"
     9  )
    10  
    11  // MachineGroupAttribute define the Attribute
    12  type MachineGroupAttribute struct {
    13  	ExternalName string `json:"externalName"`
    14  	TopicName    string `json:"groupTopic"`
    15  }
    16  
    17  // MachineGroup define the machine Group
    18  type MachineGroup struct {
    19  	Name          string   `json:"groupName"`
    20  	Type          string   `json:"groupType"`
    21  	MachineIDType string   `json:"machineIdentifyType"`
    22  	MachineIDList []string `json:"machineList"`
    23  
    24  	Attribute MachineGroupAttribute `json:"groupAttribute"`
    25  
    26  	CreateTime     uint32
    27  	LastModifyTime uint32
    28  
    29  	project *LogProject
    30  }
    31  
    32  // Machine define the Machine
    33  type Machine struct {
    34  	IP            string
    35  	UniqueID      string `json:"machine-uniqueid"`
    36  	UserdefinedID string `json:"userdefined-id"`
    37  }
    38  
    39  // MachineList define the Machine List
    40  type MachineList struct {
    41  	Total    int
    42  	Machines []*Machine
    43  }
    44  
    45  // ListMachines returns machine list of this machine group.
    46  func (m *MachineGroup) ListMachines() (ms []*Machine, total int, err error) {
    47  	h := map[string]string{
    48  		"x-sls-bodyrawsize": "0",
    49  	}
    50  
    51  	uri := fmt.Sprintf("/machinegroups/%v/machines", m.Name)
    52  	r, err := request(m.project, "GET", uri, h, nil)
    53  	if err != nil {
    54  		return
    55  	}
    56  
    57  	buf, err := ioutil.ReadAll(r.Body)
    58  	if err != nil {
    59  		return
    60  	}
    61  
    62  	if r.StatusCode != http.StatusOK {
    63  		errMsg := &errorMessage{}
    64  		err = json.Unmarshal(buf, errMsg)
    65  		if err != nil {
    66  			err = fmt.Errorf("failed to remove config from machine group")
    67  			dump, _ := httputil.DumpResponse(r, true)
    68  			fmt.Println(dump)
    69  			return
    70  		}
    71  		err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
    72  		return
    73  	}
    74  
    75  	body := &MachineList{}
    76  	err = json.Unmarshal(buf, body)
    77  	if err != nil {
    78  		return
    79  	}
    80  
    81  	ms = body.Machines
    82  	total = body.Total
    83  
    84  	return
    85  }
    86  
    87  // GetAppliedConfigs returns applied configs of this machine group.
    88  func (m *MachineGroup) GetAppliedConfigs() (confNames []string, err error) {
    89  	confNames, err = m.project.GetAppliedConfigs(m.Name)
    90  	return
    91  }