def delete(self, req, flavor_id, id): """Deletes an existing extra spec.""" context = req.environ['nova.context'] context.can(fes_policies.POLICY_ROOT % 'delete') flavor = common.get_flavor(context, flavor_id) self._check_flavor_in_use(flavor) try: # The id object is an aggregation of multiple extra spec keys # The keys are aggregated using the ';' character # This allows multiple extra specs to be deleted in one call # This is required since some validators will raise an exception # if one extra spec exists while another is missing ids = id.split(';') for an_id in ids: del flavor.extra_specs[an_id] self._validate_extra_specs(flavor) flavor.save() except (exception.FlavorExtraSpecsNotFound, exception.FlavorNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except KeyError: msg = _("Flavor %(flavor_id)s has no extra specs with " "key %(key)s.") % dict(flavor_id=flavor_id, key=id) raise webob.exc.HTTPNotFound(explanation=msg)
def show(self, req, flavor_id, id): """Return a single extra spec item.""" context = req.environ['nova.context'] authorize(context, action='show') flavor = common.get_flavor(context, flavor_id) try: return {id: flavor.extra_specs[id]} except KeyError: msg = _("Flavor %(flavor_id)s has no extra specs with " "key %(key)s.") % dict(flavor_id=flavor_id, key=id) raise webob.exc.HTTPNotFound(explanation=msg)
def show(self, req, flavor_id, id): """Return a single extra spec item.""" context = req.environ['nova.context'] context.can(fes_policies.POLICY_ROOT % 'show', target={'project_id': context.project_id}) flavor = common.get_flavor(context, flavor_id) try: return {id: flavor.extra_specs[id]} except KeyError: msg = _("Flavor %(flavor_id)s has no extra specs with " "key %(key)s.") % dict(flavor_id=flavor_id, key=id) raise webob.exc.HTTPNotFound(explanation=msg)
def show(self, req, flavor_id, id): """Return a single extra spec item.""" context = req.environ['nova.context'] context.can(fes_policies.POLICY_ROOT % 'show') flavor = common.get_flavor(context, flavor_id) try: return {id: flavor.extra_specs[id]} except KeyError: msg = _("Flavor %(flavor_id)s has no extra specs with " "key %(key)s.") % dict(flavor_id=flavor_id, key=id) raise webob.exc.HTTPNotFound(explanation=msg)
def index(self, req, flavor_id): context = req.environ["nova.context"] authorize(context) flavor = common.get_flavor(context, flavor_id) # public flavor to all projects if flavor.is_public: explanation = _("Access list not available for public flavors.") raise webob.exc.HTTPNotFound(explanation=explanation) # private flavor to listed projects only return _marshall_flavor_access(flavor)
def index(self, req, flavor_id): context = req.environ['nova.context'] context.can(fa_policies.BASE_POLICY_NAME) flavor = common.get_flavor(context, flavor_id) # public flavor to all projects if flavor.is_public: explanation = _("Access list not available for public flavors.") raise webob.exc.HTTPNotFound(explanation=explanation) # private flavor to listed projects only return _marshall_flavor_access(flavor)
def index(self, req, flavor_id): context = req.environ['nova.context'] authorize(context) flavor = common.get_flavor(context, flavor_id) # public flavor to all projects if flavor.is_public: explanation = _("Access list not available for public flavors.") raise webob.exc.HTTPNotFound(explanation=explanation) # private flavor to listed projects only return _marshall_flavor_access(flavor)
def create(self, req, flavor_id, body): context = req.environ['nova.context'] context.can(fes_policies.POLICY_ROOT % 'create', target={}) specs = body['extra_specs'] self._check_extra_specs_value(req, specs) flavor = common.get_flavor(context, flavor_id) try: flavor.extra_specs = dict(flavor.extra_specs, **specs) flavor.save() except exception.FlavorExtraSpecUpdateCreateFailed as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except exception.FlavorNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return body
def create(self, req, flavor_id, body): context = req.environ['nova.context'] context.can(fes_policies.POLICY_ROOT % 'create') specs = body['extra_specs'] self._check_extra_specs_value(specs) flavor = common.get_flavor(context, flavor_id) try: flavor.extra_specs = dict(flavor.extra_specs, **specs) flavor.save() except exception.FlavorExtraSpecUpdateCreateFailed as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except exception.FlavorNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return body
def create(self, req, flavor_id, body): context = req.environ['nova.context'] authorize(context, action='create') specs = body['extra_specs'] self._check_extra_specs_value(specs) flavor = common.get_flavor(context, flavor_id) try: flavor.extra_specs = dict(flavor.extra_specs, **specs) flavor.save() except exception.FlavorExtraSpecUpdateCreateFailed as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except exception.FlavorNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return body
def delete(self, req, flavor_id, id): """Deletes an existing extra spec.""" context = req.environ['nova.context'] authorize(context, action='delete') flavor = common.get_flavor(context, flavor_id) try: del flavor.extra_specs[id] flavor.save() except (exception.FlavorExtraSpecsNotFound, exception.FlavorNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except KeyError: msg = _("Flavor %(flavor_id)s has no extra specs with " "key %(key)s.") % dict(flavor_id=flavor_id, key=id) raise webob.exc.HTTPNotFound(explanation=msg)
def create(self, req, flavor_id, body): context = req.environ['nova.context'] authorize(context, action='create') self._check_body(body) specs = body.get('extra_specs') self._check_extra_specs(specs) flavor = common.get_flavor(context, flavor_id) try: flavor.extra_specs = dict(flavor.extra_specs, **specs) flavor.save() except exception.FlavorExtraSpecUpdateCreateFailed as e: raise exc.HTTPConflict(explanation=e.format_message()) except exception.FlavorNotFound as error: raise exc.HTTPNotFound(explanation=error.format_message()) return body
def index(self, req, flavor_id): context = req.environ['nova.context'] authorize(context) # NOTE(alex_xu): back-compatible with db layer hard-code admin # permission checks. nova_context.require_admin_context(context) flavor = common.get_flavor(context, flavor_id) # public flavor to all projects if flavor.is_public: explanation = _("Access list not available for public flavors.") raise webob.exc.HTTPNotFound(explanation=explanation) # private flavor to listed projects only return _marshall_flavor_access(flavor)
def delete(self, req, flavor_id, id): """Deletes an existing extra spec.""" context = req.environ['nova.context'] context.can(fes_policies.POLICY_ROOT % 'delete') flavor = common.get_flavor(context, flavor_id) try: del flavor.extra_specs[id] flavor.save() except (exception.FlavorExtraSpecsNotFound, exception.FlavorNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except KeyError: msg = _("Flavor %(flavor_id)s has no extra specs with " "key %(key)s.") % dict(flavor_id=flavor_id, key=id) raise webob.exc.HTTPNotFound(explanation=msg)
def update(self, req, flavor_id, id, body): context = req.environ['nova.context'] authorize(context, action='update') self._check_extra_specs_value(body) if id not in body: expl = _('Request body and URI mismatch') raise webob.exc.HTTPBadRequest(explanation=expl) flavor = common.get_flavor(context, flavor_id) try: flavor.extra_specs = dict(flavor.extra_specs, **body) flavor.save() except exception.FlavorExtraSpecUpdateCreateFailed as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except exception.FlavorNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return body
def _remove_tenant_access(self, req, id, body): context = req.environ['nova.context'] context.can(fa_policies.POLICY_ROOT % "remove_tenant_access") vals = body['removeTenantAccess'] tenant = vals['tenant'] # NOTE(gibi): We have to load a flavor from the db here as # flavor.remove_access() will try to emit a notification and that needs # a fully loaded flavor. flavor = common.get_flavor(context, id) try: flavor.remove_access(tenant) except (exception.FlavorAccessNotFound, exception.FlavorNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return _marshall_flavor_access(flavor)
def update(self, req, flavor_id, id, body): context = req.environ['nova.context'] context.can(fes_policies.POLICY_ROOT % 'update') self._check_extra_specs_value(body) if id not in body: expl = _('Request body and URI mismatch') raise webob.exc.HTTPBadRequest(explanation=expl) flavor = common.get_flavor(context, flavor_id) try: flavor.extra_specs = dict(flavor.extra_specs, **body) flavor.save() except exception.FlavorExtraSpecUpdateCreateFailed as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except exception.FlavorNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return body
def _remove_tenant_access(self, req, id, body): context = req.environ['nova.context'] context.can( fa_policies.POLICY_ROOT % "remove_tenant_access") vals = body['removeTenantAccess'] tenant = vals['tenant'] # NOTE(gibi): We have to load a flavor from the db here as # flavor.remove_access() will try to emit a notification and that needs # a fully loaded flavor. flavor = common.get_flavor(context, id) try: flavor.remove_access(tenant) except (exception.FlavorAccessNotFound, exception.FlavorNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return _marshall_flavor_access(flavor)
def update(self, req, flavor_id, id, body): context = req.environ['nova.context'] authorize(context, action='update') self._check_extra_specs(body) if id not in body: expl = _('Request body and URI mismatch') raise exc.HTTPBadRequest(explanation=expl) if len(body) > 1: expl = _('Request body contains too many items') raise exc.HTTPBadRequest(explanation=expl) flavor = common.get_flavor(context, flavor_id) try: flavor.extra_specs = dict(flavor.extra_specs, **body) flavor.save() except exception.FlavorExtraSpecUpdateCreateFailed as e: raise exc.HTTPConflict(explanation=e.format_message()) except exception.FlavorNotFound as error: raise exc.HTTPNotFound(explanation=error.format_message()) return body
def _add_tenant_access(self, req, id, body): context = req.environ['nova.context'] context.can(fa_policies.POLICY_ROOT % "add_tenant_access") vals = body['addTenantAccess'] tenant = vals['tenant'] flavor = common.get_flavor(context, id) try: if api_version_request.is_supported(req, min_version='2.7'): if flavor.is_public: exp = _("Can not add access to a public flavor.") raise webob.exc.HTTPConflict(explanation=exp) flavor.add_access(tenant) except exception.FlavorNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.FlavorAccessExists as err: raise webob.exc.HTTPConflict(explanation=err.format_message()) return _marshall_flavor_access(flavor)
def _add_tenant_access(self, req, id, body): context = req.environ["nova.context"] authorize(context, action="add_tenant_access") vals = body["addTenantAccess"] tenant = vals["tenant"] flavor = common.get_flavor(context, id) try: if api_version_request.is_supported(req, min_version="2.7"): if flavor.is_public: exp = _("Can not add access to a public flavor.") raise webob.exc.HTTPConflict(explanation=exp) flavor.add_access(tenant) except exception.FlavorNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.FlavorAccessExists as err: raise webob.exc.HTTPConflict(explanation=err.format_message()) except exception.AdminRequired as e: raise webob.exc.HTTPForbidden(explanation=e.format_message()) return _marshall_flavor_access(flavor)
def _add_tenant_access(self, req, id, body): context = req.environ['nova.context'] authorize(context, action="add_tenant_access") vals = body['addTenantAccess'] tenant = vals['tenant'] flavor = common.get_flavor(context, id) try: req_ver = req.api_version_request if req_ver >= api_version_request.APIVersionRequest("2.7"): if flavor.is_public: exp = _("Can not add access to a public flavor.") raise webob.exc.HTTPConflict(explanation=exp) flavor.add_access(tenant) except exception.FlavorNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.FlavorAccessExists as err: raise webob.exc.HTTPConflict(explanation=err.format_message()) except exception.AdminRequired as e: raise webob.exc.HTTPForbidden(explanation=e.format_message()) return _marshall_flavor_access(flavor)
def _get_extra_specs(self, context, flavor_id): flavor = common.get_flavor(context, flavor_id) return dict(extra_specs=flavor.extra_specs)