-
Notifications
You must be signed in to change notification settings - Fork 0
/
globaldns_store.go
78 lines (59 loc) · 1.99 KB
/
globaldns_store.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
package globaldns
import (
"strings"
"sync"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
managementv3 "github.com/rancher/types/client/management/v3"
)
func Wrap(store types.Store) types.Store {
storeWrapped := &Store{
Store: store,
}
storeWrapped.mu = sync.Mutex{}
return storeWrapped
}
type Store struct {
types.Store
mu sync.Mutex
}
func (p *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
fqdn := convert.ToString(data[managementv3.GlobalDNSFieldFQDN])
p.mu.Lock()
defer p.mu.Unlock()
if err := canUseFQDN(apiContext, fqdn); err != nil {
return nil, err
}
return p.Store.Create(apiContext, schema, data)
}
func (p *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
updatedFQDN := convert.ToString(data[managementv3.GlobalDNSFieldFQDN])
existingGlobalDNS, err := p.ByID(apiContext, schema, id)
if err != nil {
return nil, err
}
fqdn := convert.ToString(existingGlobalDNS[managementv3.GlobalDNSFieldFQDN])
if !strings.EqualFold(updatedFQDN, fqdn) {
p.mu.Lock()
defer p.mu.Unlock()
if err := canUseFQDN(apiContext, updatedFQDN); err != nil {
return nil, err
}
}
return p.Store.Update(apiContext, schema, data, id)
}
func canUseFQDN(apiContext *types.APIContext, fqdnRequested string) error {
var globalDNSs []managementv3.GlobalDNS
conditions := []*types.QueryCondition{
types.NewConditionFromString(managementv3.GlobalDNSFieldFQDN, types.ModifierEQ, []string{fqdnRequested}...),
}
if err := access.List(apiContext, apiContext.Version, managementv3.GlobalDNSType, &types.QueryOptions{Conditions: conditions}, &globalDNSs); err != nil {
return err
}
if len(globalDNSs) > 0 {
return httperror.NewFieldAPIError(httperror.NotUnique, managementv3.GlobalDNSFieldFQDN, "")
}
return nil
}