diff --git a/README.md b/README.md index 0f80333..04c78ea 100644 --- a/README.md +++ b/README.md @@ -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]() -- [ ] 执行异步任务 [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]() - [x] 查看 key 列表 [keys](https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#keys) diff --git a/client.go b/client.go index 958ca55..10acf59 100644 --- a/client.go +++ b/client.go @@ -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) diff --git a/minions.go b/minions.go index 369334d..2e31c79 100644 --- a/minions.go +++ b/minions.go @@ -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 { @@ -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 }