forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
69 lines (58 loc) · 1.83 KB
/
application.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
package ccv3
import (
"bytes"
"encoding/json"
"net/url"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// Application represents a Cloud Controller V3 Application.
type Application struct {
Name string `json:"name"`
GUID string `json:"guid,omitempty"`
Relationships ApplicationRelationships `json:"relationships"`
}
type ApplicationRelationships struct {
Space Relationship `json:"space"`
}
// GetApplications lists applications with optional filters.
func (client *Client) GetApplications(query url.Values) ([]Application, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetAppsRequest,
Query: query,
})
if err != nil {
return nil, nil, err
}
var fullAppsList []Application
warnings, err := client.paginate(request, Application{}, func(item interface{}) error {
if app, ok := item.(Application); ok {
fullAppsList = append(fullAppsList, app)
} else {
return ccerror.UnknownObjectInListError{
Expected: Application{},
Unexpected: item,
}
}
return nil
})
return fullAppsList, warnings, err
}
// CreateApplication creates an application with the given settings
func (client *Client) CreateApplication(app Application) (Application, Warnings, error) {
bodyBytes, err := json.Marshal(app)
if err != nil {
return Application{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostApplicationRequest,
Body: bytes.NewBuffer(bodyBytes),
})
var responseApp Application
response := cloudcontroller.Response{
Result: &responseApp,
}
err = client.connection.Make(request, &response)
return responseApp, response.Warnings, err
}