forked from cloudfoundry/cli
/
applications.go
212 lines (178 loc) · 5.91 KB
/
applications.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package api
import (
"cf"
"cf/configuration"
"cf/net"
"fmt"
"generic"
"regexp"
"strings"
)
type AppRouteEntity struct {
Host string
Domain Resource
}
type AppRouteResource struct {
Resource
Entity AppRouteEntity
}
func (resource AppRouteResource) ToFields() (route cf.RouteFields) {
route.Guid = resource.Metadata.Guid
route.Host = resource.Entity.Host
return
}
func (resource AppRouteResource) ToModel() (route cf.RouteSummary) {
route.RouteFields = resource.ToFields()
route.Domain.Guid = resource.Entity.Domain.Metadata.Guid
route.Domain.Name = resource.Entity.Domain.Entity.Name
return
}
type ApplicationEntity struct {
Name string
State string
SpaceGuid string `json:"space_guid"`
Instances int
Memory int
Stack StackResource
Routes []AppRouteResource
EnvironmentJson map[string]string `json:"environment_json"`
}
type ApplicationResource struct {
Resource
Entity ApplicationEntity
}
func (resource ApplicationResource) ToFields() (app cf.ApplicationFields) {
app.Guid = resource.Metadata.Guid
app.Name = resource.Entity.Name
app.EnvironmentVars = resource.Entity.EnvironmentJson
app.State = strings.ToLower(resource.Entity.State)
app.InstanceCount = resource.Entity.Instances
app.Memory = uint64(resource.Entity.Memory)
app.SpaceGuid = resource.Entity.SpaceGuid
return
}
func (resource ApplicationResource) ToModel() (app cf.Application) {
app.ApplicationFields = resource.ToFields()
app.Stack = resource.Entity.Stack.ToFields()
for _, routeResource := range resource.Entity.Routes {
app.Routes = append(app.Routes, routeResource.ToModel())
}
return
}
type PaginatedApplicationResources struct {
Resources []ApplicationResource
}
type ApplicationRepository interface {
Create(params cf.AppParams) (createdApp cf.Application, apiResponse net.ApiResponse)
Read(name string) (app cf.Application, apiResponse net.ApiResponse)
Update(appGuid string, params cf.AppParams) (updatedApp cf.Application, apiResponse net.ApiResponse)
Delete(appGuid string) (apiResponse net.ApiResponse)
}
type CloudControllerApplicationRepository struct {
config *configuration.Configuration
gateway net.Gateway
}
func NewCloudControllerApplicationRepository(config *configuration.Configuration, gateway net.Gateway) (repo CloudControllerApplicationRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerApplicationRepository) Create(params cf.AppParams) (createdApp cf.Application, apiResponse net.ApiResponse) {
data, apiResponse := repo.formatAppJSON(params)
if apiResponse.IsNotSuccessful() {
return
}
path := fmt.Sprintf("%s/v2/apps", repo.config.Target)
resource := new(ApplicationResource)
apiResponse = repo.gateway.CreateResourceForResponse(path, repo.config.AccessToken, strings.NewReader(data), resource)
if apiResponse.IsNotSuccessful() {
return
}
createdApp = resource.ToModel()
return
}
func (repo CloudControllerApplicationRepository) Read(name string) (app cf.Application, apiResponse net.ApiResponse) {
path := fmt.Sprintf("%s/v2/spaces/%s/apps?q=name%s&inline-relations-depth=1", repo.config.Target, repo.config.SpaceFields.Guid, "%3A"+name)
appResources := new(PaginatedApplicationResources)
apiResponse = repo.gateway.GetResource(path, repo.config.AccessToken, appResources)
if apiResponse.IsNotSuccessful() {
return
}
if len(appResources.Resources) == 0 {
apiResponse = net.NewNotFoundApiResponse("%s %s not found", "App", name)
return
}
res := appResources.Resources[0]
app = res.ToModel()
return
}
func (repo CloudControllerApplicationRepository) Update(appGuid string, params cf.AppParams) (updatedApp cf.Application, apiResponse net.ApiResponse) {
data, apiResponse := repo.formatAppJSON(params)
if apiResponse.IsNotSuccessful() {
return
}
path := fmt.Sprintf("%s/v2/apps/%s?inline-relations-depth=1", repo.config.Target, appGuid)
resource := new(ApplicationResource)
apiResponse = repo.gateway.UpdateResourceForResponse(path, repo.config.AccessToken, strings.NewReader(data), resource)
if apiResponse.IsNotSuccessful() {
return
}
updatedApp = resource.ToModel()
return
}
var allowedAppKeys = []string{
"buildpack",
"command",
"instances",
"memory",
"name",
"space_guid",
"stack_guid",
"state",
}
func (repo CloudControllerApplicationRepository) formatAppJSON(input cf.AppParams) (data string, apiResponse net.ApiResponse) {
params := generic.NewEmptyMap()
for _, allowedKey := range allowedAppKeys {
if input.Has(allowedKey) {
params.Set(allowedKey, input.Get(allowedKey))
}
}
if params.Has("command") && params.Get("command").(string) == "null" {
params.Set("command", "")
} else if params.Has("command") {
params.Set("command", stringOrNull(params.Get("command")))
}
if params.Has("buildpack") {
params.Set("buildpack", stringOrNull(params.Get("buildpack")))
}
if params.Has("stack_guid") {
params.Set("stack_guid", stringOrNull(params.Get("stack_guid")))
}
if params.Has("state") {
params.Set("state", strings.ToUpper(params.Get("state").(string)))
}
if params.Has("name") {
reg := regexp.MustCompile("^[0-9a-zA-Z\\-_]*$")
if !reg.MatchString(params.Get("name").(string)) {
apiResponse = net.NewApiResponseWithMessage("App name is invalid: name can only contain letters, numbers, underscores and hyphens")
return
}
}
vals := []string{}
if !params.IsEmpty() {
vals = append(vals, mapToJsonValues(params)...)
}
if input.Has("env") {
envVars := input.Get("env").(generic.Map)
if !envVars.IsEmpty() {
envVal := fmt.Sprintf(`"environment_json":{%s}`, strings.Join(mapToJsonValues(envVars), ","))
vals = append(vals, envVal)
}
}
data = fmt.Sprintf("{%s}", strings.Join(vals, ","))
return
}
func (repo CloudControllerApplicationRepository) Delete(appGuid string) (apiResponse net.ApiResponse) {
path := fmt.Sprintf("%s/v2/apps/%s?recursive=true", repo.config.Target, appGuid)
return repo.gateway.DeleteResource(path, repo.config.AccessToken)
}