Skip to content

Commit 3cacfa7

Browse files
gmllta-b
authored andcommitted
add appGUID to droplet resource
1 parent 1d04f1e commit 3cacfa7

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

resources/droplet_resource.go

+78
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package resources
22

33
import (
4+
"code.cloudfoundry.org/cli/api/cloudcontroller"
45
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
6+
"encoding/json"
57
)
68

79
// Droplet represents a Cloud Controller droplet's metadata. A droplet is a set of
810
// compiled bits for a given application.
911
type Droplet struct {
12+
// AppGUID is the unique identifier of the application associated with the droplet.
13+
AppGUID string `json:"app_guid"`
1014
//Buildpacks are the detected buildpacks from the staging process.
1115
Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"`
1216
// CreatedAt is the timestamp that the Cloud Controller created the droplet.
@@ -35,3 +39,77 @@ type DropletBuildpack struct {
3539
// Version is the version of the detected buildpack.
3640
Version string `json:"version"`
3741
}
42+
43+
func (d Droplet) MarshallJSON() ([]byte, error) {
44+
type Data struct {
45+
GUID string `json:"guid,omitempty"`
46+
}
47+
48+
type RelationshipData struct {
49+
Data Data `json:"data,omitempty"`
50+
}
51+
52+
type Relationships struct {
53+
App RelationshipData `json:"app,omitempty"`
54+
}
55+
56+
type ccDroplet struct {
57+
GUID string `json:"guid,omitempty"`
58+
Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"`
59+
CreatedAt string `json:"created_at,omitempty"`
60+
Image string `json:"image,omitempty"`
61+
Stack string `json:"stack,omitempty"`
62+
State constant.DropletState `json:"state,omitempty"`
63+
Relationships *Relationships `json:"relationships,omitempty"`
64+
}
65+
66+
ccD := ccDroplet{
67+
GUID: d.GUID,
68+
Buildpacks: d.Buildpacks,
69+
CreatedAt: d.CreatedAt,
70+
Image: d.Image,
71+
Stack: d.Stack,
72+
State: d.State,
73+
}
74+
75+
if d.AppGUID != "" {
76+
ccD.Relationships = &Relationships{
77+
App: RelationshipData{Data{GUID: d.AppGUID}},
78+
}
79+
}
80+
81+
return json.Marshal(ccD)
82+
}
83+
84+
func (d *Droplet) UnmarshalJSON(data []byte) error {
85+
var alias struct {
86+
GUID string `json:"guid,omitempty"`
87+
Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"`
88+
CreatedAt string `json:"created_at,omitempty"`
89+
Image string `json:"image,omitempty"`
90+
Stack string `json:"stack,omitempty"`
91+
State constant.DropletState `json:"state,omitempty"`
92+
Relationships struct {
93+
App struct {
94+
Data struct {
95+
GUID string `json:"guid,omitempty"`
96+
} `json:"data,omitempty"`
97+
} `json:"app,omitempty"`
98+
}
99+
}
100+
101+
err := cloudcontroller.DecodeJSON(data, &alias)
102+
if err != nil {
103+
return err
104+
}
105+
106+
d.GUID = alias.GUID
107+
d.Buildpacks = alias.Buildpacks
108+
d.CreatedAt = alias.CreatedAt
109+
d.Image = alias.Image
110+
d.Stack = alias.Stack
111+
d.State = alias.State
112+
d.AppGUID = alias.Relationships.App.Data.GUID
113+
114+
return nil
115+
}

0 commit comments

Comments
 (0)