forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
103 lines (86 loc) · 2.36 KB
/
build.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
package ccv3
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
type Build struct {
CreatedAt string
GUID string
Error string
PackageGUID string
State constant.BuildState
DropletGUID string
}
func (b Build) MarshalJSON() ([]byte, error) {
var ccBuild struct {
Package struct {
GUID string `json:"guid"`
} `json:"package"`
}
ccBuild.Package.GUID = b.PackageGUID
return json.Marshal(ccBuild)
}
func (b *Build) UnmarshalJSON(data []byte) error {
var ccBuild struct {
CreatedAt string `json:"created_at,omitempty"`
GUID string `json:"guid,omitempty"`
Error string `json:"error"`
Package struct {
GUID string `json:"guid"`
} `json:"package"`
State constant.BuildState `json:"state,omitempty"`
Droplet struct {
GUID string `json:"guid"`
} `json:"droplet"`
}
if err := json.Unmarshal(data, &ccBuild); err != nil {
return err
}
b.GUID = ccBuild.GUID
b.CreatedAt = ccBuild.CreatedAt
b.Error = ccBuild.Error
b.PackageGUID = ccBuild.Package.GUID
b.State = ccBuild.State
b.DropletGUID = ccBuild.Droplet.GUID
return nil
}
// CreateBuild creates the given build, requires Package GUID to be set on the
// build.
func (client *Client) CreateBuild(build Build) (Build, Warnings, error) {
bodyBytes, err := json.Marshal(build)
if err != nil {
return Build{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostBuildRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return Build{}, nil, err
}
var responseBuild Build
response := cloudcontroller.Response{
Result: &responseBuild,
}
err = client.connection.Make(request, &response)
return responseBuild, response.Warnings, err
}
// GetBuild gets the build with the given GUID.
func (client *Client) GetBuild(guid string) (Build, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetBuildRequest,
URIParams: internal.Params{"build_guid": guid},
})
if err != nil {
return Build{}, nil, err
}
var responseBuild Build
response := cloudcontroller.Response{
Result: &responseBuild,
}
err = client.connection.Make(request, &response)
return responseBuild, response.Warnings, err
}