forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployment_resource.go
80 lines (68 loc) · 2.27 KB
/
deployment_resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package resources
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
)
type Deployment struct {
GUID string
State constant.DeploymentState
StatusValue constant.DeploymentStatusValue
StatusReason constant.DeploymentStatusReason
RevisionGUID string
DropletGUID string
CreatedAt string
UpdatedAt string
Relationships Relationships
NewProcesses []Process
}
// MarshalJSON converts a Deployment into a Cloud Controller Deployment.
func (d Deployment) MarshalJSON() ([]byte, error) {
type Revision struct {
GUID string `json:"guid,omitempty"`
}
type Droplet struct {
GUID string `json:"guid,omitempty"`
}
var ccDeployment struct {
Droplet *Droplet `json:"droplet,omitempty"`
Revision *Revision `json:"revision,omitempty"`
Relationships Relationships `json:"relationships,omitempty"`
}
if d.DropletGUID != "" {
ccDeployment.Droplet = &Droplet{d.DropletGUID}
}
if d.RevisionGUID != "" {
ccDeployment.Revision = &Revision{d.RevisionGUID}
}
ccDeployment.Relationships = d.Relationships
return json.Marshal(ccDeployment)
}
// UnmarshalJSON helps unmarshal a Cloud Controller Deployment response.
func (d *Deployment) UnmarshalJSON(data []byte) error {
var ccDeployment struct {
GUID string `json:"guid,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Relationships Relationships `json:"relationships,omitempty"`
State constant.DeploymentState `json:"state,omitempty"`
Status struct {
Value constant.DeploymentStatusValue `json:"value"`
Reason constant.DeploymentStatusReason `json:"reason"`
} `json:"status"`
Droplet Droplet `json:"droplet,omitempty"`
NewProcesses []Process `json:"new_processes,omitempty"`
}
err := cloudcontroller.DecodeJSON(data, &ccDeployment)
if err != nil {
return err
}
d.GUID = ccDeployment.GUID
d.CreatedAt = ccDeployment.CreatedAt
d.Relationships = ccDeployment.Relationships
d.State = ccDeployment.State
d.StatusValue = ccDeployment.Status.Value
d.StatusReason = ccDeployment.Status.Reason
d.DropletGUID = ccDeployment.Droplet.GUID
d.NewProcesses = ccDeployment.NewProcesses
return nil
}