Skip to content

Commit 51a29ac

Browse files
authored
add appGUID to droplet resource (#2933)
1 parent d4253ee commit 51a29ac

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

Diff for: resources/droplet_resource.go

+89
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package resources
22

33
import (
4+
"encoding/json"
5+
6+
"code.cloudfoundry.org/cli/api/cloudcontroller"
47
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
58
)
69

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

0 commit comments

Comments
 (0)