| 1 | package wings |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | ) |
| 10 | |
| 11 | type Client struct { |
| 12 | baseURL string |
| 13 | token string |
| 14 | hc *http.Client |
| 15 | } |
| 16 | |
| 17 | func New(scheme, fqdn string, port int, token string) *Client { |
| 18 | if scheme != "http" && scheme != "https" { |
| 19 | scheme = "https" |
| 20 | } |
| 21 | return &Client{ |
| 22 | baseURL: fmt.Sprintf("%s://%s:%d", scheme, fqdn, port), |
| 23 | token: token, |
| 24 | hc: &http.Client{}, |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func (c *Client) do(method, path string, body any) (*http.Response, error) { |
| 29 | var reader io.Reader |
| 30 | if body != nil { |
| 31 | b, err := json.Marshal(body) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | reader = bytes.NewReader(b) |
| 36 | } |
| 37 | req, err := http.NewRequest(method, c.baseURL+path, reader) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | req.Header.Set("Authorization", "Bearer "+c.token) |
| 42 | req.Header.Set("Content-Type", "application/json") |
| 43 | req.Header.Set("Accept", "application/json") |
| 44 | req.Header.Set("User-Agent", "Struxa/1.0") |
| 45 | resp, err := c.hc.Do(req) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 50 | resp.Body.Close() |
| 51 | return nil, fmt.Errorf("wings error %d %q", resp.StatusCode, path) |
| 52 | } |
| 53 | return resp, nil |
| 54 | } |
| 55 | |
| 56 | type UtilizationEntry struct { |
| 57 | State string `json:"state"` |
| 58 | } |
| 59 | |
| 60 | func (c *Client) GetUtilization() (map[string]UtilizationEntry, error) { |
| 61 | resp, err := c.do("GET", "/api/servers/utilization", nil) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | defer resp.Body.Close() |
| 66 | var result map[string]UtilizationEntry |
| 67 | return result, json.NewDecoder(resp.Body).Decode(&result) |
| 68 | } |
| 69 | |
| 70 | func (c *Client) SendPowerAction(uuid, action string) error { |
| 71 | resp, err := c.do("POST", "/api/servers/"+uuid+"/power", map[string]string{"action": action}) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | resp.Body.Close() |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | func (c *Client) SendCommands(uuid string, commands []string) error { |
| 80 | resp, err := c.do("POST", "/api/servers/"+uuid+"/commands", map[string][]string{"commands": commands}) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | resp.Body.Close() |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | type BackupPayload struct { |
| 89 | UUID string `json:"uuid"` |
| 90 | Ignore string `json:"ignore"` |
| 91 | Adapter string `json:"adapter"` |
| 92 | } |
| 93 | |
| 94 | func (c *Client) CreateBackup(uuid string, payload BackupPayload) error { |
| 95 | resp, err := c.do("POST", "/api/servers/"+uuid+"/backup", payload) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | resp.Body.Close() |
| 100 | return nil |
| 101 | } |