Get hostname

    If no domains are passed, the hostname is checked for a zone in infoblox,
    if no zone split on first dot.

    If domains are provided, the best match out of the list is truncated from
    the fqdn leaving the hostname.

    If no matching domains are found the fqdn is returned.

    dots at end of names are ignored.

    CLI Examples:

    .. code-block:: bash

        salt-call infoblox.get_host_hostname fqdn=localhost.xxx.t.domain.com \
            domains="['domain.com', 't.domain.com']"
        #returns: localhost.xxx

        salt-call infoblox.get_host_hostname fqdn=localhost.xxx.t.domain.com
        #returns: localhost

def get_host_hostname(name, domains=None, **api_opts):
    '''
    Get hostname

    If no domains are passed, the hostname is checked for a zone in infoblox,
    if no zone split on first dot.

    If domains are provided, the best match out of the list is truncated from
    the fqdn leaving the hostname.

    If no matching domains are found the fqdn is returned.

    dots at end of names are ignored.

    CLI Examples:

    .. code-block:: bash

        salt-call infoblox.get_host_hostname fqdn=localhost.xxx.t.domain.com \
            domains="['domain.com', 't.domain.com']"
        #returns: localhost.xxx

        salt-call infoblox.get_host_hostname fqdn=localhost.xxx.t.domain.com
        #returns: localhost
    '''
    name = name.lower().rstrip('.')
    if not domains:
        return name.split('.')[0]
    domain = get_host_domainname(name, domains, **api_opts)
    if domain and domain in name:
        return name.rsplit('.' + domain)[0]
    return name