Use init to configure plugin
This change implements the removal of the configuration methods in Spyglass. Class fields will now be set by the init method instead. Depends-On: Ib26636f1eb4146902ee801af5bcce53d137be2ad Change-Id: Ia7e53d900de14d1089b0323ddb08717b07b12de9
This commit is contained in:
parent
d43d73da94
commit
8d3c35289e
@ -29,58 +29,27 @@ LOG = logging.getLogger(__name__)
|
||||
|
||||
class ExcelPlugin(BaseDataSourcePlugin):
|
||||
|
||||
def __init__(self, region):
|
||||
def __init__(self, region, **kwargs):
|
||||
super().__init__(region)
|
||||
LOG.info("Excel Plugin Initializing")
|
||||
self.source_type = "excel"
|
||||
self.source_name = "spyglass-plugin-xls"
|
||||
|
||||
# Configuration parameters
|
||||
self.excel_path = None
|
||||
self.excel_spec = None
|
||||
if 'excel_file' not in kwargs:
|
||||
raise ExcelFileNotSpecified()
|
||||
self.excel_path = kwargs["excel_file"]
|
||||
if 'excel_spec' not in kwargs:
|
||||
raise ExcelSpecNotSpecified()
|
||||
self.excel_spec = kwargs["excel_spec"]
|
||||
|
||||
# Raw data from excel
|
||||
self.parsed_xl_data = None
|
||||
|
||||
LOG.info("Initiated data extractor plugin:{}".format(self.source_name))
|
||||
|
||||
def set_config_opts(self, conf):
|
||||
"""Placeholder to set configuration options specific to each plugin.
|
||||
|
||||
:param dict conf: Configuration options as dict
|
||||
|
||||
Example: conf = { 'excel_spec': 'spec1.yaml',
|
||||
'excel_path': 'excel.xls' }
|
||||
|
||||
Each plugin will have their own config opts.
|
||||
"""
|
||||
|
||||
self.excel_path = conf["excel_path"]
|
||||
self.excel_spec = conf["excel_spec"]
|
||||
|
||||
# Extract raw data from excel sheets
|
||||
self._get_excel_obj()
|
||||
self._extract_raw_data_from_excel()
|
||||
return
|
||||
|
||||
def get_plugin_conf(self, **kwargs):
|
||||
"""Validates the plugin param from CLI and return if correct
|
||||
|
||||
Ideally the CLICK module shall report an error if excel file
|
||||
and excel specs are not specified. The below code has been
|
||||
written as an additional safeguard.
|
||||
"""
|
||||
if 'excel_file' not in kwargs:
|
||||
raise ExcelFileNotSpecified()
|
||||
excel_file_info = kwargs["excel_file"]
|
||||
if 'excel_spec' not in kwargs:
|
||||
raise ExcelSpecNotSpecified()
|
||||
excel_spec_info = kwargs["excel_spec"]
|
||||
plugin_conf = {
|
||||
"excel_path": excel_file_info,
|
||||
"excel_spec": excel_spec_info,
|
||||
}
|
||||
return plugin_conf
|
||||
LOG.info("Initiated data extractor plugin:{}".format(self.source_name))
|
||||
|
||||
def get_racks(self, region):
|
||||
"""Return list of racks in the region
|
||||
|
@ -43,52 +43,30 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
|
||||
def test___init__(self):
|
||||
region = 'test_region'
|
||||
result = ExcelPlugin(region)
|
||||
result = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
self.assertEqual(region, result.region)
|
||||
self.assertEqual('excel', result.source_type)
|
||||
self.assertEqual('spyglass-plugin-xls', result.source_name)
|
||||
self.assertEqual(None, result.excel_path)
|
||||
self.assertEqual(None, result.excel_spec)
|
||||
self.assertEqual(None, result.parsed_xl_data)
|
||||
|
||||
@mock.patch('spyglass_plugin_xls.excel_parser.ExcelParser', autospec=True)
|
||||
def test_set_config_opts(self, excel_parser):
|
||||
region = 'test_region'
|
||||
result = ExcelPlugin(region)
|
||||
config = {'excel_spec': EXCEL_SPEC_PATH, 'excel_path': EXCEL_FILE_PATH}
|
||||
result.excel_spec = EXCEL_SPEC_PATH
|
||||
result.excel_path = EXCEL_FILE_PATH
|
||||
result.set_config_opts(config)
|
||||
self.assertEqual(config['excel_path'], result.excel_path)
|
||||
self.assertEqual(config['excel_spec'], result.excel_spec)
|
||||
self.assertEqual(EXCEL_FILE_PATH, result.excel_path)
|
||||
self.assertEqual(EXCEL_SPEC_PATH, result.excel_spec)
|
||||
self.assertIsInstance(result.excel_obj, ExcelParser)
|
||||
self.assertTrue(result.parsed_xl_data)
|
||||
|
||||
def test_get_plugin_conf(self):
|
||||
expected_result = {
|
||||
'excel_path': 'ExcelFile.xlsx',
|
||||
'excel_spec': 'ExcelSpec.yaml'
|
||||
}
|
||||
def test___init___no_excel_file(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
result = obj.get_plugin_conf(
|
||||
excel_file='ExcelFile.xlsx', excel_spec='ExcelSpec.yaml')
|
||||
self.assertDictEqual(expected_result, result)
|
||||
|
||||
def test_get_plugin_conf_no_excel_file(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
with self.assertRaises(ExcelFileNotSpecified):
|
||||
obj.get_plugin_conf(excel_spec='ExcelSpec.yaml')
|
||||
ExcelPlugin(region, excel_spec=EXCEL_SPEC_PATH)
|
||||
|
||||
def test_get_plugin_conf_no_excel_spec(self):
|
||||
def test___init___no_excel_spec(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
with self.assertRaises(ExcelSpecNotSpecified):
|
||||
obj.get_plugin_conf(excel_file='ExcelFile.xlsx')
|
||||
ExcelPlugin(region, excel_file=EXCEL_FILE_PATH)
|
||||
|
||||
def test_get_racks(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj.get_racks(region)
|
||||
self.assertEqual(2, len(result))
|
||||
@ -103,7 +81,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
|
||||
def test_get_hosts(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj.get_hosts(region)
|
||||
self.assertEqual(12, len(result))
|
||||
@ -115,7 +94,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
|
||||
def test_get_hosts_using_rack(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj.get_hosts(region, 'rack73')
|
||||
self.assertEqual(6, len(result))
|
||||
@ -160,7 +140,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
}
|
||||
network_data = deepcopy(self.site_data['network_data'])
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj.get_networks(region)
|
||||
self.assertEqual(7, len(result))
|
||||
@ -178,7 +159,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
|
||||
def test_get_ips(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
host_name = 'cab2r72c15'
|
||||
result = obj.get_ips(region, host_name)
|
||||
@ -192,7 +174,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
expected_ldap_data['domain'] = 'example'
|
||||
expected_ldap_data['url'] = expected_ldap_data['url'].split(' ')[1]
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj.get_ldap_information(region)
|
||||
self.assertDictEqual(expected_ldap_data, result)
|
||||
@ -200,7 +183,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
def test_get_ntp_servers(self):
|
||||
expected_ntp_servers = deepcopy(self.site_data['site_info']['ntp'][:1])
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj.get_ntp_servers(region)
|
||||
self.assertIsInstance(result, models.ServerList)
|
||||
@ -212,7 +196,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
self.site_data['site_info']['dns'][2]
|
||||
]
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj.get_dns_servers(region)
|
||||
self.assertIsInstance(result, models.ServerList)
|
||||
@ -220,7 +205,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
|
||||
def test_get_domain_name(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj.get_domain_name(region)
|
||||
self.assertEqual(self.site_data['site_info']['domain'], result)
|
||||
@ -233,7 +219,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
'physical_location_id'] = expected_location_data.pop(
|
||||
'physical_location')
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj.get_location_information(region)
|
||||
self.assertDictEqual(expected_location_data, result)
|
||||
@ -257,7 +244,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
expected_ldap_data['url'] = expected_ldap_data['url'].split(' ')[1]
|
||||
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = self.site_data
|
||||
result = obj.get_site_info(region)
|
||||
self.assertIsInstance(result, models.SiteInfo)
|
||||
@ -276,7 +264,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
@mock.patch('spyglass_plugin_xls.excel_parser.ExcelParser')
|
||||
def test__get_excel_obj(self, excel_parser):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.excel_spec = EXCEL_SPEC_PATH
|
||||
obj.excel_path = EXCEL_FILE_PATH
|
||||
obj._get_excel_obj()
|
||||
@ -284,7 +273,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
|
||||
def test__extract_raw_data_from_excel(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.excel_obj = mock.MagicMock(spec=ExcelParser)
|
||||
obj.excel_obj.get_data.return_value = 'success'
|
||||
obj._extract_raw_data_from_excel()
|
||||
@ -327,7 +317,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
|
||||
def test__get_rack(self):
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
result = obj._get_rack('cab2r72c15')
|
||||
self.assertEqual('r72', result)
|
||||
|
||||
@ -343,7 +334,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
]
|
||||
}
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj._get_rackwise_hosts()
|
||||
self.assertDictEqual(expected_data, result)
|
||||
@ -351,7 +343,8 @@ class TestExcelPlugin(unittest.TestCase):
|
||||
def test__get_rack_data(self):
|
||||
expected_data = {'r72': 'rack72', 'r73': 'rack73'}
|
||||
region = 'test_region'
|
||||
obj = ExcelPlugin(region)
|
||||
obj = ExcelPlugin(
|
||||
region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH)
|
||||
obj.parsed_xl_data = deepcopy(self.site_data)
|
||||
result = obj._get_rack_data()
|
||||
self.assertDictEqual(expected_data, result)
|
||||
|
Loading…
x
Reference in New Issue
Block a user