Skip to content

Commit

Permalink
add appGUID to droplet resource
Browse files Browse the repository at this point in the history
  • Loading branch information
gmllt committed Jun 27, 2024
1 parent 36abb81 commit 0d0ada0
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions resources/droplet_resource.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
}

0 comments on commit 0d0ada0

Please sign in to comment.