'Updates or creates instance metadata.
If delete is True, metadata items that are not specified in the
`metadata` argument will be deleted.'

@check_instance_lock
@check_instance_state(vm_state=[vm_states.ACTIVE, vm_states.PAUSED, vm_states.SUSPENDED, vm_states.STOPPED], task_state=None)
def update_instance_metadata(self, context, instance, metadata, delete=False):

    orig = dict(instance.metadata)
    if delete:
        _metadata = metadata
    else:
        _metadata = dict(instance.metadata)
        _metadata.update(metadata)
    self._check_metadata_properties_quota(context, _metadata)
    instance.metadata = _metadata
    instance.save()
    diff = _diff_dict(orig, instance.metadata)
    self.compute_rpcapi.change_instance_metadata(context, instance=instance, diff=diff)
    return _metadata
