github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/cloudcontroller/ccv3/application.go (about) 1 package ccv3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 11 ) 12 13 // Application represents a Cloud Controller V3 Application. 14 type Application struct { 15 // GUID is the unique application identifier. 16 GUID string 17 // StackName is the name of the stack on which the application runs. 18 StackName string 19 // LifecycleBuildpacks is a list of the names of buildpacks. 20 LifecycleBuildpacks []string 21 // LifecycleType is the type of the lifecycle. 22 LifecycleType constant.AppLifecycleType 23 // Metadata is used for custom tagging of API resources 24 Metadata *Metadata 25 // Name is the name given to the application. 26 Name string 27 // Relationships list the relationships to the application. 28 Relationships Relationships 29 // State is the desired state of the application. 30 State constant.ApplicationState 31 } 32 33 // MarshalJSON converts an Application into a Cloud Controller Application. 34 func (a Application) MarshalJSON() ([]byte, error) { 35 ccApp := ccApplication{ 36 Name: a.Name, 37 Relationships: a.Relationships, 38 Metadata: a.Metadata, 39 } 40 41 if a.LifecycleType == constant.AppLifecycleTypeDocker { 42 ccApp.setDockerLifecycle() 43 } else if a.LifecycleType == constant.AppLifecycleTypeBuildpack { 44 if len(a.LifecycleBuildpacks) > 0 || a.StackName != "" { 45 if a.hasAutodetectedBuildpack() { 46 ccApp.setAutodetectedBuildpackLifecycle(a) 47 } else { 48 ccApp.setBuildpackLifecycle(a) 49 } 50 } 51 } 52 53 return json.Marshal(ccApp) 54 } 55 56 // UnmarshalJSON helps unmarshal a Cloud Controller Application response. 57 func (a *Application) UnmarshalJSON(data []byte) error { 58 lifecycle := ccLifecycle{} 59 ccApp := ccApplication{ 60 Lifecycle: &lifecycle, 61 } 62 63 err := cloudcontroller.DecodeJSON(data, &ccApp) 64 if err != nil { 65 return err 66 } 67 68 a.GUID = ccApp.GUID 69 a.StackName = lifecycle.Data.Stack 70 a.LifecycleBuildpacks = lifecycle.Data.Buildpacks 71 a.LifecycleType = lifecycle.Type 72 a.Name = ccApp.Name 73 a.Relationships = ccApp.Relationships 74 a.State = ccApp.State 75 a.Metadata = ccApp.Metadata 76 77 return nil 78 } 79 80 func (a Application) hasAutodetectedBuildpack() bool { 81 if len(a.LifecycleBuildpacks) == 0 { 82 return false 83 } 84 return a.LifecycleBuildpacks[0] == constant.AutodetectBuildpackValueDefault || a.LifecycleBuildpacks[0] == constant.AutodetectBuildpackValueNull 85 } 86 87 type ccLifecycle struct { 88 Type constant.AppLifecycleType `json:"type,omitempty"` 89 Data struct { 90 Buildpacks []string `json:"buildpacks,omitempty"` 91 Stack string `json:"stack,omitempty"` 92 } `json:"data"` 93 } 94 95 type ccApplication struct { 96 Name string `json:"name,omitempty"` 97 Relationships Relationships `json:"relationships,omitempty"` 98 Lifecycle interface{} `json:"lifecycle,omitempty"` 99 GUID string `json:"guid,omitempty"` 100 State constant.ApplicationState `json:"state,omitempty"` 101 Metadata *Metadata `json:"metadata,omitempty"` 102 } 103 104 func (ccApp *ccApplication) setAutodetectedBuildpackLifecycle(a Application) { 105 var nullBuildpackLifecycle struct { 106 Type constant.AppLifecycleType `json:"type,omitempty"` 107 Data struct { 108 Buildpacks []string `json:"buildpacks"` 109 Stack string `json:"stack,omitempty"` 110 } `json:"data"` 111 } 112 nullBuildpackLifecycle.Type = constant.AppLifecycleTypeBuildpack 113 nullBuildpackLifecycle.Data.Stack = a.StackName 114 ccApp.Lifecycle = nullBuildpackLifecycle 115 } 116 117 func (ccApp *ccApplication) setBuildpackLifecycle(a Application) { 118 var lifecycle ccLifecycle 119 lifecycle.Type = a.LifecycleType 120 lifecycle.Data.Buildpacks = a.LifecycleBuildpacks 121 lifecycle.Data.Stack = a.StackName 122 ccApp.Lifecycle = lifecycle 123 } 124 125 func (ccApp *ccApplication) setDockerLifecycle() { 126 ccApp.Lifecycle = ccLifecycle{ 127 Type: constant.AppLifecycleTypeDocker, 128 } 129 } 130 131 // CreateApplication creates an application with the given settings. 132 func (client *Client) CreateApplication(app Application) (Application, Warnings, error) { 133 bodyBytes, err := json.Marshal(app) 134 if err != nil { 135 return Application{}, nil, err 136 } 137 138 request, err := client.newHTTPRequest(requestOptions{ 139 RequestName: internal.PostApplicationRequest, 140 Body: bytes.NewReader(bodyBytes), 141 }) 142 if err != nil { 143 return Application{}, nil, err 144 } 145 146 var responseApp Application 147 response := cloudcontroller.Response{ 148 DecodeJSONResponseInto: &responseApp, 149 } 150 err = client.connection.Make(request, &response) 151 152 return responseApp, response.Warnings, err 153 } 154 155 // GetApplications lists applications with optional queries. 156 func (client *Client) GetApplications(query ...Query) ([]Application, Warnings, error) { 157 request, err := client.newHTTPRequest(requestOptions{ 158 RequestName: internal.GetApplicationsRequest, 159 Query: query, 160 }) 161 if err != nil { 162 return nil, nil, err 163 } 164 165 var fullAppsList []Application 166 warnings, err := client.paginate(request, Application{}, func(item interface{}) error { 167 if app, ok := item.(Application); ok { 168 fullAppsList = append(fullAppsList, app) 169 } else { 170 return ccerror.UnknownObjectInListError{ 171 Expected: Application{}, 172 Unexpected: item, 173 } 174 } 175 return nil 176 }) 177 178 return fullAppsList, warnings, err 179 } 180 181 // UpdateApplication updates an application with the given settings. 182 func (client *Client) UpdateApplication(app Application) (Application, Warnings, error) { 183 bodyBytes, err := json.Marshal(app) 184 if err != nil { 185 return Application{}, nil, err 186 } 187 188 request, err := client.newHTTPRequest(requestOptions{ 189 RequestName: internal.PatchApplicationRequest, 190 Body: bytes.NewReader(bodyBytes), 191 URIParams: map[string]string{"app_guid": app.GUID}, 192 }) 193 if err != nil { 194 return Application{}, nil, err 195 } 196 197 var responseApp Application 198 response := cloudcontroller.Response{ 199 DecodeJSONResponseInto: &responseApp, 200 } 201 err = client.connection.Make(request, &response) 202 203 return responseApp, response.Warnings, err 204 } 205 206 // UpdateApplicationRestart restarts the given application. 207 func (client *Client) UpdateApplicationRestart(appGUID string) (Application, Warnings, error) { 208 request, err := client.newHTTPRequest(requestOptions{ 209 RequestName: internal.PostApplicationActionRestartRequest, 210 URIParams: map[string]string{"app_guid": appGUID}, 211 }) 212 if err != nil { 213 return Application{}, nil, err 214 } 215 216 var responseApp Application 217 response := cloudcontroller.Response{ 218 DecodeJSONResponseInto: &responseApp, 219 } 220 err = client.connection.Make(request, &response) 221 222 return responseApp, response.Warnings, err 223 } 224 225 // UpdateApplicationStart starts the given application. 226 func (client *Client) UpdateApplicationStart(appGUID string) (Application, Warnings, error) { 227 request, err := client.newHTTPRequest(requestOptions{ 228 RequestName: internal.PostApplicationActionStartRequest, 229 URIParams: map[string]string{"app_guid": appGUID}, 230 }) 231 if err != nil { 232 return Application{}, nil, err 233 } 234 235 var responseApp Application 236 response := cloudcontroller.Response{ 237 DecodeJSONResponseInto: &responseApp, 238 } 239 err = client.connection.Make(request, &response) 240 241 return responseApp, response.Warnings, err 242 } 243 244 // UpdateApplicationStop stops the given application. 245 func (client *Client) UpdateApplicationStop(appGUID string) (Application, Warnings, error) { 246 request, err := client.newHTTPRequest(requestOptions{ 247 RequestName: internal.PostApplicationActionStopRequest, 248 URIParams: map[string]string{"app_guid": appGUID}, 249 }) 250 if err != nil { 251 return Application{}, nil, err 252 } 253 254 var responseApp Application 255 response := cloudcontroller.Response{ 256 DecodeJSONResponseInto: &responseApp, 257 } 258 err = client.connection.Make(request, &response) 259 260 return responseApp, response.Warnings, err 261 }