github.com/naphatkrit/deis@v1.12.3/client/controller/models/apps/apps.go (about) 1 package apps 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strconv" 7 "strings" 8 9 "github.com/deis/deis/client/controller/api" 10 "github.com/deis/deis/client/controller/client" 11 ) 12 13 // List lists apps on a Deis controller. 14 func List(c *client.Client, results int) ([]api.App, int, error) { 15 body, count, err := c.LimitedRequest("/v1/apps/", results) 16 17 if err != nil { 18 return []api.App{}, -1, err 19 } 20 21 var apps []api.App 22 if err = json.Unmarshal([]byte(body), &apps); err != nil { 23 return []api.App{}, -1, err 24 } 25 26 return apps, count, nil 27 } 28 29 // New creates a new app. 30 func New(c *client.Client, id string) (api.App, error) { 31 body := []byte{} 32 33 var err error 34 if id != "" { 35 req := api.AppCreateRequest{ID: id} 36 body, err = json.Marshal(req) 37 38 if err != nil { 39 return api.App{}, err 40 } 41 } 42 43 resBody, err := c.BasicRequest("POST", "/v1/apps/", body) 44 45 if err != nil { 46 return api.App{}, err 47 } 48 49 app := api.App{} 50 if err = json.Unmarshal([]byte(resBody), &app); err != nil { 51 return api.App{}, err 52 } 53 54 return app, nil 55 } 56 57 // Get app details from a Deis controller. 58 func Get(c *client.Client, appID string) (api.App, error) { 59 u := fmt.Sprintf("/v1/apps/%s/", appID) 60 61 body, err := c.BasicRequest("GET", u, nil) 62 63 if err != nil { 64 return api.App{}, err 65 } 66 67 app := api.App{} 68 69 if err = json.Unmarshal([]byte(body), &app); err != nil { 70 return api.App{}, err 71 } 72 73 return app, nil 74 } 75 76 // Logs retrieves logs from an app. 77 func Logs(c *client.Client, appID string, lines int) (string, error) { 78 u := fmt.Sprintf("/v1/apps/%s/logs", appID) 79 80 if lines > 0 { 81 u += "?log_lines=" + strconv.Itoa(lines) 82 } 83 84 body, err := c.BasicRequest("GET", u, nil) 85 86 if err != nil { 87 return "", err 88 } 89 90 return strings.Trim(body, `"`), nil 91 } 92 93 // Run one time command in an app. 94 func Run(c *client.Client, appID string, command string) (api.AppRunResponse, error) { 95 req := api.AppRunRequest{Command: command} 96 body, err := json.Marshal(req) 97 98 if err != nil { 99 return api.AppRunResponse{}, err 100 } 101 102 u := fmt.Sprintf("/v1/apps/%s/run", appID) 103 104 resBody, err := c.BasicRequest("POST", u, body) 105 106 if err != nil { 107 return api.AppRunResponse{}, err 108 } 109 110 out := make([]interface{}, 2) 111 112 if err = json.Unmarshal([]byte(resBody), &out); err != nil { 113 return api.AppRunResponse{}, err 114 } 115 116 return api.AppRunResponse{Output: out[1].(string), ReturnCode: int(out[0].(float64))}, nil 117 } 118 119 // Delete an app. 120 func Delete(c *client.Client, appID string) error { 121 u := fmt.Sprintf("/v1/apps/%s/", appID) 122 123 _, err := c.BasicRequest("DELETE", u, nil) 124 return err 125 } 126 127 // Transfer an app to another user. 128 func Transfer(c *client.Client, appID string, username string) error { 129 u := fmt.Sprintf("/v1/apps/%s/", appID) 130 131 req := api.AppUpdateRequest{Owner: username} 132 body, err := json.Marshal(req) 133 134 if err != nil { 135 return err 136 } 137 138 _, err = c.BasicRequest("POST", u, body) 139 return err 140 }