[ADD] add uuid for TcpRuleStruct

This commit is contained in:
GLYASAI 2018-11-22 10:38:55 +08:00
parent 0a20990804
commit 6bb5216e10
4 changed files with 48 additions and 35 deletions

View File

@ -41,15 +41,15 @@ func CreateGatewayManager(dbmanager db.Manager) *GatewayAction {
// AddHttpRule adds http rule to db if it doesn't exists.
func (g *GatewayAction) AddHttpRule(req *apimodel.HttpRuleStruct) error {
httpRule := &model.HttpRule{
UUID: req.HttpRuleID,
ServiceID: req.ServiceID,
ContainerPort: req.ContainerPort,
Domain: req.Domain,
Path: req.Path,
Header: req.Header,
Cookie: req.Cookie,
IP: req.IP,
CertificateID: req.CertificateID,
UUID: req.HttpRuleID,
ServiceID: req.ServiceID,
ContainerPort: req.ContainerPort,
Domain: req.Domain,
Path: req.Path,
Header: req.Header,
Cookie: req.Cookie,
IP: req.IP,
CertificateID: req.CertificateID,
}
// begin transaction
@ -72,10 +72,10 @@ func (g *GatewayAction) AddHttpRule(req *apimodel.HttpRuleStruct) error {
for _, ruleExtension := range req.RuleExtensions {
re := &model.RuleExtension{
UUID: util.NewUUID(),
UUID: util.NewUUID(),
RuleID: httpRule.UUID,
Key: ruleExtension.Key,
Value: ruleExtension.Value,
Key: ruleExtension.Key,
Value: ruleExtension.Value,
}
if err := db.GetManager().RuleExtensionDaoTransactions(tx).AddModel(re); err != nil {
tx.Rollback()
@ -93,7 +93,7 @@ func (g *GatewayAction) AddHttpRule(req *apimodel.HttpRuleStruct) error {
// UpdateHttpRule updates http rule
func (g *GatewayAction) UpdateHttpRule(req *apimodel.HttpRuleStruct) error {
tx := db.GetManager().Begin()
tx := db.GetManager().Begin()
rule, err := g.dbmanager.HttpRuleDaoTransactions(tx).GetHttpRuleByID(req.HttpRuleID)
if err != nil {
@ -116,10 +116,10 @@ func (g *GatewayAction) UpdateHttpRule(req *apimodel.HttpRuleStruct) error {
}
// add new certificate
cert := &model.Certificate{
UUID: req.CertificateID,
UUID: req.CertificateID,
CertificateName: req.CertificateName,
Certificate: req.Certificate,
PrivateKey: req.PrivateKey,
Certificate: req.Certificate,
PrivateKey: req.PrivateKey,
}
if err := g.dbmanager.CertificateDaoTransactions(tx).AddModel(cert); err != nil {
tx.Rollback()
@ -128,10 +128,10 @@ func (g *GatewayAction) UpdateHttpRule(req *apimodel.HttpRuleStruct) error {
// add new rule extensions
for _, ruleExtension := range req.RuleExtensions {
re := &model.RuleExtension{
UUID: util.NewUUID(),
UUID: util.NewUUID(),
RuleID: rule.UUID,
Key: ruleExtension.Key,
Value: ruleExtension.Value,
Key: ruleExtension.Key,
Value: ruleExtension.Value,
}
if err := db.GetManager().RuleExtensionDaoTransactions(tx).AddModel(re); err != nil {
tx.Rollback()
@ -222,11 +222,11 @@ func (g *GatewayAction) UpdateCertificate(req apimodel.HttpRuleStruct, httpRule
// AddTcpRule adds tcp rule.
func (g *GatewayAction) AddTcpRule(req *apimodel.TcpRuleStruct) error {
tcpRule := &model.TcpRule{
UUID: util.NewUUID(),
ServiceID: req.ServiceID,
ContainerPort: req.ContainerPort,
IP: req.IP,
Port: req.Port,
UUID: req.TcpRuleId,
ServiceID: req.ServiceID,
ContainerPort: req.ContainerPort,
IP: req.IP,
Port: req.Port,
}
// begin transaction
@ -262,8 +262,7 @@ func (g *GatewayAction) UpdateTcpRule(req *apimodel.TcpRuleStruct) error {
// begin transaction
tx := db.GetManager().Begin()
// get old tcp rule
tcpRule, err := g.dbmanager.TcpRuleDaoTransactions(tx).GetTcpRuleByServiceIDAndContainerPort(req.ServiceID,
req.ContainerPort)
tcpRule, err := g.dbmanager.TcpRuleDaoTransactions(tx).GetTcpRuleByID(req.TcpRuleId)
if err != nil {
tx.Rollback()
return err
@ -274,6 +273,8 @@ func (g *GatewayAction) UpdateTcpRule(req *apimodel.TcpRuleStruct) error {
return err
}
// update tcp rule
tcpRule.ServiceID = req.ServiceID
tcpRule.ContainerPort = req.ContainerPort
tcpRule.IP = req.IP
tcpRule.Port = req.Port
if err := g.dbmanager.TcpRuleDaoTransactions(tx).UpdateModel(tcpRule); err != nil {
@ -305,8 +306,7 @@ func (g *GatewayAction) UpdateTcpRule(req *apimodel.TcpRuleStruct) error {
func (g *GatewayAction) DeleteTcpRule(req *apimodel.TcpRuleStruct) error {
// begin transaction
tx := db.GetManager().Begin()
tcpRule, err := db.GetManager().TcpRuleDaoTransactions(tx).GetTcpRuleByServiceIDAndContainerPort(req.ServiceID,
req.ContainerPort)
tcpRule, err := db.GetManager().TcpRuleDaoTransactions(tx).GetTcpRuleByID(req.TcpRuleId)
if err != nil {
tx.Rollback()
return err

View File

@ -20,7 +20,7 @@ package model
//HttpRuleStruct -
type HttpRuleStruct struct {
HttpRuleID string `json:"http_rule_id" validate:"http_rule_id:required"`
HttpRuleID string `json:"http_rule_id" validate:"http_rule_id|required"`
ServiceID string `json:"service_id"`
ContainerPort int `json:"container_port"`
Domain string `json:"domain"`
@ -36,12 +36,12 @@ type HttpRuleStruct struct {
}
type TcpRuleStruct struct {
//TcpRuleId string `json:"tcp_rule_id" validate:"tcp_rule_id:required"`
ServiceID string `json:"service_id" validate:"service_id|required"`
ContainerPort int `json:"container_port" validate:"container_port|required"`
IP string `json:"ip"`
Port int `json:"port"`
RuleExtensions []*RuleExtensionStruct `json:"rule_extensions"`
TcpRuleId string `json:"tcp_rule_id" validate:"tcp_rule_id|required"`
ServiceID string `json:"service_id"`
ContainerPort int `json:"container_port"`
IP string `json:"ip"`
Port int `json:"port"`
RuleExtensions []*RuleExtensionStruct `json:"rule_extensions"`
}
type RuleExtensionStruct struct {

View File

@ -432,5 +432,6 @@ type HttpRuleDao interface {
type TcpRuleDao interface {
Dao
GetTcpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) (*model.TcpRule, error)
GetTcpRuleByID(id string) (*model.TcpRule, error)
DeleteTcpRule(tcpRule *model.TcpRule) error
}

View File

@ -245,6 +245,18 @@ func (s *TcpRuleDaoTmpl) GetTcpRuleByServiceIDAndContainerPort(serviceID string,
return result, nil
}
// GetTcpRuleByID gets a TcpRule based on tcpRuleID
func (s *TcpRuleDaoTmpl) GetTcpRuleByID(id string) (*model.TcpRule, error) {
result := &model.TcpRule{}
if err := s.DB.Where("uuid = ?", id).Find(result).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return result, nil
}
return nil, err
}
return result, nil
}
func (s *TcpRuleDaoTmpl) DeleteTcpRule(tcpRule *model.TcpRule) error {
return s.DB.Where("uuid = ?", tcpRule.UUID).Delete(tcpRule).Error
}