mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(region): aws vpc delete related route table & internet gateway
This commit is contained in:
@@ -1448,3 +1448,55 @@ func (self *SAwsRegionDriver) ValidateCreateVpcData(ctx context.Context, userCre
|
||||
}
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func (self *SAwsRegionDriver) RequestDeleteVpc(ctx context.Context, userCred mcclient.TokenCredential, region *models.SCloudregion, vpc *models.SVpc, task taskman.ITask) error {
|
||||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||||
provider := vpc.GetCloudprovider()
|
||||
if provider == nil {
|
||||
return nil, fmt.Errorf("vpc %s(%s) related provider not found", vpc.GetName(), vpc.GetName())
|
||||
}
|
||||
|
||||
region, err := vpc.GetIRegion()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "vpc.GetIRegion")
|
||||
}
|
||||
ivpc, err := region.GetIVpcById(vpc.GetExternalId())
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
// already deleted, do nothing
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "region.GetIVpcById")
|
||||
}
|
||||
|
||||
// remove related secgroups
|
||||
segs, err := ivpc.GetISecurityGroups()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetISecurityGroups")
|
||||
}
|
||||
|
||||
for i := range segs {
|
||||
err = segs[i].Delete()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "DeleteSecurityGroup")
|
||||
}
|
||||
}
|
||||
|
||||
_, _, result := models.SecurityGroupCacheManager.SyncSecurityGroupCaches(ctx, userCred, provider, []cloudprovider.ICloudSecurityGroup{}, vpc)
|
||||
if result.IsError() {
|
||||
return nil, fmt.Errorf("SyncSecurityGroupCaches %s", result.Result())
|
||||
}
|
||||
|
||||
err = ivpc.Delete()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "ivpc.Delete")
|
||||
}
|
||||
err = cloudprovider.WaitDeleted(ivpc, 10*time.Second, 300*time.Second)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cloudprovider.WaitDeleted")
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -322,3 +322,15 @@ func (self *SRegion) GetRouteTable(id string) (*SRouteTable, error) {
|
||||
return nil, errors.ErrDuplicateId
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SRegion) DeleteRouteTable(rid string) error {
|
||||
input := &ec2.DeleteRouteTableInput{}
|
||||
input.SetRouteTableId(rid)
|
||||
|
||||
_, err := self.ec2Client.DeleteRouteTable(input)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "DeleteRouteTable")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
+71
-10
@@ -162,11 +162,24 @@ delete all security groups associated with the VPC (except the default one),
|
||||
delete all route tables associated with the VPC (except the default one), and so on.
|
||||
*/
|
||||
func (self *SVpc) Delete() error {
|
||||
err := self.DetachInternetGateway()
|
||||
err := self.DeleteInternetGateways()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "DetachInternetGateway")
|
||||
return errors.Wrap(err, "DeleteInternetGateways")
|
||||
}
|
||||
// 删除vpc会同步删除关联的安全组
|
||||
|
||||
// 删除路由表. todo: 3.7版本路由表开放之后,需要同步状态到平台
|
||||
rts, err := self.GetIRouteTables()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "GetIRouteTables")
|
||||
}
|
||||
|
||||
for i := range rts {
|
||||
err = self.region.DeleteRouteTable(rts[i].GetId())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "DeleteRouteTable")
|
||||
}
|
||||
}
|
||||
|
||||
return self.region.DeleteVpc(self.VpcId)
|
||||
}
|
||||
|
||||
@@ -226,9 +239,13 @@ func (self *SVpc) assignSecurityGroup(secgroupId string, instanceId string) erro
|
||||
}
|
||||
|
||||
func (self *SVpc) fetchSecurityGroups() error {
|
||||
if len(self.VpcId) == 0 {
|
||||
return fmt.Errorf("fetchSecurityGroups vpc id is empty")
|
||||
}
|
||||
|
||||
secgroups, _, err := self.region.GetSecurityGroups(self.VpcId, "", "", 0, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "GetSecurityGroups")
|
||||
}
|
||||
|
||||
self.secgroups = make([]cloudprovider.ICloudSecurityGroup, len(secgroups))
|
||||
@@ -425,7 +442,7 @@ func (self *SVpc) AttachInternetGateway(igwId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SVpc) DetachInternetGateway() error {
|
||||
func (self *SVpc) DetachInternetGateways() error {
|
||||
igws, err := self.region.GetInternetGateways(self.GetId())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "GetInternetGateways")
|
||||
@@ -433,11 +450,7 @@ func (self *SVpc) DetachInternetGateway() error {
|
||||
|
||||
if len(igws) > 0 {
|
||||
for i := range igws {
|
||||
input := ec2.DetachInternetGatewayInput{}
|
||||
input.SetInternetGatewayId(igws[i].InternetGatewayID)
|
||||
input.SetVpcId(self.GetId())
|
||||
|
||||
_, err := self.region.ec2Client.DetachInternetGateway(&input)
|
||||
err = self.DetachInternetGateway(igws[i].GetId())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "DetachInternetGateway")
|
||||
}
|
||||
@@ -447,6 +460,54 @@ func (self *SVpc) DetachInternetGateway() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SVpc) DetachInternetGateway(igwId string) error {
|
||||
input := ec2.DetachInternetGatewayInput{}
|
||||
input.SetInternetGatewayId(igwId)
|
||||
input.SetVpcId(self.GetId())
|
||||
|
||||
_, err := self.region.ec2Client.DetachInternetGateway(&input)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "DetachInternetGateway")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SVpc) DeleteInternetGateway(igwId string) error {
|
||||
input := ec2.DeleteInternetGatewayInput{}
|
||||
input.SetInternetGatewayId(igwId)
|
||||
|
||||
_, err := self.region.ec2Client.DeleteInternetGateway(&input)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "DeleteInternetGateway")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SVpc) DeleteInternetGateways() error {
|
||||
igws, err := self.region.GetInternetGateways(self.GetId())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "GetInternetGateways")
|
||||
}
|
||||
|
||||
if len(igws) > 0 {
|
||||
for i := range igws {
|
||||
err = self.DetachInternetGateway(igws[i].GetId())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "DetachInternetGateway")
|
||||
}
|
||||
|
||||
err = self.DeleteInternetGateway(igws[i].GetId())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "DeleteInternetGateway")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SRegion) getVpc(vpcId string) (*SVpc, error) {
|
||||
if len(vpcId) == 0 {
|
||||
return nil, fmt.Errorf("GetVpc vpc id should not be empty.")
|
||||
|
||||
Reference in New Issue
Block a user