Skip to content

Commit

Permalink
Remove quotas with no change, 0 value
Browse files Browse the repository at this point in the history
Loop quotas and remove any keys with 0 value as these are marked as no change.
Only make call to set relevant quota if they have values to change.
Removed dataclasses import as no longer used.
  • Loading branch information
DaveW-STFC committed Feb 28, 2025
1 parent 56f7bdc commit c5ca9b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
8 changes: 8 additions & 0 deletions actions/quota.set.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,34 @@ parameters:
required: false
cores:
description: New value for the cores quota. 0 means no change
default: 0
type: integer
gigabytes:
description: New value for the gigabytes quota. 0 means no change
default: 0
type: integer
instances:
description: New value for the instances quota. 0 means no change
default: 0
type: integer
backups:
description: New value for the backups quota. 0 means no change
default: 0
type: integer
ram:
description: New value for the ram quota (This is in MB). 0 means no change
default: 0
type: integer
security_groups:
description: New value for the security groups quota. 0 means no change
default: 0
type: integer
snapshots:
description: New value for the snapshots quota. 0 means no change
default: 0
type: integer
volumes:
description: New value for the volumes quota. 0 means no change
default: 0
type: integer
runner_type: python-script
17 changes: 11 additions & 6 deletions lib/openstack_api/openstack_quota.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import dataclasses

from openstack.connection import Connection

from exceptions.missing_mandatory_param_error import MissingMandatoryParamError
from structs.quota_details import QuotaDetails
from dataclasses import dataclass


# pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -35,9 +32,17 @@ def set_quota(conn: Connection, details: QuotaDetails):
'gigabytes': details.gigabytes
}

conn.set_compute_quotas(project_id, **compute_quotas)
conn.set_network_quotas(project_id, **network_quotas)
conn.set_volume_quotas(project_id, **volume_quotas)
for quota in [compute_quotas, network_quotas, volume_quotas]:
for key, value in list(quota.items()):
if value == 0:
quota.pop(key)

if len(compute_quotas) > 0:
conn.set_compute_quotas(project_id, **compute_quotas)
if len(network_quotas) > 0:
conn.set_network_quotas(project_id, **network_quotas)
if len(volume_quotas) > 0:
conn.set_volume_quotas(project_id, **volume_quotas)


def show_quota(conn: Connection, project_id: str):
Expand Down
5 changes: 2 additions & 3 deletions tests/lib/openstack_api/test_openstack_quota.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import dataclasses
from unittest.mock import MagicMock, call
import pytest

Expand Down Expand Up @@ -32,7 +31,7 @@ def test_set_quota_cores():
set_quota(mock_conn, mock_details)
mock_conn.identity.find_project.assert_called_once_with("foo", ignore_missing=False)
mock_conn.set_compute_quotas.assert_called_once_with(
mock_conn.identity.find_project.return_value.id, cores=1, instances=0, ram=0
mock_conn.identity.find_project.return_value.id, cores=1
)


Expand All @@ -48,7 +47,7 @@ def test_set_quota_security_group_rules():
set_quota(mock_conn, mock_details)
mock_conn.identity.find_project.assert_called_once_with("foo", ignore_missing=False)
mock_conn.set_network_quotas.assert_called_once_with(
mock_conn.identity.find_project.return_value.id, floating_ips=0, security_group_rules=1, security_groups=0
mock_conn.identity.find_project.return_value.id, security_group_rules=1
)


Expand Down

0 comments on commit c5ca9b0

Please sign in to comment.