-
Notifications
You must be signed in to change notification settings - Fork 927
/
build.go
97 lines (81 loc) · 2.53 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
package ccv3
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// Build represent the process of staging an application package.
type Build struct {
// CreatedAt is the time with zone when the build was created.
CreatedAt string
// DropletGUID is the unique identifier for the resulting droplet from the
// staging process.
DropletGUID string
// Error describes errors during the build process.
Error string
// GUID is the unique build identifier.
GUID string
// PackageGUID is the unique identifier for package that is the input to the
// staging process.
PackageGUID string
// State is the state of the build.
State constant.BuildState
}
// MarshalJSON converts a Build into a Cloud Controller Application.
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)
}
// UnmarshalJSON helps unmarshal a Cloud Controller Build response.
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"`
}
err := cloudcontroller.DecodeJSON(data, &ccBuild)
if 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) {
var responseBody Build
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.PostBuildRequest,
RequestBody: build,
ResponseBody: &responseBody,
})
return responseBody, warnings, err
}
// GetBuild gets the build with the given GUID.
func (client *Client) GetBuild(guid string) (Build, Warnings, error) {
var responseBody Build
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.GetBuildRequest,
URIParams: internal.Params{"build_guid": guid},
ResponseBody: &responseBody,
})
return responseBody, warnings, err
}