Beispiel #1
0
    def _create(self, req, body):
        context = req.environ['nova.context']
        authorize(context)
        if not self.is_valid_body(body, 'flavor'):
            msg = _("Invalid request body")
            raise webob.exc.HTTPBadRequest(explanation=msg)
        vals = body['flavor']
        name = vals.get('name')
        flavorid = vals.get('id')
        memory = vals.get('ram')
        vcpus = vals.get('vcpus')
        root_gb = vals.get('disk')
        ephemeral_gb = vals.get('OS-FLV-EXT-DATA:ephemeral', 0)
        swap = vals.get('swap', 0)
        rxtx_factor = vals.get('rxtx_factor', 1.0)
        is_public = vals.get('os-flavor-access:is_public', True)

        try:
            flavor = flavors.create(name, memory, vcpus, root_gb,
                                    ephemeral_gb=ephemeral_gb,
                                    flavorid=flavorid, swap=swap,
                                    rxtx_factor=rxtx_factor,
                                    is_public=is_public)
            if not flavor['is_public']:
                flavors.add_flavor_access(flavor['flavorid'],
                                          context.project_id, context)
            req.cache_db_flavor(flavor)
        except (exception.FlavorExists,
                exception.FlavorIdExists) as err:
            raise webob.exc.HTTPConflict(explanation=err.format_message())
        except exception.InvalidInput as exc:
            raise webob.exc.HTTPBadRequest(explanation=exc.format_message())

        return self._view_builder.show(req, flavor)
Beispiel #2
0
    def _create(self, req, body):
        context = req.environ['nova.context']
        authorize(context)

        vals = body['flavor']

        name = vals['name']
        flavorid = vals.get('id')
        memory = vals['ram']
        vcpus = vals['vcpus']
        root_gb = vals['disk']
        ephemeral_gb = vals.get('ephemeral', 0)
        swap = vals.get('swap', 0)
        rxtx_factor = vals.get('os-flavor-rxtx:rxtx_factor', 1.0)
        is_public = vals.get('flavor-access:is_public', True)

        try:
            flavor = flavors.create(name, memory, vcpus, root_gb,
                                    ephemeral_gb=ephemeral_gb,
                                    flavorid=flavorid, swap=swap,
                                    rxtx_factor=rxtx_factor,
                                    is_public=is_public)
            if not flavor['is_public']:
                flavors.add_flavor_access(flavor['flavorid'],
                                          context.project_id, context)
            req.cache_db_flavor(flavor)
        except (exception.FlavorExists,
                exception.FlavorIdExists) as err:
            raise webob.exc.HTTPConflict(explanation=err.format_message())

        return self._view_builder.show(req, flavor)
Beispiel #3
0
 def test_add_flavor_access_already_exists(self):
     user_id = "fake"
     project_id = "fake"
     ctxt = context.RequestContext(user_id, project_id, is_admin=True)
     flavor_id = "flavor1"
     flavors.create("some flavor", 256, 1, 120, 100, flavorid=flavor_id)
     flavors.add_flavor_access(flavor_id, project_id, ctxt=ctxt)
     self.assertRaises(exception.FlavorAccessExists, flavors.add_flavor_access, flavor_id, project_id, ctxt)
Beispiel #4
0
    def test_remove_flavor_access(self):
        user_id = "fake"
        project_id = "fake"
        ctxt = context.RequestContext(user_id, project_id, is_admin=True)
        flavor_id = "flavor1"
        flavors.create("some flavor", 256, 1, 120, 100, flavorid=flavor_id)
        flavors.add_flavor_access(flavor_id, project_id, ctxt)
        flavors.remove_flavor_access(flavor_id, project_id, ctxt)

        projects = flavors.get_flavor_access_by_flavor_id(flavor_id, ctxt)
        self.assertEqual([], projects)
Beispiel #5
0
    def _addTenantAccess(self, req, id, body):
        context = req.environ['nova.context']
        authorize(context)
        self._check_body(body)

        vals = body['addTenantAccess']
        tenant = vals['tenant']

        try:
            flavors.add_flavor_access(id, tenant, context)
        except exception.FlavorAccessExists as err:
            raise webob.exc.HTTPConflict(explanation=err.format_message())

        return _marshall_flavor_access(id)
Beispiel #6
0
 def test_add_instance_type_access(self):
     user_id = "fake"
     project_id = "fake"
     ctxt = context.RequestContext(user_id, project_id, is_admin=True)
     flavor_id = "flavor1"
     type_ref = flavors.create("some flavor", 256, 1, 120, 100, flavorid=flavor_id)
     access_ref = flavors.add_flavor_access(flavor_id, project_id, ctxt=ctxt)
     self.assertEqual(access_ref["project_id"], project_id)
     self.assertEqual(access_ref["instance_type_id"], type_ref["id"])
    def _add_tenant_access(self, req, id, body):
        context = req.environ['nova.context']
        authorize(context)
        if not self.is_valid_body(body, 'add_tenant_access'):
            raise webob.exc.HTTPBadRequest(explanation=_("Invalid request"))

        vals = body['add_tenant_access']
        try:
            tenant = vals['tenant_id']
        except KeyError:
            raise webob.exc.HTTPBadRequest(
                explanation=_("tenant_id is required"))

        try:
            flavors.add_flavor_access(id, tenant, context)
        except exception.FlavorAccessExists as err:
            raise webob.exc.HTTPConflict(explanation=err.format_message())
        except exception.FlavorNotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())

        return _marshall_flavor_access(id)
Beispiel #8
0
    def _create(self, req, body):
        context = req.environ["nova.context"]
        authorize(context)

        if not self.is_valid_body(body, "flavor"):
            raise webob.exc.HTTPBadRequest("Invalid request body ")

        vals = body["flavor"]

        name = vals.get("name")
        flavorid = vals.get("id")
        memory = vals.get("ram")
        vcpus = vals.get("vcpus")
        root_gb = vals.get("disk")
        ephemeral_gb = vals.get("ephemeral", 0)
        swap = vals.get("swap", 0)
        rxtx_factor = vals.get("os-flavor-rxtx:rxtx_factor", 1.0)
        is_public = vals.get("flavor-access:is_public", True)

        try:
            flavor = flavors.create(
                name,
                memory,
                vcpus,
                root_gb,
                ephemeral_gb=ephemeral_gb,
                flavorid=flavorid,
                swap=swap,
                rxtx_factor=rxtx_factor,
                is_public=is_public,
            )
            if not flavor["is_public"]:
                flavors.add_flavor_access(flavor["flavorid"], context.project_id, context)
            req.cache_db_flavor(flavor)
        except (exception.FlavorExists, exception.FlavorIdExists) as err:
            raise webob.exc.HTTPConflict(explanation=err.format_message())
        except exception.InvalidInput as exc:
            raise webob.exc.HTTPBadRequest(explanation=exc.format_message())

        return self._view_builder.show(req, flavor)
Beispiel #9
0
 def test_add_flavor_access_already_exists(self):
     user_id = 'fake'
     project_id = 'fake'
     ctxt = context.RequestContext(user_id, project_id, is_admin=True)
     flavor_id = 'flavor1'
     type_ref = flavors.create('some flavor', 256, 1, 120, 100,
                                       flavorid=flavor_id)
     access_ref = flavors.add_flavor_access(flavor_id,
                                                          project_id,
                                                          ctxt=ctxt)
     self.assertRaises(exception.FlavorAccessExists,
                       flavors.add_flavor_access,
                       flavor_id, project_id, ctxt)
Beispiel #10
0
    def test_remove_flavor_access(self):
        user_id = 'fake'
        project_id = 'fake'
        ctxt = context.RequestContext(user_id, project_id, is_admin=True)
        flavor_id = 'flavor1'
        type_ref = flavors.create('some flavor', 256, 1, 120, 100,
                                          flavorid=flavor_id)
        access_ref = flavors.add_flavor_access(flavor_id, project_id,
                                                      ctxt)
        flavors.remove_flavor_access(flavor_id, project_id, ctxt)

        projects = flavors.get_flavor_access_by_flavor_id(flavor_id,
                                                                 ctxt)
        self.assertEqual([], projects)
Beispiel #11
0
    def create(self, req, body):
        """Extend Server create action with resource allocation.
        """
        if not self.is_valid_body(body, 'server'):
            raise exc.HTTPUnprocessableEntity()
        context = req.environ['nova.context']

        server_dict = body['server']
        flavor_dict = server_dict.get('flavor')

        if flavor_dict:
            # only authorize if they have the flavor_dict otherwise the policy
            # is always applied whether it is present or not
            authorize(context)

            # verify input parameters
            if server_dict.get('flavorRef'):
                msg = _("only one of flavorRef or flavor can be specified.")
                raise exc.HTTPBadRequest(explanation=msg)
            for opt in ['ram', 'vcpus', 'disk']:
                try:
                    val = int(flavor_dict.get(opt))
                    if opt == 'disk':
                        assert val >= 0
                    else:
                        assert val > 0
                except (ValueError, TypeError, AssertionError):
                    if opt == 'disk':
                        msg = _("%s argument must be an integer") % opt
                    else:
                        msg = _("%s argument must be a positive integer") % opt
                    raise exc.HTTPBadRequest(explanation=msg)

            memory_mb = flavor_dict.get('ram')
            vcpus = flavor_dict.get('vcpus')
            root_gb = flavor_dict.get('disk')
            ephemeral_gb = flavor_dict.get('OS-FLV-EXT-DATA:ephemeral')

            # query the flavor
            flavor_name = self._generate_unique_name(flavor_dict)
            inst_type = None
            try:
                inst_type = flavors. \
                    get_flavor_by_name(flavor_name, context)
            except exception.FlavorNotFoundByName:
                LOG.debug("Flavor not found. Creating...")

            # create flavor if no matched flavor
            if not inst_type:
                if ephemeral_gb is None:
                    ephemeral_gb = 0
                try:
                    inst_type = flavors.create(flavor_name,
                                          memory_mb,
                                          vcpus,
                                          root_gb,
                                          ephemeral_gb=ephemeral_gb,
                                          flavorid=None,
                                          swap=0,
                                          rxtx_factor=1.0,
                                          is_public=False)
                    admin_context = (context.is_admin and
                                    context or context.elevated())
                    flavors.add_flavor_access(inst_type['flavorid'],
                                admin_context.project_id, admin_context)
                    self._add_extra_specs(context, flavor_dict, inst_type)
                    req.cache_db_flavor(inst_type)
                except (exception.FlavorExists,
                        exception.FlavorIdExists):
                    try:
                        inst_type = flavors. \
                            get_flavor_by_name(flavor_name, context)
                    except exception.FlavorNotFoundByName:
                        raise exception.FlavorCreateFailed
                except exception.FlavorCreateFailed:
                    msg = _('An unknown db error has occurred. ')
                    raise exc.HTTPInternalServerError(explanation=msg)

            # update flavorRef parameters
            server_dict['flavorRef'] = inst_type['flavorid']
            del server_dict['flavor']
        elif not server_dict.get('flavorRef'):
            msg = _("Missing flavorRef or flavor attribute.")
            raise exc.HTTPBadRequest(explanation=msg)
        yield