'Creates a new instance event.'

@extensions.expected_errors((400, 403, 404))
@wsgi.response(200)
@validation.schema(server_external_events.create, '2.1', '2.50')
@validation.schema(server_external_events.create_v251, '2.51')
def create(self, req, body):

    context = req.environ['nova.context']
    context.can((see_policies.POLICY_ROOT % 'create'))
    response_events = []
    accepted_events = []
    accepted_instances = set()
    result = 200
    body_events = body['events']
    instance_uuids = set([event['server_uuid'] for event in body_events])
    instance_mappings = objects.InstanceMappingList.get_by_instance_uuids(context, list(instance_uuids))
    instances = self._get_instances_all_cells(context, instance_uuids, instance_mappings)
    for _event in body_events:
        client_event = dict(_event)
        event = objects.InstanceExternalEvent(context)
        event.instance_uuid = client_event.pop('server_uuid')
        event.name = client_event.pop('name')
        event.status = client_event.pop('status', 'completed')
        event.tag = client_event.pop('tag', None)
        response_events.append(_event)
        instance = instances.get(event.instance_uuid)
        if (not instance):
            LOG.debug('Dropping    event    %(name)s:%(tag)s    for    unknown    instance    %(instance_uuid)s', {'name': event.name, 'tag': event.tag, 'instance_uuid': event.instance_uuid})
            _event['status'] = 'failed'
            _event['code'] = 404
            result = 207
            continue
        if (not self._is_event_tag_present_when_required(event)):
            LOG.debug('Event    tag    is    missing    for    instance    %(instance)s.    Dropping    event    %(event)s', {'instance': event.instance_uuid, 'event': event.name})
            _event['status'] = 'failed'
            _event['code'] = 400
            result = 207
        elif instance.host:
            accepted_events.append(event)
            accepted_instances.add(instance)
            LOG.info('Creating    event    %(name)s:%(tag)s    for    instance    %(instance_uuid)s    on    %(host)s', {'name': event.name, 'tag': event.tag, 'instance_uuid': event.instance_uuid, 'host': instance.host})
            _event['status'] = 'completed'
            _event['code'] = 200
        else:
            LOG.debug('Unable    to    find    a    host    for    instance    %(instance)s.    Dropping    event    %(event)s', {'instance': event.instance_uuid, 'event': event.name})
            _event['status'] = 'failed'
            _event['code'] = 422
            result = 207
    if accepted_events:
        self.compute_api.external_instance_event(context, accepted_instances, accepted_events)
    else:
        msg = _('No    instances    found    for    any    event')
        raise webob.exc.HTTPNotFound(explanation=msg)
    robj = wsgi.ResponseObject({'events': response_events})
    robj._code = result
    return robj
