This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
/
app.go
152 lines (136 loc) · 3.7 KB
/
app.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
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
141
142
143
144
145
146
147
148
149
150
151
152
package remote
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"code.cloudfoundry.org/cflocal/cfplugin"
)
type App struct {
CLI cfplugin.CliConnection
UI UI
HTTP *http.Client
}
type UI interface {
Warn(format string, a ...interface{})
}
type AppEnv struct {
Staging map[string]string `json:"staging_env_json"`
Running map[string]string `json:"running_env_json"`
App map[string]string `json:"environment_json"`
}
func (a *App) Command(name string) (string, error) {
appJSON, _, err := a.get(name, "")
if err != nil {
return "", err
}
defer appJSON.Close()
var app struct{ Entity struct{ Command string } }
if err := json.NewDecoder(appJSON).Decode(&app); err != nil {
return "", err
}
return app.Entity.Command, nil
}
func (a *App) Env(name string) (*AppEnv, error) {
appEnvJSON, _, err := a.get(name, "/env")
if err != nil {
return nil, err
}
defer appEnvJSON.Close()
var env AppEnv
if err := json.NewDecoder(appEnvJSON).Decode(&env); err != nil {
return nil, err
}
return &env, nil
}
func (a *App) SetEnv(name string, env map[string]string) error {
body := &bytes.Buffer{}
if err := json.NewEncoder(body).Encode(struct {
Env map[string]string `json:"environment_json"`
}{env}); err != nil {
return err
}
return a.put(name, "", body, "application/x-www-form-urlencoded", int64(body.Len()))
}
func (a *App) Restart(name string) error {
_, err := a.CLI.CliCommand("restart", name)
return err
}
func (a *App) get(name, appEndpoint string) (body io.ReadCloser, size int64, err error) {
response, err := a.doAppRequest(name, "GET", appEndpoint, nil, "", 0, http.StatusOK)
if err != nil {
return nil, 0, err
}
return response.Body, response.ContentLength, nil
}
func (a *App) put(name, appEndpoint string, body io.Reader, contentType string, contentLength int64) error {
response, err := a.doAppRequest(name, "PUT", appEndpoint, body, contentType, contentLength, http.StatusCreated)
if err != nil {
return err
}
return response.Body.Close()
}
func (a *App) doAppRequest(name, method, appEndpoint string, body io.Reader, contentType string, contentLength int64, desiredStatus int) (*http.Response, error) {
if err := a.checkAuth(); err != nil {
return nil, err
}
appModel, err := a.CLI.GetApp(name)
if err != nil {
return nil, err
}
endpoint := fmt.Sprintf("/v2/apps/%s", path.Join(appModel.Guid, appEndpoint))
response, err := a.doRequest(method, endpoint, body, contentType, contentLength, desiredStatus)
if err != nil {
return nil, err
}
return response, nil
}
func (a *App) doRequest(method, endpoint string, body io.Reader, contentType string, contentLength int64, desiredStatus int) (*http.Response, error) {
target, err := a.CLI.ApiEndpoint()
if err != nil {
return nil, err
}
targetURL, err := url.Parse(target)
if err != nil {
return nil, err
}
targetURL.Path = path.Join(targetURL.Path, endpoint)
request, err := http.NewRequest(method, targetURL.String(), body)
if err != nil {
return nil, err
}
token, err := a.CLI.AccessToken()
if err != nil {
return nil, err
}
request.Header.Set("Authorization", token)
if contentType != "" {
request.Header.Set("Content-Type", contentType)
}
if contentLength > 0 {
request.ContentLength = contentLength
}
response, err := a.HTTP.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode != desiredStatus {
response.Body.Close()
return nil, fmt.Errorf("unexpected '%s' from: %s %s", response.Status, method, targetURL.String())
}
return response, nil
}
func (a *App) checkAuth() error {
loggedIn, err := a.CLI.IsLoggedIn()
if err != nil {
return err
}
if !loggedIn {
return errors.New("must be authenticated with Cloud Foundry API")
}
return nil
}