forked from raystack/frontier
/
relation_repository.go
238 lines (208 loc) · 6.58 KB
/
relation_repository.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package postgres
import (
"context"
"errors"
"fmt"
"strings"
"database/sql"
"github.com/doug-martin/goqu/v9"
"github.com/goto/shield/core/relation"
"github.com/goto/shield/internal/schema"
"github.com/goto/shield/pkg/db"
newrelic "github.com/newrelic/go-agent"
)
type RelationRepository struct {
dbc *db.Client
}
func NewRelationRepository(dbc *db.Client) *RelationRepository {
return &RelationRepository{
dbc: dbc,
}
}
func (r RelationRepository) Create(ctx context.Context, relationToCreate relation.RelationV2) (relation.RelationV2, error) {
query, params, err := dialect.Insert(TABLE_RELATIONS).Rows(
goqu.Record{
"subject_namespace_id": relationToCreate.Subject.Namespace,
"subject_id": relationToCreate.Subject.ID,
"object_namespace_id": relationToCreate.Object.NamespaceID,
"object_id": relationToCreate.Object.ID,
"role_id": schema.GetRoleID(relationToCreate.Object.NamespaceID, relationToCreate.Subject.RoleID),
}).OnConflict(
goqu.DoUpdate("subject_namespace_id, subject_id, object_namespace_id, object_id, role_id", goqu.Record{
"subject_namespace_id": relationToCreate.Subject.Namespace,
})).Returning(&relationCols{}).ToSQL()
if err != nil {
return relation.RelationV2{}, fmt.Errorf("%w: %s", queryErr, err)
}
var relationModel Relation
if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error {
nrCtx := newrelic.FromContext(ctx)
if nrCtx != nil {
nr := newrelic.DatastoreSegment{
Product: newrelic.DatastorePostgres,
Collection: TABLE_RELATIONS,
Operation: "Create",
StartTime: nrCtx.StartSegmentNow(),
}
defer nr.End()
}
return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&relationModel)
}); err != nil {
err = checkPostgresError(err)
switch {
case errors.Is(err, errForeignKeyViolation):
return relation.RelationV2{}, fmt.Errorf("%w: %s", relation.ErrInvalidDetail, err)
default:
return relation.RelationV2{}, err
}
}
return relationModel.transformToRelationV2(), nil
}
func (r RelationRepository) List(ctx context.Context) ([]relation.RelationV2, error) {
query, params, err := dialect.Select(&relationCols{}).From(TABLE_RELATIONS).ToSQL()
if err != nil {
return []relation.RelationV2{}, fmt.Errorf("%w: %s", queryErr, err)
}
var fetchedRelations []Relation
if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error {
nrCtx := newrelic.FromContext(ctx)
if nrCtx != nil {
nr := newrelic.DatastoreSegment{
Product: newrelic.DatastorePostgres,
Collection: TABLE_RELATIONS,
Operation: "List",
StartTime: nrCtx.StartSegmentNow(),
}
defer nr.End()
}
return r.dbc.SelectContext(ctx, &fetchedRelations, query, params...)
}); err != nil {
// List should return empty list and no error instead
if errors.Is(err, sql.ErrNoRows) {
return []relation.RelationV2{}, nil
}
return []relation.RelationV2{}, fmt.Errorf("%w: %s", dbErr, err)
}
var transformedRelations []relation.RelationV2
for _, r := range fetchedRelations {
transformedRelations = append(transformedRelations, r.transformToRelationV2())
}
return transformedRelations, nil
}
func (r RelationRepository) Get(ctx context.Context, id string) (relation.RelationV2, error) {
if strings.TrimSpace(id) == "" {
return relation.RelationV2{}, relation.ErrInvalidID
}
query, params, err := dialect.Select(&relationCols{}).From(TABLE_RELATIONS).
Where(goqu.Ex{
"id": id,
}).ToSQL()
if err != nil {
return relation.RelationV2{}, fmt.Errorf("%w: %s", queryErr, err)
}
var relationModel Relation
if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error {
nrCtx := newrelic.FromContext(ctx)
if nrCtx != nil {
nr := newrelic.DatastoreSegment{
Product: newrelic.DatastorePostgres,
Collection: TABLE_RELATIONS,
Operation: "Get",
StartTime: nrCtx.StartSegmentNow(),
}
defer nr.End()
}
return r.dbc.GetContext(ctx, &relationModel, query, params...)
}); err != nil {
err = checkPostgresError(err)
switch {
case errors.Is(err, sql.ErrNoRows):
return relation.RelationV2{}, relation.ErrNotExist
case errors.Is(err, errInvalidTexRepresentation):
return relation.RelationV2{}, relation.ErrInvalidUUID
default:
return relation.RelationV2{}, err
}
}
return relationModel.transformToRelationV2(), nil
}
func (r RelationRepository) DeleteByID(ctx context.Context, id string) error {
if strings.TrimSpace(id) == "" {
return relation.ErrInvalidID
}
query, params, err := dialect.Delete(TABLE_RELATIONS).Where(goqu.Ex{
"id": id,
}).ToSQL()
if err != nil {
return fmt.Errorf("%w: %s", queryErr, err)
}
return r.dbc.WithTimeout(ctx, func(ctx context.Context) error {
nrCtx := newrelic.FromContext(ctx)
if nrCtx != nil {
nr := newrelic.DatastoreSegment{
Product: newrelic.DatastorePostgres,
Collection: TABLE_RELATIONS,
Operation: "DeleteByID",
StartTime: nrCtx.StartSegmentNow(),
}
defer nr.End()
}
result, err := r.dbc.ExecContext(ctx, query, params...)
if err != nil {
err = checkPostgresError(err)
switch {
case errors.Is(err, errInvalidTexRepresentation):
return relation.ErrInvalidUUID
default:
return err
}
}
count, err := result.RowsAffected()
if err != nil {
return err
}
if count > 0 {
return nil
}
// TODO make this idempotent
return relation.ErrNotExist
})
}
// Update TO_DEPRECIATE
func (r RelationRepository) Update(ctx context.Context, rel relation.Relation) (relation.Relation, error) {
return relation.Relation{}, nil
}
func (r RelationRepository) GetByFields(ctx context.Context, rel relation.RelationV2) (relation.RelationV2, error) {
var fetchedRelation Relation
like := "%:" + rel.Subject.RoleID
query, _, err := dialect.Select(&relationCols{}).From(TABLE_RELATIONS).Where(goqu.Ex{
"subject_id": rel.Subject.ID,
"object_id": rel.Object.ID,
"role_id": goqu.Op{"like": like},
}).ToSQL()
if err != nil {
return relation.RelationV2{}, fmt.Errorf("%w: %s", queryErr, err)
}
if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error {
nrCtx := newrelic.FromContext(ctx)
if nrCtx != nil {
nr := newrelic.DatastoreSegment{
Product: newrelic.DatastorePostgres,
Collection: TABLE_RELATIONS,
Operation: "GetByFields",
StartTime: nrCtx.StartSegmentNow(),
}
defer nr.End()
}
return r.dbc.GetContext(ctx, &fetchedRelation, query)
}); err != nil {
err = checkPostgresError(err)
switch {
case errors.Is(err, sql.ErrNoRows):
return relation.RelationV2{}, relation.ErrNotExist
default:
return relation.RelationV2{}, err
}
}
return fetchedRelation.transformToRelationV2(), nil
}