Skip to content

Commit

Permalink
Merge pull request #293 from ricardobranco777/fix_gce
Browse files Browse the repository at this point in the history
GCE: Also cleanup forwarding rules
  • Loading branch information
asmorodskyi authored Aug 10, 2023
2 parents cecc978 + febdea9 commit a0ac689
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ PCW has two main flows :
b. Snapshots in all region defined
c. Volumes in all regions defined
d. VPC's ( deletion of VPC means deletion of all assigned to VPC entities first ( security groups , networks etc. ))
- For GCE deleting only images (check details in [ocw/lib/gce.py](ocw/lib/gce.py))
- For GCE deleting disks, images & network resources (check details in [ocw/lib/gce.py](ocw/lib/gce.py))
- For Openstack deleting instances, images & keypairs (check details in [ocw/lib/openstack.py](ocw/lib/openstack.py)


Expand Down
17 changes: 17 additions & 0 deletions ocw/lib/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _delete_resource(self, api_call, resource_name, *_, **kwargs) -> None:
resource_type = {
self.compute_client().disks: "disk",
self.compute_client().firewalls: "firewall",
self.compute_client().forwardingRules: "forwardingRule",
self.compute_client().images: "image",
self.compute_client().instances: "instance",
self.compute_client().networks: "network",
Expand Down Expand Up @@ -118,6 +119,7 @@ def cleanup_all(self) -> None:
self.cleanup_disks()
self.cleanup_images()
self.cleanup_firewalls()
self.cleanup_forwarding_rules()
self.cleanup_routes()
self.cleanup_subnetworks()
self.cleanup_networks()
Expand Down Expand Up @@ -158,6 +160,21 @@ def cleanup_firewalls(self) -> None:
self.compute_client().firewalls, firewall["name"], project=self.project, firewall=firewall["name"]
)

def cleanup_forwarding_rules(self) -> None:
self.log_dbg("Forwarding rules cleanup")
for region in self.list_regions():
rules = [
rule for rule in self._paginated(self.compute_client().forwardingRules, project=self.project, region=region)
if basename(rule["network"]) not in self.__skip_networks
]
self.log_dbg(f"{len(rules)} forwarding_rules found")
for rule in rules:
if self.is_outdated(parse(rule["creationTimestamp"]).astimezone(timezone.utc)):
self._delete_resource(
self.compute_client().forwardingRules, rule["name"],
project=self.project, region=region, forwardingRule=rule["name"]
)

def cleanup_routes(self) -> None:
self.log_dbg("Routes cleanup")
routes = [
Expand Down
9 changes: 8 additions & 1 deletion tests/test_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def list_next(self, *args, **kwargs):
return self.responses.pop(0)

def delete(self, *args, **kwargs):
for resource in ('image', 'disk', 'instance', 'firewall', 'route', 'network', 'subnetwork'):
for resource in ('image', 'disk', 'instance', 'firewall', 'forwardingRule', 'route', 'network', 'subnetwork'):
if resource in kwargs:
self.deleted_resources.append(kwargs[resource])
if len(self.responses) > 0:
Expand All @@ -50,6 +50,7 @@ def something():
class MockClient:
def disks(self): pass
def firewalls(self): pass
def forwardingRules(self): pass
def images(self): pass
def instances(self): pass
def networks(self): pass
Expand Down Expand Up @@ -148,6 +149,10 @@ def test_cleanup_firewalls(gce):
_test_cleanup(gce, "firewalls", gce.cleanup_firewalls)


def test_cleanup_forwarding_rules(gce):
_test_cleanup(gce, "forwardingRules", gce.cleanup_forwarding_rules)


def test_cleanup_routes(gce):
_test_cleanup(gce, "routes", gce.cleanup_routes)

Expand All @@ -164,13 +169,15 @@ def test_cleanup_all(gce):
gce.cleanup_disks = MagicMock()
gce.cleanup_images = MagicMock()
gce.cleanup_firewalls = MagicMock()
gce.cleanup_forwarding_rules = MagicMock()
gce.cleanup_routes = MagicMock()
gce.cleanup_subnetworks = MagicMock()
gce.cleanup_networks = MagicMock()
gce.cleanup_all()
gce.cleanup_disks.assert_called_once()
gce.cleanup_images.assert_called_once()
gce.cleanup_firewalls.assert_called_once()
gce.cleanup_forwarding_rules.assert_called_once()
gce.cleanup_routes.assert_called_once()
gce.cleanup_networks.assert_called_once()
gce.cleanup_subnetworks.assert_called_once()
Expand Down

0 comments on commit a0ac689

Please sign in to comment.