def create(mac=None, template=None, run_id=None): ''' Create a config for a specific MAC from a template run_id is optional CLI Examples:: salt '*' tftp.create template='test.template' mac='11:22:33:44:55:66' salt '*' tftp.create template='test.template' mac='11:22:33:44:55:66'\\ run_id=2590 ''' if mac is None or template is None: return "mac and template must be specified" conf = _spoke_config(_salt_config('config')) tftproot = conf.get("TFTP", "tftp_root") tftp = SpokeTFTP(tftproot) if run_id is None: try: result = tftp.create(mac, template) except error.SpokeError as e: result = common.handle_error(e) else: try: result = tftp.create(mac, template, run_id) except error.SpokeError as e: result = common.handle_error(e) return result
def setUp(self): """Create test tftp file structure.""" for d in self.tftp_root, self.tftp_dir: if not (os.path.exists(d)): try: os.makedirs(d) except Exception as e: raise e if not os.path.exists(self.tftp_dir + self.template): f = open(self.tftp_dir + self.template, 'w') f.write("default local\nkernel linux\nappend somearg=2") f.close tftp = SpokeTFTP(self.tftp_root) tftp.create(self.mac, self.template)
def test_create_association_runid(self): """Create mac config with run_id; return results object.""" mac = '00:00:00:00:00:02' mac_file = '01-00-00-00-00-00-02' tftp = SpokeTFTP(self.tftp_root) expected_result = [[mac_file]] result = tftp.create(mac, self.template, self.run_id)['data'] tftp.delete(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 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 test_delete_mac(self): """Delete a config; return True.""" tftp = SpokeTFTP(self.tftp_root) self.assertTrue(tftp.delete(self.mac)) tftp.create(self.mac, self.template)
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) else: msg = "Unknown action: " + request['action'] mc.fail(msg, 2) log.info('Result via Mcollective: %s' % mc.data) sys.exit(0)