Skip to content

Commit e1705a7

Browse files
committed
Add Back_conf endpoint
1 parent 8eb09b4 commit e1705a7

4 files changed

Lines changed: 225 additions & 33 deletions

File tree

msa_sdk/conf_backup.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""Module ConfProfile."""
2+
3+
import json
4+
5+
from msa_sdk.msa_api import MSA_API
6+
7+
8+
class ConfBackup(MSA_API):
9+
"""Class ConfBackup."""
10+
11+
def __init__(self):
12+
"""
13+
Initialize.
14+
15+
Parameters
16+
----------
17+
None
18+
19+
Returns
20+
-------
21+
None
22+
23+
"""
24+
MSA_API.__init__(self)
25+
self.api_path = "/conf-backup"
26+
27+
def restore(self, device_id: int, revision: str) -> None:
28+
"""
29+
Restore.
30+
31+
Parameters
32+
----------
33+
device_id: Integer
34+
Profile id
35+
revision: String
36+
Revision to apply (date or tag name).
37+
Date format is {"YYYY-MM-DD hh:mm"}
38+
39+
Returns
40+
-------
41+
None
42+
"""
43+
self.path = "{}/v1/restore/{}/{}".format(self.api_path,
44+
device_id,
45+
revision)
46+
47+
self._call_post()
48+
49+
def restore_status(self, device_id: int) -> None:
50+
"""
51+
Restore status.
52+
53+
Parameters
54+
----------
55+
device_id: Integer
56+
Profile id
57+
58+
Returns
59+
-------
60+
None
61+
"""
62+
self.path = "{}/v1/restore-status/{}".format(self.api_path, device_id)
63+
64+
self._call_put()
65+
66+
def backup_status(self, device_id: int) -> str:
67+
"""
68+
Backup Status.
69+
70+
Parameters
71+
----------
72+
device_id: Integer
73+
Profile id
74+
75+
Returns
76+
-------
77+
Status: String
78+
"""
79+
self.path = "{}/v1/backup-status/{}".format(self.api_path, device_id)
80+
81+
self._call_get()
82+
83+
return json.loads(self.content)['status']
84+
85+
def backup(self, device_id: int) -> None:
86+
"""
87+
Backup Status.
88+
89+
Parameters
90+
----------
91+
device_id: Integer
92+
Profile id
93+
94+
Returns
95+
-------
96+
Status: String
97+
"""
98+
self.path = "{}/v1/backup/{}".format(self.api_path, device_id)
99+
100+
self._call_post()

msa_sdk/msa_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def _call_post(self, data=None, timeout=60):
237237
self._content = self.response.text
238238
self.check_response()
239239

240-
def _call_get(self, timeout=60):
240+
def _call_get(self, timeout=60, params={}):
241241
"""
242242
Call -XGET. This is a private method.
243243
@@ -254,7 +254,8 @@ def _call_get(self, timeout=60):
254254
}
255255

256256
url = self.url + self.path
257-
self.response = requests.get(url, headers=headers, timeout=timeout)
257+
self.response = requests.get(url, headers=headers, timeout=timeout,
258+
params=params)
258259
self._content = self.response.text
259260
self.check_response()
260261

tests/test_conf_backup.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Test Repository
3+
"""
4+
from unittest.mock import MagicMock
5+
from unittest.mock import patch
6+
7+
from util import conf_backup_fixture
8+
9+
10+
def test_restore(conf_backup_fixture):
11+
"""
12+
Test restore
13+
"""
14+
15+
with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post:
16+
conf_backup = conf_backup_fixture
17+
18+
conf_backup.restore(200, "2021-09-22 10:21")
19+
20+
assert conf_backup.path == \
21+
"/conf-backup/v1/restore/200/2021-09-22 10:21"
22+
mock_call_post.assert_called_once_with()
23+
24+
25+
def test_restore_status(conf_backup_fixture):
26+
"""
27+
Tetst restore status
28+
"""
29+
30+
with patch('msa_sdk.msa_api.MSA_API._call_put') as mock_call_put:
31+
conf_backup = conf_backup_fixture
32+
33+
conf_backup.restore_status(200)
34+
35+
assert conf_backup.path == \
36+
"/conf-backup/v1/restore-status/200"
37+
mock_call_put.assert_called_once_with()
38+
39+
40+
def test_backup_status(conf_backup_fixture):
41+
"""Test Backup Status"""
42+
43+
with patch('requests.get') as mock_get:
44+
conf_backup = conf_backup_fixture
45+
46+
mock_get.return_value.text = ('{"message": "Backup successful",'
47+
' "status": "RUNNING"}')
48+
49+
conf_backup.backup_status(200)
50+
51+
assert conf_backup.path == \
52+
"/conf-backup/v1/backup-status/200"
53+
54+
55+
def test_backup_status_content(conf_backup_fixture):
56+
"""Test Backup Status content"""
57+
58+
with patch('requests.get') as mock_get:
59+
conf_backup = conf_backup_fixture
60+
61+
mock_get.return_value.text = ('{"message": "Backup successful",'
62+
' "status": "RUNNING"}')
63+
64+
assert conf_backup.backup_status(200) == "RUNNING"
65+
66+
67+
def test_backup(conf_backup_fixture):
68+
"""Test backup"""
69+
70+
with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post:
71+
conf_backup = conf_backup_fixture
72+
73+
conf_backup.backup(200)
74+
75+
assert conf_backup.path == "/conf-backup/v1/backup/200"
76+
mock_call_post.assert_called_once_with()

tests/util.py

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
from msa_sdk.conf_backup import ConfBackup
78
from msa_sdk.conf_profile import ConfProfile
89
from msa_sdk.customer import Customer
910
from msa_sdk.device import Device
@@ -39,6 +40,7 @@ def device_info():
3940
'"logMoreEnabled":false,"mailAlerting":false,"reporting":false,'
4041
'"useNat":true,"snmpCommunity":""}')
4142

43+
4244
def device_list():
4345
"""
4446
@@ -49,37 +51,38 @@ def device_list():
4951
String: list of mocked device ids
5052
5153
"""
52-
return ('[{"prefix":"FST","id":130,"name":"BNG-1","sdType":{"id":0,"isObsolete":false,'
53-
'"useWizard":true,"key":"SdType_1_113","manId":1,"manufacturer":"Cisco","model":"IOS",'
54-
'"modId":113,"supportedProfiles":"NULL,DES,3DES","type":0,"familyId":0,"proxy":false,'
55-
'"managed":true,"hasBeenDeleted":false,"utm":false,"reportMail":false,"reportFirewall":false,'
56-
'"stringyfiedIdent":"CiscoIOS","typeHS":"H","name":"CiscoIOS"},"alertMail":true,"silver":false,'
57-
'"gold":false,"report":false,"tamper":false,"ha":false,"dmz":false,"modelFromAsset":"CiscoIOSv",'
58-
'"managed":true,"confProfile":false,"planningProfile":false,"externalReference":"FST130",'
59-
'"groupId":0,"groupName":"","maintenanceMode":false,"modId":113,"manId":1,"customerId":6,'
60-
'"monitorPflId":[],"haPeerId":null,"visibleName":"BNG-1-FST130","ubiId":"FST130",'
61-
'"deviceId":{"id":130,"prefix":"FST","ubiId":"FST130","name":"BNG-1","externalReference":"FST130",'
62-
'"operatorId":0,"displayName":"BNG-1-FST130","displayNameForJsps":"BNG-1-FST130","type":"","hostname":null},'
63-
'"idAsLong":130,"pingStatus":"OK"},{"prefix":"FST","id":127,"name":"M2K-1","sdType":{"id":0,"isObsolete":false,'
64-
'"useWizard":true,"key":"SdType_1_113","manId":1,"manufacturer":"Cisco","model":"IOS","modId":113,'
65-
'"supportedProfiles":"NULL,DES,3DES","type":0,"familyId":0,"proxy":false,"managed":true,"hasBeenDeleted":false,'
66-
'"utm":false,"reportMail":false,"reportFirewall":false,"stringyfiedIdent":"CiscoIOS","typeHS":"H",'
67-
'"name":"CiscoIOS"},"alertMail":true,"silver":false,"gold":false,"report":false,"tamper":false,"ha":false,'
68-
'"dmz":false,"modelFromAsset":"CiscoIOSv","managed":true,"confProfile":false,"planningProfile":false,'
69-
'"externalReference":"FST127","groupId":0,"groupName":"","maintenanceMode":false,"modId":113,"manId":1,'
70-
'"customerId":6,"monitorPflId":[],"haPeerId":null,"visibleName":"M2K-1-FST127","ubiId":"FST127",'
71-
'"deviceId":{"id":127,"prefix":"FST","ubiId":"FST127","name":"M2K-1","externalReference":"FST127","operatorId":0,'
72-
'"displayName":"M2K-1-FST127","displayNameForJsps":"M2K-1-FST127","type":"","hostname":null},"idAsLong":127,'
73-
'"pingStatus":"OK"},{"prefix":"FST","id":125,"name":"M2K-2","sdType":{"id":0,"isObsolete":false,"useWizard":true,'
74-
'"key":"SdType_1_113","manId":1,"manufacturer":"Cisco","model":"IOS","modId":113,"supportedProfiles":"NULL,DES,3DES",'
75-
'"type":0,"familyId":0,"proxy":false,"managed":true,"hasBeenDeleted":false,"utm":false,"reportMail":false,'
76-
'"reportFirewall":false,"stringyfiedIdent":"CiscoIOS","typeHS":"H","name":"CiscoIOS"},"alertMail":false,"silver":false,'
77-
'"gold":false,"report":false,"tamper":false,"ha":false,"dmz":false,"modelFromAsset":"CiscoIOSv","managed":true,'
78-
'"confProfile":false,"planningProfile":false,"externalReference":"FST125","groupId":0,"groupName":"",'
79-
'"maintenanceMode":false,"modId":113,"manId":1,"customerId":6,"monitorPflId":[],"haPeerId":null,'
80-
'"visibleName":"M2K-2-FST125","ubiId":"FST125","deviceId":{"id":125,"prefix":"FST","ubiId":"FST125",'
81-
'"name":"M2K-2","externalReference":"FST125","operatorId":0,"displayName":"M2K-2-FST125",'
82-
'"displayNameForJsps":"M2K-2-FST125","type":"","hostname":null},"idAsLong":125,"pingStatus":"OK"}]')
54+
return (
55+
'[{"prefix":"FST","id":130,"name":"BNG-1","sdType":{"id":0,"isObsolete":false,'
56+
'"useWizard":true,"key":"SdType_1_113","manId":1,"manufacturer":"Cisco","model":"IOS",'
57+
'"modId":113,"supportedProfiles":"NULL,DES,3DES","type":0,"familyId":0,"proxy":false,'
58+
'"managed":true,"hasBeenDeleted":false,"utm":false,"reportMail":false,"reportFirewall":false,'
59+
'"stringyfiedIdent":"CiscoIOS","typeHS":"H","name":"CiscoIOS"},"alertMail":true,"silver":false,'
60+
'"gold":false,"report":false,"tamper":false,"ha":false,"dmz":false,"modelFromAsset":"CiscoIOSv",'
61+
'"managed":true,"confProfile":false,"planningProfile":false,"externalReference":"FST130",'
62+
'"groupId":0,"groupName":"","maintenanceMode":false,"modId":113,"manId":1,"customerId":6,'
63+
'"monitorPflId":[],"haPeerId":null,"visibleName":"BNG-1-FST130","ubiId":"FST130",'
64+
'"deviceId":{"id":130,"prefix":"FST","ubiId":"FST130","name":"BNG-1","externalReference":"FST130",'
65+
'"operatorId":0,"displayName":"BNG-1-FST130","displayNameForJsps":"BNG-1-FST130","type":"","hostname":null},'
66+
'"idAsLong":130,"pingStatus":"OK"},{"prefix":"FST","id":127,"name":"M2K-1","sdType":{"id":0,"isObsolete":false,'
67+
'"useWizard":true,"key":"SdType_1_113","manId":1,"manufacturer":"Cisco","model":"IOS","modId":113,'
68+
'"supportedProfiles":"NULL,DES,3DES","type":0,"familyId":0,"proxy":false,"managed":true,"hasBeenDeleted":false,'
69+
'"utm":false,"reportMail":false,"reportFirewall":false,"stringyfiedIdent":"CiscoIOS","typeHS":"H",'
70+
'"name":"CiscoIOS"},"alertMail":true,"silver":false,"gold":false,"report":false,"tamper":false,"ha":false,'
71+
'"dmz":false,"modelFromAsset":"CiscoIOSv","managed":true,"confProfile":false,"planningProfile":false,'
72+
'"externalReference":"FST127","groupId":0,"groupName":"","maintenanceMode":false,"modId":113,"manId":1,'
73+
'"customerId":6,"monitorPflId":[],"haPeerId":null,"visibleName":"M2K-1-FST127","ubiId":"FST127",'
74+
'"deviceId":{"id":127,"prefix":"FST","ubiId":"FST127","name":"M2K-1","externalReference":"FST127","operatorId":0,'
75+
'"displayName":"M2K-1-FST127","displayNameForJsps":"M2K-1-FST127","type":"","hostname":null},"idAsLong":127,'
76+
'"pingStatus":"OK"},{"prefix":"FST","id":125,"name":"M2K-2","sdType":{"id":0,"isObsolete":false,"useWizard":true,'
77+
'"key":"SdType_1_113","manId":1,"manufacturer":"Cisco","model":"IOS","modId":113,"supportedProfiles":"NULL,DES,3DES",'
78+
'"type":0,"familyId":0,"proxy":false,"managed":true,"hasBeenDeleted":false,"utm":false,"reportMail":false,'
79+
'"reportFirewall":false,"stringyfiedIdent":"CiscoIOS","typeHS":"H","name":"CiscoIOS"},"alertMail":false,"silver":false,'
80+
'"gold":false,"report":false,"tamper":false,"ha":false,"dmz":false,"modelFromAsset":"CiscoIOSv","managed":true,'
81+
'"confProfile":false,"planningProfile":false,"externalReference":"FST125","groupId":0,"groupName":"",'
82+
'"maintenanceMode":false,"modId":113,"manId":1,"customerId":6,"monitorPflId":[],"haPeerId":null,'
83+
'"visibleName":"M2K-2-FST125","ubiId":"FST125","deviceId":{"id":125,"prefix":"FST","ubiId":"FST125",'
84+
'"name":"M2K-2","externalReference":"FST125","operatorId":0,"displayName":"M2K-2-FST125",'
85+
'"displayNameForJsps":"M2K-2-FST125","type":"","hostname":null},"idAsLong":125,"pingStatus":"OK"}]')
8386

8487

8588
def customer_info():
@@ -232,3 +235,15 @@ def conf_profile_fixture():
232235
mock_host_port.return_value = ('api_hostname', '8080')
233236
conf_profile = ConfProfile(100)
234237
return conf_profile
238+
239+
240+
@pytest.fixture
241+
def conf_backup_fixture():
242+
"""Confbackup fixture."""
243+
with patch('requests.post') as mock_post:
244+
mock_post.return_value.json.return_value = {'token': '12345qwert'}
245+
246+
with patch('msa_sdk.msa_api.host_port') as mock_host_port:
247+
mock_host_port.return_value = ('api_hostname', '8080')
248+
conf_backup = ConfBackup()
249+
return conf_backup

0 commit comments

Comments
 (0)