Skip to content

Commit 59303eb

Browse files
committed
Merge pull request #432 from wojtek0806/commandmixin.variables
Commandmixin.variables
2 parents 4cc1668 + 715f5d4 commit 59303eb

7 files changed

Lines changed: 22 additions & 21 deletions

File tree

f5/bigip/mixins.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,9 @@ def load(self, **kwargs):
214214

215215
def exec_cmd(self, command, **kwargs):
216216

217-
cmds = ['cp', 'generate', 'install', 'load', 'mv', 'publish',
218-
'reboot', 'restart', 'reset-stats', 'run', 'save',
219-
'send-mail', 'start', 'stop']
217+
cmds = self._meta_data['allowed_commands']
220218

221-
if command not in cmds:
219+
if command not in self._meta_data['allowed_commands']:
222220
error_message = "The command value {0} does not exist" \
223221
"Valid commands are {1}".format(command, cmds)
224222
raise InvalidCommand(error_message)

f5/bigip/resource.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ def __init__(self, container):
349349
:param container: instance is an attribute of a ResourceBase container
350350
"""
351351
super(ResourceBase, self).__init__(container)
352+
# Commands you can run on a resource or collection, we define it here
353+
self._meta_data['allowed_commands'] = []
352354

353355
def _update(self, **kwargs):
354356
"""wrapped with update, override that in a subclass to customize"""

f5/bigip/test/test_mixins.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import json
1616
import pytest
1717

18-
1918
from f5.bigip.mixins import CommandExecutionMixin
20-
from f5.bigip.mixins import InvalidCommand
2119
from f5.bigip.mixins import ToDictMixin
2220
from f5.bigip.mixins import UnnamedResourceMixin
2321
from f5.bigip.mixins import UnsupportedMethod
@@ -136,8 +134,3 @@ def test_load_raises(self):
136134
command_resource = CommandExecutionMixin()
137135
with pytest.raises(UnsupportedMethod):
138136
command_resource.load()
139-
140-
def test_invalid_command_raises(self):
141-
command_resource = CommandExecutionMixin()
142-
with pytest.raises(InvalidCommand):
143-
command_resource.exec_cmd('foo', name='test')

f5/bigip/tm/cm/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ def __init__(self, cm):
4545
Devices, Device_Groups, Traffic_Groups, Trust_Domains,
4646
Sync_Status, Add_To_Trust, Remove_From_Trust,
4747
]
48+
self._meta_data['allowed_commands'].append('run')

f5/bigip/tm/cm/trust.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(self, cm):
3737
('device', 'deviceName', 'username', 'password'))
3838
self._meta_data['required_json_kind'] = \
3939
'tm:cm:add-to-trust:runstate'
40+
self._meta_data['allowed_commands'].append('run')
4041

4142

4243
class Remove_From_Trust(UnnamedResourceMixin, CommandExecutionMixin, Resource):
@@ -57,3 +58,4 @@ def __init__(self, cm):
5758
('deviceName',))
5859
self._meta_data['required_json_kind'] = \
5960
'tm:cm:remove-from-trust:runstate'
61+
self._meta_data['allowed_commands'].append('run')

f5/bigip/tm/sys/config.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@
2525
REST Kind
2626
``tm:sys:config:*``
2727
"""
28-
28+
from f5.bigip.mixins import CommandExecutionMixin
2929
from f5.bigip.mixins import UnnamedResourceMixin
3030
from f5.bigip.resource import ResourceBase
3131

3232

33-
class Config(UnnamedResourceMixin, ResourceBase):
33+
class Config(UnnamedResourceMixin, ResourceBase,
34+
CommandExecutionMixin):
3435
def __init__(self, sys):
3536
super(Config, self).__init__(sys)
3637
self._meta_data['allowed_lazy_attributes'] = []
3738
self._meta_data['attribute_registry'] = {}
39+
self._meta_data['allowed_commands'].append('save',
40+
'load')
3841

3942
def update(self, **kwargs):
4043
'''Update is not supported for Config
@@ -44,11 +47,3 @@ def update(self, **kwargs):
4447
raise self.UnsupportedMethod(
4548
"%s does not support the update method" % self.__class__.__name__
4649
)
47-
48-
def save(self):
49-
'''Save the configuration on the device. '''
50-
payload = {'command': 'save'}
51-
session = self._meta_data['bigip']._meta_data['icr_session']
52-
uri = self._meta_data['uri']
53-
response = session.post(uri, json=payload)
54-
self._local_update(response.json())

test/functional/cm/test_trust.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
#
1515

16+
from f5.bigip.mixins import InvalidCommand
1617
import pytest
1718

1819

@@ -77,3 +78,12 @@ def test_run(self, request, bigip, peer):
7778
# Verify devices sync state is Standalone
7879
assert check_sync(request, bigip) == u"Standalone"
7980
assert check_sync(request, peer) == u"Standalone"
81+
82+
def test_invalid_cmd_meta(self, request, bigip):
83+
dvcs = bigip.cm
84+
with pytest.raises(InvalidCommand):
85+
dvcs.add_to_trust.exec_cmd('foo', name='fooname',
86+
device='foodev',
87+
deviceName='foo_name',
88+
username='foouser',
89+
caDevice=True, password='foopasswd')

0 commit comments

Comments
 (0)