def test_default_type_update(self): default_type = db.project_default_volume_type_set( self.ctxt, fake.VOLUME_TYPE_ID, fake.PROJECT_ID) self.assertEqual(fake.PROJECT_ID, default_type.project_id) self.assertEqual(fake.VOLUME_TYPE_ID, default_type.volume_type_id) # update to type 2 db.project_default_volume_type_set(self.ctxt, fake.VOLUME_TYPE2_ID, fake.PROJECT_ID) default_type = db.project_default_volume_type_get( self.ctxt, project_id=fake.PROJECT_ID) self.assertEqual(fake.PROJECT_ID, default_type.project_id) self.assertEqual(fake.VOLUME_TYPE2_ID, default_type.volume_type_id) # update to type 3 db.project_default_volume_type_set(self.ctxt, fake.VOLUME_TYPE3_ID, fake.PROJECT_ID) default_type = db.project_default_volume_type_get( self.ctxt, project_id=fake.PROJECT_ID) self.assertEqual(fake.PROJECT_ID, default_type.project_id) self.assertEqual(fake.VOLUME_TYPE3_ID, default_type.volume_type_id) # back to original db.project_default_volume_type_set(self.ctxt, fake.VOLUME_TYPE_ID, fake.PROJECT_ID) default_type = db.project_default_volume_type_get( self.ctxt, project_id=fake.PROJECT_ID) self.assertEqual(fake.PROJECT_ID, default_type.project_id) self.assertEqual(fake.VOLUME_TYPE_ID, default_type.volume_type_id) db.project_default_volume_type_unset(self.ctxt, default_type.project_id)
def test_default_type_delete(self): db.project_default_volume_type_set(self.ctxt, fake.VOLUME_TYPE_ID, fake.PROJECT_ID) default_types = db.project_default_volume_type_get(self.ctxt) self.assertEqual(1, len(default_types)) db.project_default_volume_type_unset(self.ctxt, default_types[0].project_id) default_types = db.project_default_volume_type_get(self.ctxt) self.assertEqual(0, len(default_types))
def get_default_volume_type(contxt=None): """Get the default volume type. :raises VolumeTypeDefaultMisconfiguredError: when the configured default is not found """ if contxt: project_default = db.project_default_volume_type_get( contxt, contxt.project_id) if project_default: return get_volume_type(contxt, project_default.volume_type_id) name = CONF.default_volume_type ctxt = context.get_admin_context() vol_type = {} try: vol_type = get_volume_type_by_name(ctxt, name) except (exception.VolumeTypeNotFoundByName, exception.InvalidVolumeType): # Couldn't find volume type with the name in default_volume_type # flag, record this issue and raise exception LOG.exception('Default volume type is not found. ' 'Please check default_volume_type config:') raise exception.VolumeTypeDefaultMisconfiguredError( volume_type_name=name) return vol_type
def test_default_type_get(self): db.project_default_volume_type_set(self.ctxt, fake.VOLUME_TYPE_ID, fake.PROJECT_ID) default_type = db.project_default_volume_type_get( self.ctxt, project_id=fake.PROJECT_ID) self.assertEqual(fake.PROJECT_ID, default_type.project_id) self.assertEqual(fake.VOLUME_TYPE_ID, default_type.volume_type_id) db.project_default_volume_type_unset(self.ctxt, default_type.project_id)
def index(self, req): """Return a list of default types.""" context = req.environ['cinder.context'] try: context.authorize(policy.GET_ALL_POLICY) except exception.NotAuthorized: explanation = _("You are not authorized to perform this " "operation.") raise exc.HTTPForbidden(explanation=explanation) default_types = db.project_default_volume_type_get(context) return self._view_builder.index(default_types)
def detail(self, req, id): """Return detail of a default type.""" context = req.environ['cinder.context'] project_id = id utils.validate_project_and_authorize(context, project_id, policy.GET_POLICY) default_type = db.project_default_volume_type_get(context, project_id) if not default_type: raise exception.VolumeTypeProjectDefaultNotFound( project_id=project_id) return self._view_builder.detail(default_type)