forked from cloudfoundry/cli
/
domain.go
334 lines (277 loc) · 6.06 KB
/
domain.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package cf
import (
"cf/formatters"
"errors"
"fmt"
"generic"
"github.com/codegangsta/cli"
"strconv"
"strings"
"time"
)
type InstanceState string
const (
InstanceStarting InstanceState = "starting"
InstanceRunning = "running"
InstanceFlapping = "flapping"
InstanceDown = "down"
)
type BasicFields struct {
Guid string
Name string
}
func (model BasicFields) String() string {
return model.Name
}
type OrganizationFields struct {
BasicFields
QuotaDefinition QuotaFields
}
type Organization struct {
OrganizationFields
Spaces []SpaceFields
Domains []DomainFields
}
type SpaceFields struct {
BasicFields
}
type Space struct {
SpaceFields
Organization OrganizationFields
Applications []ApplicationFields
ServiceInstances []ServiceInstanceFields
Domains []DomainFields
}
type ApplicationFields struct {
BasicFields
BuildpackUrl string
Command string
DiskQuota uint64 // in Megabytes
EnvironmentVars map[string]string
InstanceCount int
Memory uint64 // in Megabytes
RunningInstances int
State string
SpaceGuid string
}
type ApplicationSet []Application
type Application struct {
ApplicationFields
Stack Stack
Routes []RouteSummary
}
func (model Application) ToParams() (params AppParams) {
params = NewAppParams(generic.NewMap(map[interface{}]interface{}{
"guid": model.Guid,
"name": model.Name,
"buildpack": model.BuildpackUrl,
"command": model.Command,
"disk_quota": model.DiskQuota,
"instances": model.InstanceCount,
"memory": model.Memory,
"state": strings.ToUpper(model.State),
"stack_guid": model.Stack.Guid,
"space_guid": model.SpaceGuid,
"env": generic.NewMap(model.EnvironmentVars),
}))
return
}
type AppSummary struct {
ApplicationFields
RouteSummaries []RouteSummary
}
type AppParams generic.Map
func NewEmptyAppParams() AppParams {
return generic.NewMap()
}
func NewAppParams(data generic.Map) AppParams {
return data
}
func NewAppParamsFromContext(c *cli.Context) (appParams AppParams, err error) {
appParams = NewEmptyAppParams()
if len(c.Args()) > 0 {
appParams.Set("name", c.Args()[0])
}
if c.String("b") != "" {
appParams.Set("buildpack", c.String("b"))
}
if c.String("m") != "" {
var memory uint64
memory, err = formatters.ToMegabytes(c.String("m"))
if err != nil {
err = errors.New(fmt.Sprintf("Invalid memory param: %s\n%s", c.String("m"), err))
return
}
appParams.Set("memory", memory)
}
if c.String("c") != "" {
appParams.Set("command", c.String("c"))
}
if c.String("c") == "null" {
appParams.Set("command", "")
}
if c.String("i") != "" {
var instances int
instances, err = strconv.Atoi(c.String("i"))
if err != nil {
err = errors.New(fmt.Sprintf("Invalid instances param: %s\n%s", c.String("i"), err))
return
}
appParams.Set("instances", instances)
}
if c.String("s") != "" {
appParams.Set("stack", c.String("s"))
}
if c.String("t") != "" {
var timeout int
timeout, err = strconv.Atoi(c.String("t"))
if err != nil {
err = errors.New(fmt.Sprintf("Invalid timeout param: %s\n%s", c.String("t"), err))
return
}
appParams.Set("health_check_timeout", timeout)
}
return
}
type AppSet []AppParams
type AppFileFields struct {
Path string
Sha1 string
Size int64
}
type DomainFields struct {
BasicFields
OwningOrganizationGuid string
Shared bool
}
func (model DomainFields) UrlForHost(host string) string {
if host == "" {
return model.Name
}
return fmt.Sprintf("%s.%s", host, model.Name)
}
type Domain struct {
DomainFields
}
type EventFields struct {
InstanceIndex int
Timestamp time.Time
ExitDescription string
ExitStatus int
}
type RouteFields struct {
Guid string
Host string
}
type Route struct {
RouteSummary
Space SpaceFields
Apps []ApplicationFields
}
type RouteSummary struct {
RouteFields
Domain DomainFields
}
func (model RouteSummary) URL() string {
if model.Host == "" {
return model.Domain.Name
}
return fmt.Sprintf("%s.%s", model.Host, model.Domain.Name)
}
type Stack struct {
BasicFields
Description string
}
type AppInstanceFields struct {
State InstanceState
Since time.Time
CpuUsage float64 // percentage
DiskQuota uint64 // in bytes
DiskUsage uint64
MemQuota uint64
MemUsage uint64
}
type ServicePlanFields struct {
BasicFields
}
type ServicePlan struct {
ServicePlanFields
ServiceOffering ServiceOfferingFields
}
type ServiceOfferingFields struct {
Guid string
Label string
Provider string
Version string
Description string
DocumentationUrl string
}
type ServiceOfferings []ServiceOffering
type ServiceOffering struct {
ServiceOfferingFields
Plans []ServicePlanFields
}
func (s ServiceOfferings) Len() int {
return len(s)
}
func (s ServiceOfferings) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ServiceOfferings) Less(i, j int) bool {
return s[i].Label < s[j].Label
}
type ServiceInstanceFields struct {
BasicFields
SysLogDrainUrl string
ApplicationNames []string
Params map[string]string
}
type ServiceInstanceSet []ServiceInstance
type ServiceInstance struct {
ServiceInstanceFields
ServiceBindings []ServiceBindingFields
ServicePlan ServicePlanFields
ServiceOffering ServiceOfferingFields
}
func (inst ServiceInstance) IsUserProvided() bool {
return inst.ServicePlan.Guid == ""
}
type ServiceBindingFields struct {
Guid string
Url string
AppGuid string
}
func NewQuotaFields(name string, memory uint64) (q QuotaFields) {
q.Name = name
q.MemoryLimit = memory
return
}
type QuotaFields struct {
BasicFields
MemoryLimit uint64 // in Megabytes
}
type ServiceAuthTokenFields struct {
Guid string
Label string
Provider string
Token string
}
type ServiceBroker struct {
BasicFields
Username string
Password string
Url string
}
type UserFields struct {
Guid string
Username string
Password string
IsAdmin bool
}
type Buildpack struct {
BasicFields
Position *int
Enabled *bool
Key string
Filename string
}