github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+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 `json:"guid,omitempty"` 17 // LifecycleBuildpacks is a list of the names of buildpacks. 18 LifecycleBuildpacks []string `json:"-"` 19 // LifecycleType is the type of the lifecycle. 20 LifecycleType constant.AppLifecycleType `json:"-"` 21 // Name is the name given to the application. 22 Name string `json:"name,omitempty"` 23 // Relationships list the relationships to the application. 24 Relationships Relationships `json:"relationships,omitempty"` 25 // State is the desired state of the application. 26 State constant.ApplicationState `json:"state,omitempty"` 27 } 28 29 // MarshalJSON converts an Application into a Cloud Controller Application. 30 func (a Application) MarshalJSON() ([]byte, error) { 31 type rawApp Application 32 var ccApp struct { 33 Lifecycle map[string]interface{} `json:"lifecycle,omitempty"` 34 35 rawApp 36 } 37 38 ccApp.rawApp = (rawApp)(a) 39 40 switch a.LifecycleType { 41 case constant.AppLifecycleTypeBuildpack: 42 if len(a.LifecycleBuildpacks) > 0 { 43 ccApp.Lifecycle = map[string]interface{}{} 44 ccApp.Lifecycle["type"] = a.LifecycleType 45 ccApp.Lifecycle["data"] = map[string]interface{}{} 46 switch a.LifecycleBuildpacks[0] { 47 case "default", "null": 48 ccApp.Lifecycle["data"] = map[string][]string{ 49 "buildpacks": nil, 50 } 51 default: 52 ccApp.Lifecycle["data"] = map[string][]string{ 53 "buildpacks": a.LifecycleBuildpacks, 54 } 55 } 56 } 57 case constant.AppLifecycleTypeDocker: 58 ccApp.Lifecycle = map[string]interface{}{} 59 ccApp.Lifecycle["type"] = a.LifecycleType 60 ccApp.Lifecycle["data"] = map[string]interface{}{} 61 } 62 63 ccApp.GUID = "" 64 return json.Marshal(ccApp) 65 } 66 67 // UnmarshalJSON helps unmarshal a Cloud Controller Application response. 68 func (a *Application) UnmarshalJSON(data []byte) error { 69 type rawApp Application 70 var ccApp struct { 71 *rawApp 72 73 Lifecycle struct { 74 Type constant.AppLifecycleType 75 Data struct { 76 Buildpacks []string 77 } 78 } 79 } 80 81 ccApp.rawApp = (*rawApp)(a) 82 83 err := cloudcontroller.DecodeJSON(data, &ccApp) 84 if err != nil { 85 return err 86 } 87 88 a.LifecycleType = ccApp.Lifecycle.Type 89 a.LifecycleBuildpacks = ccApp.Lifecycle.Data.Buildpacks 90 91 return nil 92 } 93 94 // CreateApplication creates an application with the given settings. 95 func (client *Client) CreateApplication(app Application) (Application, Warnings, error) { 96 bodyBytes, err := json.Marshal(app) 97 if err != nil { 98 return Application{}, nil, err 99 } 100 101 request, err := client.newHTTPRequest(requestOptions{ 102 RequestName: internal.PostApplicationRequest, 103 Body: bytes.NewReader(bodyBytes), 104 }) 105 if err != nil { 106 return Application{}, nil, err 107 } 108 109 var responseApp Application 110 response := cloudcontroller.Response{ 111 Result: &responseApp, 112 } 113 err = client.connection.Make(request, &response) 114 115 return responseApp, response.Warnings, err 116 } 117 118 // GetApplications lists applications with optional queries. 119 func (client *Client) GetApplications(query ...Query) ([]Application, Warnings, error) { 120 request, err := client.newHTTPRequest(requestOptions{ 121 RequestName: internal.GetApplicationsRequest, 122 Query: query, 123 }) 124 if err != nil { 125 return nil, nil, err 126 } 127 128 var fullAppsList []Application 129 warnings, err := client.paginate(request, Application{}, func(item interface{}) error { 130 if app, ok := item.(Application); ok { 131 fullAppsList = append(fullAppsList, app) 132 } else { 133 return ccerror.UnknownObjectInListError{ 134 Expected: Application{}, 135 Unexpected: item, 136 } 137 } 138 return nil 139 }) 140 141 return fullAppsList, warnings, err 142 } 143 144 // UpdateApplication updates an application with the given settings. 145 func (client *Client) UpdateApplication(app Application) (Application, Warnings, error) { 146 bodyBytes, err := json.Marshal(app) 147 if err != nil { 148 return Application{}, nil, err 149 } 150 151 request, err := client.newHTTPRequest(requestOptions{ 152 RequestName: internal.PatchApplicationRequest, 153 Body: bytes.NewReader(bodyBytes), 154 URIParams: map[string]string{"app_guid": app.GUID}, 155 }) 156 if err != nil { 157 return Application{}, nil, err 158 } 159 160 var responseApp Application 161 response := cloudcontroller.Response{ 162 Result: &responseApp, 163 } 164 err = client.connection.Make(request, &response) 165 166 return responseApp, response.Warnings, err 167 } 168 169 // UpdateApplicationStart starts the given application. 170 func (client *Client) UpdateApplicationStart(appGUID string) (Application, Warnings, error) { 171 request, err := client.newHTTPRequest(requestOptions{ 172 RequestName: internal.PostApplicationActionStartRequest, 173 URIParams: map[string]string{"app_guid": appGUID}, 174 }) 175 if err != nil { 176 return Application{}, nil, err 177 } 178 179 var responseApp Application 180 response := cloudcontroller.Response{ 181 Result: &responseApp, 182 } 183 err = client.connection.Make(request, &response) 184 185 return responseApp, response.Warnings, err 186 } 187 188 // UpdateApplicationStop stops the given application. 189 func (client *Client) UpdateApplicationStop(appGUID string) (Application, Warnings, error) { 190 request, err := client.newHTTPRequest(requestOptions{ 191 RequestName: internal.PostApplicationActionStopRequest, 192 URIParams: map[string]string{"app_guid": appGUID}, 193 }) 194 if err != nil { 195 return Application{}, nil, err 196 } 197 198 var responseApp Application 199 response := cloudcontroller.Response{ 200 Result: &responseApp, 201 } 202 err = client.connection.Make(request, &response) 203 204 return responseApp, response.Warnings, err 205 }