This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
/
service.go
147 lines (133 loc) · 4.09 KB
/
service.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
package notifications
import (
"context"
"github.com/gorilla/mux"
"github.com/leg100/otf/internal"
"github.com/leg100/otf/internal/logr"
"github.com/leg100/otf/internal/pubsub"
"github.com/leg100/otf/internal/rbac"
"github.com/leg100/otf/internal/sql"
"github.com/leg100/otf/internal/tfeapi"
)
type (
Service struct {
logr.Logger
workspaceAuthorizer internal.Authorizer // authorize workspaces actions
db *pgdb
api *tfe
broker *pubsub.Broker[*Config]
}
Options struct {
*sql.DB
*sql.Listener
*tfeapi.Responder
logr.Logger
WorkspaceAuthorizer internal.Authorizer
}
)
func NewService(opts Options) *Service {
svc := Service{
Logger: opts.Logger,
workspaceAuthorizer: opts.WorkspaceAuthorizer,
db: &pgdb{opts.DB},
}
svc.api = &tfe{
Service: &svc,
Responder: opts.Responder,
}
// Register with broker so that it can relay run events
svc.broker = pubsub.NewBroker(
opts.Logger,
opts.Listener,
"notification_configurations",
func(ctx context.Context, id string, action sql.Action) (*Config, error) {
if action == sql.DeleteAction {
return &Config{ID: id}, nil
}
return svc.db.get(ctx, id)
},
)
return &svc
}
func (s *Service) AddHandlers(r *mux.Router) {
s.api.addHandlers(r)
}
func (s *Service) Watch(ctx context.Context) (<-chan pubsub.Event[*Config], func()) {
return s.broker.Subscribe(ctx)
}
func (s *Service) Create(ctx context.Context, workspaceID string, opts CreateConfigOptions) (*Config, error) {
subject, err := s.workspaceAuthorizer.CanAccess(ctx, rbac.CreateNotificationConfigurationAction, workspaceID)
if err != nil {
return nil, err
}
nc, err := NewConfig(workspaceID, opts)
if err != nil {
s.Error(err, "constructing notification config", "subject", subject)
return nil, err
}
if err := s.db.create(ctx, nc); err != nil {
s.Error(err, "creating notification config", "config", nc, "subject", subject)
return nil, err
}
s.Info("creating notification config", "config", nc, "subject", subject)
return nc, nil
}
func (s *Service) Update(ctx context.Context, id string, opts UpdateConfigOptions) (*Config, error) {
var subject internal.Subject
updated, err := s.db.update(ctx, id, func(nc *Config) (err error) {
subject, err = s.workspaceAuthorizer.CanAccess(ctx, rbac.UpdateNotificationConfigurationAction, nc.WorkspaceID)
if err != nil {
return err
}
return nc.update(opts)
})
if err != nil {
s.Error(err, "updating notification config", "id", id, "subject", subject)
return nil, err
}
s.Info("updated notification config", "updated", updated, "subject", subject)
return updated, nil
}
func (s *Service) Get(ctx context.Context, id string) (*Config, error) {
nc, err := s.db.get(ctx, id)
if err != nil {
s.Error(err, "retrieving notification config", "id", id)
return nil, err
}
subject, err := s.workspaceAuthorizer.CanAccess(ctx, rbac.GetNotificationConfigurationAction, nc.WorkspaceID)
if err != nil {
return nil, err
}
s.V(9).Info("retrieved notification config", "config", nc, "subject", subject)
return nc, nil
}
func (s *Service) List(ctx context.Context, workspaceID string) ([]*Config, error) {
subject, err := s.workspaceAuthorizer.CanAccess(ctx, rbac.ListNotificationConfigurationsAction, workspaceID)
if err != nil {
return nil, err
}
configs, err := s.db.list(ctx, workspaceID)
if err != nil {
s.Error(err, "listing notification configs", "id", workspaceID)
return nil, err
}
s.V(9).Info("listed notification configs", "total", len(configs), "subject", subject)
return configs, nil
}
func (s *Service) Delete(ctx context.Context, id string) error {
nc, err := s.db.get(ctx, id)
if err != nil {
s.Error(err, "retrieving notification config", "id", id)
return err
}
subject, err := s.workspaceAuthorizer.CanAccess(ctx, rbac.DeleteNotificationConfigurationAction, nc.WorkspaceID)
if err != nil {
return err
}
if err := s.db.delete(ctx, id); err != nil {
s.Error(err, "deleting notification config", "id", id)
return err
}
s.Info("deleted notification config", "config", nc, "subject", subject)
return nil
}