forked from OctopusDeploy/go-octopusdeploy
/
machine_cleanup_policy.go
72 lines (60 loc) · 2.21 KB
/
machine_cleanup_policy.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
package octopusdeploy
import (
"encoding/json"
"time"
"github.com/go-playground/validator/v10"
"github.com/go-playground/validator/v10/non-standard/validators"
)
type MachineCleanupPolicy struct {
DeleteMachinesBehavior string `json:"DeleteMachinesBehavior" validate:"required,oneof=DeleteUnavailableMachines DoNotDelete"`
DeleteMachinesElapsedTimeSpan time.Duration `json:"DeleteMachinesElapsedTimeSpan,omitempty"`
}
func NewMachineCleanupPolicy() *MachineCleanupPolicy {
return &MachineCleanupPolicy{
DeleteMachinesBehavior: "DoNotDelete",
DeleteMachinesElapsedTimeSpan: time.Hour,
}
}
// MarshalJSON returns a machine policy as its JSON encoding.
func (m *MachineCleanupPolicy) MarshalJSON() ([]byte, error) {
machineCleanupPolicy := struct {
DeleteMachinesBehavior string `json:"DeleteMachinesBehavior" validate:"required,oneof=DeleteUnavailableMachines DoNotDelete"`
DeleteMachinesElapsedTimeSpan string `json:"DeleteMachinesElapsedTimeSpan,omitempty"`
}{
DeleteMachinesBehavior: m.DeleteMachinesBehavior,
DeleteMachinesElapsedTimeSpan: ToTimeSpan(m.DeleteMachinesElapsedTimeSpan),
}
return json.Marshal(machineCleanupPolicy)
}
// UnmarshalJSON sets this Kubernetes endpoint to its representation in JSON.
func (m *MachineCleanupPolicy) UnmarshalJSON(data []byte) error {
var fields struct {
DeleteMachinesBehavior string `json:"DeleteMachinesBehavior" validate:"required,oneof=DeleteUnavailableMachines DoNotDelete"`
DeleteMachinesElapsedTimeSpan string `json:"DeleteMachinesElapsedTimeSpan,omitempty"`
}
err := json.Unmarshal(data, &fields)
if err != nil {
return err
}
// validate JSON representation
v := validator.New()
err = v.Struct(fields)
if err != nil {
return err
}
if len(fields.DeleteMachinesElapsedTimeSpan) > 0 {
m.DeleteMachinesElapsedTimeSpan = FromTimeSpan(fields.DeleteMachinesElapsedTimeSpan)
}
m.DeleteMachinesBehavior = fields.DeleteMachinesBehavior
return nil
}
// Validate checks the state of the machine policy and returns an error if
// invalid.
func (m *MachineCleanupPolicy) Validate() error {
v := validator.New()
err := v.RegisterValidation("notblank", validators.NotBlank)
if err != nil {
return err
}
return v.Struct(m)
}