From f0deb530e511c54db2a2f7495a6b2749c6c8a1a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Fri, 25 Jan 2019 21:04:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=88=A0=E9=99=A4routetables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/models/vpcs.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/compute/models/vpcs.go b/pkg/compute/models/vpcs.go index 87ac70cc8f..44dc3628c4 100644 --- a/pkg/compute/models/vpcs.go +++ b/pkg/compute/models/vpcs.go @@ -102,7 +102,7 @@ func (self *SVpc) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCr } func (self *SVpc) ValidateDeleteCondition(ctx context.Context) error { - if self.GetNetworkCount() > 0 || self.GetRouteTableCount() > 0 { + if self.GetNetworkCount() > 0 { return httperrors.NewNotEmptyError("VPC not empty") } if self.Id == DEFAULT_VPC_ID { @@ -150,8 +150,19 @@ func (self *SVpc) GetNetworkCount() int { return q.Count() } +func (self *SVpc) GetRouteTableQuery() *sqlchemy.SQuery { + return RouteTableManager.Query().Equals("vpc_id", self.Id) +} + +func (self *SVpc) GetRouteTables() []SRouteTable { + q := self.GetRouteTableQuery() + routes := []SRouteTable{} + db.FetchModelObjects(RouteTableManager, q, &routes) + return routes +} + func (self *SVpc) GetRouteTableCount() int { - return RouteTableManager.Query().Equals("vpc_id", self.Id).Count() + return self.GetRouteTableQuery().Count() } func (self *SVpc) getMoreDetails(extra *jsonutils.JSONDict) *jsonutils.JSONDict { @@ -479,6 +490,10 @@ func (self *SVpc) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCr func (self *SVpc) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error { db.OpsLog.LogEvent(self, db.ACT_DELOCATE, self.GetShortDesc(ctx), userCred) self.SetStatus(userCred, VPC_STATUS_DELETED, "real delete") + routes := self.GetRouteTables() + for i := 0; i < len(routes); i++ { + routes[i].RealDelete(ctx, userCred) + } return self.SEnabledStatusStandaloneResourceBase.Delete(ctx, userCred) }