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
/
consul.go
101 lines (89 loc) · 2.69 KB
/
consul.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
98
99
100
101
/**
* @Time : 2019/7/17 2:19 PM
* @Author : yuntinghu1003@gmail.com
* @File : consul
* @Software: GoLand
*/
package repository
import (
"github.com/hashicorp/consul/api"
"github.com/jinzhu/gorm"
"github.com/kplcloud/kplcloud/src/repository/types"
)
type ConsulRepository interface {
Find(ns, name string) (*types.Consul, bool)
FirstOrCreate(entry *api.ACLEntry, ns, name string) (consul *types.Consul, err error)
UpdateOrCreate(entry *api.ACLEntry, ns, name string) (err error)
Count(ns, name string) (count int, err error)
FindOffsetLimit(ns, name string, offset, limit int) (res []*types.Consul, err error)
Delete(ns, name string) error
}
type consul struct {
db *gorm.DB
}
func NewConsulReporitory(db *gorm.DB) ConsulRepository {
return &consul{db: db}
}
func (c *consul) Find(ns, name string) (*types.Consul, bool) {
var rs types.Consul
res := c.db.First(&rs, "namespace = ? AND name = ?", ns, name).RecordNotFound()
return &rs, res
}
func (c *consul) FirstOrCreate(entry *api.ACLEntry, ns, name string) (consul *types.Consul, err error) {
class := types.Consul{
Namespace: ns,
Name: name,
Type: entry.Type,
Rules: entry.Rules,
Token: entry.ID,
CreateIndex: int64(entry.CreateIndex),
ModifyIndex: int64(entry.ModifyIndex),
}
err = c.db.FirstOrCreate(&class, types.Consul{
Namespace: ns,
Name: name,
}).Error
return &class, err
}
func (c *consul) UpdateOrCreate(entry *api.ACLEntry, ns, name string) (err error) {
var temp types.Consul
consul, notExistState := c.Find(ns, name)
consul.Rules = entry.Rules
consul.Type = entry.Type
consul.Token = entry.ID
consul.CreateIndex = int64(entry.CreateIndex)
consul.ModifyIndex = int64(entry.ModifyIndex)
if notExistState == true {
return c.db.Model(&temp).Where("namespace = ? AND name = ?", ns, name).Update(consul).Error
} else {
consul.Name = name
consul.Namespace = ns
return c.db.Save(consul).Error
}
}
func (c *consul) Count(ns, name string) (count int, err error) {
query := c.db.Model(&types.Consul{})
if ns != "" {
query = query.Where("namespace = ?", ns)
}
if name != "" {
query = query.Where("name like ?", "%"+name+"%")
}
err = query.Count(&count).Error
return
}
func (c *consul) FindOffsetLimit(ns, name string, offset, limit int) (res []*types.Consul, err error) {
var list []*types.Consul
query := c.db.Model(&types.Consul{})
if ns != "" {
query = query.Where("namespace = ?", ns)
}
if name != "" {
query = query.Where("name like ?", "%"+name+"%")
}
err = query.Offset(offset).Limit(limit).Find(&list).Error
return list, err
}
func (c *consul) Delete(ns, name string) error {
return c.db.Where("namespace = ? AND name = ?", ns, name).Delete(types.Consul{}).Error
}