-
Notifications
You must be signed in to change notification settings - Fork 944
/
Copy pathapplication_resource.go
164 lines (140 loc) · 5.08 KB
/
application_resource.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
package resources
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
)
// Application represents a Cloud Controller V3 Application.
type Application struct {
// GUID is the unique application identifier.
GUID string
// StackName is the name of the stack on which the application runs.
StackName string
// LifecycleBuildpacks is a list of the names of buildpacks.
LifecycleBuildpacks []string
// LifecycleType is the type of the lifecycle.
LifecycleType constant.AppLifecycleType
// Metadata is used for custom tagging of API resources
Metadata *Metadata
// Name is the name given to the application.
Name string
// SpaceGUID is the unique identifier of the parent space.
SpaceGUID string
// State is the desired state of the application.
State constant.ApplicationState
// Credentials are used by Cloud Native Buildpacks lifecycle to pull buildpacks
Credentials map[string]interface{}
// CurrentDropletGUID is the unique identifier of the droplet currently attached to the application.
CurrentDropletGUID string
}
// ApplicationNameOnly represents only the name field of a Cloud Controller V3 Application
type ApplicationNameOnly struct {
// Name is the name given to the application.
Name string `json:"name,omitempty"`
}
// MarshalJSON converts an Application into a Cloud Controller Application.
func (a Application) MarshalJSON() ([]byte, error) {
ccApp := ccApplication{
Name: a.Name,
Metadata: a.Metadata,
}
ccApp.Relationships = Relationships{}
if a.SpaceGUID != "" {
ccApp.Relationships[constant.RelationshipTypeSpace] = Relationship{GUID: a.SpaceGUID}
}
if a.CurrentDropletGUID != "" {
ccApp.Relationships[constant.RelationshipTypeCurrentDroplet] = Relationship{GUID: a.CurrentDropletGUID}
}
if a.LifecycleType == constant.AppLifecycleTypeDocker {
ccApp.setDockerLifecycle()
} else if a.LifecycleType == constant.AppLifecycleTypeBuildpack || a.LifecycleType == constant.AppLifecycleTypeCNB {
if len(a.LifecycleBuildpacks) > 0 || a.StackName != "" {
if a.hasAutodetectedBuildpack() {
ccApp.setAutodetectedBuildpackLifecycle(a)
} else {
ccApp.setBuildpackLifecycle(a)
}
}
}
return json.Marshal(ccApp)
}
// UnmarshalJSON helps unmarshal a Cloud Controller Application response.
func (a *Application) UnmarshalJSON(data []byte) error {
lifecycle := ccLifecycle{}
ccApp := ccApplication{
Lifecycle: &lifecycle,
}
err := cloudcontroller.DecodeJSON(data, &ccApp)
if err != nil {
return err
}
a.GUID = ccApp.GUID
a.StackName = lifecycle.Data.Stack
a.LifecycleBuildpacks = lifecycle.Data.Buildpacks
a.LifecycleType = lifecycle.Type
a.Name = ccApp.Name
a.SpaceGUID = ccApp.Relationships[constant.RelationshipTypeSpace].GUID
if _, ok := ccApp.Relationships[constant.RelationshipTypeCurrentDroplet]; ok {
a.CurrentDropletGUID = ccApp.Relationships[constant.RelationshipTypeCurrentDroplet].GUID
}
a.State = ccApp.State
a.Metadata = ccApp.Metadata
return nil
}
func (a Application) Started() bool {
return a.State == constant.ApplicationStarted
}
func (a Application) Stopped() bool {
return a.State == constant.ApplicationStopped
}
func (a Application) hasAutodetectedBuildpack() bool {
if len(a.LifecycleBuildpacks) == 0 {
return false
}
return a.LifecycleBuildpacks[0] == constant.AutodetectBuildpackValueDefault || a.LifecycleBuildpacks[0] == constant.AutodetectBuildpackValueNull
}
type ccCredentials map[string]interface{}
func (ccCredentials) UnmarshalJSON(data []byte) error {
return nil
}
type ccLifecycle struct {
Type constant.AppLifecycleType `json:"type,omitempty"`
Data struct {
Buildpacks []string `json:"buildpacks,omitempty"`
Stack string `json:"stack,omitempty"`
Credentials ccCredentials `json:"credentials,omitempty"`
} `json:"data"`
}
type ccApplication struct {
Name string `json:"name,omitempty"`
Relationships Relationships `json:"relationships,omitempty"`
Lifecycle interface{} `json:"lifecycle,omitempty"`
GUID string `json:"guid,omitempty"`
State constant.ApplicationState `json:"state,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
}
func (ccApp *ccApplication) setAutodetectedBuildpackLifecycle(a Application) {
var nullBuildpackLifecycle struct {
Type constant.AppLifecycleType `json:"type,omitempty"`
Data struct {
Buildpacks []string `json:"buildpacks"`
Stack string `json:"stack,omitempty"`
} `json:"data"`
}
nullBuildpackLifecycle.Type = constant.AppLifecycleTypeBuildpack
nullBuildpackLifecycle.Data.Stack = a.StackName
ccApp.Lifecycle = nullBuildpackLifecycle
}
func (ccApp *ccApplication) setBuildpackLifecycle(a Application) {
var lifecycle ccLifecycle
lifecycle.Type = a.LifecycleType
lifecycle.Data.Buildpacks = a.LifecycleBuildpacks
lifecycle.Data.Stack = a.StackName
lifecycle.Data.Credentials = a.Credentials
ccApp.Lifecycle = lifecycle
}
func (ccApp *ccApplication) setDockerLifecycle() {
ccApp.Lifecycle = ccLifecycle{
Type: constant.AppLifecycleTypeDocker,
}
}