Skip to content

Commit

Permalink
Merge pull request #214 from liketic/fix-list-machines
Browse files Browse the repository at this point in the history
Add size and offset for ListMachines
  • Loading branch information
shabicheng authored Jul 19, 2023
2 parents da8250f + 0f4a99c commit 8d1518c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 41 additions & 0 deletions client_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions token_auto_update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8d1518c

Please sign in to comment.