Skip to content

Commit 316d54d

Browse files
committed
add appGUID to droplet resource
1 parent 1e1c456 commit 316d54d

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

Diff for: resources/droplet_resource.go

+88
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,87 @@ 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 ccDroplet struct {
45+
GUID string `json:"guid,omitempty"`
46+
Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"`
47+
CreatedAt string `json:"created_at,omitempty"`
48+
Image string `json:"image,omitempty"`
49+
Stack string `json:"stack,omitempty"`
50+
State constant.DropletState `json:"state,omitempty"`
51+
Relationships *struct {
52+
App struct {
53+
Data struct {
54+
GUID string `json:"guid,omitempty"`
55+
} `json:"data,omitempty"`
56+
} `json:"app,omitempty"`
57+
} `json:"relationships,omitempty"`
58+
}
59+
60+
ccD := ccDroplet{
61+
GUID: d.GUID,
62+
Buildpacks: d.Buildpacks,
63+
CreatedAt: d.CreatedAt,
64+
Image: d.Image,
65+
Stack: d.Stack,
66+
State: d.State,
67+
}
68+
69+
if d.AppGUID != "" {
70+
ccD.Relationships = &struct {
71+
App struct {
72+
Data struct {
73+
GUID string `json:"guid,omitempty"`
74+
} `json:"data,omitempty"`
75+
} `json:"app,omitempty"`
76+
}{
77+
App: struct {
78+
Data struct {
79+
GUID string `json:"guid,omitempty"`
80+
} `json:"data,omitempty"`
81+
}{
82+
Data: struct {
83+
GUID string `json:"guid,omitempty"`
84+
}{
85+
GUID: d.AppGUID,
86+
},
87+
},
88+
}
89+
}
90+
91+
return json.Marshal(ccD)
92+
}
93+
94+
func (d *Droplet) UnmarshalJSON(data []byte) error {
95+
var alias struct {
96+
GUID string `json:"guid,omitempty"`
97+
Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"`
98+
CreatedAt string `json:"created_at,omitempty"`
99+
Image string `json:"image,omitempty"`
100+
Stack string `json:"stack,omitempty"`
101+
State constant.DropletState `json:"state,omitempty"`
102+
Relationships struct {
103+
App struct {
104+
Data struct {
105+
GUID string `json:"guid,omitempty"`
106+
} `json:"data,omitempty"`
107+
} `json:"app,omitempty"`
108+
}
109+
}
110+
111+
err := cloudcontroller.DecodeJSON(data, &alias)
112+
if err != nil {
113+
return err
114+
}
115+
116+
d.GUID = alias.GUID
117+
d.Buildpacks = alias.Buildpacks
118+
d.CreatedAt = alias.CreatedAt
119+
d.Image = alias.Image
120+
d.Stack = alias.Stack
121+
d.State = alias.State
122+
d.AppGUID = alias.Relationships.App.Data.GUID
123+
124+
return nil
125+
}

0 commit comments

Comments
 (0)