-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from stfc/refactor-openstack-api-quota
MAINT: Openstack API refactor - quota
- Loading branch information
Showing
3 changed files
with
106 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,39 @@ | ||
from openstack_api.openstack_connection import OpenstackConnection | ||
from openstack_api.openstack_identity import OpenstackIdentity | ||
from openstack_api.openstack_wrapper_base import OpenstackWrapperBase | ||
from openstack.connection import Connection | ||
|
||
from exceptions.missing_mandatory_param_error import MissingMandatoryParamError | ||
from structs.quota_details import QuotaDetails | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class OpenstackQuota(OpenstackWrapperBase): | ||
def __init__(self, connection_cls=OpenstackConnection): | ||
super().__init__(connection_cls) | ||
self._identity_api = OpenstackIdentity(connection_cls) | ||
|
||
def _update_quota( | ||
self, cloud_account: str, project_id: str, quota_id: str, new_val: int | ||
): | ||
""" | ||
Updates a given quota if the quota has a non-zero value | ||
""" | ||
if new_val == 0: | ||
return | ||
|
||
with self._connection_cls(cloud_account) as conn: | ||
kwargs = {"quota": project_id, quota_id: new_val} | ||
conn.network.update_quota(**kwargs) | ||
|
||
def set_quota(self, cloud_account: str, details: QuotaDetails): | ||
""" | ||
Sets quota(s) for a given project. Any 0 values are ignored. | ||
:param cloud_account: The associated credentials to use | ||
:param details: The details of the quota(s) to set | ||
""" | ||
project = self._identity_api.find_mandatory_project( | ||
cloud_account, details.project_identifier | ||
) | ||
project_id = project.id | ||
|
||
self._update_quota( | ||
cloud_account, project_id, "floating_ips", details.num_floating_ips | ||
) | ||
self._update_quota( | ||
cloud_account, | ||
project_id, | ||
"security_group_rules", | ||
details.num_security_group_rules, | ||
) | ||
def set_quota(conn: Connection, details: QuotaDetails): | ||
""" | ||
Sets quota(s) for a given project. Any 0 values are ignored. | ||
:param conn: openstack connection object | ||
:param details: The details of the quota(s) to set | ||
""" | ||
details.project_identifier = details.project_identifier.strip() | ||
if not details.project_identifier: | ||
raise MissingMandatoryParamError("The project name is missing") | ||
project_id = conn.identity.find_project( | ||
details.project_identifier, ignore_missing=False | ||
).id | ||
|
||
_update_quota(conn, project_id, "floating_ips", details.num_floating_ips) | ||
|
||
_update_quota( | ||
conn, | ||
project_id, | ||
"security_group_rules", | ||
details.num_security_group_rules, | ||
) | ||
|
||
|
||
def _update_quota(conn: Connection, project_id: str, quota_id: str, new_val: int): | ||
""" | ||
Updates a given quota if the quota has a non-zero value | ||
""" | ||
if new_val == 0: | ||
return | ||
|
||
kwargs = {"quota": project_id, quota_id: new_val} | ||
conn.network.update_quota(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from unittest.mock import MagicMock, call | ||
import pytest | ||
|
||
from exceptions.missing_mandatory_param_error import MissingMandatoryParamError | ||
from openstack_api.openstack_quota import set_quota | ||
from structs.quota_details import QuotaDetails | ||
|
||
|
||
def test_set_quota_project_missing(): | ||
""" | ||
Tests that set quota raises error when project identifier missing | ||
""" | ||
mock_details = QuotaDetails( | ||
project_identifier=" ", num_floating_ips=1, num_security_group_rules=1 | ||
) | ||
mock_conn = MagicMock() | ||
with pytest.raises(MissingMandatoryParamError): | ||
set_quota(mock_conn, mock_details) | ||
|
||
|
||
def test_set_quota_floating_ips(): | ||
""" | ||
Tests that setting floating IPs update the quota correctly | ||
""" | ||
mock_details = QuotaDetails( | ||
project_identifier="foo", num_floating_ips=1, num_security_group_rules=0 | ||
) | ||
mock_conn = MagicMock() | ||
set_quota(mock_conn, mock_details) | ||
mock_conn.identity.find_project.assert_called_once_with("foo", ignore_missing=False) | ||
mock_conn.network.update_quota.assert_called_once_with( | ||
quota=mock_conn.identity.find_project.return_value.id, floating_ips=1 | ||
) | ||
|
||
|
||
def test_set_quota_security_group_rules(): | ||
""" | ||
Tests that setting security group rules update the quota correctly | ||
""" | ||
mock_details = QuotaDetails( | ||
project_identifier="foo", num_floating_ips=0, num_security_group_rules=1 | ||
) | ||
mock_conn = MagicMock() | ||
set_quota(mock_conn, mock_details) | ||
mock_conn.identity.find_project.assert_called_once_with("foo", ignore_missing=False) | ||
mock_conn.network.update_quota.assert_called_once_with( | ||
quota=mock_conn.identity.find_project.return_value.id, security_group_rules=1 | ||
) | ||
|
||
|
||
def test_set_quota_everything(): | ||
""" | ||
Tests that setting every supported quota in a single test | ||
""" | ||
mock_details = QuotaDetails( | ||
project_identifier="foo", num_floating_ips=10, num_security_group_rules=20 | ||
) | ||
mock_conn = MagicMock() | ||
set_quota(mock_conn, mock_details) | ||
mock_conn.identity.find_project.assert_called_once_with("foo", ignore_missing=False) | ||
mock_conn.network.update_quota.assert_has_calls( | ||
[ | ||
call( | ||
quota=mock_conn.identity.find_project.return_value.id, floating_ips=10 | ||
), | ||
call( | ||
quota=mock_conn.identity.find_project.return_value.id, | ||
security_group_rules=20, | ||
), | ||
] | ||
) |
This file was deleted.
Oops, something went wrong.