高热共公日志库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.0 KiB

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