-
Notifications
You must be signed in to change notification settings - Fork 58
/
deployment.go
259 lines (223 loc) · 8.28 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package camunda_client_go
import (
"bytes"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strconv"
)
// Deployment a client for Deployment API
type Deployment struct {
client *Client
}
// ResDeployment a JSON array of deployment objects
type ResDeployment struct {
// The id of the deployment
Id string `json:"id"`
// The name of the deployment
Name string `json:"name"`
// The source of the deployment
Source string `json:"source"`
// The tenant id of the deployment
TenantId string `json:"tenantId"`
// The date and time of the deployment.
DeploymentTime Time `json:"deploymentTime"`
}
// ResDeploymentCreate a JSON object corresponding to the DeploymentWithDefinitions interface in the engine
type ResDeploymentCreate struct {
// The id of the deployment
Id string `json:"id"`
// The name of the deployment
Name string `json:"name"`
// The source of the deployment
Source string `json:"source"`
// The tenant id of the deployment
TenantId string `json:"tenant_id"`
// The time when the deployment was created
DeploymentTime Time `json:"deployment_time"`
// Link to the newly created deployment with method, href and rel
Links []ResLink `json:"links"`
// A JSON Object containing a property for each of the process definitions,
// which are successfully deployed with that deployment
DeployedProcessDefinitions map[string]ResProcessDefinition `json:"deployedProcessDefinitions"`
// A JSON Object containing a property for each of the case definitions,
// which are successfully deployed with that deployment
DeployedCaseDefinitions map[string]ResCaseDefinition `json:"deployedCaseDefinitions"`
// A JSON Object containing a property for each of the decision definitions,
// which are successfully deployed with that deployment
DeployedDecisionDefinitions map[string]ResDecisionDefinition `json:"deployedDecisionDefinitions"`
// A JSON Object containing a property for each of the decision requirements definitions,
// which are successfully deployed with that deployment
DeployedDecisionRequirementsDefinitions map[string]ResDecisionRequirementsDefinition `json:"deployedDecisionRequirementsDefinitions"`
}
// ReqDeploymentCreate a request to deployment create
type ReqDeploymentCreate struct {
DeploymentName string
EnableDuplicateFiltering *bool
DeployChangedOnly *bool
DeploymentSource *string
TenantId *string
Resources map[string]interface{}
}
// ReqRedeploy a request to redeploy
type ReqRedeploy struct {
// A list of deployment resource ids to re-deploy
ResourceIds *string `json:"resourceIds,omitempty"`
// A list of deployment resource names to re-deploy
ResourceNames *string `json:"resourceNames,omitempty"`
// Sets the source of the deployment
Source *string `json:"source,omitempty"`
}
// ResDeploymentResource a JSON array containing all deployment resources of the given deployment
type ResDeploymentResource struct {
// The id of the deployment resource
Id string `json:"id"`
// The name of the deployment resource
Name string `json:"name"`
// The id of the deployment
DeploymentId string `json:"deploymentId"`
}
// GetList a queries for deployments that fulfill given parameters. Parameters may be the properties of deployments,
// such as the id or name or a range of the deployment time. The size of the result set can be retrieved by using
// the Get Deployment count method.
// Query parameters described in the documentation:
// https://docs.camunda.org/manual/latest/reference/rest/deployment/get-query/#query-parameters
func (d *Deployment) GetList(query map[string]string) (deployments []*ResDeployment, err error) {
res, err := d.client.doGet("/deployment", query)
if err != nil {
return
}
err = d.client.readJsonResponse(res, &deployments)
return
}
// GetListCount a queries for the number of deployments that fulfill given parameters.
// Takes the same parameters as the Get Deployments method
func (d *Deployment) GetListCount(query map[string]string) (count int, err error) {
res, err := d.client.doGet("/deployment/count", query)
if err != nil {
return
}
resCount := ResCount{}
err = d.client.readJsonResponse(res, &resCount)
return resCount.Count, err
}
// Get retrieves a deployment by id, according to the Deployment interface of the engine
func (d *Deployment) Get(id string) (deployment ResDeployment, err error) {
res, err := d.client.doGet("/deployment/"+id, map[string]string{})
if err != nil {
return
}
err = d.client.readJsonResponse(res, &deployment)
return
}
// Create creates a deployment
func (d *Deployment) Create(deploymentCreate ReqDeploymentCreate) (deployment *ResDeploymentCreate, err error) {
deployment = &ResDeploymentCreate{}
var data []byte
body := bytes.NewBuffer(data)
w := multipart.NewWriter(body)
if err = w.WriteField("deployment-name", deploymentCreate.DeploymentName); err != nil {
return nil, err
}
if deploymentCreate.EnableDuplicateFiltering != nil {
if err = w.WriteField("enable-duplicate-filtering", strconv.FormatBool(*deploymentCreate.EnableDuplicateFiltering)); err != nil {
return nil, err
}
}
if deploymentCreate.DeployChangedOnly != nil {
if err = w.WriteField("deploy-changed-only", strconv.FormatBool(*deploymentCreate.DeployChangedOnly)); err != nil {
return nil, err
}
}
if deploymentCreate.DeploymentSource != nil {
if err = w.WriteField("deployment-source", *deploymentCreate.DeploymentSource); err != nil {
return nil, err
}
}
if deploymentCreate.TenantId != nil {
if err = w.WriteField("tenant-id", *deploymentCreate.TenantId); err != nil {
return nil, err
}
}
for key, resource := range deploymentCreate.Resources {
var fw io.Writer
if x, ok := resource.(io.Closer); ok {
defer x.Close()
}
if x, ok := resource.(*os.File); ok {
if fw, err = w.CreateFormFile(key, x.Name()); err != nil {
return nil, err
}
} else if _, ok := resource.(multipart.File); ok {
if fw, err = w.CreateFormFile(key, key); err != nil {
return nil, err
}
} else {
if fw, err = w.CreateFormField(key); err != nil {
return nil, err
}
}
if r, ok := resource.(io.Reader); ok {
if _, err = io.Copy(fw, r); err != nil {
return nil, err
}
}
}
if err := w.Close(); err != nil {
return nil, err
}
res, err := d.client.do(http.MethodPost, "/deployment/create", map[string]string{}, body, w.FormDataContentType())
if err != nil {
return nil, err
}
err = d.client.readJsonResponse(res, deployment)
return deployment, err
}
// Redeploy a re-deploys an existing deployment.
// The deployment resources to re-deploy can be restricted by using the properties resourceIds or resourceNames.
// If no deployment resources to re-deploy are passed then all existing resources of the given deployment
// are re-deployed
func (d *Deployment) Redeploy(id string, req ReqRedeploy) (deployment *ResDeploymentCreate, err error) {
deployment = &ResDeploymentCreate{}
res, err := d.client.doPostJson("/deployment/"+id+"/redeploy", map[string]string{}, &req)
if err != nil {
return
}
err = d.client.readJsonResponse(res, deployment)
return
}
// GetResources retrieves all deployment resources of a given deployment
func (d *Deployment) GetResources(id string) (resources []*ResDeploymentResource, err error) {
res, err := d.client.doGet("/deployment/"+id+"/resources", map[string]string{})
if err != nil {
return
}
err = d.client.readJsonResponse(res, &resources)
return
}
// GetResource retrieves a deployment resource by resource id for the given deployment
func (d *Deployment) GetResource(id, resourceId string) (resource *ResDeploymentResource, err error) {
resource = &ResDeploymentResource{}
res, err := d.client.doGet("/deployment/"+id+"/resources/"+resourceId, map[string]string{})
if err != nil {
return
}
err = d.client.readJsonResponse(res, &resource)
return
}
// GetResourceBinary retrieves the binary content of a deployment resource for the given deployment by id
func (d *Deployment) GetResourceBinary(id, resourceId string) (data []byte, err error) {
res, err := d.client.doGet("/deployment/"+id+"/resources/"+resourceId+"/data", map[string]string{})
if err != nil {
return
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
// Delete deletes a deployment by id
func (d *Deployment) Delete(id string, query map[string]string) error {
err := d.client.doDelete("/deployment/"+id, query)
return err
}