Skip to content

Commit a99a185

Browse files
authored
Merge pull request #1467 from jasonrahm/issue.1463
Issue #1463 - Add delete_collection support
2 parents 0f8833a + 9cfebaf commit a99a185

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

f5/bigip/resource.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
from f5.sdk_exception import MissingRequiredCommandParameter
121121
from f5.sdk_exception import MissingRequiredCreationParameter
122122
from f5.sdk_exception import MissingRequiredReadParameter
123+
from f5.sdk_exception import MissingRequiredRequestsParameter
123124
from f5.sdk_exception import RequestParamKwargCollision
124125
from f5.sdk_exception import UnregisteredKind
125126
from f5.sdk_exception import UnsupportedMethod
@@ -817,6 +818,34 @@ def get_collection(self, **kwargs):
817818
raise UnregisteredKind(error_message)
818819
return list_of_contents
819820

821+
def _delete_collection(self, **kwargs):
822+
"""wrapped with delete_collection, override that in a sublcass to customize """
823+
error_message = "The request must include \"requests_params\": {\"params\": \"options=<glob pattern>\"} as kwarg"
824+
try:
825+
if kwargs['requests_params']['params'].split('=')[0] != 'options':
826+
raise MissingRequiredRequestsParameter(error_message)
827+
except KeyError:
828+
raise
829+
830+
requests_params = self._handle_requests_params(kwargs)
831+
delete_uri = self._meta_data['uri']
832+
session = self._meta_data['bigip']._meta_data['icr_session']
833+
834+
session.delete(delete_uri, **requests_params)
835+
836+
def delete_collection(self, **kwargs):
837+
"""One can not simply delete a collection.
838+
839+
This is to support odata usage via the options request parameter:
840+
841+
``requests_params={'params': 'options=glob_pattern'}``
842+
843+
where glob_pattern can be used to delete one or all of a particular
844+
collection. Not submitting the requests params will fail, and specifying
845+
patterns that match default resources will fail as well.
846+
"""
847+
self._delete_collection(**kwargs)
848+
820849

821850
class Resource(ResourceBase):
822851
"""Base class to represent a Configurable Resource on the device.

f5/bigip/test/functional/test_resource.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import pytest
1717

18+
from f5.sdk_exception import MissingRequiredRequestsParameter
1819
from icontrol.exceptions import iControlUnexpectedHTTPError
1920

2021

@@ -29,3 +30,20 @@ def test_load_example_resource(request, mgmt_root):
2930
assert x.kind == 'tm:ltm:pool:poolcollectionstate'
3031
assert x.kind != 'tm:ltm:pool:poolstate'
3132
assert x.items[0].get('name') is None
33+
34+
35+
def test_missing_required_requests_parameters(request, mgmt_root):
36+
with pytest.raises(MissingRequiredRequestsParameter) as error:
37+
# SHould be options, not option
38+
mgmt_root.tm.ltm.profile.tcps.delete_collection(requests_params={'params': 'option=*'})
39+
assert 'The request must include "requests_params": {"params": "options=' in str(error.value.message)
40+
41+
with pytest.raises(KeyError) as error:
42+
# Should be params, not param
43+
mgmt_root.tm.ltm.profile.tcps.delete_collection(requests_params={'param': 'option=*'})
44+
assert 'params' in str(error.value.message)
45+
46+
with pytest.raises(KeyError) as error:
47+
# request_params should be present
48+
mgmt_root.tm.ltm.profile.tcps.delete_collection()
49+
assert 'requests_params' in str(error.value.message)

f5/sdk_exception.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ class MissingRequiredReadParameter(F5SDKError):
134134
pass
135135

136136

137+
class MissingRequiredRequestsParameter(F5SDKError):
138+
"""Raises this when a request parameter is required"""
139+
140+
137141
class MissingUpdateParameter(F5SDKError):
138142
"""Raises this when update requires specific parameters together."""
139143
pass

0 commit comments

Comments
 (0)