'Updates a single Instance object with some updates dict.
Returns the updated instance.'

def update_instance(self, context, instance, updates):

    if instance.obj_attr_is_set('id'):
        instance.update(updates)
        inst_map = self._get_instance_map_or_none(context, instance.uuid)
        if (inst_map and (inst_map.cell_mapping is not None) and (not CONF.cells.enable)):
            with nova_context.target_cell(context, inst_map.cell_mapping) as cctxt:
                with instance.obj_alternate_context(cctxt):
                    instance.save()
        else:
            instance.save()
    else:
        try:
            build_req = objects.BuildRequest.get_by_instance_uuid(context, instance.uuid)
            instance = build_req.instance
            instance.update(updates)
            build_req.save()
        except exception.BuildRequestNotFound:
            expected_attrs = ['flavor', 'pci_devices', 'numa_topology', 'tags', 'metadata', 'system_metadata', 'security_groups', 'info_cache']
            inst_map = self._get_instance_map_or_none(context, instance.uuid)
            if (inst_map and (inst_map.cell_mapping is not None)):
                with nova_context.target_cell(context, inst_map.cell_mapping) as cctxt:
                    instance = objects.Instance.get_by_uuid(cctxt, instance.uuid, expected_attrs=expected_attrs)
                    instance.update(updates)
                    instance.save()
            else:
                instance = objects.Instance.get_by_uuid(context, instance.uuid, expected_attrs=expected_attrs)
                instance.update(updates)
                instance.save()
    return instance
