-
Notifications
You must be signed in to change notification settings - Fork 67
/
config_resource.go
170 lines (132 loc) 路 3.07 KB
/
config_resource.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
package configresource
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/resourcemanager"
)
var Operations = []resourcemanager.Operation{
resourcemanager.OperationGet,
resourcemanager.OperationUpdate,
}
type Config struct {
ID id.ID `mapstructure:"id"`
Name string `mapstructure:"name"`
AnalyticsEnabled bool `mapstructure:"analyticsEnabled"`
}
func (c Config) HasID() bool {
return c.ID.String() != ""
}
func (c Config) Validate() error {
return nil
}
func (c Config) IsAnalyticsEnabled() bool {
if os.Getenv("TRACETEST_DEV") != "" {
return false
}
return c.AnalyticsEnabled
}
type option func(*Repository)
func WithPublisher(p publisher) option {
return func(r *Repository) {
r.publisher = p
}
}
func NewRepository(db *sql.DB, opts ...option) *Repository {
repo := &Repository{
db: db,
}
for _, opt := range opts {
opt(repo)
}
return repo
}
const (
ResourceID = "/app/config/update"
ResourceName = "Config"
ResourceNamePlural = "Configs"
)
type publisher interface {
Publish(resourceID string, message any)
}
type Repository struct {
db *sql.DB
publisher publisher
}
func (r *Repository) publish(config Config) {
if r.publisher == nil {
return
}
r.publisher.Publish(ResourceID, config)
}
func (r *Repository) Current(ctx context.Context) Config {
cfg, err := r.Get(ctx, id.ID("current"))
if err != nil {
// TODO: log error
return defaultConfig
}
return cfg
}
const selectQuery = `SELECT "analytics_enabled" FROM config`
var defaultConfig = Config{
ID: id.ID("current"),
Name: "Config",
AnalyticsEnabled: true,
}
func (r *Repository) Get(ctx context.Context, i id.ID) (Config, error) {
cfg := defaultConfig
err := r.db.
QueryRowContext(ctx, selectQuery).
Scan(
&cfg.AnalyticsEnabled,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return cfg, nil
}
return Config{}, fmt.Errorf("sql query: %w", err)
}
return cfg, nil
}
const (
deleteQuery = "DELETE FROM config"
insertQuery = `INSERT INTO config ("analytics_enabled") VALUES ($1)`
)
func (r *Repository) Update(ctx context.Context, updated Config) (Config, error) {
// enforce ID and name
updated = Config{
ID: id.ID("current"),
Name: "Config",
AnalyticsEnabled: updated.AnalyticsEnabled,
}
tx, err := r.db.BeginTx(ctx, nil)
defer tx.Rollback()
if err != nil {
return Config{}, err
}
_, err = tx.ExecContext(ctx, deleteQuery)
if err != nil {
return Config{}, fmt.Errorf("sql exec delete: %w", err)
}
_, err = tx.ExecContext(ctx, insertQuery, updated.AnalyticsEnabled)
if err != nil {
return Config{}, fmt.Errorf("sql exec insert: %w", err)
}
err = tx.Commit()
if err != nil {
return Config{}, fmt.Errorf("commit: %w", err)
}
r.publish(updated)
return updated, nil
}
func (r *Repository) SetID(cfg Config, id id.ID) Config {
cfg.ID = id
return cfg
}
func (r *Repository) Provision(ctx context.Context, updated Config) error {
_, err := r.Update(ctx, updated)
return err
}