-
Notifications
You must be signed in to change notification settings - Fork 1
/
servicegroup.go
66 lines (53 loc) · 2.29 KB
/
servicegroup.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
package servicegroup
import (
"errors"
"regexp"
"github.com/ScoreTrak/ScoreTrak/pkg/service"
"github.com/gofrs/uuid"
"gorm.io/gorm"
)
// ServiceGroup model describes a grouping of services.
type ServiceGroup struct {
ID uuid.UUID `json:"id,omitempty" gorm:"type:uuid;primary_key;"`
// Name is a name of the service group. This value is typically used when integrating with pkg/platform
Name string `json:"name" gorm:"not null;unique;default:null"`
// DisplayName is not used for anything other than displaying service group on web-ui
DisplayName string `json:"display_name,omitempty" gorm:"unique"`
// Enabled enables or disables the check_service
Enabled *bool `json:"enabled,omitempty" gorm:"not null;default:false"`
// SkipHelper Skips pkg/platform automation
SkipHelper bool `json:"skip_helper,omitempty" gorm:"-"`
// Label is used to help with deployment of workers when using pkg/platform. For instance, specifying Label:default, will deploy all workers in k8s's daemonset where nodes have a label scoretrak_worker: default
Label string `json:"label,omitempty"`
// Services represents all services that are part of a given service group
Services []*service.Service `json:"services,omitempty" gorm:"foreignkey:ServiceGroupID; constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT"`
}
func (ServiceGroup) TableName() string {
return "service_groups"
}
var ErrNameDoesNotPassValidation = errors.New(`name must pass following regex: "^[\.a-zA-Z0-9_-]+(#ephemeral)?$", and be less than 64 characters`)
// BeforeSave ensures that name is set to correct value. In particular, the name should adhere to ^[\.a-zA-Z0-9_-]+(#ephemeral)?$ regex as per https://github.com/nsqio/go-nsq/blob/04552936c57a26026c39e10a8993805e0f5a73d0/protocol.go
func (s *ServiceGroup) BeforeSave(tx *gorm.DB) (err error) {
if s.Name != "" {
if len(s.Name) > 1 && len(s.Name) <= 64 && regexp.MustCompile(`^[\.a-zA-Z0-9_-]+(#ephemeral)?$`).MatchString(s.Name) {
return
}
return ErrNameDoesNotPassValidation
}
return nil
}
var ErrNameIsRequired = errors.New("name is required")
// BeforeCreate ensures UUID is set.
func (s *ServiceGroup) BeforeCreate(tx *gorm.DB) (err error) {
if s.Name == "" {
return ErrNameIsRequired
}
if s.ID == uuid.Nil {
u, err := uuid.NewV4()
if err != nil {
return err
}
s.ID = u
}
return nil
}