def set_api_version_request(self): """Set API version request based on the request header information.""" hdr_string = microversion_parse.get_version(self.headers, service_type='masakari') if hdr_string is None: self.api_version_request = api_version.APIVersionRequest( api_version.DEFAULT_API_VERSION) elif hdr_string == 'latest': # 'latest' is a special keyword which is equivalent to # requesting the maximum version of the API supported self.api_version_request = api_version.max_api_version() else: self.api_version_request = api_version.APIVersionRequest( hdr_string) # Check that the version requested is within the global # minimum/maximum of supported API versions if not self.api_version_request.matches( api_version.min_api_version(), api_version.max_api_version()): raise exception.InvalidGlobalAPIVersion( req_ver=self.api_version_request.get_string(), min_ver=api_version.min_api_version().get_string(), max_ver=api_version.max_api_version().get_string())
def test_check_for_versions_intersection_negative(self): func_list = [ versioned_method.VersionedMethod( 'foo', (api_version.APIVersionRequest('2.1')), api_version.APIVersionRequest('2.4'), None), versioned_method.VersionedMethod( 'foo', (api_version.APIVersionRequest('2.11')), (api_version.APIVersionRequest('3.1')), None), versioned_method.VersionedMethod( 'foo', (api_version.APIVersionRequest('2.8')), api_version.APIVersionRequest('2.9'), None), ] result = (wsgi.Controller.check_for_versions_intersection( func_list=func_list)) self.assertFalse(result) func_list = [ versioned_method.VersionedMethod( 'foo', (api_version.APIVersionRequest('2.12')), api_version.APIVersionRequest('2.14'), None), versioned_method.VersionedMethod( 'foo', (api_version.APIVersionRequest('3.0')), api_version.APIVersionRequest('3.4'), None) ] result = (wsgi.Controller.check_for_versions_intersection( func_list=func_list)) self.assertFalse(result)
def test_get_string(self): vers1_string = "1.0" vers1 = api_version_request.APIVersionRequest(vers1_string) self.assertEqual(vers1_string, vers1.get_string()) self.assertRaises(ValueError, api_version_request.APIVersionRequest().get_string)
def test_api_version_request_header(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("1.0") request = wsgi.Request.blank('/') request.headers = self._make_microversion_header('1.0') request.set_api_version_request() self.assertEqual(api_version.APIVersionRequest("1.0"), request.api_version_request)
def wrapper(*args, **kwargs): min_ver = api_version.APIVersionRequest(min_version) max_ver = api_version.APIVersionRequest(max_version) if 'req' in kwargs: ver = kwargs['req'].api_version_request else: ver = args[1].api_version_request ver.matches(min_ver, max_ver) # Only validate against the schema if it lies within # the version range specified. Note that if both min # and max are not specified the validator will always # be run. schema_validator = validators._SchemaValidator(request_body_schema) schema_validator.validate(kwargs['body']) return func(*args, **kwargs)
def blank(*args, **kwargs): kwargs['base_url'] = 'http://localhost/v1' use_admin_context = kwargs.pop('use_admin_context', False) project_id = kwargs.pop('project_id', uuidsentinel.fake_project_id) version = kwargs.pop('version', api_version.DEFAULT_API_VERSION) out = os_wsgi.Request.blank(*args, **kwargs) out.environ['masakari.context'] = FakeRequestContext( user_id=uuidsentinel.fake_user_id, project_id=project_id, is_admin=use_admin_context) out.api_version_request = api_version.APIVersionRequest(version) return out
def decorator(f): obj_min_ver = api_version.APIVersionRequest(min_ver) if max_ver: obj_max_ver = api_version.APIVersionRequest(max_ver) else: obj_max_ver = api_version.APIVersionRequest() # Add to list of versioned methods registered func_name = f.__name__ new_func = versioned_method.VersionedMethod( func_name, obj_min_ver, obj_max_ver, f) func_dict = getattr(cls, VER_METHOD_ATTR, {}) if not func_dict: setattr(cls, VER_METHOD_ATTR, func_dict) func_list = func_dict.get(func_name, []) if not func_list: func_dict[func_name] = func_list func_list.append(new_func) # Ensure the list is sorted by minimum version (reversed) # so later when we work through the list in order we find # the method which has the latest version which supports # the version requested. is_intersect = Controller.check_for_versions_intersection( func_list) if is_intersect: raise exception.ApiVersionsIntersect( name=new_func.name, min_ver=new_func.start_version, max_ver=new_func.end_version, ) func_list.sort(key=lambda f: f.start_version, reverse=True) return f
def test_api_version_request_header_none(self): request = wsgi.Request.blank('/') request.set_api_version_request() self.assertEqual( api_version.APIVersionRequest(api_version.DEFAULT_API_VERSION), request.api_version_request)
class FakeRequest(object): api_version_request = api_version.APIVersionRequest("1.0") environ = {}
def __init__(self, *args, **kwargs): super(Request, self).__init__(*args, **kwargs) self._extension_data = {'db_items': {}} if not hasattr(self, 'api_version_request'): self.api_version_request = api_version.APIVersionRequest()
def test_null_version(self): v = api_version_request.APIVersionRequest() self.assertTrue(v.is_null())
def _test_string(version, exp_major, exp_minor): v = api_version_request.APIVersionRequest(version) self.assertEqual(v.ver_major, exp_major) self.assertEqual(v.ver_minor, exp_minor)
def __init__(self, version=None): if version is None: version = '1.0' self.api_version_request = api_version.APIVersionRequest(version)