From cbd85b1b6c4fef4618746e618df44e3fba604eae 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/azure/instancenic.go | 4 ++-- pkg/multicloud/azure/securitygroup.go | 33 +++++++++++++++++++-------- 2 files changed, 26 insertions(+), 11 deletions(-) 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") + } } } }