def test_get_missing_mac(self): """Search for missing mac config; return empty list.""" mac = '00:00:00:00:00:02' tftp = SpokeTFTP(self.tftp_root) result = tftp.search(mac)['data'] expected_result = [] self.assertEqual(result, expected_result)
def test_get_tftp_mac(self): """Search for a tftp mac config; return list.""" # mac should be returned as lower case, with s/:/- and with prefix mac = self.tftp_mac_prefix + '-' + '00-0c-29-57-3b-31' tftp = SpokeTFTP(self.tftp_root) result = tftp.search(self.mac)['data'] expected_result = [[mac]] self.assertEqual(result, expected_result)
def test_get_all_macs_templates(self): """Search for all macs and templates; return list.""" # macs should be returned as lower case, with s/:/- and with prefix raw_mac1 = '00-0c-29-57-3b-31' raw_mac3 = '00-00-00-00-00-03' raw_mac4 = '00-00-00-00-00-04' template = self.template template2 = 'newtemplate.template' open(self.tftp_dir + template2, 'w').close() tftp = SpokeTFTP(self.tftp_root) tftp.create(raw_mac3, template2) tftp.create(raw_mac4, template2) result = tftp.search()['data'] expected_result = [{'templates' : [template2, template], 'configs' : [raw_mac3, raw_mac4, raw_mac1]}] tftp.delete(raw_mac3) tftp.delete(raw_mac4) os.remove(self.tftp_dir + template2) self.assertEqual(result, expected_result)
def search(mac=None, template=None): ''' Return dict with list of templates and configs CLI Examples:: salt '*' tftp.search salt '*' tftp.search mac='11:22:33:44:55:66' salt '*' tftp.search template='test.template' ''' try: conf = _spoke_config(_salt_config('config')) tftproot = conf.get("TFTP", "tftp_root") tftp = SpokeTFTP(tftproot) result = tftp.search(mac, template) except error.SpokeError as e: result = common.handle_error(e) return result
def test_get_all_macs_templates(self): """Search for all macs and templates; return list.""" # macs should be returned as lower case, with s/:/- and with prefix raw_mac1 = '00-0c-29-57-3b-31' raw_mac3 = '00-00-00-00-00-03' raw_mac4 = '00-00-00-00-00-04' template = self.template template2 = 'newtemplate.template' open(self.tftp_dir + template2, 'w').close() tftp = SpokeTFTP(self.tftp_root) tftp.create(raw_mac3, template2) tftp.create(raw_mac4, template2) result = tftp.search()['data'] expected_result = [{ 'templates': [template2, template], 'configs': [raw_mac3, raw_mac4, raw_mac1] }] tftp.delete(raw_mac3) tftp.delete(raw_mac4) os.remove(self.tftp_dir + template2) self.assertEqual(result, expected_result)
try: tftp = SpokeTFTP() except Exception as e: mc.fail(e) if request['action'] == 'search': try: mac = request['data']['mac'] except KeyError: mac = None try: target = request['data']['target'] except KeyError: target = None try: mc.data = tftp.search(mac=mac, target=target) except error.SpokeError as e: mc.fail(e.msg, e.exit_code) elif request['action'] == 'create': mac = request['data']['mac'] target = request['data']['target'] try: mc.data = tftp.create(mac, target) except error.SpokeError as e: mc.fail(e.msg, e.exit_code) elif request['action'] == 'delete': mac = request['data']['mac'] try: mc.data = tftp.delete(mac) except error.SpokeError as e: mc.fail(e.msg, e.exit_code)