1 # Copyright (c) 2015 Mirantis, Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 from pecan import hooks
18 from neutron.api.v2 import attributes as v2_attributes
19 from neutron.api.v2 import base as v2_base
22 class BodyValidationHook(hooks.PecanHook):
26 def before(self, state):
27 if state.request.method not in ('POST', 'PUT'):
29 resource = state.request.context.get('resource')
30 collection = state.request.context.get('collection')
31 neutron_context = state.request.context['neutron_context']
32 is_create = state.request.method == 'POST'
35 # Prepare data to be passed to the plugin from request body
36 data = v2_base.Controller.prepare_request_body(
41 v2_attributes.get_collection_info(collection),
43 if collection in data:
44 state.request.context['resources'] = [item[resource] for item in
47 state.request.context['resources'] = [data[resource]]