-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
97 lines (85 loc) · 2.75 KB
/
store.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
// Copyright 2024 Linka Cloud All rights reserved.
//
// 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.
package openfga
import (
"context"
"errors"
"fmt"
"time"
openfgav1 "github.com/openfga/api/proto/openfga/v1"
parser "github.com/openfga/language/pkg/go/transformer"
)
type store struct {
id string
name string
createdAt time.Time
updatedAt time.Time
c *client
}
func (s *store) ID() string {
return s.id
}
func (s *store) Name() string {
return s.name
}
func (s *store) CreatedAt() time.Time {
return s.createdAt
}
func (s *store) UpdatedAt() time.Time {
return s.updatedAt
}
func (s *store) AuthorizationModel(ctx context.Context, id string) (Model, error) {
res, err := s.c.c.ReadAuthorizationModel(ctx, &openfgav1.ReadAuthorizationModelRequest{StoreId: s.id, Id: id})
if err != nil {
return nil, err
}
return &model{m: res.AuthorizationModel, s: s}, nil
}
func (s *store) ListAuthorizationModels(ctx context.Context) ([]Model, error) {
res, err := s.c.c.ReadAuthorizationModels(ctx, &openfgav1.ReadAuthorizationModelsRequest{StoreId: s.id})
if err != nil {
return nil, fmt.Errorf("failed to list authorization models: %w", err)
}
var models []Model
for _, m := range res.AuthorizationModels {
models = append(models, &model{m: m, s: s})
}
return models, nil
}
func (s *store) LastAuthorizationModel(ctx context.Context) (Model, error) {
res, err := s.c.c.ReadAuthorizationModels(ctx, &openfgav1.ReadAuthorizationModelsRequest{StoreId: s.id})
if err != nil {
return nil, err
}
if len(res.AuthorizationModels) == 0 {
return nil, errors.New("not found")
}
return &model{m: res.AuthorizationModels[len(res.AuthorizationModels)-1], s: s}, nil
}
func (s *store) WriteAuthorizationModel(ctx context.Context, dsl string) (Model, error) {
m, err := parser.TransformDSLToProto(dsl)
if err != nil {
return nil, fmt.Errorf("failed to parse model: %w", err)
}
res, err := s.c.c.WriteAuthorizationModel(ctx, &openfgav1.WriteAuthorizationModelRequest{
StoreId: s.id,
TypeDefinitions: m.TypeDefinitions,
SchemaVersion: m.SchemaVersion,
Conditions: m.Conditions,
})
if err != nil {
return nil, fmt.Errorf("failed to write authorization model: %w", err)
}
return s.AuthorizationModel(ctx, res.AuthorizationModelId)
}