186 lines
4.6 KiB
Go
186 lines
4.6 KiB
Go
package models
|
|
|
|
import (
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/iwind/TeaGo/Tea"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
"github.com/iwind/TeaGo/types"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
HTTPDNSClusterStateEnabled = 1
|
|
HTTPDNSClusterStateDisabled = 0
|
|
)
|
|
|
|
type HTTPDNSClusterDAO dbs.DAO
|
|
|
|
func NewHTTPDNSClusterDAO() *HTTPDNSClusterDAO {
|
|
return dbs.NewDAO(&HTTPDNSClusterDAO{
|
|
DAOObject: dbs.DAOObject{
|
|
DB: Tea.Env,
|
|
Table: "edgeHTTPDNSClusters",
|
|
Model: new(HTTPDNSCluster),
|
|
PkName: "id",
|
|
},
|
|
}).(*HTTPDNSClusterDAO)
|
|
}
|
|
|
|
var SharedHTTPDNSClusterDAO *HTTPDNSClusterDAO
|
|
|
|
func init() {
|
|
dbs.OnReady(func() {
|
|
SharedHTTPDNSClusterDAO = NewHTTPDNSClusterDAO()
|
|
})
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) CreateCluster(tx *dbs.Tx, name string, serviceDomain string, defaultTTL int32, fallbackTimeoutMs int32, installDir string, tlsPolicyJSON []byte, isOn bool, isDefault bool) (int64, error) {
|
|
if isDefault {
|
|
err := this.Query(tx).
|
|
State(HTTPDNSClusterStateEnabled).
|
|
Set("isDefault", false).
|
|
UpdateQuickly()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
var op = NewHTTPDNSClusterOperator()
|
|
op.Name = name
|
|
op.ServiceDomain = serviceDomain
|
|
op.DefaultTTL = defaultTTL
|
|
op.FallbackTimeoutMs = fallbackTimeoutMs
|
|
op.InstallDir = installDir
|
|
op.IsOn = isOn
|
|
op.IsDefault = isDefault
|
|
op.CreatedAt = time.Now().Unix()
|
|
op.UpdatedAt = time.Now().Unix()
|
|
op.State = HTTPDNSClusterStateEnabled
|
|
if len(tlsPolicyJSON) > 0 {
|
|
op.TLSPolicy = tlsPolicyJSON
|
|
}
|
|
err := this.Save(tx, op)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return types.Int64(op.Id), nil
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, serviceDomain string, defaultTTL int32, fallbackTimeoutMs int32, installDir string, tlsPolicyJSON []byte, isOn bool, isDefault bool) error {
|
|
if isDefault {
|
|
err := this.Query(tx).
|
|
State(HTTPDNSClusterStateEnabled).
|
|
Neq("id", clusterId).
|
|
Set("isDefault", false).
|
|
UpdateQuickly()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
var op = NewHTTPDNSClusterOperator()
|
|
op.Id = clusterId
|
|
op.Name = name
|
|
op.ServiceDomain = serviceDomain
|
|
op.DefaultTTL = defaultTTL
|
|
op.FallbackTimeoutMs = fallbackTimeoutMs
|
|
op.InstallDir = installDir
|
|
op.IsOn = isOn
|
|
op.IsDefault = isDefault
|
|
op.UpdatedAt = time.Now().Unix()
|
|
if len(tlsPolicyJSON) > 0 {
|
|
op.TLSPolicy = tlsPolicyJSON
|
|
}
|
|
return this.Save(tx, op)
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) DisableCluster(tx *dbs.Tx, clusterId int64) error {
|
|
_, err := this.Query(tx).
|
|
Pk(clusterId).
|
|
Set("state", HTTPDNSClusterStateDisabled).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) FindEnabledCluster(tx *dbs.Tx, clusterId int64) (*HTTPDNSCluster, error) {
|
|
one, err := this.Query(tx).
|
|
Pk(clusterId).
|
|
State(HTTPDNSClusterStateEnabled).
|
|
Find()
|
|
if one == nil {
|
|
return nil, err
|
|
}
|
|
return one.(*HTTPDNSCluster), nil
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) FindEnabledClusterName(tx *dbs.Tx, clusterId int64) (string, error) {
|
|
return this.Query(tx).
|
|
Pk(clusterId).
|
|
State(HTTPDNSClusterStateEnabled).
|
|
Result("name").
|
|
FindStringCol("")
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) ListEnabledClusters(tx *dbs.Tx, offset int64, size int64, keyword string) (result []*HTTPDNSCluster, err error) {
|
|
query := this.Query(tx).
|
|
State(HTTPDNSClusterStateEnabled).
|
|
AscPk()
|
|
if len(keyword) > 0 {
|
|
query = query.Where("(name LIKE :kw OR serviceDomain LIKE :kw)").Param("kw", "%"+keyword+"%")
|
|
}
|
|
if size > 0 {
|
|
query = query.Offset(offset).Limit(size)
|
|
}
|
|
_, err = query.Slice(&result).FindAll()
|
|
return
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) CountEnabledClusters(tx *dbs.Tx, keyword string) (int64, error) {
|
|
query := this.Query(tx).State(HTTPDNSClusterStateEnabled)
|
|
if len(keyword) > 0 {
|
|
query = query.Where("(name LIKE :kw OR serviceDomain LIKE :kw)").Param("kw", "%"+keyword+"%")
|
|
}
|
|
return query.Count()
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) FindAllEnabledClusters(tx *dbs.Tx) (result []*HTTPDNSCluster, err error) {
|
|
_, err = this.Query(tx).
|
|
State(HTTPDNSClusterStateEnabled).
|
|
AscPk().
|
|
Slice(&result).
|
|
FindAll()
|
|
return
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) FindDefaultPrimaryClusterId(tx *dbs.Tx) (int64, error) {
|
|
col, err := this.Query(tx).
|
|
State(HTTPDNSClusterStateEnabled).
|
|
Attr("isDefault", true).
|
|
Result("id").
|
|
AscPk().
|
|
FindCol(nil)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if col == nil {
|
|
return 0, nil
|
|
}
|
|
return types.Int64(col), nil
|
|
}
|
|
|
|
func (this *HTTPDNSClusterDAO) UpdateDefaultCluster(tx *dbs.Tx, clusterId int64) error {
|
|
err := this.Query(tx).
|
|
State(HTTPDNSClusterStateEnabled).
|
|
Set("isDefault", false).
|
|
UpdateQuickly()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = this.Query(tx).
|
|
Pk(clusterId).
|
|
State(HTTPDNSClusterStateEnabled).
|
|
Set("isDefault", true).
|
|
Update()
|
|
return err
|
|
}
|