Skip to content

Commit

Permalink
replace implementation to parse subnet and adjusted var names
Browse files Browse the repository at this point in the history
  • Loading branch information
ehvs committed Dec 19, 2024
1 parent 1844f18 commit 86f49f2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
10 changes: 0 additions & 10 deletions pkg/api/util/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ func Split(subnetID string) (string, string, error) {
return strings.Join(parts[:len(parts)-2], "/"), parts[len(parts)-1], nil
}

// Split splits the given subnetID to get the subnet ResourceGroup Name
func SplitRG(subnetID string) (string, error) {
parts := strings.Split(subnetID, "/")
if len(parts) != 11 {
return "", fmt.Errorf("subnet ID %q has incorrect length", subnetID)
}
resourceGroupName := parts[4]
return resourceGroupName, nil
}

// NetworkSecurityGroupID returns the NetworkSecurityGroup ID for a given subnet ID
func NetworkSecurityGroupID(oc *api.OpenShiftCluster, subnetID string) (string, error) {
infraID := oc.Properties.InfraID
Expand Down
29 changes: 8 additions & 21 deletions pkg/util/purge/resourcegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"context"
"sort"

apisubnet "github.com/Azure/ARO-RP/pkg/api/util/subnet"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
mgmtfeatures "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-07-01/features"
"github.com/Azure/go-autorest/autorest/azure"
)

// CleanResourceGroups loop through the resourgroups in the subscription
Expand Down Expand Up @@ -64,34 +63,22 @@ func (rc *ResourceCleaner) cleanResourceGroup(ctx context.Context, resourceGroup

// cleanNetworking lists subnets in vnets and unnassign security groups
func (rc *ResourceCleaner) cleanNetworking(ctx context.Context, resourceGroup mgmtfeatures.ResourceGroup) error {
netwSecurityGroups, err := rc.securitygroupscli.List(ctx, *resourceGroup.Name, nil)
networkSecurityGroups, err := rc.securitygroupscli.List(ctx, *resourceGroup.Name, nil)
if err != nil {
return err
}

for _, networkSecGroup := range netwSecurityGroups {
for _, networkSecGroup := range networkSecurityGroups {
if networkSecGroup.Properties == nil || networkSecGroup.Properties.Subnets == nil {
continue
}

for _, nsgSubnet := range networkSecGroup.Properties.Subnets {

vnetID, subnetName, err := apisubnet.Split(*nsgSubnet.ID)
if err != nil {
return err
}

vnetName, err := azure.ParseResourceID(vnetID)
r, err := arm.ParseResourceID(*nsgSubnet.ID)
if err != nil {
return err
}

subnetRGName, err := apisubnet.SplitRG(*nsgSubnet.ID)
if err != nil {
return err
}

subnet, err := rc.subnet.Get(ctx, subnetRGName, vnetName.ResourceName, subnetName, nil)
subnet, err := rc.subnet.Get(ctx, r.ResourceGroupName, r.Parent.Name, r.Name, nil)
if err != nil {
return err
}
Expand All @@ -105,13 +92,13 @@ func (rc *ResourceCleaner) cleanNetworking(ctx context.Context, resourceGroup mg

subnet.Properties.NetworkSecurityGroup = nil

err = rc.subnet.CreateOrUpdateAndWait(ctx, subnetRGName, vnetName.ResourceName, subnetName, subnet.Subnet, nil)
err = rc.subnet.CreateOrUpdateAndWait(ctx, r.ResourceGroupName, r.Parent.Name, r.Name, subnet.Subnet, nil)
if err != nil {
return err
}
rc.log.Printf("[DRY-RUN=False] Resources Dettaching: NSG RG: %s - NSG: %v || Subnet RG: %v vnetName.ResourceName: %s - subnetName: %s", *resourceGroup.Name, *networkSecGroup.Name, subnetRGName, vnetName.ResourceName, subnetName)
rc.log.Printf("[DRY-RUN=False] Resources Dettaching: NSG RG: %s - NSG: %v || Subnet RG: %v vnetName.ResourceName: %s - subnetName: %s", *resourceGroup.Name, *networkSecGroup.Name, r.ResourceGroupName, r.Parent.Name, r.Name)
} else {
rc.log.Printf("[DRY-RUN=True] Resources Dettaching: NSG RG: %s - NSG: %v || Subnet RG: %v vnetName.ResourceName: %s - subnetName: %s", *resourceGroup.Name, *networkSecGroup.Name, subnetRGName, vnetName.ResourceName, subnetName)
rc.log.Printf("[DRY-RUN=True] Resources Dettaching: NSG RG: %s - NSG: %v || Subnet RG: %v vnetName.ResourceName: %s - subnetName: %s", *resourceGroup.Name, *networkSecGroup.Name, r.ResourceGroupName, r.Parent.Name, r.Name)
}
}
}
Expand Down

0 comments on commit 86f49f2

Please sign in to comment.