Skip to content

Commit

Permalink
feat(client): add AsyncRun
Browse files Browse the repository at this point in the history
  • Loading branch information
daixijun committed Mar 8, 2021
1 parent 6773bb1 commit 54664bd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
- [x] 登出 [logout](https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#logout)
- [x] 查看 minion 列表 [minions](https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#minions)
- [x] 查看 minion 详情 [minion](<https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#get--minions-(mid)>)
- [ ] 执行异步任务 [post-minions](https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#post--minions)
- [x] 执行异步任务 [post-minions](https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#post--minions)
- [x] 查看 job 列表 [jobs](https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#minions)
- [x] 查看 job 详情 [job](<https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#get--jobs-(jid)>)
- [x] 查看 key 列表 [keys](https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#keys)
Expand Down
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Client interface {
Logout(ctx context.Context) error
ListMinions(ctx context.Context) (*MinionResponse, error)
GetMinion(ctx context.Context, mid string) (*MinionResponse, error)
AsyncRun(ctx context.Context, payload *AsyncRunRequest) (*AsyncRunResponse, error)
ListKeys(ctx context.Context) (*KeysResponse, error)
GetKey(ctx context.Context, mid string) (*KeyDetailResponse, error)
ListJobs(ctx context.Context)(*JobsResponse, error)
Expand Down
31 changes: 29 additions & 2 deletions minions.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ type MinionResponse struct {
Return []map[string]Minion `json:"return"`
}

type AsyncRunRequest struct {
Client string `json:"client"`
Target string `json:"tgt"`
Function string `json:"fun"`
}

type AsyncRunResponse struct {
Return []struct {
Jid string `json:"jid"`
Minions []string `json:"minions"`
} `json:"return"`
Links struct {
Jobs []struct {
Href string `json:"href"`
} `json:"jobs"`
} `json:"_links"`
}

func (c *client) ListMinions(ctx context.Context) (*MinionResponse, error) {
// data, err := testClient.doRequest(ctx, "GET", "minions", nil)
// if err != nil {
Expand Down Expand Up @@ -140,8 +158,17 @@ func (c *client) GetMinion(ctx context.Context, mid string) (*MinionResponse, er
}

var minion MinionResponse
if err := json.Unmarshal(data, &minion); err != nil {
err = json.Unmarshal(data, &minion)
return &minion, err
}

func (c *client) AsyncRun(ctx context.Context, payload *AsyncRunRequest) (*AsyncRunResponse, error) {
data, err := c.doRequest(ctx, "POST", "minions", payload)
if err != nil {
return nil, err
}
return &minion, nil

var resp AsyncRunResponse
err = json.Unmarshal(data, &resp)
return &resp, err
}

0 comments on commit 54664bd

Please sign in to comment.