diff --git a/msa_sdk/order_stack.py b/msa_sdk/order_stack.py new file mode 100644 index 00000000..c23a5f68 --- /dev/null +++ b/msa_sdk/order_stack.py @@ -0,0 +1,76 @@ +"""Module Orderstack.""" + +import json + +from msa_sdk.device import Device + + +class OrderStack(Device): + """Class OrderStack.""" + + def __init__(self, device_id): + """Initialize.""" + Device.__init__(self, device_id=device_id) + self.api_path = '/orderstack' + + + def add_command_in_stack(self, command: str, params, + timeout=300) -> None: + """ + + To push multiple commands in a stack. + + Parameters + ----------- + command: String + CRUID method in microservice to add to the stack. + + params: dict + Parameters in a dict format: + + { + "simple_firewall": { + "1": { + "object_id": "1", + "src_ip": "3.4.5.7", + "dst_port": "44" + }, + "2": { + "object_id": "2", + "src_ip": "3.4.5.6", + "dst_port": "42" + } + } + timeout: int timeout in sec (300 secondes by default) + + Returns + -------- + None + + """ + self.action = 'Adds a command in the stack' + self.path = '{}/command/{}/{}'.format(self.api_path, + self.device_id, + command) + self._call_put(params, timeout) + + + def apply_command_stacked(self, timeout=300) -> None: + """ + Execute all the commands stacked for the device. + + Parameters + ----------- + device_id: int + Id of the device. + timeout: int + Timeout in seconds (300 seconds by default) + Returns + ------- + None + + """ + self.action = 'Execute all the commands stacked for the device' + self.path = '{}/execute/{}'.format(self.api_path, self.device_id) + + self._call_post(timeout=timeout) diff --git a/tests/test_order_stack.py b/tests/test_order_stack.py new file mode 100644 index 00000000..8663c964 --- /dev/null +++ b/tests/test_order_stack.py @@ -0,0 +1,42 @@ +""" +Test Order Stack +""" +import json +from unittest.mock import patch + +import pytest +from util import orderstack_fixture # pylint: disable=unused-import + + +@patch('msa_sdk.device.Device.read') +def test_add_command_in_stack(_, orderstack_fixture): + """ + Test Add command in the stack + """ + + local_path_command_stack = '/orderstack/command/21594/UPDATE' + + with patch('msa_sdk.msa_api.MSA_API._call_put') as mock_call_put: + orderstack = orderstack_fixture + orderstack.add_command_in_stack('UPDATE', {"1": {"subnet": "mySubnet1"}, "2": {"subnet": "mySubnet2"}}, 50) + + assert orderstack.path == local_path_command_stack + + mock_call_put.assert_called_once_with({"1": {"subnet": "mySubnet1"}, "2": {"subnet": "mySubnet2"}}, 50) + + +@patch('msa_sdk.device.Device.read') +def test_apply_command_stacked(_, orderstack_fixture): + """ + Test Apply command in the stack + """ + + local_path_apply = '/orderstack/execute/21594' + + with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post: + orderstack = orderstack_fixture + orderstack.apply_command_stacked(timeout=50) + + assert orderstack.path == local_path_apply + + mock_call_post.assert_called_once_with(timeout=50) diff --git a/tests/util.py b/tests/util.py index 18cf8e77..ff9dd7df 100644 --- a/tests/util.py +++ b/tests/util.py @@ -11,6 +11,7 @@ from msa_sdk.device import Device from msa_sdk.orchestration import Orchestration from msa_sdk.order import Order +from msa_sdk.order_stack import OrderStack from msa_sdk.profile import Profile from msa_sdk.repository import Repository @@ -224,6 +225,21 @@ def order_fixture(): return order +@pytest.fixture +def orderstack_fixture(): + """Orderstack fixture.""" + with patch('requests.post') as mock_post: + mock_post.return_value.json.return_value = {'token': '12345qwert'} + + with patch('requests.get') as mock_call_get: + mock_call_get.return_value.text = device_info() + + with patch('msa_sdk.msa_api.host_port') as mock_host_port: + mock_host_port.return_value = ('api_hostname', '8080') + orderstack = OrderStack(1234) + return orderstack + + @pytest.fixture def customer_fixture(): """Create Customer fixture."""