/
helm_workload_gen.go
730 lines (649 loc) · 29.4 KB
/
helm_workload_gen.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// generated by 'threeport-sdk gen' for API model boilerplate' - do not edit
package handlers
import (
"errors"
"fmt"
echo "github.com/labstack/echo/v4"
api "github.com/threeport/threeport/pkg/api"
iapi "github.com/threeport/threeport/pkg/api-server/v0"
v0 "github.com/threeport/threeport/pkg/api/v0"
notifications "github.com/threeport/threeport/pkg/notifications/v0"
gorm "gorm.io/gorm"
"net/http"
"time"
)
///////////////////////////////////////////////////////////////////////////////
// HelmWorkloadDefinition
///////////////////////////////////////////////////////////////////////////////
// @Summary GetHelmWorkloadDefinitionVersions gets the supported versions for the helm workload definition API.
// @Description Get the supported API versions for helm workload definitions.
// @ID helmWorkloadDefinition-get-versions
// @Produce json
// @Success 200 {object} api.RESTAPIVersions "OK"
// @Router /helm-workload-definitions/versions [GET]
func (h Handler) GetHelmWorkloadDefinitionVersions(c echo.Context) error {
return c.JSON(http.StatusOK, api.RestapiVersions[string(v0.ObjectTypeHelmWorkloadDefinition)])
}
// @Summary adds a new helm workload definition.
// @Description Add a new helm workload definition to the Threeport database.
// @ID add-v0-helmWorkloadDefinition
// @Accept json
// @Produce json
// @Param helmWorkloadDefinition body v0.HelmWorkloadDefinition true "HelmWorkloadDefinition object"
// @Success 201 {object} v0.Response "Created"
// @Failure 400 {object} v0.Response "Bad Request"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-definitions [POST]
func (h Handler) AddHelmWorkloadDefinition(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadDefinition
var helmWorkloadDefinition v0.HelmWorkloadDefinition
// check for empty payload, unsupported fields, GORM Model fields, optional associations, etc.
if id, err := iapi.PayloadCheck(c, false, objectType, helmWorkloadDefinition); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
if err := c.Bind(&helmWorkloadDefinition); err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
// check for missing required fields
if id, err := iapi.ValidateBoundData(c, helmWorkloadDefinition, objectType); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
// check for duplicate names
var existingHelmWorkloadDefinition v0.HelmWorkloadDefinition
nameUsed := true
result := h.DB.Where("name = ?", helmWorkloadDefinition.Name).First(&existingHelmWorkloadDefinition)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
nameUsed = false
} else {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
}
if nameUsed {
return iapi.ResponseStatus409(c, nil, errors.New("object with provided name already exists"), objectType)
}
// persist to DB
if result := h.DB.Create(&helmWorkloadDefinition); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// notify controller if reconciliation is required
if !*helmWorkloadDefinition.Reconciled {
notifPayload, err := helmWorkloadDefinition.NotificationPayload(
notifications.NotificationOperationCreated,
false,
time.Now().Unix(),
)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
h.JS.Publish(v0.HelmWorkloadDefinitionCreateSubject, *notifPayload)
}
response, err := v0.CreateResponse(nil, helmWorkloadDefinition, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus201(c, *response)
}
// @Summary gets all helm workload definitions.
// @Description Get all helm workload definitions from the Threeport database.
// @ID get-v0-helmWorkloadDefinitions
// @Accept json
// @Produce json
// @Param name query string false "helm workload definition search by name"
// @Success 200 {object} v0.Response "OK"
// @Failure 400 {object} v0.Response "Bad Request"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-definitions [GET]
func (h Handler) GetHelmWorkloadDefinitions(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadDefinition
params, err := c.(*iapi.CustomContext).GetPaginationParams()
if err != nil {
return iapi.ResponseStatus400(c, ¶ms, err, objectType)
}
var filter v0.HelmWorkloadDefinition
if err := c.Bind(&filter); err != nil {
return iapi.ResponseStatus500(c, ¶ms, err, objectType)
}
var totalCount int64
if result := h.DB.Model(&v0.HelmWorkloadDefinition{}).Where(&filter).Count(&totalCount); result.Error != nil {
return iapi.ResponseStatus500(c, ¶ms, result.Error, objectType)
}
records := &[]v0.HelmWorkloadDefinition{}
if result := h.DB.Order("ID asc").Where(&filter).Limit(params.Size).Offset((params.Page - 1) * params.Size).Find(records); result.Error != nil {
return iapi.ResponseStatus500(c, ¶ms, result.Error, objectType)
}
response, err := v0.CreateResponse(v0.CreateMeta(params, totalCount), *records, objectType)
if err != nil {
return iapi.ResponseStatus500(c, ¶ms, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}
// @Summary gets a helm workload definition.
// @Description Get a particular helm workload definition from the database.
// @ID get-v0-helmWorkloadDefinition
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Success 200 {object} v0.Response "OK"
// @Failure 404 {object} v0.Response "Not Found"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-definitions/{id} [GET]
func (h Handler) GetHelmWorkloadDefinition(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadDefinition
helmWorkloadDefinitionID := c.Param("id")
var helmWorkloadDefinition v0.HelmWorkloadDefinition
if result := h.DB.First(&helmWorkloadDefinition, helmWorkloadDefinitionID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
response, err := v0.CreateResponse(nil, helmWorkloadDefinition, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}
// @Summary updates specific fields for an existing helm workload definition.
// @Description Update a helm workload definition in the database. Provide one or more fields to update.
// @Description Note: This API endpint is for updating helm workload definition objects only.
// @Description Request bodies that include related objects will be accepted, however
// @Description the related objects will not be changed. Call the patch or put method for
// @Description each particular existing object to change them.
// @ID update-v0-helmWorkloadDefinition
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Param helmWorkloadDefinition body v0.HelmWorkloadDefinition true "HelmWorkloadDefinition object"
// @Success 200 {object} v0.Response "OK"
// @Failure 400 {object} v0.Response "Bad Request"
// @Failure 404 {object} v0.Response "Not Found"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-definitions/{id} [PATCH]
func (h Handler) UpdateHelmWorkloadDefinition(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadDefinition
helmWorkloadDefinitionID := c.Param("id")
var existingHelmWorkloadDefinition v0.HelmWorkloadDefinition
if result := h.DB.First(&existingHelmWorkloadDefinition, helmWorkloadDefinitionID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// check for empty payload, invalid or unsupported fields, optional associations, etc.
if id, err := iapi.PayloadCheck(c, true, objectType, existingHelmWorkloadDefinition); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
// bind payload
var updatedHelmWorkloadDefinition v0.HelmWorkloadDefinition
if err := c.Bind(&updatedHelmWorkloadDefinition); err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
// update object in database
if result := h.DB.Model(&existingHelmWorkloadDefinition).Updates(updatedHelmWorkloadDefinition); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// notify controller if reconciliation is required
if !*existingHelmWorkloadDefinition.Reconciled {
notifPayload, err := existingHelmWorkloadDefinition.NotificationPayload(
notifications.NotificationOperationUpdated,
false,
time.Now().Unix(),
)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
h.JS.Publish(v0.HelmWorkloadDefinitionUpdateSubject, *notifPayload)
}
response, err := v0.CreateResponse(nil, existingHelmWorkloadDefinition, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}
// @Summary updates an existing helm workload definition by replacing the entire object.
// @Description Replace a helm workload definition in the database. All required fields must be provided.
// @Description If any optional fields are not provided, they will be null post-update.
// @Description Note: This API endpint is for updating helm workload definition objects only.
// @Description Request bodies that include related objects will be accepted, however
// @Description the related objects will not be changed. Call the patch or put method for
// @Description each particular existing object to change them.
// @ID replace-v0-helmWorkloadDefinition
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Param helmWorkloadDefinition body v0.HelmWorkloadDefinition true "HelmWorkloadDefinition object"
// @Success 200 {object} v0.Response "OK"
// @Failure 400 {object} v0.Response "Bad Request"
// @Failure 404 {object} v0.Response "Not Found"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-definitions/{id} [PUT]
func (h Handler) ReplaceHelmWorkloadDefinition(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadDefinition
helmWorkloadDefinitionID := c.Param("id")
var existingHelmWorkloadDefinition v0.HelmWorkloadDefinition
if result := h.DB.First(&existingHelmWorkloadDefinition, helmWorkloadDefinitionID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// check for empty payload, invalid or unsupported fields, optional associations, etc.
if id, err := iapi.PayloadCheck(c, true, objectType, existingHelmWorkloadDefinition); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
// bind payload
var updatedHelmWorkloadDefinition v0.HelmWorkloadDefinition
if err := c.Bind(&updatedHelmWorkloadDefinition); err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
// check for missing required fields
if id, err := iapi.ValidateBoundData(c, updatedHelmWorkloadDefinition, objectType); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
// persist provided data
updatedHelmWorkloadDefinition.ID = existingHelmWorkloadDefinition.ID
if result := h.DB.Session(&gorm.Session{FullSaveAssociations: false}).Omit("CreatedAt", "DeletedAt").Save(&updatedHelmWorkloadDefinition); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// reload updated data from DB
if result := h.DB.First(&existingHelmWorkloadDefinition, helmWorkloadDefinitionID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
response, err := v0.CreateResponse(nil, existingHelmWorkloadDefinition, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}
// @Summary deletes a helm workload definition.
// @Description Delete a helm workload definition by ID from the database.
// @ID delete-v0-helmWorkloadDefinition
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Success 200 {object} v0.Response "OK"
// @Failure 404 {object} v0.Response "Not Found"
// @Failure 409 {object} v0.Response "Conflict"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-definitions/{id} [DELETE]
func (h Handler) DeleteHelmWorkloadDefinition(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadDefinition
helmWorkloadDefinitionID := c.Param("id")
var helmWorkloadDefinition v0.HelmWorkloadDefinition
if result := h.DB.Preload("HelmWorkloadInstances").First(&helmWorkloadDefinition, helmWorkloadDefinitionID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// check to make sure no dependent instances exist for this definition
if len(helmWorkloadDefinition.HelmWorkloadInstances) != 0 {
err := errors.New("helm workload definition has related helm workload instances - cannot be deleted")
return iapi.ResponseStatus409(c, nil, err, objectType)
}
// schedule for deletion if not already scheduled
// if scheduled and reconciled, delete object from DB
// if scheduled but not reconciled, return 409 (controller is working on it)
if helmWorkloadDefinition.DeletionScheduled == nil {
// schedule for deletion
reconciled := false
timestamp := time.Now().UTC()
scheduledHelmWorkloadDefinition := v0.HelmWorkloadDefinition{
Reconciliation: v0.Reconciliation{
DeletionScheduled: ×tamp,
Reconciled: &reconciled,
}}
if result := h.DB.Model(&helmWorkloadDefinition).Updates(scheduledHelmWorkloadDefinition); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// notify controller
notifPayload, err := helmWorkloadDefinition.NotificationPayload(
notifications.NotificationOperationDeleted,
false,
time.Now().Unix(),
)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
h.JS.Publish(v0.HelmWorkloadDefinitionDeleteSubject, *notifPayload)
} else {
if helmWorkloadDefinition.DeletionConfirmed == nil {
// if deletion scheduled but not reconciled, return 409 - deletion
// already underway
return iapi.ResponseStatus409(c, nil, errors.New(fmt.Sprintf(
"object with ID %d already being deleted",
*helmWorkloadDefinition.ID,
)), objectType)
} else {
// object scheduled for deletion and confirmed - it can be deleted
// from DB
if result := h.DB.Delete(&helmWorkloadDefinition); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
}
}
response, err := v0.CreateResponse(nil, helmWorkloadDefinition, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}
///////////////////////////////////////////////////////////////////////////////
// HelmWorkloadInstance
///////////////////////////////////////////////////////////////////////////////
// @Summary GetHelmWorkloadInstanceVersions gets the supported versions for the helm workload instance API.
// @Description Get the supported API versions for helm workload instances.
// @ID helmWorkloadInstance-get-versions
// @Produce json
// @Success 200 {object} api.RESTAPIVersions "OK"
// @Router /helm-workload-instances/versions [GET]
func (h Handler) GetHelmWorkloadInstanceVersions(c echo.Context) error {
return c.JSON(http.StatusOK, api.RestapiVersions[string(v0.ObjectTypeHelmWorkloadInstance)])
}
// @Summary adds a new helm workload instance.
// @Description Add a new helm workload instance to the Threeport database.
// @ID add-v0-helmWorkloadInstance
// @Accept json
// @Produce json
// @Param helmWorkloadInstance body v0.HelmWorkloadInstance true "HelmWorkloadInstance object"
// @Success 201 {object} v0.Response "Created"
// @Failure 400 {object} v0.Response "Bad Request"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-instances [POST]
func (h Handler) AddHelmWorkloadInstance(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadInstance
var helmWorkloadInstance v0.HelmWorkloadInstance
// check for empty payload, unsupported fields, GORM Model fields, optional associations, etc.
if id, err := iapi.PayloadCheck(c, false, objectType, helmWorkloadInstance); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
if err := c.Bind(&helmWorkloadInstance); err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
// check for missing required fields
if id, err := iapi.ValidateBoundData(c, helmWorkloadInstance, objectType); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
// check for duplicate names
var existingHelmWorkloadInstance v0.HelmWorkloadInstance
nameUsed := true
result := h.DB.Where("name = ?", helmWorkloadInstance.Name).First(&existingHelmWorkloadInstance)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
nameUsed = false
} else {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
}
if nameUsed {
return iapi.ResponseStatus409(c, nil, errors.New("object with provided name already exists"), objectType)
}
// persist to DB
if result := h.DB.Create(&helmWorkloadInstance); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// notify controller if reconciliation is required
if !*helmWorkloadInstance.Reconciled {
notifPayload, err := helmWorkloadInstance.NotificationPayload(
notifications.NotificationOperationCreated,
false,
time.Now().Unix(),
)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
h.JS.Publish(v0.HelmWorkloadInstanceCreateSubject, *notifPayload)
}
response, err := v0.CreateResponse(nil, helmWorkloadInstance, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus201(c, *response)
}
// @Summary gets all helm workload instances.
// @Description Get all helm workload instances from the Threeport database.
// @ID get-v0-helmWorkloadInstances
// @Accept json
// @Produce json
// @Param name query string false "helm workload instance search by name"
// @Success 200 {object} v0.Response "OK"
// @Failure 400 {object} v0.Response "Bad Request"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-instances [GET]
func (h Handler) GetHelmWorkloadInstances(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadInstance
params, err := c.(*iapi.CustomContext).GetPaginationParams()
if err != nil {
return iapi.ResponseStatus400(c, ¶ms, err, objectType)
}
var filter v0.HelmWorkloadInstance
if err := c.Bind(&filter); err != nil {
return iapi.ResponseStatus500(c, ¶ms, err, objectType)
}
var totalCount int64
if result := h.DB.Model(&v0.HelmWorkloadInstance{}).Where(&filter).Count(&totalCount); result.Error != nil {
return iapi.ResponseStatus500(c, ¶ms, result.Error, objectType)
}
records := &[]v0.HelmWorkloadInstance{}
if result := h.DB.Order("ID asc").Where(&filter).Limit(params.Size).Offset((params.Page - 1) * params.Size).Find(records); result.Error != nil {
return iapi.ResponseStatus500(c, ¶ms, result.Error, objectType)
}
response, err := v0.CreateResponse(v0.CreateMeta(params, totalCount), *records, objectType)
if err != nil {
return iapi.ResponseStatus500(c, ¶ms, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}
// @Summary gets a helm workload instance.
// @Description Get a particular helm workload instance from the database.
// @ID get-v0-helmWorkloadInstance
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Success 200 {object} v0.Response "OK"
// @Failure 404 {object} v0.Response "Not Found"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-instances/{id} [GET]
func (h Handler) GetHelmWorkloadInstance(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadInstance
helmWorkloadInstanceID := c.Param("id")
var helmWorkloadInstance v0.HelmWorkloadInstance
if result := h.DB.First(&helmWorkloadInstance, helmWorkloadInstanceID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
response, err := v0.CreateResponse(nil, helmWorkloadInstance, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}
// @Summary updates specific fields for an existing helm workload instance.
// @Description Update a helm workload instance in the database. Provide one or more fields to update.
// @Description Note: This API endpint is for updating helm workload instance objects only.
// @Description Request bodies that include related objects will be accepted, however
// @Description the related objects will not be changed. Call the patch or put method for
// @Description each particular existing object to change them.
// @ID update-v0-helmWorkloadInstance
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Param helmWorkloadInstance body v0.HelmWorkloadInstance true "HelmWorkloadInstance object"
// @Success 200 {object} v0.Response "OK"
// @Failure 400 {object} v0.Response "Bad Request"
// @Failure 404 {object} v0.Response "Not Found"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-instances/{id} [PATCH]
func (h Handler) UpdateHelmWorkloadInstance(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadInstance
helmWorkloadInstanceID := c.Param("id")
var existingHelmWorkloadInstance v0.HelmWorkloadInstance
if result := h.DB.First(&existingHelmWorkloadInstance, helmWorkloadInstanceID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// check for empty payload, invalid or unsupported fields, optional associations, etc.
if id, err := iapi.PayloadCheck(c, true, objectType, existingHelmWorkloadInstance); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
// bind payload
var updatedHelmWorkloadInstance v0.HelmWorkloadInstance
if err := c.Bind(&updatedHelmWorkloadInstance); err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
// update object in database
if result := h.DB.Model(&existingHelmWorkloadInstance).Updates(updatedHelmWorkloadInstance); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// notify controller if reconciliation is required
if !*existingHelmWorkloadInstance.Reconciled {
notifPayload, err := existingHelmWorkloadInstance.NotificationPayload(
notifications.NotificationOperationUpdated,
false,
time.Now().Unix(),
)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
h.JS.Publish(v0.HelmWorkloadInstanceUpdateSubject, *notifPayload)
}
response, err := v0.CreateResponse(nil, existingHelmWorkloadInstance, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}
// @Summary updates an existing helm workload instance by replacing the entire object.
// @Description Replace a helm workload instance in the database. All required fields must be provided.
// @Description If any optional fields are not provided, they will be null post-update.
// @Description Note: This API endpint is for updating helm workload instance objects only.
// @Description Request bodies that include related objects will be accepted, however
// @Description the related objects will not be changed. Call the patch or put method for
// @Description each particular existing object to change them.
// @ID replace-v0-helmWorkloadInstance
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Param helmWorkloadInstance body v0.HelmWorkloadInstance true "HelmWorkloadInstance object"
// @Success 200 {object} v0.Response "OK"
// @Failure 400 {object} v0.Response "Bad Request"
// @Failure 404 {object} v0.Response "Not Found"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-instances/{id} [PUT]
func (h Handler) ReplaceHelmWorkloadInstance(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadInstance
helmWorkloadInstanceID := c.Param("id")
var existingHelmWorkloadInstance v0.HelmWorkloadInstance
if result := h.DB.First(&existingHelmWorkloadInstance, helmWorkloadInstanceID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// check for empty payload, invalid or unsupported fields, optional associations, etc.
if id, err := iapi.PayloadCheck(c, true, objectType, existingHelmWorkloadInstance); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
// bind payload
var updatedHelmWorkloadInstance v0.HelmWorkloadInstance
if err := c.Bind(&updatedHelmWorkloadInstance); err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
// check for missing required fields
if id, err := iapi.ValidateBoundData(c, updatedHelmWorkloadInstance, objectType); err != nil {
return iapi.ResponseStatusErr(id, c, nil, errors.New(err.Error()), objectType)
}
// persist provided data
updatedHelmWorkloadInstance.ID = existingHelmWorkloadInstance.ID
if result := h.DB.Session(&gorm.Session{FullSaveAssociations: false}).Omit("CreatedAt", "DeletedAt").Save(&updatedHelmWorkloadInstance); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// reload updated data from DB
if result := h.DB.First(&existingHelmWorkloadInstance, helmWorkloadInstanceID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
response, err := v0.CreateResponse(nil, existingHelmWorkloadInstance, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}
// @Summary deletes a helm workload instance.
// @Description Delete a helm workload instance by ID from the database.
// @ID delete-v0-helmWorkloadInstance
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Success 200 {object} v0.Response "OK"
// @Failure 404 {object} v0.Response "Not Found"
// @Failure 409 {object} v0.Response "Conflict"
// @Failure 500 {object} v0.Response "Internal Server Error"
// @Router /v0/helm-workload-instances/{id} [DELETE]
func (h Handler) DeleteHelmWorkloadInstance(c echo.Context) error {
objectType := v0.ObjectTypeHelmWorkloadInstance
helmWorkloadInstanceID := c.Param("id")
var helmWorkloadInstance v0.HelmWorkloadInstance
if result := h.DB.First(&helmWorkloadInstance, helmWorkloadInstanceID); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return iapi.ResponseStatus404(c, nil, result.Error, objectType)
}
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// schedule for deletion if not already scheduled
// if scheduled and reconciled, delete object from DB
// if scheduled but not reconciled, return 409 (controller is working on it)
if helmWorkloadInstance.DeletionScheduled == nil {
// schedule for deletion
reconciled := false
timestamp := time.Now().UTC()
scheduledHelmWorkloadInstance := v0.HelmWorkloadInstance{
Reconciliation: v0.Reconciliation{
DeletionScheduled: ×tamp,
Reconciled: &reconciled,
}}
if result := h.DB.Model(&helmWorkloadInstance).Updates(scheduledHelmWorkloadInstance); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
// notify controller
notifPayload, err := helmWorkloadInstance.NotificationPayload(
notifications.NotificationOperationDeleted,
false,
time.Now().Unix(),
)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
h.JS.Publish(v0.HelmWorkloadInstanceDeleteSubject, *notifPayload)
} else {
if helmWorkloadInstance.DeletionConfirmed == nil {
// if deletion scheduled but not reconciled, return 409 - deletion
// already underway
return iapi.ResponseStatus409(c, nil, errors.New(fmt.Sprintf(
"object with ID %d already being deleted",
*helmWorkloadInstance.ID,
)), objectType)
} else {
// object scheduled for deletion and confirmed - it can be deleted
// from DB
if result := h.DB.Delete(&helmWorkloadInstance); result.Error != nil {
return iapi.ResponseStatus500(c, nil, result.Error, objectType)
}
}
}
response, err := v0.CreateResponse(nil, helmWorkloadInstance, objectType)
if err != nil {
return iapi.ResponseStatus500(c, nil, err, objectType)
}
return iapi.ResponseStatus200(c, *response)
}