|
1 | 1 | package resources
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "code.cloudfoundry.org/cli/api/cloudcontroller" |
4 | 5 | "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
|
| 6 | + "encoding/json" |
5 | 7 | )
|
6 | 8 |
|
7 | 9 | // Droplet represents a Cloud Controller droplet's metadata. A droplet is a set of
|
8 | 10 | // compiled bits for a given application.
|
9 | 11 | type Droplet struct {
|
| 12 | + // AppGUID is the unique identifier of the application associated with the droplet. |
| 13 | + AppGUID string `json:"app_guid"` |
10 | 14 | //Buildpacks are the detected buildpacks from the staging process.
|
11 | 15 | Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"`
|
12 | 16 | // CreatedAt is the timestamp that the Cloud Controller created the droplet.
|
@@ -35,3 +39,77 @@ type DropletBuildpack struct {
|
35 | 39 | // Version is the version of the detected buildpack.
|
36 | 40 | Version string `json:"version"`
|
37 | 41 | }
|
| 42 | + |
| 43 | +func (d Droplet) MarshallJSON() ([]byte, error) { |
| 44 | + type Data struct { |
| 45 | + GUID string `json:"guid,omitempty"` |
| 46 | + } |
| 47 | + |
| 48 | + type RelationshipData struct { |
| 49 | + Data Data `json:"data,omitempty"` |
| 50 | + } |
| 51 | + |
| 52 | + type Relationships struct { |
| 53 | + App RelationshipData `json:"app,omitempty"` |
| 54 | + } |
| 55 | + |
| 56 | + type ccDroplet struct { |
| 57 | + GUID string `json:"guid,omitempty"` |
| 58 | + Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"` |
| 59 | + CreatedAt string `json:"created_at,omitempty"` |
| 60 | + Image string `json:"image,omitempty"` |
| 61 | + Stack string `json:"stack,omitempty"` |
| 62 | + State constant.DropletState `json:"state,omitempty"` |
| 63 | + Relationships *Relationships `json:"relationships,omitempty"` |
| 64 | + } |
| 65 | + |
| 66 | + ccD := ccDroplet{ |
| 67 | + GUID: d.GUID, |
| 68 | + Buildpacks: d.Buildpacks, |
| 69 | + CreatedAt: d.CreatedAt, |
| 70 | + Image: d.Image, |
| 71 | + Stack: d.Stack, |
| 72 | + State: d.State, |
| 73 | + } |
| 74 | + |
| 75 | + if d.AppGUID != "" { |
| 76 | + ccD.Relationships = &Relationships{ |
| 77 | + App: RelationshipData{Data{GUID: d.AppGUID}}, |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return json.Marshal(ccD) |
| 82 | +} |
| 83 | + |
| 84 | +func (d *Droplet) UnmarshalJSON(data []byte) error { |
| 85 | + var alias struct { |
| 86 | + GUID string `json:"guid,omitempty"` |
| 87 | + Buildpacks []DropletBuildpack `json:"buildpacks,omitempty"` |
| 88 | + CreatedAt string `json:"created_at,omitempty"` |
| 89 | + Image string `json:"image,omitempty"` |
| 90 | + Stack string `json:"stack,omitempty"` |
| 91 | + State constant.DropletState `json:"state,omitempty"` |
| 92 | + Relationships struct { |
| 93 | + App struct { |
| 94 | + Data struct { |
| 95 | + GUID string `json:"guid,omitempty"` |
| 96 | + } `json:"data,omitempty"` |
| 97 | + } `json:"app,omitempty"` |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + err := cloudcontroller.DecodeJSON(data, &alias) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + d.GUID = alias.GUID |
| 107 | + d.Buildpacks = alias.Buildpacks |
| 108 | + d.CreatedAt = alias.CreatedAt |
| 109 | + d.Image = alias.Image |
| 110 | + d.Stack = alias.Stack |
| 111 | + d.State = alias.State |
| 112 | + d.AppGUID = alias.Relationships.App.Data.GUID |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
0 commit comments