github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv3/application.go (about) 1 package ccv3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "net/url" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 11 ) 12 13 type AppLifecycleType string 14 15 const ( 16 BuildpackAppLifecycleType AppLifecycleType = "buildpack" 17 DockerAppLifecycleType AppLifecycleType = "docker" 18 ) 19 20 // Application represents a Cloud Controller V3 Application. 21 type Application struct { 22 Name string `json:"name,omitempty"` 23 Relationships Relationships `json:"relationships,omitempty"` 24 GUID string `json:"guid,omitempty"` 25 State string `json:"state,omitempty"` 26 Lifecycle AppLifecycle `json:"lifecycle,omitempty"` 27 } 28 29 type AppLifecycle struct { 30 Type AppLifecycleType `json:"type,omitempty"` 31 Data AppLifecycleData `json:"data,omitempty"` 32 } 33 34 type AppLifecycleData struct { 35 Buildpacks []string `json:"buildpacks,omitempty"` 36 } 37 38 func (a Application) MarshalJSON() ([]byte, error) { 39 var ccApp struct { 40 Name string `json:"name,omitempty"` 41 Relationships Relationships `json:"relationships,omitempty"` 42 Lifecycle map[string]interface{} `json:"lifecycle,omitempty"` 43 } 44 45 ccApp.Name = a.Name 46 ccApp.Relationships = a.Relationships 47 48 switch a.Lifecycle.Type { 49 case BuildpackAppLifecycleType: 50 if len(a.Lifecycle.Data.Buildpacks) > 0 { 51 switch a.Lifecycle.Data.Buildpacks[0] { 52 case "default", "null": 53 ccApp.Lifecycle = map[string]interface{}{ 54 "type": a.Lifecycle.Type, 55 "data": map[string]interface{}{ 56 "buildpacks": nil, 57 }, 58 } 59 default: 60 ccApp.Lifecycle = map[string]interface{}{ 61 "type": a.Lifecycle.Type, 62 "data": map[string]interface{}{ 63 "buildpacks": a.Lifecycle.Data.Buildpacks, 64 }, 65 } 66 } 67 } 68 case DockerAppLifecycleType: 69 ccApp.Lifecycle = map[string]interface{}{ 70 "type": a.Lifecycle.Type, 71 "data": map[string]interface{}{}, 72 } 73 } 74 75 return json.Marshal(ccApp) 76 } 77 78 // DropletRelationship represents the relationship between a V3 Droplet and its 79 // V3 Application 80 type DropletRelationship struct { 81 Relationship Relationship `json:"data"` 82 Links APILinks `json:"links"` 83 } 84 85 // GetApplications lists applications with optional filters. 86 func (client *Client) GetApplications(query url.Values) ([]Application, Warnings, error) { 87 request, err := client.newHTTPRequest(requestOptions{ 88 RequestName: internal.GetAppsRequest, 89 Query: query, 90 }) 91 if err != nil { 92 return nil, nil, err 93 } 94 95 var fullAppsList []Application 96 warnings, err := client.paginate(request, Application{}, func(item interface{}) error { 97 if app, ok := item.(Application); ok { 98 fullAppsList = append(fullAppsList, app) 99 } else { 100 return ccerror.UnknownObjectInListError{ 101 Expected: Application{}, 102 Unexpected: item, 103 } 104 } 105 return nil 106 }) 107 108 return fullAppsList, warnings, err 109 } 110 111 // CreateApplication creates an application with the given settings 112 func (client *Client) CreateApplication(app Application) (Application, Warnings, error) { 113 bodyBytes, err := json.Marshal(app) 114 if err != nil { 115 return Application{}, nil, err 116 } 117 118 request, err := client.newHTTPRequest(requestOptions{ 119 RequestName: internal.PostApplicationRequest, 120 Body: bytes.NewReader(bodyBytes), 121 }) 122 if err != nil { 123 return Application{}, nil, err 124 } 125 126 var responseApp Application 127 response := cloudcontroller.Response{ 128 Result: &responseApp, 129 } 130 err = client.connection.Make(request, &response) 131 132 return responseApp, response.Warnings, err 133 } 134 135 func (client *Client) DeleteApplication(appGUID string) (string, Warnings, error) { 136 request, err := client.newHTTPRequest(requestOptions{ 137 RequestName: internal.DeleteApplicationRequest, 138 URIParams: internal.Params{"app_guid": appGUID}, 139 }) 140 if err != nil { 141 return "", nil, err 142 } 143 144 response := cloudcontroller.Response{} 145 err = client.connection.Make(request, &response) 146 147 return response.ResourceLocationURL, response.Warnings, err 148 } 149 150 // UpdateApplication updates an application with the given settings 151 func (client *Client) UpdateApplication(app Application) (Application, Warnings, error) { 152 bodyBytes, err := json.Marshal(app) 153 if err != nil { 154 return Application{}, nil, err 155 } 156 157 request, err := client.newHTTPRequest(requestOptions{ 158 RequestName: internal.PatchApplicationRequest, 159 Body: bytes.NewReader(bodyBytes), 160 URIParams: map[string]string{"app_guid": app.GUID}, 161 }) 162 if err != nil { 163 return Application{}, nil, err 164 } 165 166 var responseApp Application 167 response := cloudcontroller.Response{ 168 Result: &responseApp, 169 } 170 err = client.connection.Make(request, &response) 171 172 return responseApp, response.Warnings, err 173 } 174 175 func (client *Client) SetApplicationDroplet(appGUID string, dropletGUID string) (Relationship, Warnings, error) { 176 relationship := Relationship{GUID: dropletGUID} 177 bodyBytes, err := json.Marshal(relationship) 178 if err != nil { 179 return Relationship{}, nil, err 180 } 181 182 request, err := client.newHTTPRequest(requestOptions{ 183 RequestName: internal.PatchApplicationCurrentDropletRequest, 184 URIParams: map[string]string{"app_guid": appGUID}, 185 Body: bytes.NewReader(bodyBytes), 186 }) 187 if err != nil { 188 return Relationship{}, nil, err 189 } 190 191 var responseRelationship Relationship 192 response := cloudcontroller.Response{ 193 Result: &responseRelationship, 194 } 195 err = client.connection.Make(request, &response) 196 197 return responseRelationship, response.Warnings, err 198 } 199 200 func (client *Client) StopApplication(appGUID string) (Warnings, error) { 201 request, err := client.newHTTPRequest(requestOptions{ 202 RequestName: internal.PostApplicationStopRequest, 203 URIParams: map[string]string{"app_guid": appGUID}, 204 }) 205 if err != nil { 206 return nil, err 207 } 208 209 response := cloudcontroller.Response{} 210 err = client.connection.Make(request, &response) 211 212 return response.Warnings, err 213 } 214 215 func (client *Client) StartApplication(appGUID string) (Application, Warnings, error) { 216 request, err := client.newHTTPRequest(requestOptions{ 217 RequestName: internal.PostApplicationStartRequest, 218 URIParams: map[string]string{"app_guid": appGUID}, 219 }) 220 if err != nil { 221 return Application{}, nil, err 222 } 223 224 var responseApp Application 225 response := cloudcontroller.Response{ 226 Result: &responseApp, 227 } 228 err = client.connection.Make(request, &response) 229 230 return responseApp, response.Warnings, err 231 }