Skip to content

Commit 451554c

Browse files
f5-rahmcaphrim007
authored andcommitted
Issue #538 - Adding unix-mv command (#681)
* Issue #538 - Adding unix-mv command Added files: f5/bigip/tm/util/Unix_Mv.py f5/bigip/tm/util/test/test_unix_mv.py test/functional/tm/util/test_unix_mv.py Updated file: f5/bigip/tm/util/__init__.py * Issue #538 - reformatted to avoid carriage return in string Updated file: test/function/tm/util/test_unix_mv.py
1 parent 9d63243 commit 451554c

4 files changed

Lines changed: 153 additions & 0 deletions

File tree

f5/bigip/tm/util/Unix_Mv.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-mv``
21+
22+
GUI Path
23+
N/A
24+
25+
REST Kind
26+
``tm:util:unix-mv:*``
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_Mv(UnnamedResource, CommandExecutionMixin):
35+
36+
def __init__(self, util):
37+
super(Unix_Mv, self).__init__(util)
38+
self._meta_data['required_command_parameters'].update(('utilCmdArgs',))
39+
self._meta_data['required_json_kind'] = 'tm:util:unix-mv: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/mv'):
54+
raise UtilError('%s' % self.commandResult.split(' ', 1)[1])
55+
else:
56+
return self

f5/bigip/tm/util/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
"""
2929

3030
from f5.bigip.resource import PathElement
31+
from f5.bigip.tm.util.Unix_Mv import Unix_Mv
3132
from f5.bigip.tm.util.Unix_Rm import Unix_Rm
3233

3334

3435
class Util(PathElement):
3536
def __init__(self, bigip):
3637
super(Util, self).__init__(bigip)
3738
self._meta_data['allowed_lazy_attributes'] = [
39+
Unix_Mv,
3840
Unix_Rm
3941
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_Mv import Unix_Mv
21+
22+
23+
@pytest.fixture
24+
def FakeUnixMv():
25+
fake_sys = mock.MagicMock()
26+
fake_mv = Unix_Mv(fake_sys)
27+
return fake_mv
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_mv
37+
38+
39+
class TestUnixMvCommand(object):
40+
def test_command_unix_mv(self, FakeiControl):
41+
FakeiControl.exec_cmd('run',
42+
utilCmdArgs='/var/tmp/tf1.txt /var/tmp/tf2.txt')
43+
session = FakeiControl._meta_data['bigip']._meta_data['icr_session']
44+
assert session.post.call_args == mock.call(
45+
'https://host:443/mgmt/tm/util/unix-mv/',
46+
json={'utilCmdArgs': '/var/tmp/tf1.txt /var/tmp/tf2.txt',
47+
'command': 'run'}
48+
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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_mv(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'
31+
32+
fm1 = mgmt_root.tm.util.unix_mv.exec_cmd(
33+
'run',
34+
utilCmdArgs='{0}/{1} {0}/testmove.txt'.format(
35+
tpath_name, ntf_basename))
36+
37+
# validate object was created
38+
assert fm1.utilCmdArgs == '{0}/{1} {0}/testmove.txt'.format(tpath_name,
39+
ntf_basename)
40+
41+
# if command was successful, commandResult should not be present
42+
assert 'commandResult' not in fm1.__dict__
43+
44+
# UtilError should be raised when non-existent file is mentioned
45+
with pytest.raises(UtilError):
46+
mgmt_root.tm.util.unix_mv.exec_cmd(
47+
'run', utilCmdArgs='{0}/mf.txt'.format(tpath_name))

0 commit comments

Comments
 (0)