machine_group.go 1.8 KB

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