Newer
Older
package storage
import (
"context"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/errors"
)
type ExportConfiguration struct {
ExportName string
Contexts []string
Policies map[string]interface{}
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
}
type Storage struct {
exportConfig *mongo.Collection
logger *zap.Logger
}
func New(db *mongo.Client, dbname, collection string, logger *zap.Logger) (*Storage, error) {
if err := db.Ping(context.Background(), nil); err != nil {
return nil, err
}
return &Storage{
exportConfig: db.Database(dbname).Collection(collection),
logger: logger,
}, nil
}
func (s *Storage) ExportConfiguration(ctx context.Context, exportName string) (*ExportConfiguration, error) {
result := s.exportConfig.FindOne(ctx, bson.M{
"exportName": exportName,
})
if result.Err() != nil {
if strings.Contains(result.Err().Error(), "no documents in result") {
return nil, errors.New(errors.NotFound, "export configuration not found")
}
return nil, result.Err()
}
var expcfg ExportConfiguration
if err := result.Decode(&expcfg); err != nil {
return nil, err
}
return &expcfg, nil
}