代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。