Skip to content

Commit 9d63243

Browse files
f5-rahmcaphrim007
authored andcommitted
Adding tm/util/unix-rm (#678)
* Issue #538 - Feature introduction for utility commands; unix-rm up first. Updated Files: f5/bigip/tm/__init__.py Added Files: f5/bigip/tm/util/__init__.py f5/bigip/tm/util/Unix_Rm.py f5/bigip/tm/test/test_unix_rm.py test/functional/tm/util/__init__.py (empty file) test/functional/tm/util/test_unix_rm.py * Issue #538 - Fixed assertion in functional test * Issue #538 - Removed unnecessary return statement and added test coverage f5/bigip/tm/util/Unix_Rm.py - removed unnecessary return statement test/functional/tm/util/test_unix_rm.py - added functional test to cover failure case
1 parent 4e22961 commit 9d63243

6 files changed

Lines changed: 186 additions & 0 deletions

File tree

f5/bigip/tm/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from f5.bigip.tm.shared import Shared
2727
from f5.bigip.tm.sys import Sys
2828
from f5.bigip.tm.transaction import Transactions
29+
from f5.bigip.tm.util import Util
2930
from f5.bigip.tm.vcmp import Vcmp
3031

3132

@@ -42,5 +43,6 @@ def __init__(self, bigip):
4243
Shared,
4344
Sys,
4445
Transactions,
46+
Util,
4547
Vcmp
4648
]

f5/bigip/tm/util/Unix_Rm.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# coding=utf-8
2+
#
3+
# Copyright 2016 F5 Networks Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
"""BIG-IP® utility module
18+
19+
REST URI
20+
``http://localhost/mgmt/tm/util/unix-rm``
21+
22+
GUI Path
23+
N/A
24+
25+
REST Kind
26+
``tm:util:unix-rm:*``
27+
"""
28+
29+
from f5.bigip.mixins import CommandExecutionMixin
30+
from f5.bigip.resource import UnnamedResource
31+
from f5.utils.util_exceptions import UtilError
32+
33+
34+
class Unix_Rm(UnnamedResource, CommandExecutionMixin):
35+
36+
def __init__(self, util):
37+
super(Unix_Rm, self).__init__(util)
38+
self._meta_data['required_command_parameters'].update(('utilCmdArgs',))
39+
self._meta_data['required_json_kind'] = 'tm:util:unix-rm:runstate'
40+
self._meta_data['allowed_commands'].append('run')
41+
42+
def _exec_cmd(self, command, **kwargs):
43+
kwargs['command'] = command
44+
self._check_exclusive_parameters(**kwargs)
45+
requests_params = self._handle_requests_params(kwargs)
46+
self._check_command_parameters(**kwargs)
47+
session = self._meta_data['bigip']._meta_data['icr_session']
48+
response = session.post(
49+
self._meta_data['uri'], json=kwargs, **requests_params)
50+
self._local_update(response.json())
51+
52+
if 'commandResult' in self.__dict__:
53+
if self.commandResult.startswith('/bin/rm'):
54+
raise UtilError('%s' % self.commandResult.split(' ', 1)[1])
55+
else:
56+
return self

f5/bigip/tm/util/__init__.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# coding=utf-8
2+
#
3+
# Copyright 2016 F5 Networks Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
"""BIG-IP® Utility (util) module
19+
20+
REST URI
21+
``http://localhost/mgmt/tm/util/``
22+
23+
GUI Path
24+
``System``
25+
26+
REST Kind
27+
N/A -- HTTP GET returns an error
28+
"""
29+
30+
from f5.bigip.resource import PathElement
31+
from f5.bigip.tm.util.Unix_Rm import Unix_Rm
32+
33+
34+
class Util(PathElement):
35+
def __init__(self, bigip):
36+
super(Util, self).__init__(bigip)
37+
self._meta_data['allowed_lazy_attributes'] = [
38+
Unix_Rm
39+
]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2016 F5 Networks Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
import mock
17+
import pytest
18+
19+
from f5.bigip import ManagementRoot
20+
from f5.bigip.tm.util.Unix_Rm import Unix_Rm
21+
22+
23+
@pytest.fixture
24+
def FakeUnixRm():
25+
fake_sys = mock.MagicMock()
26+
fake_rm = Unix_Rm(fake_sys)
27+
return fake_rm
28+
29+
30+
@pytest.fixture
31+
def FakeiControl(fakeicontrolsession):
32+
mr = ManagementRoot('host', 'fake_admin', 'fake_admin')
33+
mock_session = mock.MagicMock()
34+
mock_session.post.return_value.json.return_value = {}
35+
mr._meta_data['icr_session'] = mock_session
36+
return mr.tm.util.unix_rm
37+
38+
39+
class TestUnixRmCommand(object):
40+
def test_command_unix_rm(self, FakeiControl):
41+
FakeiControl.exec_cmd('run', utilCmdArgs='/var/tmp/testfile.txt')
42+
session = FakeiControl._meta_data['bigip']._meta_data['icr_session']
43+
assert session.post.call_args == mock.call(
44+
'https://host:443/mgmt/tm/util/unix-rm/',
45+
json={'utilCmdArgs': '/var/tmp/testfile.txt', 'command': 'run'}
46+
)

test/functional/tm/util/__init__.py

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
# Copyright 2016 F5 Networks Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import pytest
18+
19+
from f5.utils.util_exceptions import UtilError
20+
import os
21+
from tempfile import NamedTemporaryFile
22+
23+
24+
def test_E_unix_rm(mgmt_root):
25+
ntf = NamedTemporaryFile(delete=False)
26+
ntf_basename = os.path.basename(ntf.name)
27+
ntf.write('text for test file')
28+
ntf.seek(0)
29+
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
30+
tpath_name = '/var/config/rest/downloads/{0}'.format(ntf_basename)
31+
32+
fr1 = mgmt_root.tm.util.unix_rm.exec_cmd('run', utilCmdArgs=tpath_name)
33+
34+
# validate object was created
35+
assert fr1.utilCmdArgs == '/var/config/rest/downloads/{0}'.format(
36+
ntf_basename)
37+
38+
# if command was successful, commandResult should not be present
39+
assert 'commandResult' not in fr1.__dict__
40+
41+
# UtilError should be raised when non-existent file is mentioned
42+
with pytest.raises(UtilError):
43+
mgmt_root.tm.util.unix_rm.exec_cmd('run', utilCmdArgs='testfile.txt')

0 commit comments

Comments
 (0)