From 0f4a99c88d683447e5afc3342d8375ed1dcfb41a Mon Sep 17 00:00:00 2001 From: "ke.like" Date: Wed, 19 Jul 2023 15:30:30 +0800 Subject: [PATCH] Add size and offset for ListMachines --- client_interface.go | 1 + client_project.go | 41 +++++++++++++++++++++++++++++++++++++ token_auto_update_client.go | 10 +++++++++ 3 files changed, 52 insertions(+) diff --git a/client_interface.go b/client_interface.go index 405145d9..fdc298f2 100644 --- a/client_interface.go +++ b/client_interface.go @@ -126,6 +126,7 @@ type ClientInterface interface { ListMachineGroup(project string, offset, size int) (m []string, total int, err error) // ListMachines list all machines in machineGroupName ListMachines(project, machineGroupName string) (ms []*Machine, total int, err error) + ListMachinesV2(project, machineGroupName string, offset, size int) (ms []*Machine, total int, err error) // CheckMachineGroupExist check machine group exist or not CheckMachineGroupExist(project string, machineGroup string) (bool, error) // GetMachineGroup retruns machine group according by machine group name. diff --git a/client_project.go b/client_project.go index 1e153c05..ea164138 100644 --- a/client_project.go +++ b/client_project.go @@ -119,6 +119,47 @@ func (c *Client) ListMachines(project, machineGroupName string) (ms []*Machine, return } +func (c *Client) ListMachinesV2(project, machineGroupName string, offset, size int) (ms []*Machine, total int, err error) { + h := map[string]string{ + "x-log-bodyrawsize": "0", + } + uri := fmt.Sprintf("/machinegroups/%v/machines?offset=%v&size=%v", machineGroupName, offset, size) + r, err := c.request(project, "GET", uri, h, nil) + if err != nil { + return + } + defer r.Body.Close() + buf, err := ioutil.ReadAll(r.Body) + if err != nil { + return + } + + if r.StatusCode != http.StatusOK { + errMsg := &Error{} + err = json.Unmarshal(buf, errMsg) + if err != nil { + err = fmt.Errorf("failed to unmarshal list machines response: %v", err) + if IsDebugLevelMatched(1) { + dump, _ := httputil.DumpResponse(r, true) + level.Error(Logger).Log("msg", string(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 +} + // CheckLogstoreExist check logstore exist or not func (c *Client) CheckLogstoreExist(project string, logstore string) (bool, error) { proj := convert(c, project) diff --git a/token_auto_update_client.go b/token_auto_update_client.go index 9ae4b583..28b22621 100644 --- a/token_auto_update_client.go +++ b/token_auto_update_client.go @@ -326,6 +326,16 @@ func (c *TokenAutoUpdateClient) ListMachines(project, machineGroupName string) ( return } +func (c *TokenAutoUpdateClient) ListMachinesV2(project, machineGroupName string, offset, size int) (ms []*Machine, total int, err error) { + for i := 0; i < c.maxTryTimes; i++ { + ms, total, err = c.logClient.ListMachinesV2(project, machineGroupName, offset, size) + if !c.processError(err) { + return + } + } + return +} + func (c *TokenAutoUpdateClient) CheckLogstoreExist(project string, logstore string) (ok bool, err error) { for i := 0; i < c.maxTryTimes; i++ { ok, err = c.logClient.CheckLogstoreExist(project, logstore)