Skip to content

Commit

Permalink
fix(azure): secgroup delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito committed Oct 30, 2023
1 parent 0385f4b commit cbd85b1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/multicloud/azure/instancenic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
}
Expand Down
33 changes: 24 additions & 9 deletions pkg/multicloud/azure/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
}
}
}
Expand Down

0 comments on commit cbd85b1

Please sign in to comment.