-
Notifications
You must be signed in to change notification settings - Fork 19
/
gorm.go
208 lines (174 loc) · 5.9 KB
/
gorm.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2016-2022 Fraunhofer AISEC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $$\ $$\ $$\ $$\
// $$ | $$ |\__| $$ |
// $$$$$$$\ $$ | $$$$$$\ $$\ $$\ $$$$$$$ |$$\ $$$$$$\ $$$$$$\ $$$$$$\
// $$ _____|$$ |$$ __$$\ $$ | $$ |$$ __$$ |$$ |\_$$ _| $$ __$$\ $$ __$$\
// $$ / $$ |$$ / $$ |$$ | $$ |$$ / $$ |$$ | $$ | $$ / $$ |$$ | \__|
// $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$\ $$ | $$ |$$ |
// \$$$$$$\ $$ |\$$$$$ |\$$$$$ |\$$$$$$ |$$ | \$$$ |\$$$$$ |$$ |
// \_______|\__| \______/ \______/ \_______|\__| \____/ \______/ \__|
//
// This file is part of Clouditor Community Edition.
package gorm
import (
"errors"
"fmt"
"clouditor.io/clouditor/api/assessment"
"clouditor.io/clouditor/api/auth"
"clouditor.io/clouditor/api/orchestrator"
"clouditor.io/clouditor/persistence"
"github.com/sirupsen/logrus"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
)
var log *logrus.Entry
type storage struct {
db *gorm.DB
// for options: (set default when not in opts)
dialector gorm.Dialector
config gorm.Config
// types contains all types that we need to auto-migrate into database tables
types []any
// maxConn is the maximum number of connections. 0 means unlimited.
maxConn int
}
// DefaultTypes contains a list of internal types that need to be migrated by default
var DefaultTypes = []any{
&auth.User{},
&orchestrator.CloudService{},
&assessment.MetricImplementation{},
&assessment.Metric{},
&orchestrator.Certificate{},
&orchestrator.State{},
&orchestrator.Requirement{},
}
// StorageOption is a functional option type to configure the GORM storage. E.g. WithInMemory or WithPostgres
type StorageOption func(*storage)
// WithInMemory is an option to configure Storage to use an in memory DB
func WithInMemory() StorageOption {
return func(s *storage) {
s.dialector = sqlite.Open(":memory:")
}
}
// WithMaxOpenConns is an option to configure the maximum number of open connections
func WithMaxOpenConns(max int) StorageOption {
return func(s *storage) {
s.maxConn = max
}
}
// WithPostgres is an option to configure Storage to use a Postgres DB
func WithPostgres(host string, port int16, user string, pw string, db string) StorageOption {
return func(s *storage) {
s.dialector = postgres.Open(fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=disable", user, pw, host, port, db))
}
}
// WithLogger is an option to configure Storage to use a Logger
func WithLogger(logger logger.Interface) StorageOption {
return func(s *storage) {
s.config.Logger = logger
}
}
// WithAdditionalAutoMigration is an option to add additional types to GORM's auto-migration.
func WithAdditionalAutoMigration(types ...any) StorageOption {
return func(s *storage) {
s.types = append(s.types, types...)
}
}
func init() {
log = logrus.WithField("component", "storage")
}
// NewStorage creates a new storage using GORM (which DB to use depends on the StorageOption)
func NewStorage(opts ...StorageOption) (s persistence.Storage, err error) {
log.Println("Creating storage")
// Create storage with default gorm config
g := &storage{
config: gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
},
types: DefaultTypes,
}
// Add options and/or override default ones
for _, o := range opts {
o(g)
}
if g.dialector == nil {
WithInMemory()(g)
}
g.db, err = gorm.Open(g.dialector, &g.config)
if err != nil {
return nil, err
}
if g.maxConn > 0 {
sql, err := g.db.DB()
if err != nil {
return nil, fmt.Errorf("could not retrieve sql.DB: %v", err)
}
sql.SetMaxOpenConns(g.maxConn)
}
// After successful DB initialization, migrate the schema
if err = g.db.AutoMigrate(g.types...); err != nil {
err = fmt.Errorf("error during auto-migration: %w", err)
return
}
s = g
return
}
func (s *storage) Create(r any) error {
return s.db.Create(r).Error
}
func (s *storage) Get(r any, conds ...any) (err error) {
// Preload all associations for r being filled with all items (including relationships)
err = s.db.Preload(clause.Associations).First(r, conds...).Error
// if record is not found, use the error message defined in the persistence package
if errors.Is(err, gorm.ErrRecordNotFound) {
err = persistence.ErrRecordNotFound
}
return
}
func (s *storage) List(r any, offset int, limit int, conds ...any) error {
var query = s.db
if limit != -1 {
query = s.db.Limit(limit)
}
return query.Offset(offset).Preload(clause.Associations).Find(r, conds...).Error
}
func (s *storage) Count(r any, conds ...any) (count int64, err error) {
err = s.db.Model(r).Where(conds).Count(&count).Error
return
}
func (s *storage) Save(r any, conds ...any) error {
return s.db.Where(conds).Save(r).Error
}
// Update will update the record with non-zero fields. Note that to get the entire updated record you have to call Get
func (s *storage) Update(r any, query any, args ...any) error {
return s.db.Model(r).Where(query, args).Updates(r).Error
}
// Delete deletes record with given id. If no record was found, returns ErrRecordNotFound
func (s *storage) Delete(r any, conds ...any) error {
// Remove record r with given ID
tx := s.db.Delete(r, conds...)
if err := tx.Error; err != nil { // db error
return err
}
// No record with given ID found
if tx.RowsAffected == 0 {
return persistence.ErrRecordNotFound
}
return nil
}