def test_get_missing_dhcp_host_option(self): """Fetch missing DHCP host option; return empty List.""" dhcp_option = 'routers 10.0.0.1' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group, self.dhcp_host) result = attr.get(self.dhcp_option_attr, dhcp_option)['data'] expected_result = [] self.assertEqual(result, expected_result)
def test_create_dhcp_host_option_twice(self): """Create the same DHCP option on a host twice; raise AleadyExists.""" dhcp_option = 'domain-name "aethernet-local"' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group, self.dhcp_host) attr.create(self.dhcp_option_attr, dhcp_option) self.assertRaises(error.AlreadyExists, attr.create, self.dhcp_option_attr, dhcp_option)
def test_create_dhcp_service_option_twice(self): """Create a DHCP option twice; return True.""" dhcp_option = 'domain-name "aethernet-local"' attr = SpokeDHCPAttr(self.dhcp_server) attr.create(self.dhcp_option_attr, dhcp_option) self.assertRaises(error.AlreadyExists, attr.create, self.dhcp_option_attr, dhcp_option)
def test_get_missing_dhcp_host_option(self): """Fetch missing DHCP host option; return empty List.""" dhcp_option = "routers 10.0.0.1" attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group, self.dhcp_host) result = attr.get(self.dhcp_option_attr, dhcp_option)["data"] expected_result = [] self.assertEqual(result, expected_result)
def test_get_missing_dhcp_group_option(self): """Fetch missing DHCP group option; return empty List.""" dhcp_option = 'routers 10.0.0.1' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group) result = attr.get(self.dhcp_option_attr, dhcp_option)['data'] expected_result = [] self.assertEqual(result, expected_result)
def attr_search(dhcp_server, dhcp_group, dhcp_host, attr_type): try: from spoke.lib.dhcp import SpokeDHCPAttr attr = SpokeDHCPAttr(dhcp_server, dhcp_group, dhcp_host) result = attr.get(attr_type) except error.SpokeError as e: result = common.handle_error(e) return result
def test_create_dhcp_service_option(self): """Create a DHCP option on a service object; return True.""" dhcp_option = 'domain-name "aethernet-local"' attr = SpokeDHCPAttr(self.dhcp_server) result = attr.create(self.dhcp_option_attr, dhcp_option)['data'] service_name = self.dhcp_server + self.dhcp_conf_suffix dn = 'cn=%s,%s' % (service_name, self.base_dn) dn_info = {self.dhcp_option_attr: [dhcp_option]} expected_result = [(dn, dn_info)] self.assertEqual(result, expected_result)
def test_create_dhcp_service_option(self): """Create a DHCP option on a service object; return True.""" dhcp_option = 'domain-name "aethernet-local"' attr = SpokeDHCPAttr(self.dhcp_server) result = attr.create(self.dhcp_option_attr, dhcp_option)["data"] service_name = self.dhcp_server + self.dhcp_conf_suffix dn = "cn=%s,%s" % (service_name, self.base_dn) dn_info = {self.dhcp_option_attr: [dhcp_option]} expected_result = [(dn, dn_info)] self.assertEqual(result, expected_result)
def test_get_dhcp_service_option(self): """Fetch DHCP option; return option value.""" dhcp_option = "routers 10.0.0.1" attr = SpokeDHCPAttr(self.dhcp_server) attr.create(self.dhcp_option_attr, dhcp_option) result = attr.get(self.dhcp_option_attr, dhcp_option)["data"] service_name = self.dhcp_server + self.dhcp_conf_suffix dn = "cn=%s,%s" % (service_name, self.base_dn) dn_info = {self.dhcp_option_attr: [dhcp_option]} expected_result = [(dn, dn_info)] self.assertEqual(result, expected_result)
def test_create_dhcp_group_option(self): """Create a DHCP option on a group object; return results object.""" dhcp_option = 'domain-name "aethernet-local"' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group) result = attr.create(self.dhcp_option_attr, dhcp_option)['data'] service_name = self.dhcp_server + self.dhcp_conf_suffix service_dn = 'cn=%s,%s' % (service_name, self.base_dn) group_dn = 'cn=%s,%s' % (self.dhcp_group, service_dn) dn_info = {self.dhcp_option_attr: [dhcp_option]} expected_result = [(group_dn, dn_info)] self.assertEqual(result, expected_result)
def test_get_dhcp_service_option(self): """Fetch DHCP option; return option value.""" dhcp_option = 'routers 10.0.0.1' attr = SpokeDHCPAttr(self.dhcp_server) attr.create(self.dhcp_option_attr, dhcp_option) result = attr.get(self.dhcp_option_attr, dhcp_option)['data'] service_name = self.dhcp_server + self.dhcp_conf_suffix dn = 'cn=%s,%s' % (service_name, self.base_dn) dn_info = {self.dhcp_option_attr: [dhcp_option]} expected_result = [(dn, dn_info)] self.assertEqual(result, expected_result)
def test_get_dhcp_group_option(self): """Fetch DHCP option on group; return option value.""" dhcp_option = 'domain-name "aethernet-local"' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group) attr.create(self.dhcp_option_attr, dhcp_option) result = attr.get(self.dhcp_option_attr, dhcp_option)["data"] service_name = self.dhcp_server + self.dhcp_conf_suffix service_dn = "cn=%s,%s" % (service_name, self.base_dn) group_dn = "cn=%s,%s" % (self.dhcp_group, service_dn) dn_info = {self.dhcp_option_attr: [dhcp_option]} expected_result = [(group_dn, dn_info)] self.assertEqual(result, expected_result)
def test_get_dhcp_group_option(self): """Fetch DHCP option on group; return option value.""" dhcp_option = 'domain-name "aethernet-local"' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group) attr.create(self.dhcp_option_attr, dhcp_option) result = attr.get(self.dhcp_option_attr, dhcp_option)['data'] service_name = self.dhcp_server + self.dhcp_conf_suffix service_dn = 'cn=%s,%s' % (service_name, self.base_dn) group_dn = 'cn=%s,%s' % (self.dhcp_group, service_dn) dn_info = {self.dhcp_option_attr: [dhcp_option]} expected_result = [(group_dn, dn_info)] self.assertEqual(result, expected_result)
def test_create_two_dhcp_service_options(self): """Create two DHCP options; return results object.""" dhcp_option1 = 'domain-name "aethernet-local"' dhcp_option2 = 'root-path "10.0.16.16:/usr/local/netboot/nfsroot"' attr = SpokeDHCPAttr(self.dhcp_server) attr.create(self.dhcp_option_attr, dhcp_option1) result = attr.create(self.dhcp_option_attr, dhcp_option2)['data'] service_name = self.dhcp_server + self.dhcp_conf_suffix dn = 'cn=%s,%s' % (service_name, self.base_dn) dn_info = {self.dhcp_option_attr: [dhcp_option1, dhcp_option2]} expected_result = [(dn, dn_info)] self.assertEqual(result, expected_result)
def test_create_two_dhcp_service_options(self): """Create two DHCP options; return results object.""" dhcp_option1 = 'domain-name "aethernet-local"' dhcp_option2 = 'root-path "10.0.16.16:/usr/local/netboot/nfsroot"' attr = SpokeDHCPAttr(self.dhcp_server) attr.create(self.dhcp_option_attr, dhcp_option1) result = attr.create(self.dhcp_option_attr, dhcp_option2)["data"] service_name = self.dhcp_server + self.dhcp_conf_suffix dn = "cn=%s,%s" % (service_name, self.base_dn) dn_info = {self.dhcp_option_attr: [dhcp_option1, dhcp_option2]} expected_result = [(dn, dn_info)] self.assertEqual(result, expected_result)
def test_create_two_dhcp_host_options(self): """Create two DHCP options on a host object; return True.""" dhcp_option1 = 'domain-name "aethernet-local"' dhcp_option2 = 'root-path "10.0.16.16:/usr/local/netboot/nfsroot"' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group, self.dhcp_host) attr.create(self.dhcp_option_attr, dhcp_option1) result = attr.create(self.dhcp_option_attr, dhcp_option2)['data'] service_name = self.dhcp_server + self.dhcp_conf_suffix service_dn = 'cn=%s,%s' % (service_name, self.base_dn) group_dn = 'cn=%s,%s' % (self.dhcp_group, service_dn) host_dn = 'cn=%s,%s' % (self.dhcp_host, group_dn) dn_info = {self.dhcp_option_attr: [dhcp_option1, dhcp_option2]} expected_result = [(host_dn, dn_info)] self.assertEqual(result, expected_result)
def reservation_create(dhcp_server, dhcp_group, host_name, mac, ip): try: from spoke.lib.dhcp import SpokeDHCPHost from spoke.lib.dhcp import SpokeDHCPAttr host = SpokeDHCPHost(dhcp_server, dhcp_group) try: host.create(host_name) except error.AlreadyExists: pass attr = SpokeDHCPAttr(dhcp_server, dhcp_group, host_name) attr.create("dhcpHWAddress", "ethernet %s" % mac) attr.create("dhcpStatements", "fixed-address %s" % ip) attr.create("dhcpOption", "host-name \"%s\"" % host_name) result = host.get(host_name) except error.SpokeError as e: result = common.handle_error(e) return result
def test_create_multiple_dhcp_group_options(self): """Create multiple DHCP options on a group; return results object.""" dhcp_option = 'domain-name "aethernet-local"' dhcp_option2 = 'domain-name "aethernet.com"' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group) attr.create(self.dhcp_option_attr, dhcp_option) attr.create(self.dhcp_option_attr, dhcp_option2) result = attr.get(self.dhcp_option_attr, dhcp_option)['data'] service_name = self.dhcp_server + self.dhcp_conf_suffix service_dn = 'cn=%s,%s' % (service_name, self.base_dn) group_dn = 'cn=%s,%s' % (self.dhcp_group, service_dn) dn_info = {self.dhcp_option_attr: [dhcp_option, dhcp_option2]} expected_result = [(group_dn, dn_info)] self.assertEqual(result, expected_result)
def reservation_create(dhcp_server, dhcp_group, host_name, mac, ip): try: from spoke.lib.dhcp import SpokeDHCPHost from spoke.lib.dhcp import SpokeDHCPAttr host = SpokeDHCPHost(dhcp_server, dhcp_group) try: host.create(host_name) except error.AlreadyExists: pass attr = SpokeDHCPAttr(dhcp_server, dhcp_group, host_name) attr.create("dhcpHWAddress", "ethernet %s" % mac ) attr.create("dhcpStatements", "fixed-address %s" %ip ) attr.create("dhcpOption", "host-name \"%s\"" %host_name ) result = host.get(host_name) except error.SpokeError as e: result = common.handle_error(e) return result
def test_create_multiple_dhcp_group_options(self): """Create multiple DHCP options on a group; return results object.""" dhcp_option = 'domain-name "aethernet-local"' dhcp_option2 = 'domain-name "aethernet.com"' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group) attr.create(self.dhcp_option_attr, dhcp_option) attr.create(self.dhcp_option_attr, dhcp_option2) result = attr.get(self.dhcp_option_attr, dhcp_option)["data"] service_name = self.dhcp_server + self.dhcp_conf_suffix service_dn = "cn=%s,%s" % (service_name, self.base_dn) group_dn = "cn=%s,%s" % (self.dhcp_group, service_dn) dn_info = {self.dhcp_option_attr: [dhcp_option, dhcp_option2]} expected_result = [(group_dn, dn_info)] self.assertEqual(result, expected_result)
def reservation_create(host_name, mac, ip, dhcp_server=None, dhcp_group=None): try: conf = _spoke_config(_salt_config('config')) if not dhcp_server: dhcp_server = conf.get('DHCP', 'dhcp_def_server') if not dhcp_group: dhcp_group = conf.get('DHCP', 'dhcp_def_group') from spoke.lib.dhcp import SpokeDHCPHost from spoke.lib.dhcp import SpokeDHCPAttr host = SpokeDHCPHost(dhcp_server, dhcp_group) try: host.create(host_name) except error.AlreadyExists: pass attr = SpokeDHCPAttr(dhcp_server, dhcp_group, host_name) attr.create("dhcpHWAddress", "ethernet %s" % mac) attr.create("dhcpStatements", "fixed-address %s" % ip) attr.create("dhcpOption", "host-name \"%s\"" % host_name) result = host.get(host_name) except error.SpokeError as e: result = common.handle_error(e) return result
def test_delete_dhcp_host_option(self): """Delete DHCP host option; return True.""" dhcp_option = "ddns-update-style none" attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group, self.dhcp_host) attr.create(self.dhcp_option_attr, dhcp_option) self.assertTrue(attr.delete(self.dhcp_option_attr, dhcp_option))
def test_delete_dhcp_host_option(self): """Delete DHCP host option; return True.""" dhcp_option = 'ddns-update-style none' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group, self.dhcp_host) attr.create(self.dhcp_option_attr, dhcp_option) self.assertTrue(attr.delete(self.dhcp_option_attr, dhcp_option))
def test_delete_dhcp_service_option(self): """Delete DHCP option; return True.""" dhcp_option = 'ddns-update-style none' attr = SpokeDHCPAttr(self.dhcp_server) attr.create(self.dhcp_option_attr, dhcp_option) self.assertTrue(attr.delete(self.dhcp_option_attr, dhcp_option))
try: hostname = request['data']['hostname'] except KeyError: mc.fail("Missing input hostname", 2) if request['action'] == 'create': try: mac = request['data']['mac'] ip = request['data']['ip'] try: mc.info('Creating host %s' % hostname) host.create(hostname) except error.AlreadyExists: mc.info('Host %s already exists' % hostname) mc.info('Adding DHCP attributes for host %s' % hostname) attr = SpokeDHCPAttr(server, group, hostname) attr.create("dhcpHWAddress", "ethernet %s" % mac ) attr.create("dhcpStatements", "fixed-address %s" %ip ) attr.create("dhcpOption", "host-name \"%s\"" %hostname ) mc.data = host.get(hostname) except error.SpokeError, e: mc.fail(e.msg, e.exit_code) elif request['action'] == 'search': try: hostname = request['data']['hostname'] except KeyError: hostname = None try: mc.data = host.get(hostname) attrs = mc.data['data'][0][1] mc.mac = attrs['dhcpHWAddress'][0].split()[1]
try: hostname = request['data']['hostname'] except KeyError: mc.fail("Missing input hostname", 2) if request['action'] == 'create': try: mac = request['data']['mac'] ip = request['data']['ip'] try: mc.info('Creating host %s' % hostname) host.create(hostname) except error.AlreadyExists: mc.info('Host %s already exists' % hostname) mc.info('Adding DHCP attributes for host %s' % hostname) attr = SpokeDHCPAttr(server, group, hostname) attr.create("dhcpHWAddress", "ethernet %s" % mac) attr.create("dhcpStatements", "fixed-address %s" % ip) attr.create("dhcpOption", "host-name \"%s\"" % hostname) mc.data = host.get(hostname) except error.SpokeError, e: mc.fail(e.msg, e.exit_code) elif request['action'] == 'search': try: hostname = request['data']['hostname'] except KeyError: hostname = None try: mc.data = host.get(hostname) attrs = mc.data['data'][0][1] mc.mac = attrs['dhcpHWAddress'][0].split()[1]
def test_delete_missing_dhcp_host_option(self): """Delete missing DHCP host option; raise NotFound.""" dhcp_option = 'max-lease-time 7200' attr = SpokeDHCPAttr(self.dhcp_server, self.dhcp_group, self.dhcp_host) self.assertRaises(error.NotFound, attr.delete, self.dhcp_option_attr, dhcp_option)