def test_get_all_no_custom(self):
     """Test that if we haven't yet added any custom resource classes, that
     we only get a list of ResourceClass objects representing the standard
     classes.
     """
     rcs = rc_obj.get_all(self.ctx)
     self.assertEqual(len(orc.STANDARDS), len(rcs))
    def test_destroy_fail_with_inventory(self):
        """Test that we raise an exception when attempting to delete a resource
        class that is referenced in an inventory record.
        """
        rc = rc_obj.ResourceClass(
            self.ctx,
            name='CUSTOM_IRON_NFV',
        )
        rc.create()
        rp = rp_obj.ResourceProvider(
            self.ctx,
            name='my rp',
            uuid=uuidsentinel.rp,
        )
        rp.create()
        inv = inv_obj.Inventory(
            resource_provider=rp,
            resource_class='CUSTOM_IRON_NFV',
            total=1,
        )
        rp.set_inventory([inv])

        self.assertRaises(exception.ResourceClassInUse, rc.destroy)

        rp.set_inventory([])
        rc.destroy()
        rc_list = rc_obj.get_all(self.ctx)
        rc_ids = (r.id for r in rc_list)
        self.assertNotIn(rc.id, rc_ids)
    def test_destroy(self):
        rc = rc_obj.ResourceClass(
            self.ctx,
            name='CUSTOM_IRON_NFV',
        )
        rc.create()
        rc_list = rc_obj.get_all(self.ctx)
        rc_ids = (r.id for r in rc_list)
        self.assertIn(rc.id, rc_ids)

        rc = rc_obj.ResourceClass.get_by_name(
            self.ctx,
            'CUSTOM_IRON_NFV',
        )

        rc.destroy()
        rc_list = rc_obj.get_all(self.ctx)
        rc_ids = (r.id for r in rc_list)
        self.assertNotIn(rc.id, rc_ids)

        # Verify rc cache was purged of the old entry
        self.assertRaises(exception.ResourceClassNotFound,
                          rc_obj.ResourceClass.get_by_name, self.ctx,
                          'CUSTOM_IRON_NFV')
    def test_get_all_with_custom(self):
        """Test that if we add some custom resource classes, that we get a list
        of ResourceClass objects representing the standard classes as well as
        the custom classes.
        """
        customs = [
            ('CUSTOM_IRON_NFV', 10001),
            ('CUSTOM_IRON_ENTERPRISE', 10002),
        ]
        with self.placement_db.get_engine().connect() as conn:
            for custom in customs:
                c_name, c_id = custom
                ins = rc_obj._RC_TBL.insert().values(id=c_id, name=c_name)
                conn.execute(ins)

        rcs = rc_obj.get_all(self.ctx)
        expected_count = (len(orc.STANDARDS) + len(customs))
        self.assertEqual(expected_count, len(rcs))
Exemple #5
0
def list_resource_classes(req):
    """GET a list of resource classes.

    On success return a 200 and an application/json body representing
    a collection of resource classes.
    """
    context = req.environ['placement.context']
    context.can(policies.LIST)
    want_version = req.environ[microversion.MICROVERSION_ENVIRON]
    rcs = rc_obj.get_all(context)

    response = req.response
    output, last_modified = _serialize_resource_classes(
        req.environ, rcs, want_version)
    response.body = encodeutils.to_utf8(jsonutils.dumps(output))
    response.content_type = 'application/json'
    if want_version.matches((1, 15)):
        response.last_modified = last_modified
        response.cache_control = 'no-cache'
    return response