feat: add vm power actions
This commit is contained in:
@@ -163,6 +163,36 @@ func (c *Client) GetTaskStatus(ctx context.Context, node string, upid string) (T
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) PowerVM(ctx context.Context, node string, vmid int, action string) (string, error) {
|
||||
response, err := c.Post(ctx, fmt.Sprintf(
|
||||
"/nodes/%s/qemu/%d/status/%s",
|
||||
url.PathEscape(node),
|
||||
vmid,
|
||||
url.PathEscape(action),
|
||||
), nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode >= http.StatusBadRequest {
|
||||
_, _ = io.Copy(io.Discard, response.Body)
|
||||
return "", fmt.Errorf("proxmox returned %s", response.Status)
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Data string `json:"data"`
|
||||
}
|
||||
if err := json.NewDecoder(response.Body).Decode(&body); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if body.Data == "" {
|
||||
return "", fmt.Errorf("proxmox response missing UPID")
|
||||
}
|
||||
|
||||
return body.Data, nil
|
||||
}
|
||||
|
||||
func (c *Client) Do(ctx context.Context, method string, path string, body []byte) (*http.Response, error) {
|
||||
var lastErr error
|
||||
attempts := c.retries + 1
|
||||
|
||||
Reference in New Issue
Block a user