-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachines.go
102 lines (82 loc) · 2.75 KB
/
machines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package valyent
import (
"fmt"
stdHTTP "net/http"
"github.com/valyentdev/ravel/api"
)
func (client *Client) CreateMachine(fleetID string, opts api.CreateMachinePayload) (*api.Machine, error) {
machine := &api.Machine{}
err := client.PerformRequest("POST", fmt.Sprintf("/v1/fleets/%s/machines", fleetID), opts, machine)
if err != nil {
return nil, fmt.Errorf("failed to create machine: %v", err)
}
return machine, nil
}
func (client *Client) Exec(fleetID, machineID string, opts api.ExecOptions) error {
err := client.PerformRequest("POST",
fmt.Sprintf("/v1/fleets/%s/machines/%s/exec", fleetID, machineID), opts, nil)
if err != nil {
return fmt.Errorf("failed to exec command: %v", err)
}
return nil
}
func (client *Client) GetMachines(fleetID string) ([]api.Machine, error) {
machines := []api.Machine{}
err := client.PerformRequest("GET", fmt.Sprintf("/v1/fleets/%s/machines", fleetID), nil, &machines)
if err != nil {
return nil, fmt.Errorf("failed to retrieve machines from the api: %v", err)
}
return machines, nil
}
func (client *Client) GetMachineEvents(fleetID, machineID string) ([]api.MachineEvent, error) {
events := []api.MachineEvent{}
err := client.PerformRequest("GET", fmt.Sprintf("/v1/fleets/%s/machines/%s/events", fleetID, machineID), nil, &events)
if err != nil {
return nil, fmt.Errorf("failed to retrieve events from the api: %v", err)
}
return events, nil
}
func (client *Client) DeleteMachine(fleetID, machineID string, force bool) error {
// Initialize response data struct.
resp := map[string]any{}
// Compute request's path.
path := fmt.Sprintf("/v1/fleets/%s/machines/%s?force=%t", fleetID, machineID, force)
// Actually call the API.
err := client.PerformRequest(
stdHTTP.MethodDelete,
path,
nil, &resp,
)
if err != nil {
return fmt.Errorf("failed to delete machine: %v", resp["detail"])
}
return nil
}
func (client *Client) GetLogs(fleetID, machineID string) ([]api.LogEntry, error) {
// Initialize a list of log entries to later be filled by the actual values from the API.
logEntries := []api.LogEntry{}
// Let's fetch the actual HTTP API for log entries.
err := client.PerformRequest(
"GET",
fmt.Sprintf("/v1/fleets/%s/machines/%s/logs", fleetID, machineID),
nil, &logEntries,
)
if err != nil {
return nil, err
}
return logEntries, nil
}
func (client *Client) StartMachine(fleetID, machineID string) error {
err := client.PerformRequest("POST", fmt.Sprintf("/fleets/%s/machines/%s/start", fleetID, machineID), nil, nil)
if err != nil {
return err
}
return nil
}
func (client *Client) StopMachine(fleetID, machineID string) error {
err := client.PerformRequest("POST", fmt.Sprintf("/fleets/%s/machines/%s/stop", fleetID, machineID), nil, nil)
if err != nil {
return err
}
return nil
}