Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add appGUID attribute to droplet resource [main] #2932

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
}
Comment on lines +44 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for the difference between these structs and the ones for the unmarshal (named structs vs anonymous structs)? My suggestion is if we are keeping these inside functions personally I would prefer to have them anonymous.


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
}
Loading