[ADD] delete gateway resources when deleting service

This commit is contained in:
GLYASAI 2018-12-28 12:33:37 +08:00
parent f6624ee8be
commit 47cf4fce33
4 changed files with 145 additions and 6 deletions

View File

@ -1607,6 +1607,29 @@ func (s *ServiceAction) TransServieToDelete(serviceID string) error {
return err
}
}
// delete gateway related resources
httpRules, err := db.GetManager().HttpRuleDaoTransactions(tx).ListByServiceID(serviceID)
if err != nil {
tx.Rollback()
return err
}
for _, r := range httpRules {
if err := db.GetManager().HttpRuleDaoTransactions(tx).DeleteHttpRuleByID(r.UUID); err != nil {
tx.Rollback()
return err
}
}
tcpRules, err := db.GetManager().TcpRuleDaoTransactions(tx).ListByServiceID(serviceID)
if err != nil {
tx.Rollback()
return err
}
for _, r := range tcpRules {
if err := db.GetManager().TcpRuleDaoTransactions(tx).DeleteTcpRule(r); err != nil {
tx.Rollback()
return err
}
}
//删除plugin etcd资源
prefixK := fmt.Sprintf("/resources/define/%s/%s", service.TenantID, service.ServiceAlias)
ctx, cancel := context.WithCancel(context.Background())

View File

@ -362,6 +362,7 @@ type HTTPRuleDao interface {
GetHttpRuleByID(id string) (*model.HTTPRule, error)
GetHttpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) ([]*model.HTTPRule, error)
DeleteHttpRuleByID(id string) error
ListByServiceID(serviceID string) ([]*model.HTTPRule, error)
}
// TCPRuleDao -
@ -370,6 +371,7 @@ type TCPRuleDao interface {
GetTcpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) ([]*model.TCPRule, error)
GetTcpRuleByID(id string) (*model.TCPRule, error)
DeleteTcpRule(tcpRule *model.TCPRule) error
ListByServiceID(serviceID string) ([]*model.TCPRule, error)
}
// IPPortDao -

View File

@ -37,7 +37,7 @@ func TestIPPortImpl_UpdateModel(t *testing.T) {
ipport := &model.IPPort{
UUID: util.NewUUID(),
IP: "127.0.0.1",
IP: "127.0.0.1",
Port: 8888,
}
if err := GetManager().IPPortDao().AddModel(ipport); err != nil {
@ -62,7 +62,7 @@ func TestIPPortImpl_DeleteIPPortByIPAndPort(t *testing.T) {
ipport := &model.IPPort{
UUID: util.NewUUID(),
IP: "127.0.0.1",
IP: "127.0.0.1",
Port: 8888,
}
if err := GetManager().IPPortDao().AddModel(ipport); err != nil {
@ -94,7 +94,7 @@ func TestIPPortImpl_GetIPByPort(t *testing.T) {
ipport := &model.IPPort{
UUID: util.NewUUID(),
IP: "127.0.0.1",
IP: "127.0.0.1",
Port: 8888,
}
if err := GetManager().IPPortDao().AddModel(ipport); err != nil {
@ -122,7 +122,7 @@ func TestIPPortImpl_GetIPPortByIPAndPort(t *testing.T) {
ipport := &model.IPPort{
UUID: util.NewUUID(),
IP: "127.0.0.1",
IP: "127.0.0.1",
Port: 8888,
}
if err := GetManager().IPPortDao().AddModel(ipport); err != nil {
@ -152,7 +152,7 @@ func TestIPPoolImpl_AddModel(t *testing.T) {
tx.Commit()
ippool := &model.IPPool{
EID: util.NewUUID(),
EID: util.NewUUID(),
CIDR: "192.168.11.11/24",
}
@ -182,7 +182,7 @@ func TestIPPoolImpl_UpdateModel(t *testing.T) {
tx.Commit()
ippool := &model.IPPool{
EID: util.NewUUID(),
EID: util.NewUUID(),
CIDR: "192.168.11.11/24",
}
@ -197,3 +197,99 @@ func TestIPPoolImpl_UpdateModel(t *testing.T) {
t.Errorf("Expected %s for CIDR, but returned %s", "192.168.22.22/24", ippool.CIDR)
}
}
func TestHTTPRuleImpl_ListByServiceID(t *testing.T) {
if err := CreateManager(dbconfig.Config{
DBType: "sqlite3",
}); err != nil {
t.Fatal(err)
}
tx := GetManager().Begin()
tx.Delete(model.HTTPRule{})
tx.Commit()
rules, err := GetManager().HttpRuleDao().ListByServiceID("")
if err != nil {
t.Fatal(err)
}
if len(rules) != 0 {
t.Errorf("Expected 0 for len(rules), but returned %v", len(rules))
}
serviceID := util.NewUUID()
rules = []*model.HTTPRule{
{
UUID: util.NewUUID(),
ServiceID: serviceID,
},
{
UUID: util.NewUUID(),
ServiceID: serviceID,
},
{
UUID: util.NewUUID(),
ServiceID: serviceID,
},
}
for _, rule := range rules {
err := GetManager().HttpRuleDao().AddModel(rule)
if err != nil {
t.Fatal(err)
}
}
rules, err = GetManager().HttpRuleDao().ListByServiceID(serviceID)
if err != nil {
t.Fatal(err)
}
if len(rules) != 3 {
t.Errorf("Expected 3 for len(rules), but returned %v", len(rules))
}
}
func TestTCPRuleImpl_ListByServiceID(t *testing.T) {
if err := CreateManager(dbconfig.Config{
DBType: "sqlite3",
}); err != nil {
t.Fatal(err)
}
tx := GetManager().Begin()
tx.Delete(model.TCPRule{})
tx.Commit()
rules, err := GetManager().TcpRuleDao().ListByServiceID("")
if err != nil {
t.Fatal(err)
}
if len(rules) != 0 {
t.Errorf("Expected 0 for len(rules), but returned %v", len(rules))
}
serviceID := util.NewUUID()
rules = []*model.TCPRule{
{
UUID: util.NewUUID(),
ServiceID: serviceID,
},
{
UUID: util.NewUUID(),
ServiceID: serviceID,
},
{
UUID: util.NewUUID(),
ServiceID: serviceID,
},
}
for _, rule := range rules {
err := GetManager().TcpRuleDao().AddModel(rule)
if err != nil {
t.Fatal(err)
}
}
rules, err = GetManager().TcpRuleDao().ListByServiceID(serviceID)
if err != nil {
t.Fatal(err)
}
if len(rules) != 3 {
t.Errorf("Expected 3 for len(rules), but returned %v", len(rules))
}
}

View File

@ -201,6 +201,15 @@ func (h *HttpRuleDaoImpl) DeleteHttpRuleByID(id string) error {
return nil
}
// ListByServiceID lists all HTTPRules matching serviceID
func (h *HttpRuleDaoImpl) ListByServiceID(serviceID string) ([]*model.HTTPRule, error) {
var rules []*model.HTTPRule
if err := h.DB.Where("service_id = ?", serviceID).Find(&rules).Error; err != nil {
return nil, err
}
return rules, nil
}
// TcpRuleDaoTmpl is a implementation of TcpRuleDao
type TcpRuleDaoTmpl struct {
DB *gorm.DB
@ -263,6 +272,15 @@ func (s *TcpRuleDaoTmpl) DeleteTcpRule(tcpRule *model.TCPRule) error {
return s.DB.Where("uuid = ?", tcpRule.UUID).Delete(tcpRule).Error
}
// ListByServiceID lists all TCPRules matching serviceID
func (h *TcpRuleDaoTmpl) ListByServiceID(serviceID string) ([]*model.TCPRule, error) {
var rules []*model.TCPRule
if err := h.DB.Where("service_id = ?", serviceID).Find(&rules).Error; err != nil {
return nil, err
}
return rules, nil
}
// IPPortImpl is an implementation of dao.IPPortDao
type IPPortImpl struct {
DB *gorm.DB