'Apply changes to instance metadata to xenstore.'

def change_instance_metadata(self, instance, diff):

    try:
        vm_ref = self._get_vm_opaque_ref(instance)
    except exception.NotFound:
        LOG.warning('Unable    to    update    metadata,    VM    not    found.', instance=instance, exc_info=True)
        return
    def process_change(location, change):
        if (change[0] == '-'):
            self._remove_from_param_xenstore(vm_ref, location)
            try:
                self._delete_from_xenstore(instance, location, vm_ref=vm_ref)
            except exception.InstanceNotFound:
                pass
        elif (change[0] == '+'):
            self._add_to_param_xenstore(vm_ref, location, jsonutils.dumps(change[1]))
            try:
                self._write_to_xenstore(instance, location, change[1], vm_ref=vm_ref)
            except exception.InstanceNotFound:
                pass
    @utils.synchronized(('xenstore-' + instance['uuid']))
    def update_meta():
        for (key, change) in diff.items():
            key = self._sanitize_xenstore_key(key)
            location = ('vm-data/user-metadata/%s' % key)
            process_change(location, change)
    update_meta()
