def _get_host_instance(request, hostname_or_mac): """ Function that returns an instance from either a hostname or mac address. Arguments: hostname_or_mac -- The hostname or MAC address of the host. Exceptions Raised: JinxDataNotFoundError -- A host matching the specified hostname or MAC address could not be found. JinxInvalidStateError -- More than one host had the specified hostname or mac address. """ hosts = None if re.match(r'[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}', hostname_or_mac, re.I): # Looks like it might be a mac, try mac address first hosts = clusto.get_by_mac(hostname_or_mac) if not hosts: # If I haven't found it yet, it must be a hostname: hosts = llclusto.get_by_hostname(hostname_or_mac) if not hosts: return HttpResponseNotFound('No host was found with hostname or MAC address "%s".' % hostname_or_mac) elif len(hosts) > 1: return HttpResponseInvalidState('More than one host found with hostname or MAC address "%s".' % hostname_or_mac) else: host = hosts[0] return host
def delete_dhcp_association(request, hostname, mac_address): """ Function that deletes a hostname from a specified mac_address. Arguments: hostname -- The hostname of an entity. mac_address -- The mac_address of an entity. Exceptions Raised: JinxInvalidStateError -- More than one host had the specified hostname or mac address, or could not be found. """ hostname = hostname.lower() mac_address = mac_address.lower() try: server = clusto.get_by_mac(mac_address)[0] except IndexError: return HttpResponseInvalidState('Could not find any entities with MAC address: "%s".' % mac_address) try: clusto.begin_transaction() for (port_type, port_num, ignore, ignore) in server.port_info_tuples: if server.get_hostname(port_type, port_num) == hostname: server.del_hostname(port_type, port_num) clusto.commit() except: clusto.rollback_transaction() raise
def test_normal_call(self): response = self.do_api_call("sim8000.agni.lindenlab.com", "aa:bb:cc:11:22:33") self.assert_response_code(response, 200) host = clusto.get_by_mac("aa:bb:cc:11:22:33")[0] self.assertEquals(host.hostname, None) # Test a bogus call response = self.do_api_call("sim2000.agni.lindenlab.com", "aa:bb:cc:11:00:99") self.assert_response_code(response, 409)
def set_dhcp_association(request, hostname, mac_address): """ Function that sets a hostname to a specified mac_address. Arguments: hostname -- The hostname of an entity. mac_address -- The mac_address of an entity. Exceptions Raised: JinxInvalidStateError -- More than one host had the specified hostname or mac address, or could not be found. """ hostname = hostname.lower() mac_address = mac_address.lower() servers = clusto.get_by_mac(mac_address) ipmi_hosts = clusto.get_entities(attrs=[{'subkey': 'ipmi_mac', 'value': mac_address}]) hosts = llclusto.get_by_hostname(hostname) if not servers and not ipmi_hosts: return HttpResponseInvalidState('Could not find any entities with MAC address: "%s".' % mac_address) try: clusto.begin_transaction() for host in hosts: for (port_type, port_num, ignore, ignore) in host.port_info_tuples: if host.get_hostname(port_type, port_num) == hostname: host.del_hostname(port_type, port_num) for server in servers: for (port_type, port_num, ignore, ignore) in server.port_info_tuples: if server.get_port_attr(port_type, port_num, "mac") == mac_address: server.set_hostname(hostname, port_type, port_num) for host in ipmi_hosts: ipmi = host.ipmi if ipmi[1] == mac_address: host.set_ipmi_info(hostname, ipmi[1]) clusto.commit() except: clusto.rollback_transaction() raise
def delete_dhcp_association(request, hostname, mac_address): """ Function that deletes a hostname from a specified mac_address. Arguments: hostname -- The hostname of an entity. mac_address -- The mac_address of an entity. Exceptions Raised: JinxInvalidStateError -- More than one host had the specified hostname or mac address, or could not be found. """ hostname = hostname.lower() mac_address = mac_address.lower() server = clusto.get_by_mac(mac_address) server_ipmi = clusto.get_entities(attrs=[{'subkey': 'ipmi_mac', 'value': mac_address}]) if len(server) < 1: if len(server_ipmi) < 1: return HttpResponseInvalidState('Could not find any entities with MAC address: "%s".' % mac_address) elif len(server_ipmi) > 1: return HttpResponseInvalidState('Found multiple IPMI entities with MAC address: "%s: %s".' % mac_address, server_ipmi) elif len(server) > 1: return HttpResponseInvalidState('Found multiple entities with MAC address: "%s: %s".' % mac_address, server) try: clusto.begin_transaction() if server: server = server[0] for (port_type, port_num, ignore, ignore) in server.port_info_tuples: if server.get_hostname(port_type, port_num) == hostname: server.del_hostname(port_type, port_num) if server_ipmi: server = server_ipmi[0] if server.ipmi[0] == hostname: server.del_ipmi_hostname() clusto.commit() except: clusto.rollback_transaction() raise
def get_host_or_mac_object(request, hostname_or_mac): """ Returns an object for a hostname or a mac address... Arguments: hostname_or_mac -- The hostname or mac address of an entity. """ hosts = None if re.match(r'[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}', hostname_or_mac, re.I): hosts = clusto.get_by_mac(hostname_or_mac) if not hosts: hosts = llclusto.get_by_hostname(hostname_or_mac) if not hosts: return HttpResponseNotFound('No host was found with hostname or MAC address "%s".' % hostname_or_mac) elif len(hosts) > 1: return HttpResponse('More than one host found with hostname or MAC address "%s".' % hostname_or_mac, status=409) else: return hosts[0]