diff --git a/msa_sdk/device.py b/msa_sdk/device.py index 1bc506d0..c6409c74 100644 --- a/msa_sdk/device.py +++ b/msa_sdk/device.py @@ -14,7 +14,7 @@ def __init__(self, customer_id=None, name=None, manufacturer_id=None, device_external=None, log_enabled=True, log_more_enabled=True, mail_alerting=True, reporting=False, snmp_community="ubiqube", - device_id=None, management_port=None): + device_id=None, management_port=None, hostname=None): """ Initialize. @@ -48,6 +48,8 @@ def __init__(self, customer_id=None, name=None, manufacturer_id=None, Device ID management_port: Integer Management Port + hostname: String + Hostname fail: Bool Fail creating the device @@ -79,6 +81,7 @@ def __init__(self, customer_id=None, name=None, manufacturer_id=None, self.configuration = {} self.device_id = device_id self.management_port = management_port + self.hostname=hostname self.fail = None if device_id: @@ -112,6 +115,7 @@ def create(self): "mailAlerting": self.mail_alerting, "passwordAdmin": self.password_admin, "externalReference": self.device_external, + "hostname": self.hostname, "login": self.login, "name": self.name, "password": self.password, @@ -282,6 +286,8 @@ def read(self, by_ref=False): self.mail_alerting = device_info["mailAlerting"] self.use_nat = device_info["useNat"] self.snmp_community = device_info["snmpCommunity"] + self.device_external = device_info["externalReference"] + self.hostname = device_info["hostname"] return self.content diff --git a/msa_sdk/profile.py b/msa_sdk/profile.py new file mode 100644 index 00000000..fcd357f7 --- /dev/null +++ b/msa_sdk/profile.py @@ -0,0 +1,47 @@ +""" +This module provides the Profile class for interacting with profiles in the MSA SDK. + +The Profile class inherits from MSA_API and provides methods to check the existence +of profiles and perform other profile-related operations. +""" + +import json + +from msa_sdk.msa_api import MSA_API + + +class Profile(MSA_API): + """ + A class to represent a profile in the MSA SDK. + + This class provides methods to interact with profiles. + """ + + def __init__(self): + """ + Initialize a Profile instance. + + Sets up the API path for profile operations. + """ + MSA_API.__init__(self) + self.api_path = "/profile" + + def exist(self, reference) -> bool: + """ + Check if a profile exists by reference. + + Parameters + ---------- + reference : str + The reference identifier for the profile. + + Returns + ------- + bool + True if the profile exists, False otherwise. + """ + self.action = 'Check Profile exist by reference' + self.path = '{}/v1/exist/{}'.format(self.api_path, reference) + self._call_post() + result = json.loads(self.content) + return result.get('exist', False) \ No newline at end of file diff --git a/tests/test_profie.py b/tests/test_profie.py new file mode 100644 index 00000000..ad0022ff --- /dev/null +++ b/tests/test_profie.py @@ -0,0 +1,20 @@ +""" +Test Profile +""" + +from unittest.mock import patch + +from util import profile_fixture + + +def test_exist(profile_fixture): + """ + Test exist profile by reference + """ + with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post: + profile = profile_fixture + reference = "test1" + profile.exist(reference) + + assert profile.path == "/profile/v1/exist/test1" + mock_call_post.assert_called_once() \ No newline at end of file diff --git a/tests/util.py b/tests/util.py index e6183f3a..18cf8e77 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.profile import Profile from msa_sdk.repository import Repository @@ -39,7 +40,7 @@ def device_info(): '"managementInterface":"","login":"root",' '"password":"$ubiqube","passwordAdmin":"","logEnabled":false,' '"logMoreEnabled":false,"mailAlerting":false,"reporting":false,' - '"useNat":true,"snmpCommunity":""}') + '"useNat":true,"snmpCommunity":"","hostname":"test"}') def device_list(): @@ -248,6 +249,17 @@ def conf_profile_fixture(): conf_profile = ConfProfile(100) return conf_profile +@pytest.fixture +def profile_fixture(): + """Profile fixture.""" + with patch('requests.post') as mock_post: + mock_post.return_value.json.return_value = {'token': '12345qwert'} + + with patch('msa_sdk.msa_api.host_port') as mock_host_port: + mock_host_port.return_value = ('api_hostname', '8080') + profile = Profile() + return profile + @pytest.fixture def conf_backup_fixture():