-#
-# Functions for interacting with the roles table in the database
-#
-# Mark Huang <mlhuang@cs.princeton.edu>
-# Copyright (C) 2006 The Trustees of Princeton University
-#
-# $Id: Roles.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $
-#
+from types import StringTypes
+from PLC.Faults import *
+from PLC.Parameter import Parameter
+from PLC.NovaTable import NovaObject, NovaTable
-class Roles(dict):
+class Role(NovaObject):
"""
- Representation of the roles table in the database.
+ Representation of a row in the roles table. To use,
+ instantiate with a dict of values.
"""
- # Role IDs equal to or lower than this number are for use by real
- # accounts. Other role IDs are used internally.
- role_max = 500
+ fields = {
+ 'id': Parameter(str, "Role identifier"),
+ 'name': Parameter(str, "Role", max = 100),
+ }
- def __init__(self, api):
- sql = "SELECT * FROM roles" \
- " WHERE role_id <= %d" % self.role_max
+ def sync(self, insert=False, validate=True):
+ NovaObject.sync(self, insert, validate)
+ if insert == True or id not in self:
+ self.object = self.api.client_shell.keystone.roles.create(**self)
+
+
+class Roles(NovaTable):
+ def __init__(self, api, role_filter={}):
+ self.api = api
+ if not role_filter:
+ roles = self.api.client_shell.keystone.roles.findall()
+ elif isinstance(role_filter, StringTypes):
+ roles = [self.api.client_shell.keystone.roles.find(id=role_filter)]
+ elif isinstance(role_filter, (list, tuple, set)):
+ roles = self.api.client_shell.keystone.roles.findall()
+ roles = [role for role in roles if role.id in role_filter]
+ else:
+ raise PLCInvalidArgument, "Wrong role filter %r"%role_filter
+
+ #self.append(NovaObject.object_to_dict(role, Role.fields))
+ for role in roles:
+ role = Role(self.api, object=role)
+ self.append(role)
+
+
- for row in api.db.selectall(sql):
- self[row['role_id']] = row['name']
- self[row['name']] = row['role_id']