# Buggy code
def vb_get_network_addresses(machine_name=None, machine=None):
    """
    TODO distinguish between private and public addresses

    A valid machine_name or a machine is needed to make this work!

    !!!
    Guest prerequisite: GuestAddition
    !!!

    Thanks to Shrikant Havale for the StackOverflow answer http://stackoverflow.com/a/29335390

    More information on guest properties: https://www.virtualbox.org/manual/ch04.html#guestadd-guestprops

    @param machine_name:
    @type machine_name: str
    @param machine:
    @type machine: IMachine
    @return: All the IPv4 addresses we could get
    @rtype: str[]
    """
    if machine_name:
        machine = vb_get_box().findMachine(machine_name)

    ip_addresses = []
    # We can't trust virtualbox to give us up to date guest properties if the machine isn't running
    # For some reason it may give us outdated (cached?) values
    if machine.state == _virtualboxManager.constants.MachineState_Running:
        total_slots = int(
            machine.getGuestPropertyValue("/VirtualBox/GuestInfo/Net/Count")
        )
        for i in range(total_slots):
            try:
                address = machine.getGuestPropertyValue(
                    "/VirtualBox/GuestInfo/Net/{0}/V4/IP".format(i)
                )
                if address:
                    ip_addresses.append(address)
            except Exception as e:
                log.debug(e.message)

    return ip_addresses


# Traceback (ValueError)
[DEBUG   ] Reading configuration from /etc/salt/cloud
[DEBUG   ] Reading configuration from /etc/salt/master
[DEBUG   ] Using cached minion ID from /etc/salt/minion_id: salt-master.local
[DEBUG   ] Missing configuration file: /etc/salt/cloud.providers
[DEBUG   ] Including configuration from '/etc/salt/cloud.providers.d/virtualbox.conf'
[DEBUG   ] Reading configuration from /etc/salt/cloud.providers.d/virtualbox.conf
[DEBUG   ] Missing configuration file: /etc/salt/cloud.profiles
[DEBUG   ] Including configuration from '/etc/salt/cloud.profiles.d/virtualbox.conf'
[DEBUG   ] Reading configuration from /etc/salt/cloud.profiles.d/virtualbox.conf
[DEBUG   ] Configuration file path: /etc/salt/cloud
[WARNING ] Insecure logging configuration detected! Sensitive data may be logged.
[INFO    ] salt-cloud starting
[DEBUG   ] Could not LazyLoad parallels.avail_sizes: 'parallels' __virtual__ returned False
[DEBUG   ] LazyLoaded parallels.avail_locations
[DEBUG   ] LazyLoaded proxmox.avail_sizes
[DEBUG   ] Could not LazyLoad saltify.destroy: 'saltify.destroy' is not available.
[DEBUG   ] Could not LazyLoad saltify.avail_sizes: 'saltify.avail_sizes' is not available.
[DEBUG   ] Could not LazyLoad saltify.avail_images: 'saltify.avail_images' is not available.
[DEBUG   ] Could not LazyLoad saltify.avail_locations: 'saltify.avail_locations' is not available.
[DEBUG   ] Could not LazyLoad rackspace.reboot: 'rackspace.reboot' is not available.
[DEBUG   ] LazyLoaded openstack.list_locations
[DEBUG   ] Could not LazyLoad rackspace.list_locations: 'rackspace.list_locations' is not available.
[DEBUG   ] Could not LazyLoad virtualbox.optimize_providers: 'virtualbox.optimize_providers' is not available.
[DEBUG   ] The 'virtualbox' cloud driver is unable to be optimized.
[DEBUG   ] Could not LazyLoad parallels.avail_sizes: 'parallels' __virtual__ returned False
[DEBUG   ] LazyLoaded parallels.avail_locations
[DEBUG   ] LazyLoaded proxmox.avail_sizes
[DEBUG   ] Could not LazyLoad saltify.destroy: 'saltify.destroy' is not available.
[DEBUG   ] Could not LazyLoad saltify.avail_sizes: 'saltify.avail_sizes' is not available.
[DEBUG   ] Could not LazyLoad saltify.avail_images: 'saltify.avail_images' is not available.
[DEBUG   ] Could not LazyLoad saltify.avail_locations: 'saltify.avail_locations' is not available.
[DEBUG   ] Could not LazyLoad rackspace.reboot: 'rackspace.reboot' is not available.
[DEBUG   ] LazyLoaded openstack.list_locations
[DEBUG   ] Could not LazyLoad rackspace.list_locations: 'rackspace.list_locations' is not available.
[DEBUG   ] Generating minion keys for 'ubuntu-test-minion'
[DEBUG   ] Going to fire event: starting create
[DEBUG   ] LazyLoaded cloud.filter_event
[DEBUG   ] MasterEvent PUB socket URI: /var/run/salt/master/master_event_pub.ipc
[DEBUG   ] MasterEvent PULL socket URI: /var/run/salt/master/master_event_pull.ipc
[DEBUG   ] Initializing new IPCClient for path: /var/run/salt/master/master_event_pull.ipc
[DEBUG   ] Sending event: tag = salt/cloud/ubuntu-test-minion/creating; data = {'profile': 'virtualbox', '_stamp': '2017-09-09T20:27:45.565481', 'name': 'ubuntu-test-minion', 'driver': 'virtualbox', 'provider': 'virtualbox-config:virtualbox', 'event': 'starting create'}
[DEBUG   ] MasterEvent PUB socket URI: /var/run/salt/master/master_event_pub.ipc
[DEBUG   ] MasterEvent PULL socket URI: /var/run/salt/master/master_event_pull.ipc
[DEBUG   ] Initializing new IPCClient for path: /var/run/salt/master/master_event_pull.ipc
[DEBUG   ] Sending event: tag = salt/cloud/ubuntu-test-minion/requesting; data = {'_stamp': '2017-09-09T20:27:45.592241', 'clone_from': 'ubuntu_clone_16.04  Clone', 'name': 'ubuntu-test-minion', 'event': 'requesting instance'}
[INFO    ] Clone virtualbox machine ubuntu-test-minion from ubuntu_clone_16.04  Clone
[INFO    ] Finished cloning ubuntu-test-minion from ubuntu_clone_16.04  Clone
[INFO    ] Starting machine ubuntu-test-minion in state PoweredOff
[INFO    ] Started machine ubuntu-test-minion
[ERROR   ] There was a profile error: invalid literal for int() with base 10: ''
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/cloud/cli.py", line 284, in run
self.config.get('names')
File "/usr/lib/python2.7/dist-packages/salt/cloud/__init__.py", line 1457, in run_profile
ret[name] = self.create(vm_)
File "/usr/lib/python2.7/dist-packages/salt/cloud/__init__.py", line 1287, in create
output = self.clouds[func](vm_)
File "/usr/lib/python2.7/dist-packages/salt/cloud/clouds/virtualbox.py", line 167, in create
ips = vb_wait_for_network_address(wait_for_ip_timeout, machine_name=vm_name)
File "/usr/lib/python2.7/dist-packages/salt/utils/virtualbox.py", line 219, in vb_wait_for_network_address
return wait_for(vb_get_network_addresses, timeout=timeout, step=step, default=[], func_kwargs=kwargs)
File "/usr/lib/python2.7/dist-packages/salt/utils/timeout.py", line 41, in wait_for
call_ret = func(*func_args, **func_kwargs)
File "/usr/lib/python2.7/dist-packages/salt/utils/virtualbox.py", line 282, in vb_get_network_addresses
total_slots = int(machine.getGuestPropertyValue('/VirtualBox/GuestInfo/Net/Count'))
ValueError: invalid literal for int() with base 10: ''
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 1133, in __global_logging_exception_handler
exc_type, exc_value, exc_traceback
File "/usr/lib/python2.7/logging/__init__.py", line 1193, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 327, in _log
self, level, msg, args, exc_info=exc_info, extra=extra
File "/usr/lib/python2.7/logging/__init__.py", line 1285, in _log
record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 350, in makeRecord
_msg = msg.decode(salt_system_encoding, 'replace')
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
TypeError: 'NoneType' object is not callable

Original exception was:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: __import__ not found
Failed to log an error record
Original error follows:
Wrapping an interface object for the gateway failed

Traceback (most recent call last):
File "/usr/lib/virtualbox/sdk/bindings/xpcom/python/xpcom/server/policy.py", line 212, in _MakeInterfaceParam_
import xpcom.xpt
<type 'exceptions.ImportError'>: No module named xpt
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 1133, in __global_logging_exception_handler
exc_type, exc_value, exc_traceback
File "/usr/lib/python2.7/logging/__init__.py", line 1193, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 327, in _log
self, level, msg, args, exc_info=exc_info, extra=extra
File "/usr/lib/python2.7/logging/__init__.py", line 1285, in _log
record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 350, in makeRecord
_msg = msg.decode(salt_system_encoding, 'replace')
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
TypeError: 'NoneType' object is not callable

Original exception was:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: __import__ not found
Failed to log an error record
Original error follows:
The exception handler _CallMethodException_ failed!

Traceback (most recent call last):
File "/usr/lib/virtualbox/sdk/bindings/xpcom/python/xpcom/server/policy.py", line 335, in _CallMethodException_
self._doHandleException("<building method repr>", sys.exc_info())
File "/usr/lib/virtualbox/sdk/bindings/xpcom/python/xpcom/server/policy.py", line 312, in _doHandleException
logger.exception("Unhandled exception calling '%s'", func_name)
File "/usr/lib/python2.7/logging/__init__.py", line 1200, in exception
self.error(msg, *args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1193, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 327, in _log
self, level, msg, args, exc_info=exc_info, extra=extra
File "/usr/lib/python2.7/logging/__init__.py", line 1285, in _log
record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 350, in makeRecord
_msg = msg.decode(salt_system_encoding, 'replace')
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
<type 'exceptions.TypeError'>: 'NoneType' object is not callable
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 1133, in __global_logging_exception_handler
exc_type, exc_value, exc_traceback
File "/usr/lib/python2.7/logging/__init__.py", line 1193, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 327, in _log
self, level, msg, args, exc_info=exc_info, extra=extra
File "/usr/lib/python2.7/logging/__init__.py", line 1285, in _log
record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
File "/usr/lib/python2.7/dist-packages/salt/log/setup.py", line 350, in makeRecord
_msg = msg.decode(salt_system_encoding, 'replace')
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
TypeError: 'NoneType' object is not callable

Original exception was:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: __import__ not found
Failed to log an error record
Original error follows:
The function 'observe' failed

Traceback (most recent call last):
File "/usr/lib/virtualbox/sdk/bindings/xpcom/python/xpcom/server/policy.py", line 281, in _CallMethod_
return 0, func(*params)
File "/usr/lib/virtualbox/sdk/bindings/xpcom/python/xpcom/components.py", line 233, in observe
xpcom.client._shutdown()
<type 'exceptions.TypeError'>: 'NoneType' object is not callable

# Fixed code
def vb_get_network_addresses(machine_name=None, machine=None):
    """
    TODO distinguish between private and public addresses

    A valid machine_name or a machine is needed to make this work!

    !!!
    Guest prerequisite: GuestAddition
    !!!

    Thanks to Shrikant Havale for the StackOverflow answer http://stackoverflow.com/a/29335390

    More information on guest properties: https://www.virtualbox.org/manual/ch04.html#guestadd-guestprops

    @param machine_name:
    @type machine_name: str
    @param machine:
    @type machine: IMachine
    @return: All the IPv4 addresses we could get
    @rtype: str[]
    """
    if machine_name:
        machine = vb_get_box().findMachine(machine_name)

    ip_addresses = []
    # We can't trust virtualbox to give us up to date guest properties if the machine isn't running
    # For some reason it may give us outdated (cached?) values
    if machine.state == _virtualboxManager.constants.MachineState_Running:
        try:
            total_slots = int(
                machine.getGuestPropertyValue("/VirtualBox/GuestInfo/Net/Count")
            )
        except ValueError:
            total_slots = 0
        for i in range(total_slots):
            try:
                address = machine.getGuestPropertyValue(
                    "/VirtualBox/GuestInfo/Net/{0}/V4/IP".format(i)
                )
                if address:
                    ip_addresses.append(address)
            except Exception as e:
                log.debug(e.message)

    return ip_addresses