This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
persistentvolumeclaim.go
85 lines (73 loc) · 2.37 KB
/
persistentvolumeclaim.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
/**
* @Time : 2019-06-26 15:14
* @Author : solacowa@gmail.com
* @File : persistentvolumeclaim
* @Software: GoLand
*/
package repository
import (
"encoding/json"
"github.com/jinzhu/gorm"
"github.com/kplcloud/kplcloud/src/repository/types"
"gopkg.in/guregu/null.v3"
)
type PvcRepository interface {
Find(ns, name string) (rs *types.PersistentVolumeClaim, err error)
FindBy(ns string, offset, limit int) (res []*types.PersistentVolumeClaim, err error)
Count(ns string) (int64, error)
Delete(ns, name string) (err error)
FindAll() (res []*types.PersistentVolumeClaim, err error)
FirstOrCreate(ns, name, accessModes, storage, storageClassName, detail, selector string, labels map[string]string) error
}
type pvc struct {
db *gorm.DB
}
func NewPvcRepository(db *gorm.DB) PvcRepository {
return &pvc{db: db}
}
func (c *pvc) FindAll() (res []*types.PersistentVolumeClaim, err error) {
err = c.db.Find(&res).Error
return
}
func (c *pvc) FirstOrCreate(ns, name, accessModes, storage, storageClassName, detail, selector string, labels map[string]string) error {
b, _ := json.Marshal(labels)
class := types.PersistentVolumeClaim{
Name: name,
Namespace: ns,
Detail: null.StringFrom(detail),
Labels: null.StringFrom(string(b)),
Selector: null.StringFrom(selector),
Storage: storage,
StorageClassName: null.StringFrom(storageClassName),
AccessModes: accessModes,
}
return c.db.FirstOrCreate(&class, types.PersistentVolumeClaim{
Namespace: ns,
Name: name,
}).Error
}
func (c *pvc) Find(ns, name string) (rs *types.PersistentVolumeClaim, err error) {
var res types.PersistentVolumeClaim
err = c.db.First(&res, "name = ? AND namespace = ?", name, ns).Error
return &res, nil
}
func (c *pvc) Delete(ns, name string) (err error) {
pvc := types.PersistentVolumeClaim{
Name: name,
Namespace: ns,
}
return c.db.Delete(&pvc, "name = ? AND namespace = ?", name, ns).Error
}
func (c *pvc) FindBy(ns string, offset, limit int) (res []*types.PersistentVolumeClaim, err error) {
err = c.db.Order(gorm.Expr("id DESC")).
Offset(offset).Limit(limit).
Find(&res, "namespace = ?", ns).Error
return
}
func (c *pvc) Count(ns string) (int64, error) {
var count int64
if err := c.db.Model(&types.PersistentVolumeClaim{}).Where("namespace = ? ", ns).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}