diff --git a/resources/droplet_resource.go b/resources/droplet_resource.go index 27dc2574f3..75c1d988a9 100644 --- a/resources/droplet_resource.go +++ b/resources/droplet_resource.go @@ -1,12 +1,16 @@ package resources import ( + "code.cloudfoundry.org/cli/api/cloudcontroller" "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" + "encoding/json" ) // Droplet represents a Cloud Controller droplet's metadata. A droplet is a set of // compiled bits for a given application. type Droplet struct { + // AppGUID is the unique identifier of the application associated with the droplet. + AppGUID string `json:"app_guid"` //Buildpacks are the detected buildpacks from the staging process. Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"` // CreatedAt is the timestamp that the Cloud Controller created the droplet. @@ -35,3 +39,77 @@ type DropletBuildpack struct { // Version is the version of the detected buildpack. Version string `json:"version"` } + +func (d Droplet) MarshallJSON() ([]byte, error) { + type Data struct { + GUID string `json:"guid,omitempty"` + } + + type RelationshipData struct { + Data Data `json:"data,omitempty"` + } + + type Relationships struct { + App RelationshipData `json:"app,omitempty"` + } + + type ccDroplet struct { + GUID string `json:"guid,omitempty"` + Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + Image string `json:"image,omitempty"` + Stack string `json:"stack,omitempty"` + State constant.DropletState `json:"state,omitempty"` + Relationships *Relationships `json:"relationships,omitempty"` + } + + ccD := ccDroplet{ + GUID: d.GUID, + Buildpacks: d.Buildpacks, + CreatedAt: d.CreatedAt, + Image: d.Image, + Stack: d.Stack, + State: d.State, + } + + if d.AppGUID != "" { + ccD.Relationships = &Relationships{ + App: RelationshipData{Data{GUID: d.AppGUID}}, + } + } + + return json.Marshal(ccD) +} + +func (d *Droplet) UnmarshalJSON(data []byte) error { + var alias struct { + GUID string `json:"guid,omitempty"` + Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + Image string `json:"image,omitempty"` + Stack string `json:"stack,omitempty"` + State constant.DropletState `json:"state,omitempty"` + Relationships struct { + App struct { + Data struct { + GUID string `json:"guid,omitempty"` + } `json:"data,omitempty"` + } `json:"app,omitempty"` + } + } + + err := cloudcontroller.DecodeJSON(data, &alias) + if err != nil { + return err + } + + d.GUID = alias.GUID + d.Buildpacks = alias.Buildpacks + d.CreatedAt = alias.CreatedAt + d.Image = alias.Image + d.Stack = alias.Stack + d.State = alias.State + d.AppGUID = alias.Relationships.App.Data.GUID + + return nil +}