Ejemplo n.º 1
0
    def dispatch(self, controller, action_name, request, params):
        """

        @param controller:
        @param action_name:
        @param request:
        @param params:
        @return:
        """
        request_header_validator = valid.DictValidator({
            'Content-Length':
            valid.IntegerValidator(max=512),
            'Content-Type':
            valid.ConstValidator('application/json'),
        })
        controller.request = request
        controller.params = params
        action = getattr(controller, action_name)
        try:
            if request.method in ['POST', 'PUT']:
                request_header_validator(dict(request.headers))
            retval = action()
            return retval
        except LibcloudRestError, error:
            return Response(error.to_json(),
                            status=error.http_status_code,
                            mimetype='application/json')
Ejemplo n.º 2
0
 def test_dict(self):
     dict_validator = validators.DictValidator(
         {'arg1': validators.StringValidator()})
     valid_dict = {'arg1': 'str'}
     invalid_dict = {'arg1': 123}
     invalid_dict2 = {'abc': 'str'}
     self.assertTrue(dict_validator(valid_dict))
     self.assertRaises(ValidationError, dict_validator, invalid_dict)
     self.assertRaises(ValidationError, dict_validator, invalid_dict2)
Ejemplo n.º 3
0
 def test_name(self):
     int_validator = validators.IntegerValidator(name='test')
     invalid_data = 'abc'
     self.assertRaisesRegexp(ValidationError, 'test.*', int_validator,
                             invalid_data)
     dict_validator = validators.DictValidator(
         {'test_name': validators.ConstValidator('333')})
     self.assertRaisesRegexp(ValidationError, 'test_name .*',
                             dict_validator, {})
     str_validator = validators.StringValidator()
     self.assertRaisesRegexp(ValidationError,
                             '%s .*' % (str_validator.default_name),
                             str_validator, {})
Ejemplo n.º 4
0
 def preprocess_request(self, request):
     request_header_validator = valid.DictValidator({
         'Content-Length':
         valid.IntegerValidator(max=MAX_BODY_LENGTH),
         'Content-Type':
         valid.ConstValidator('application/json'),
     })
     if request.method in ['POST', 'PUT'] and\
             not re.match(self.storage_url, request.path):
         request_header_validator(dict(request.headers))
         #TODO: FIXME GK
     if request.method == 'GET':
         data = url_decode(request.query_string, cls=dict)
         request.data = json.dumps(data)
Ejemplo n.º 5
0
 def test_required(self):
     req_string_validator = validators.StringValidator()
     not_req_string_validator = validators.StringValidator(required=False)
     dict_validator = validators.DictValidator({
         'req':
         req_string_validator,
         'opt':
         not_req_string_validator,
     })
     valid_dict = {'req': 'abc'}
     valid_dict2 = {'req': 'abc', 'opt': 'def'}
     invalid_dict = {'opt': 'def'}
     self.assertTrue(dict_validator(valid_dict))
     self.assertTrue(dict_validator(valid_dict2))
     self.assertRaises(ValidationError, dict_validator, invalid_dict)
Ejemplo n.º 6
0
 def create_node(self):
     node_validator = valid.DictValidator({
         'name':
         valid.StringValidator(),
         'size_id':
         valid.StringValidator(),
         'image_id':
         valid.StringValidator(),
         'location_id':
         valid.StringValidator(required=False)
     })
     driver = self._get_driver_instance()
     try:
         node_data = json.loads(self.request.data)
     except ValueError, e:
         raise MalformedJSONError(msg=e.message)