From 24c3a0861e35dadcca99769bae10cf282cb59493 Mon Sep 17 00:00:00 2001 From: ioito Date: Mon, 30 Oct 2023 16:14:30 +0800 Subject: [PATCH] fix(azure): secgroup delete --- pkg/multicloud/aliyun/vpc.go | 10 -------- pkg/multicloud/apsara/vpc.go | 14 ------------ pkg/multicloud/azure/instancenic.go | 4 ++-- pkg/multicloud/azure/securitygroup.go | 33 +++++++++++++++++++-------- 4 files changed, 26 insertions(+), 35 deletions(-) diff --git a/pkg/multicloud/aliyun/vpc.go b/pkg/multicloud/aliyun/vpc.go index 65c114f01..42706d285 100644 --- a/pkg/multicloud/aliyun/vpc.go +++ b/pkg/multicloud/aliyun/vpc.go @@ -226,16 +226,6 @@ func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudR } func (self *SVpc) Delete() error { - secgroups, err := self.region.GetSecurityGroups(self.VpcId, "", nil) - if err != nil { - return errors.Wrapf(err, "GetSecurityGroups") - } - for i := 0; i < len(secgroups); i += 1 { - err := self.region.DeleteSecurityGroup(secgroups[i].SecurityGroupId) - if err != nil { - return errors.Wrapf(err, "DeleteSecurityGroup %s", secgroups[i].SecurityGroupId) - } - } return self.region.DeleteVpc(self.VpcId) } diff --git a/pkg/multicloud/apsara/vpc.go b/pkg/multicloud/apsara/vpc.go index 4c79058f9..9494c39c3 100644 --- a/pkg/multicloud/apsara/vpc.go +++ b/pkg/multicloud/apsara/vpc.go @@ -19,7 +19,6 @@ import ( "time" "yunion.io/x/jsonutils" - "yunion.io/x/log" "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" @@ -261,19 +260,6 @@ func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudR } func (self *SVpc) Delete() error { - err := self.fetchSecurityGroups() - if err != nil { - log.Errorf("fetchSecurityGroup for VPC delete fail %s", err) - return err - } - for i := 0; i < len(self.secgroups); i += 1 { - secgroup := self.secgroups[i].(*SSecurityGroup) - err := self.region.DeleteSecurityGroup(secgroup.SecurityGroupId) - if err != nil { - log.Errorf("deleteSecurityGroup for VPC delete fail %s", err) - return err - } - } return self.region.DeleteVpc(self.VpcId) } diff --git a/pkg/multicloud/azure/instancenic.go b/pkg/multicloud/azure/instancenic.go index c79aa7fce..4e7efc5e5 100644 --- a/pkg/multicloud/azure/instancenic.go +++ b/pkg/multicloud/azure/instancenic.go @@ -49,7 +49,7 @@ type InterfaceIPConfiguration struct { } type InterfacePropertiesFormat struct { - NetworkSecurityGroup SSecurityGroup `json:"networkSecurityGroup,omitempty"` + NetworkSecurityGroup *SSecurityGroup `json:"networkSecurityGroup,omitempty"` IPConfigurations []InterfaceIPConfiguration `json:"ipConfigurations,omitempty"` MacAddress string `json:"macAddress,omitempty"` Primary bool `json:"primary,omitempty"` @@ -101,7 +101,7 @@ func (self *SInstanceNic) InClassicNetwork() bool { func (self *SInstanceNic) updateSecurityGroup(secgroupId string) error { region := self.instance.host.zone.region if len(secgroupId) > 0 { - self.Properties.NetworkSecurityGroup = SSecurityGroup{ID: secgroupId} + self.Properties.NetworkSecurityGroup = &SSecurityGroup{ID: secgroupId} } return region.update(jsonutils.Marshal(self), nil) } diff --git a/pkg/multicloud/azure/securitygroup.go b/pkg/multicloud/azure/securitygroup.go index 0748e8f09..ae0b9ca46 100644 --- a/pkg/multicloud/azure/securitygroup.go +++ b/pkg/multicloud/azure/securitygroup.go @@ -38,7 +38,6 @@ type SecurityGroupPropertiesFormat struct { DefaultSecurityRules []SecurityRules `json:"defaultSecurityRules,omitempty"` NetworkInterfaces *[]Interface `json:"networkInterfaces,omitempty"` Subnets *[]SNetwork `json:"subnets,omitempty"` - ProvisioningState string //Possible values are: 'Updating', 'Deleting', and 'Failed' } type SSecurityGroup struct { multicloud.SSecurityGroup @@ -131,7 +130,7 @@ func (region *SRegion) AttachSecurityToInterfaces(secgroupId string, nicIds []st if err != nil { return err } - nic.Properties.NetworkSecurityGroup = SSecurityGroup{ID: secgroupId} + nic.Properties.NetworkSecurityGroup = &SSecurityGroup{ID: secgroupId} if err := region.update(jsonutils.Marshal(nic), nil); err != nil { return err } @@ -165,14 +164,30 @@ func (self *SSecurityGroup) CreateRule(opts *cloudprovider.SecurityGroupRuleCrea } func (self *SSecurityGroup) Delete() error { - if self.Properties != nil && self.Properties.NetworkInterfaces != nil { - for _, nic := range *self.Properties.NetworkInterfaces { - nic, err := self.region.GetNetworkInterface(nic.ID) - if err != nil { - return err + if self.Properties != nil { + if self.Properties.NetworkInterfaces != nil { + for _, nic := range *self.Properties.NetworkInterfaces { + nic, err := self.region.GetNetworkInterface(nic.ID) + if err != nil { + return errors.Wrapf(err, "get nic %s", nic.ID) + } + nic.Properties.NetworkSecurityGroup = nil + err = self.region.update(jsonutils.Marshal(nic), nil) + if err != nil { + return errors.Wrapf(err, "update nic") + } } - if err := self.region.update(jsonutils.Marshal(nic), nil); err != nil { - return err + } + if self.Properties.Subnets != nil { + for _, _net := range *self.Properties.Subnets { + net, err := self.region.GetNetwork(_net.ID) + if err != nil { + return errors.Wrapf(err, "get network %s", _net.ID) + } + err = self.region.update(jsonutils.Marshal(net), nil) + if err != nil { + return errors.Wrapf(err, "update network") + } } } }