Skip to content

Commit a5ce730

Browse files
committed
Issue #538 - Adding the dig DNS utility
Updated Files for Dig f5/bigip/tm/util/__init__.py Added Files for Dig f5/bigip/tm/util/Dig.py f5/bigip/tm/util/test/test_dig.py test/functional/tm/util/test_dig.py Updated Files for Bash (typo fixes from previous PR) f5/bigip/tm/util/Bash.py test/functional/tm/util/test_bash.py
1 parent 6a13161 commit a5ce730

6 files changed

Lines changed: 161 additions & 2 deletions

File tree

f5/bigip/tm/util/Bash.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Bash(UnnamedResource, CommandExecutionMixin):
3636
3737
.. note::
3838
39-
This is an unnamed resource so it has not ~Partition~Name pattern
39+
This is an unnamed resource so it has no ~Partition~Name pattern
4040
at the end of its URI.
4141
"""
4242

f5/bigip/tm/util/Dig.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/dig``
21+
22+
GUI Path
23+
N/A
24+
25+
REST Kind
26+
``tm:util:dig:*``
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 Dig(UnnamedResource, CommandExecutionMixin):
35+
"""BIG-IP® utility command
36+
37+
.. note::
38+
39+
This is an unnamed resource so it has no ~Partition~Name pattern
40+
at the end of its URI.
41+
"""
42+
43+
def __init__(self, util):
44+
super(Dig, self).__init__(util)
45+
self._meta_data['required_command_parameters'].update(('utilCmdArgs',))
46+
self._meta_data['required_json_kind'] = 'tm:util:dig:runstate'
47+
self._meta_data['allowed_commands'].append('run')
48+
49+
def _exec_cmd(self, command, **kwargs):
50+
kwargs['command'] = command
51+
self._check_exclusive_parameters(**kwargs)
52+
requests_params = self._handle_requests_params(kwargs)
53+
self._check_command_parameters(**kwargs)
54+
55+
session = self._meta_data['bigip']._meta_data['icr_session']
56+
response = session.post(
57+
self._meta_data['uri'], json=kwargs, **requests_params)
58+
self._local_update(response.json())
59+
60+
if 'commandResult' in self.__dict__:
61+
if 'Invalid option' in self.commandResult:
62+
raise UtilError('%s' % self.commandResult)
63+
else:
64+
return self
65+
else:
66+
return self

f5/bigip/tm/util/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
from f5.bigip.resource import PathElement
3131
from f5.bigip.tm.util.Bash import Bash
32+
from f5.bigip.tm.util.Dig import Dig
3233
from f5.bigip.tm.util.Unix_Ls import Unix_Ls
3334
from f5.bigip.tm.util.Unix_Mv import Unix_Mv
3435
from f5.bigip.tm.util.Unix_Rm import Unix_Rm
@@ -39,6 +40,7 @@ def __init__(self, bigip):
3940
super(Util, self).__init__(bigip)
4041
self._meta_data['allowed_lazy_attributes'] = [
4142
Bash,
43+
Dig,
4244
Unix_Ls,
4345
Unix_Mv,
4446
Unix_Rm

f5/bigip/tm/util/test/test_dig.py

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.Dig import Dig
21+
22+
23+
@pytest.fixture
24+
def FakeDig():
25+
fake_sys = mock.MagicMock()
26+
fake_dig = Dig(fake_sys)
27+
return fake_dig
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.dig
37+
38+
39+
class TestDigCommand(object):
40+
def test_command_dig(self, FakeiControl):
41+
FakeiControl.exec_cmd('run', utilCmdArgs='f5.com NS')
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/dig/',
45+
json={'utilCmdArgs': 'f5.com NS', 'command': 'run'}
46+
)

test/functional/tm/util/test_bash.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_E_bash(mgmt_root):
3939
'run',
4040
utilCmdArgs='-c df -k')
4141

42-
# commandResult should b present with data from 'df -k'
42+
# commandResult should be present with data from 'df -k'
4343
assert 'commandResult' in bash2.__dict__
4444

4545
# UtilError should be raised if -c is not specified
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.bigip.resource import MissingRequiredCommandParameter
20+
from f5.utils.util_exceptions import UtilError
21+
from icontrol.session import iControlUnexpectedHTTPError
22+
23+
24+
def test_E_dig(mgmt_root):
25+
26+
with pytest.raises(MissingRequiredCommandParameter) as err:
27+
mgmt_root.tm.util.dig.exec_cmd('run')
28+
assert "Missing required params: ['utilCmdArgs']" in err.response.text
29+
30+
dig1 = mgmt_root.tm.util.dig.exec_cmd('run', utilCmdArgs='f5.com NS')
31+
32+
# commandResult should be present with data from 'f5.com NS'
33+
assert 'commandResult' in dig1.__dict__
34+
assert 'DiG' in dig1.commandResult
35+
36+
# UtilError should be raised if there is an invalid option for dig
37+
with pytest.raises(UtilError) as err:
38+
mgmt_root.tm.util.dig.exec_cmd('run', utilCmdArgs='-9 f5.com NS')
39+
assert 'Invalid option' in err.response.text
40+
41+
# iControlUnexpectedHTTPError should be raised if quotes don't match
42+
with pytest.raises(iControlUnexpectedHTTPError) as err:
43+
mgmt_root.tm.util.dig.exec_cmd('run', utilCmdArgs='"')
44+
assert err.response.status_code == 400
45+
assert 'quotes are not balanced' in err.response.text

0 commit comments

Comments
 (0)