-
Notifications
You must be signed in to change notification settings - Fork 2k
/
database.go
96 lines (83 loc) · 2.71 KB
/
database.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
// Copyright © 2023 sealos.
//
// 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 database
import (
"context"
"github.com/labring/sealos/controllers/license/internal/util/meta"
"github.com/labring/sealos/controllers/pkg/utils/logger"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
mongoOptions "go.mongodb.org/mongo-driver/mongo/options"
)
type DataBase struct {
URI string
Client *mongo.Client
licenseCollection *mongo.Collection
clusterIDCollection *mongo.Collection
}
const (
DefaultLicenseDataBase = "sealos-license"
DefaultLicenseCollection = "license"
DefaultClusterIDCollection = "cluster-id"
)
func New(ctx context.Context, uri string) (*DataBase, error) {
client, err := mongo.Connect(ctx, mongoOptions.Client().ApplyURI(uri))
if err != nil {
return &DataBase{}, err
}
if err := client.Ping(ctx, nil); err != nil {
return nil, err
}
return &DataBase{
URI: uri,
Client: client,
licenseCollection: client.Database(DefaultLicenseDataBase).Collection(DefaultLicenseCollection),
clusterIDCollection: client.Database(DefaultLicenseDataBase).Collection(DefaultClusterIDCollection),
}, nil
}
func (db *DataBase) StoreLicenseMeta(ctx context.Context, meta *meta.Meta) error {
_, err := db.licenseCollection.InsertOne(ctx, meta)
return err
}
func (db *DataBase) GetLicenseMeta(ctx context.Context, token string) (*meta.Meta, error) {
filter := bson.M{"token": token}
lic := &meta.Meta{}
err := db.licenseCollection.FindOne(ctx, filter).Decode(lic)
return lic, err
}
func (db *DataBase) GetClusterID(ctx context.Context) (string, error) {
var result struct {
ClusterID string `bson:"cluster-id"`
}
err := db.licenseCollection.FindOne(ctx, bson.M{}).Decode(&result)
if err != nil {
return "", err
}
return result.ClusterID, nil
}
func (db *DataBase) StoreClusterID(ctx context.Context, clusterID string) error {
document := bson.M{"cluster-id": clusterID}
_, err := db.licenseCollection.InsertOne(ctx, document)
if err != nil {
return err
}
return nil
}
func (db *DataBase) Disconnect(ctx context.Context) {
err := db.Client.Disconnect(ctx)
if err != nil {
logger.Error(err, "disconnect from database failed")
return
}
}