'Terminate connection of volume from host.'

@kaminario_logger
def terminate_connection(self, volume, connector):

    if (type(volume).__name__ != 'RestObject'):
        vol_name = self.get_volume_name(volume.id)
        if (volume.replication_status == K2_REP_FAILED_OVER):
            vol_name = self.get_rep_name(vol_name)
        LOG.debug('Searching    volume:    %s    in    K2.', vol_name)
        volume_rs = self.client.search('volumes', name=vol_name)
        if (hasattr(volume_rs, 'hits') and (volume_rs.total != 0)):
            volume = volume_rs.hits[0]
    else:
        vol_name = volume.name
    host_name = self.get_initiator_host_name(connector)
    host_rs = self.client.search('hosts', name=host_name)
    if (hasattr(host_rs, 'hits') and (host_rs.total != 0) and volume):
        host = host_rs.hits[0]
        LOG.debug('Searching    and    deleting    mapping    of    volume:    %(name)s    to    host:    %(host)s', {'host': host_name, 'name': vol_name})
        map_rs = self.client.search('mappings', volume=volume, host=host)
        if (hasattr(map_rs, 'hits') and (map_rs.total != 0)):
            map_rs.hits[0].delete()
        if (self.client.search('mappings', host=host).total == 0):
            LOG.debug('Deleting    initiator    hostname:    %s    in    K2.', host_name)
            host.delete()
    else:
        LOG.warning('Host:    %s    not    found    on    K2.', host_name)
