feat: add vm power actions
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package jobs
|
||||
|
||||
const TypeProxmoxTaskPoll = "proxmox.task.poll"
|
||||
|
||||
type ProxmoxTaskPollPayload struct {
|
||||
ClusterID string `json:"cluster_id"`
|
||||
Node string `json:"node"`
|
||||
UPID string `json:"upid"`
|
||||
TargetType string `json:"target_type"`
|
||||
TargetID string `json:"target_id"`
|
||||
TenantID string `json:"tenant_id"`
|
||||
ProfileID string `json:"profile_id,omitempty"`
|
||||
Action string `json:"action"`
|
||||
SuccessStatus string `json:"success_status"`
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -108,6 +108,28 @@ func TestClientGetsTaskStatus(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientPowersVM(t *testing.T) {
|
||||
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Fatalf("method = %s, want POST", r.Method)
|
||||
}
|
||||
if r.URL.Path != "/api2/json/nodes/pve/qemu/100/status/start" {
|
||||
t.Fatalf("path = %q", r.URL.Path)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"data":"UPID:pve:1"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := newTestClient(t, server, fingerprintForServer(server))
|
||||
upid, err := client.PowerVM(context.Background(), "pve", 100, "start")
|
||||
if err != nil {
|
||||
t.Fatalf("PowerVM() error = %v", err)
|
||||
}
|
||||
if upid != "UPID:pve:1" {
|
||||
t.Fatalf("UPID = %q, want UPID:pve:1", upid)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewClientRejectsInvalidFingerprint(t *testing.T) {
|
||||
_, err := NewClient(cluster.Cluster{
|
||||
APIEndpoint: "https://pve.example.test:8006/api2/json",
|
||||
|
||||
Reference in New Issue
Block a user