-
Notifications
You must be signed in to change notification settings - Fork 6
/
program.go
155 lines (135 loc) · 4.68 KB
/
program.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
/*
Copyright 2021 Adevinta
*/
package api
import (
"errors"
"fmt"
vulcanerrors "github.com/adevinta/errors"
"time"
)
var (
// ErrInvalidProgramGroupPolicy is returned when any of the groups of
// policies in a program does not have
ErrInvalidProgramGroupPolicy = errors.New("the program must have, at least, one asset and one checktype")
// ErrNoProgramsGroupsPolicies is returned when there are any policy group
// with, at least, one asset and checktype.
ErrNoProgramsGroupsPolicies = errors.New("no PoliciesGroups defined in the current program")
)
type Program struct {
ID string `gorm:"primary_key;AUTO_INCREMENT" json:"id" sql:"DEFAULT:gen_random_uuid()"`
TeamID string
Team *Team
ProgramsGroupsPolicies []*ProgramsGroupsPolicies `json:"program_policies" validate:"required"`
Name string `json:"name" validate:"required"`
Cron string `gorm:"-" json:"cron"` // A program can have empty cron expression, e.g: a program to be run on demand.
Autosend *bool `json:"autosend"`
Disabled *bool `json:"disabled"`
Global *bool `gorm:"-" json:"global"`
CreatedAt *time.Time `json:"-"`
UpdatedAt *time.Time `json:"-"`
}
// ValidateGroupsPolicies validates that at least one of the groups policies in
// a program have, at least, one asset and one checktype.
func (p Program) ValidateGroupsPolicies() error {
if len(p.ProgramsGroupsPolicies) < 1 {
return vulcanerrors.Validation(fmt.Errorf("%w: %v ", ErrNoProgramsGroupsPolicies, p.ID))
}
var err error
for _, gp := range p.ProgramsGroupsPolicies {
if err = gp.Validate(); err == nil {
return nil
}
}
return err
}
// ProgramsGroupsPolicies defines the association between a group and a policy in a
// program.
type ProgramsGroupsPolicies struct {
ProgramID string `gorm:"primary_key" json:"program_id" validate:"required"`
Program *Program
PolicyID string `gorm:"primary_key" json:"policy_id" validate:"required"`
Policy *Policy
GroupID string `gorm:"primary_key" json:"group_id" validate:"required"`
Group *Group
}
// Validate that the ProgramsGroupsPolicies have, at least, one asset and one
// checktype in the groups policies list.
func (p ProgramsGroupsPolicies) Validate() error {
if p.Group == nil || len(p.Group.AssetGroup) < 1 {
return vulcanerrors.Validation(ErrInvalidProgramGroupPolicy)
}
if p.Policy == nil || len(p.Policy.ChecktypeSettings) < 1 {
return vulcanerrors.Validation(ErrInvalidProgramGroupPolicy)
}
return nil
}
type ProgramResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Global bool `json:"global"`
Schedule ScheduleResponse `json:"schedule"`
Autosend bool `json:"autosend"`
Disabled bool `json:"disabled"`
PolicyGroups []PolicyGroup `json:"policy_groups"`
}
type PolicyGroup struct {
Group *GroupResponse `json:"group"`
Policy *PolicyResponse `json:"policy"`
}
type ScheduleResponse struct {
Cron string `json:"cron"`
}
func (p Program) ToResponse() *ProgramResponse {
var autosend bool
// Autosend should be never nil this is just to avoid panics if for whatever
// reason this precondition is not meet.
if p.Autosend != nil {
autosend = *p.Autosend
}
var disabled bool
// Disabled should be never nil this is just to avoid panics if for whatever
// reason this precondition is not meet.
if p.Disabled != nil {
disabled = *p.Disabled
}
var global bool
if p.Global != nil {
global = *p.Global
}
response := ProgramResponse{
ID: p.ID,
Name: p.Name,
Schedule: ScheduleResponse{Cron: p.Cron},
Autosend: autosend,
Disabled: disabled,
PolicyGroups: []PolicyGroup{},
Global: global,
}
for _, v := range p.ProgramsGroupsPolicies {
v := v
if v == nil {
continue
}
policyGroup := PolicyGroup{}
if v.Group != nil {
policyGroup.Group = v.Group.ToResponse()
}
if v.Policy != nil {
policyGroup.Policy = v.Policy.ToResponse()
}
response.PolicyGroups = append(response.PolicyGroups, policyGroup)
}
return &response
}
// GlobalProgramsMetadata defines the shape of the metadata stored
// per team for a given global program.
type GlobalProgramsMetadata struct {
TeamID string `gorm:"primary_key"`
Program string `gorm:"primary_key"`
Autosend *bool
Disabled *bool
Cron string `gorm:"-" json:"cron"` // A program can have empty cron expression, e.g: a program to be run on demand.
CreatedAt *time.Time
UpdatedAt *time.Time
}