Newer
Older
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
package app
import (
"context"
"log"
"code.fbi.h-da.de/danet/gosdn/controller/customerrs"
"code.fbi.h-da.de/danet/gosdn/controller/store"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// ManagementStore is a store for apps.
type ManagementStore interface {
Add(context.Context, App) error
Update(context.Context, App) error
Delete(context.Context, App) error
Get(context.Context, store.Query) (App, error)
GetAll(context.Context) ([]App, error)
}
const storeName = "app-store.json"
// Store stores registered apps.
type Store struct {
collection *mongo.Collection
}
// NewDatabaseAppStore returns a AppStore.
func NewDatabaseAppStore(db *mongo.Database) ManagementStore {
collection := db.Collection(storeName)
return &Store{
collection: collection,
}
}
// Get takes a app's UUID or name and returns the app.
func (s *Store) Get(ctx context.Context, query store.Query) (App, error) {
var loadedApp App
if query.ID.String() != "" && query.ID != uuid.Nil {
loadedApp, err := s.getByID(ctx, query.ID)
if err != nil {
return loadedApp, err
}
return loadedApp, nil
}
loadedApp, err := s.getByName(ctx, query.Name)
if err != nil {
return loadedApp, err
}
return loadedApp, nil
}
func (s *Store) getByID(ctx context.Context, appID uuid.UUID) (loadedApp App, err error) {
result := s.collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: appID.String()}})
if result == nil {
return loadedApp, customerrs.CouldNotFindError{ID: appID}
}
err = result.Decode(&loadedApp)
if err != nil {
log.Printf("Failed marshalling %v", err)
return loadedApp, customerrs.CouldNotMarshallError{Identifier: appID, Type: loadedApp, Err: err}
}
return loadedApp, nil
}
func (s *Store) getByName(ctx context.Context, appName string) (loadedApp App, err error) {
result := s.collection.FindOne(ctx, bson.D{primitive.E{Key: "name", Value: appName}})
if result == nil {
return loadedApp, customerrs.CouldNotFindError{Name: appName}
}
err = result.Decode(&loadedApp)
if err != nil {
log.Printf("Failed marshalling %v", err)
return loadedApp, customerrs.CouldNotMarshallError{Identifier: appName, Type: loadedApp, Err: err}
}
return loadedApp, nil
}
// GetAll returns all stored apps.
func (s *Store) GetAll(ctx context.Context) (loadedApps []App, err error) {
cursor, err := s.collection.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
err = cursor.All(ctx, &loadedApps)
if err != nil {
log.Printf("Failed marshalling %v", err)
return nil, customerrs.CouldNotMarshallError{Type: loadedApps, Err: err}
}
return loadedApps, nil
}
// Add adds a app to the app store.
func (s *Store) Add(ctx context.Context, app App) (err error) {
_, err = s.collection.InsertOne(ctx, app)
if err != nil {
log.Printf("Could not create app: %v", err)
return customerrs.CouldNotCreateError{Identifier: app.GetID(), Type: app, Err: err}
}
return nil
}
// Update updates a existing app.
func (s *Store) Update(ctx context.Context, app App) (err error) {
var updatedApp App
update := bson.D{primitive.E{Key: "$set", Value: app}}
upsert := false
after := options.After
opt := options.FindOneAndUpdateOptions{
Upsert: &upsert,
ReturnDocument: &after,
}
err = s.collection.FindOneAndUpdate(
ctx, bson.M{"_id": app.GetID().String()}, update, &opt).
Decode(&updatedApp)
if err != nil {
log.Printf("Could not update app: %v", err)
return customerrs.CouldNotUpdateError{Identifier: app.GetID().String(), Type: app, Err: err}
}
return nil
}
// Delete deletes a app from the app store.
func (s *Store) Delete(ctx context.Context, app App) (err error) {
_, err = s.collection.DeleteOne(ctx, bson.D{primitive.E{Key: app.GetID().String()}})
if err != nil {
return customerrs.CouldNotDeleteError{Identifier: app.GetID().String(), Type: app, Err: err}
}
return nil
}