-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathapps.go
More file actions
140 lines (102 loc) · 2.71 KB
/
apps.go
File metadata and controls
140 lines (102 loc) · 2.71 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package apps
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/deis/workflow/client/controller/api"
"github.com/deis/workflow/client/controller/client"
)
// List lists apps on a Deis controller.
func List(c *client.Client, results int) ([]api.App, int, error) {
body, count, err := c.LimitedRequest("/v2/apps/", results)
if err != nil {
return []api.App{}, -1, err
}
var apps []api.App
if err = json.Unmarshal([]byte(body), &apps); err != nil {
return []api.App{}, -1, err
}
return apps, count, nil
}
// New creates a new app.
func New(c *client.Client, id string) (api.App, error) {
body := []byte{}
var err error
if id != "" {
req := api.AppCreateRequest{ID: id}
body, err = json.Marshal(req)
if err != nil {
return api.App{}, err
}
}
resBody, err := c.BasicRequest("POST", "/v2/apps/", body)
if err != nil {
return api.App{}, err
}
app := api.App{}
if err = json.Unmarshal([]byte(resBody), &app); err != nil {
return api.App{}, err
}
return app, nil
}
// Get app details from a Deis controller.
func Get(c *client.Client, appID string) (api.App, error) {
u := fmt.Sprintf("/v2/apps/%s/", appID)
body, err := c.BasicRequest("GET", u, nil)
if err != nil {
return api.App{}, err
}
app := api.App{}
if err = json.Unmarshal([]byte(body), &app); err != nil {
return api.App{}, err
}
return app, nil
}
// Logs retrieves logs from an app.
func Logs(c *client.Client, appID string, lines int) (string, error) {
u := fmt.Sprintf("/v2/apps/%s/logs", appID)
if lines > 0 {
u += "?log_lines=" + strconv.Itoa(lines)
}
body, err := c.BasicRequest("GET", u, nil)
if err != nil {
return "", err
}
return strings.Trim(body, `"`), nil
}
// Run one time command in an app.
func Run(c *client.Client, appID string, command string) (api.AppRunResponse, error) {
req := api.AppRunRequest{Command: command}
body, err := json.Marshal(req)
if err != nil {
return api.AppRunResponse{}, err
}
u := fmt.Sprintf("/v2/apps/%s/run", appID)
resBody, err := c.BasicRequest("POST", u, body)
if err != nil {
return api.AppRunResponse{}, err
}
res := api.AppRunResponse{}
if err = json.Unmarshal([]byte(resBody), &res); err != nil {
return api.AppRunResponse{}, err
}
return res, nil
}
// Delete an app.
func Delete(c *client.Client, appID string) error {
u := fmt.Sprintf("/v2/apps/%s/", appID)
_, err := c.BasicRequest("DELETE", u, nil)
return err
}
// Transfer an app to another user.
func Transfer(c *client.Client, appID string, username string) error {
u := fmt.Sprintf("/v2/apps/%s/", appID)
req := api.AppUpdateRequest{Owner: username}
body, err := json.Marshal(req)
if err != nil {
return err
}
_, err = c.BasicRequest("POST", u, body)
return err
}