-
Notifications
You must be signed in to change notification settings - Fork 11
/
execution_request.go
80 lines (67 loc) · 2.34 KB
/
execution_request.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
package morpheus
import (
"fmt"
"strconv"
)
var (
// ExecutionRequestPath is the API endpoint for execution requests
ExecutionRequestPath = "/api/execution-request"
)
// ExeuctionRequestResult structure parses the response from the server when running or retrieving an exeuction request
type ExecutionRequest struct {
ID int64 `json:"id"`
UniqueID string `json:"uniqueId"`
ContainerID int64 `json:"containerId"`
ServerID int64 `json:"serverId"`
InstanceID int64 `json:"instanceId"`
ResourceID int64 `json:"resourceId"`
AppID int64 `json:"appId"`
StdOut string `json:"stdOut"`
StdErr string `json:"stdErr"`
ExitCode int64 `json:"exitCode"`
Status string `json:"status"`
ExpiresAt string `json:"expiresAt"`
CreatedById int64 `json:"createdById"`
StatusMessage string `json:"statusMessage"`
ErrorMessage string `json:"errorMessage"`
Config struct{} `json:"config"`
RawData string `json:"rawData"`
}
type ExecutionRequestResult struct {
ExecutionRequest ExecutionRequest `json:"executionRequest"`
}
// Execution Request Methods
func (client *Client) GetExecutionRequest(uniqueID string, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "GET",
Path: fmt.Sprintf("%s/%s", ExecutionRequestPath, uniqueID),
QueryParams: req.QueryParams,
Result: &ExecutionRequestResult{},
})
}
func (client *Client) CreateExecutionRequest(req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "POST",
Path: fmt.Sprintf("%s/%s", ExecutionRequestPath, "execute"),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &ExecutionRequestResult{},
})
}
// helper functions
func (client *Client) ExecuteScriptOnInstance(instance Instance, script string) (stdOut string, stdErr string, err error) {
resp, err := client.CreateExecutionRequest(&Request{
QueryParams: map[string]string{
"instanceId": strconv.Itoa(int(instance.ID)),
},
Body: map[string]interface{}{
"script": script,
},
})
if err != nil {
fmt.Println("unable to run script on instance ", instance.ID, " due to ", err)
}
stdOut = resp.Result.(*ExecutionRequestResult).ExecutionRequest.StdOut
stdErr = resp.Result.(*ExecutionRequestResult).ExecutionRequest.StdErr
return
}