-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.go
152 lines (127 loc) · 4.15 KB
/
deployment.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package ccv3
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
type Deployment struct {
GUID string
State constant.DeploymentState
DropletGUID string
CreatedAt string
UpdatedAt string
Relationships Relationships
}
// MarshalJSON converts a Deployment into a Cloud Controller Deployment.
func (d Deployment) MarshalJSON() ([]byte, error) {
type Droplet struct {
GUID string `json:"guid,omitempty"`
}
var ccDeployment struct {
Droplet *Droplet `json:"droplet,omitempty"`
Relationships Relationships `json:"relationships,omitempty"`
}
if d.DropletGUID != "" {
ccDeployment.Droplet = &Droplet{d.DropletGUID}
}
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"`
Droplet Droplet `json:"droplet,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.DropletGUID = ccDeployment.Droplet.GUID
return nil
}
func (client *Client) CancelDeployment(deploymentGUID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostApplicationDeploymentActionCancelRequest,
URIParams: map[string]string{"deployment_guid": deploymentGUID},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
func (client *Client) CreateApplicationDeployment(appGUID string, dropletGUID string) (string, Warnings, error) {
dep := Deployment{
DropletGUID: dropletGUID,
Relationships: Relationships{constant.RelationshipTypeApplication: Relationship{GUID: appGUID}},
}
bodyBytes, err := json.Marshal(dep)
if err != nil {
return "", nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostApplicationDeploymentRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return "", nil, err
}
var responseDeployment Deployment
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseDeployment,
}
err = client.connection.Make(request, &response)
return responseDeployment.GUID, response.Warnings, err
}
func (client *Client) GetDeployment(deploymentGUID string) (Deployment, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetDeploymentRequest,
URIParams: internal.Params{"deployment_guid": deploymentGUID},
})
if err != nil {
return Deployment{}, nil, err
}
var responseDeployment Deployment
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseDeployment,
}
err = client.connection.Make(request, &response)
return responseDeployment, response.Warnings, err
}
func (client *Client) GetDeployments(query ...Query) ([]Deployment, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetDeploymentsRequest,
Query: query,
})
if err != nil {
return nil, nil, err // untested
}
var deployments []Deployment
warnings, err := client.paginate(request, Deployment{}, func(item interface{}) error {
if deployment, ok := item.(Deployment); ok {
deployments = append(deployments, deployment)
} else {
return ccerror.UnknownObjectInListError{
Expected: Deployment{},
Unexpected: item,
}
}
return nil
})
if err != nil {
return nil, nil, err // untested
}
return deployments, warnings, nil
}