forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildpack_resource.go
94 lines (84 loc) · 2.82 KB
/
buildpack_resource.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
package resources
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/types"
)
// Buildpack represents a Cloud Controller V3 buildpack.
type Buildpack struct {
// Enabled is true when the buildpack can be used for staging.
Enabled types.NullBool
// Filename is the uploaded filename of the buildpack.
Filename string
// GUID is the unique identifier for the buildpack.
GUID string
// Locked is true when the buildpack cannot be updated.
Locked types.NullBool
// Name is the name of the buildpack. To be used by app buildpack field.
// (only alphanumeric characters)
Name string
// Position is the order in which the buildpacks are checked during buildpack
// auto-detection.
Position types.NullInt
// Stack is the name of the stack that the buildpack will use.
Stack string
// State is the current state of the buildpack.
State string
// Links are links to related resources.
Links APILinks
// Metadata is used for custom tagging of API resources
Metadata *Metadata
}
// MarshalJSON converts a Package into a Cloud Controller Package.
func (buildpack Buildpack) MarshalJSON() ([]byte, error) {
ccBuildpack := struct {
Name string `json:"name,omitempty"`
Stack string `json:"stack,omitempty"`
Position *int `json:"position,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Locked *bool `json:"locked,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
}{
Name: buildpack.Name,
Stack: buildpack.Stack,
}
if buildpack.Position.IsSet {
ccBuildpack.Position = &buildpack.Position.Value
}
if buildpack.Enabled.IsSet {
ccBuildpack.Enabled = &buildpack.Enabled.Value
}
if buildpack.Locked.IsSet {
ccBuildpack.Locked = &buildpack.Locked.Value
}
return json.Marshal(ccBuildpack)
}
func (buildpack *Buildpack) UnmarshalJSON(data []byte) error {
var ccBuildpack struct {
GUID string `json:"guid,omitempty"`
Links APILinks `json:"links,omitempty"`
Name string `json:"name,omitempty"`
Filename string `json:"filename,omitempty"`
Stack string `json:"stack,omitempty"`
State string `json:"state,omitempty"`
Enabled types.NullBool `json:"enabled"`
Locked types.NullBool `json:"locked"`
Position types.NullInt `json:"position"`
Metadata *Metadata `json:"metadata"`
}
err := cloudcontroller.DecodeJSON(data, &ccBuildpack)
if err != nil {
return err
}
buildpack.Enabled = ccBuildpack.Enabled
buildpack.Filename = ccBuildpack.Filename
buildpack.GUID = ccBuildpack.GUID
buildpack.Locked = ccBuildpack.Locked
buildpack.Name = ccBuildpack.Name
buildpack.Position = ccBuildpack.Position
buildpack.Stack = ccBuildpack.Stack
buildpack.State = ccBuildpack.State
buildpack.Links = ccBuildpack.Links
buildpack.Metadata = ccBuildpack.Metadata
return nil
}