'Return the hostvars dictionary for the given instance.
Raise libbrook.rest.ApiException if it cannot retrieve all required information from the
Brook.io API.'

def hostvars(self, project, instance, template, api):

    hostvars = instance.to_dict()
    hostvars['brook_pid'] = hostvars.pop('pid')
    hostvars['brook_name'] = hostvars.pop('name')
    hostvars['brook_description'] = hostvars.pop('description')
    hostvars['brook_project'] = hostvars.pop('project')
    hostvars['brook_template'] = hostvars.pop('template')
    hostvars['brook_region'] = hostvars.pop('region')
    hostvars['brook_zone'] = hostvars.pop('zone')
    hostvars['brook_created_at'] = hostvars.pop('created_at')
    hostvars['brook_updated_at'] = hostvars.pop('updated_at')
    del hostvars['id']
    del hostvars['key']
    del hostvars['provider']
    del hostvars['image']
    hostvars['brook_project'] = project.name
    hostvars['brook_template'] = (template.name if template else None)
    status = api.status_instance(project_id=project.id, instance_id=instance.id)
    hostvars.update({'brook_status': status.state})
    tags = api.instance_tags(project_id=project.id, instance_id=instance.id)
    hostvars.update({'brook_tags': tags})
    addresses = api.instance_addresses(project_id=project.id, instance_id=instance.id)
    internal_ips = [address.address for address in addresses if (address.scope == 'internal')]
    external_ips = [address.address for address in addresses if (address.address and (address.scope == 'external'))]
    hostvars.update({'brook_internal_ips': internal_ips})
    hostvars.update({'brook_external_ips': external_ips})
    try:
        hostvars.update({'ansible_ssh_host': external_ips[0]})
    except IndexError:
        raise libbrook.rest.ApiException(status='502', reason='Instance    without    public    IP')
    return hostvars
