37 Star 405 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
globaldns_store.go 1.99 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.3-rc3

搜索帮助