[Et-mgmt-commits-list] [SCM] virt-factory branch, master now at c60eb873f5e6e80f270babf573fd4164af486db2

Jeff Ortel jortel at redhat.com
Wed May 30 21:39:40 UTC 2007


Hello,

This is an automated email from the git hooks/update script, it was
generated because a ref change was pushed to the repository.

Updating branch, master,
       via  c60eb873f5e6e80f270babf573fd4164af486db2 (commit)
       via  26f3ca7dcc3bc9284c6b99f1641dab745141d977 (commit)
      from  7ee012548d91756d1e3793cb8c394e55e7110f43 (commit)

- Log -----------------------------------------------------------------
commit c60eb873f5e6e80f270babf573fd4164af486db2
Merge: 26f3ca7... 7ee0125...
Author: Jeff Ortel <jortel at localhost.localdomain>
Date:   Wed May 30 17:39:33 2007 -0400

    Merge branch 'master' of git+ssh://g-jortel@et.redhat.com/git/virt-factory

commit 26f3ca7dcc3bc9284c6b99f1641dab745141d977
Author: Jeff Ortel <jortel at localhost.localdomain>
Date:   Wed May 30 17:35:04 2007 -0400

    Factor out update/get/list/delete methods into the sqlalchemy
    orm base object.
-----------------------------------------------------------------------

Diffstat:
 service/modules/deployment.py          |   50 ++++-------
 service/modules/distribution.py        |   44 +++------
 service/modules/machine.py             |   60 +++++--------
 service/modules/profile.py             |   68 ++++++--------
 service/modules/regtoken.py            |   75 +++++++---------
 service/modules/schema_version.py      |    2 +-
 service/modules/task.py                |   44 ++++------
 service/modules/upgrade_log_message.py |  153 ++++++++++++++++++++++++--------
 service/modules/user.py                |   31 ++-----
 service/modules/web_svc.py             |    4 -
 service/server/db.py                   |   77 ++++++++++++----
 11 files changed, 317 insertions(+), 291 deletions(-)

diff --git a/service/modules/deployment.py b/service/modules/deployment.py
index 20d7e74..e1ae089 100755
--- a/service/modules/deployment.py
+++ b/service/modules/deployment.py
@@ -16,8 +16,8 @@
 
 
 from server.codes import *
+from baseobj import FieldValidator
 
-import baseobj
 import profile
 import machine
 import web_svc
@@ -151,8 +151,7 @@ class Deployment(web_svc.AuthWebSvc):
          session = db.open_session()
          try:
              deployment = db.Deployment()
-             for key in (required+optional):
-                 setattr(deployment, key, args.get(key, None))
+             deployment.update(args)
              session.save(deployment)
              session.flush()
              self.cobbler_sync(deployment.data())
@@ -220,13 +219,8 @@ class Deployment(web_svc.AuthWebSvc):
 
          session = db.open_session()
          try:
-             objectid = args['id']
-             deployment = session.get(db.Deployment, objectid)
-             if deployment is None:
-                 raise NoSuchObjectException(comment=objectid)
-             for key in optional:
-                 current = getattr(deployment, key)
-                 setattr(deployment, key, args.get(key, current))
+             deployment = db.Deployment.get(session, args['id'])
+             deployment.update(args)
              session.save(deployment)
              session.flush()
              self.cobbler_sync(deployment.data())
@@ -259,10 +253,7 @@ class Deployment(web_svc.AuthWebSvc):
     def set_state(self, token, args, status_code):
         session = db.open_session()
         try:
-            objectid = args['id']
-            deployment = session.get(db.Deployment, objectid)
-            if deployment is None:
-                raise NoSuchObjectException(comment=objectid)
+            deployment = db.Deployment.get(session, args['id'])
             deployment.state = status_code
             session.save(deployment)
             session.flush()
@@ -318,12 +309,7 @@ class Deployment(web_svc.AuthWebSvc):
          FieldValidator(args).verify_required(required)
          session = db.open_session()
          try:
-             objectid = args['id']
-             deployment = session.get(db.Deployment, objectid)
-             if deployment is None:
-                 raise NoSuchObjectException(comment=objectid)
-             session.delete(deployment)
-             session.flush()
+             db.Deployment.delete(session, args['id'])
              return success()
          finally:
              session.close()
@@ -352,8 +338,8 @@ class Deployment(web_svc.AuthWebSvc):
         try:
             result = []
             offset, limit = self.offset_and_limit(args)
-            for deployment in session.query(db.Deployment).select(limit=limit, offset=offset):
-                result.append(deployment.data())
+            for deployment in db.Deployment.list(session, offset, limit):
+                result.append(self.expand(deployment))
             return success(result)
         finally:
             session.close()
@@ -394,7 +380,6 @@ class Deployment(web_svc.AuthWebSvc):
             - netboot_enabled (optional)
             - puppet_node_diff (optional)
             - is_locked (optional)
-        # TODO: nested structures.
         """
         required = ('hostname',)
         FieldValidator(args).verify_required(required)
@@ -405,7 +390,7 @@ class Deployment(web_svc.AuthWebSvc):
             offset, limit = self.offset_and_limit(args)
             query = session.query(db.Deployment)
             for deployment in query.select_by(hostname == hostname, offset=offset, limit=limit):
-                result.append(deployment.data())
+                result.append(self.expand(deployment))
             return success(result)
         finally:
             session.close()
@@ -430,7 +415,6 @@ class Deployment(web_svc.AuthWebSvc):
              - netboot_enabled (optional)
              - puppet_node_diff (optional)
              - is_locked (optional)
-        # TODO: nested structures.
         """
         required = ('registration_token',)
         FieldValidator(args).verify_required(required)
@@ -441,7 +425,7 @@ class Deployment(web_svc.AuthWebSvc):
             offset, limit = self.offset_and_limit(args)
             query = session.query(db.Deployment)
             for deployment in query.select_by(registration_token == regtoken, offset=offset, limit=limit):
-                result.append(deployment.data())
+                result.append(self.expand(deployment))
             return success(result)
         finally:
             session.close()
@@ -500,21 +484,23 @@ class Deployment(web_svc.AuthWebSvc):
             - netboot_enabled (optional)
             - puppet_node_diff (optional)
             - is_locked (optional)
-        # TODO: nested structures.
         """
         required = ('id',)
         FieldValidator(args).verify_required(required)
         session = db.open_session()
         try:
-            objectid = args['id']
-            deployment = session.get(db.Deployment, objectid)
-            if deployment is None:
-                raise NoSuchObjectException(comment=objectid)
-            return success(deployment.data())
+            deployment = db.Deployment.get(session, args['id'])
+            return success(self.expand(deployment))
         finally:
             session.close()
 
 
+    def expand(self, deployment):
+        result = deployment.data()
+        result['machine'] = deployment.machine.data()
+        result['profile'] = deployment.profile.data()
+        return result
+
 
 methods = Deployment()
 register_rpc = methods.register_rpc
diff --git a/service/modules/distribution.py b/service/modules/distribution.py
index ed617b8..f791849 100755
--- a/service/modules/distribution.py
+++ b/service/modules/distribution.py
@@ -15,8 +15,8 @@
 ##
 
 from server.codes import *
+from baseobj import FieldValidator
 
-import baseobj
 import provisioning
 import cobbler
 import web_svc
@@ -53,12 +53,11 @@ class Distribution(web_svc.AuthWebSvc):
         """
         required = ('kernel', 'initrd', 'name', 'architecture')
         optional = ('options', 'kickstart', 'kernel_options', 'kickstart_metadata')
-        self.__validate(args, required)
+        self.validatelidate(args, required)
         session = db.open_session()
         try:
             distribution = db.Distribution()
-            for key in (required+optional):
-                setattr(distribution, key, args.get(key, None))
+            distribution.update(args)
             session.save(distribution)
             session.flush()
             self.cobbler_sync(distribution.data())
@@ -87,16 +86,11 @@ class Distribution(web_svc.AuthWebSvc):
         """
         required = ('id',)
         optional = ('kernel', 'initrd', 'name', 'architecture', 'kickstart', 'kernel_options', 'kickstart_metadata')
-        self.__validate(args, required)
+        self.validate(args, required)
         session = db.open_session()
         try:
-            objectid = args['id']
-            distribution = session.get(db.Distribution, objectid)
-            if distribution is None:
-                raise NoSuchObjectException(comment=objectid)
-            for key in optional:
-                current = getattr(distribution, key)
-                setattr(distribution, key, args.get(key, current))
+            distribution = db.Distribution.get(session, args['id'])
+            distribution.update(args)
             session.save(distribution)
             session.flush()
             self.cobbler_sync(distribution.data())
@@ -116,12 +110,7 @@ class Distribution(web_svc.AuthWebSvc):
         FieldValidator(args).verify_required(required)
         session = db.open_session()
         try:
-            objectid = args['id']
-            distribution = session.get(db.Distribution, objectid)
-            if distribution is None:
-                raise NoSuchObjectException(comment=objectid)
-            session.delete(distribution)
-            session.flush()
+            db.Distribution.delete(session, args['id'])
             return success()
         finally:
             session.close()
@@ -148,7 +137,7 @@ class Distribution(web_svc.AuthWebSvc):
          try:
              result = []
              offset, limit = self.offset_and_limit(args)
-             for distribution in query(db.Distribution).select(offset=offset, limit=limit):
+             for distribution in db.Distribution.list(session, offset, limit):
                  result.append(distribution.data())
              return success(result)
          finally:
@@ -177,10 +166,7 @@ class Distribution(web_svc.AuthWebSvc):
         FieldValidator(args).verify_required(required)
         session = db.open_session()
         try:
-            objectid = args['id']
-            distribution = ssn.get(db.Distribution, objectid)
-            if deployment is None:
-                raise NoSuchObjectException(comment=objectid)
+            distribution = db.Distribution.get(session, args['id'])
             return success(distribution.data())
         finally:
             session.close()
@@ -217,12 +203,12 @@ class Distribution(web_svc.AuthWebSvc):
             session.close()
 
 
-    def __validate(self, args, required):
-        validator = FieldValidator(args)
-        validator.verify_required(required)
-        validator.verify_file('kernel', 'initrd')
-        validator.verify_printable('name', 'kernel_options')
-        validator.verify_enum('architecture', VALID_ARCHS)
+    def validate(self, args, required):
+        vdr = FieldValidator(args)
+        vdr.verify_required(required)
+        vdr.verify_file('kernel', 'initrd')
+        vdr.verify_printable('name', 'kernel_options')
+        vdr.verify_enum('architecture', VALID_ARCHS)
 
 
 methods = Distribution()
diff --git a/service/modules/machine.py b/service/modules/machine.py
index 945d0c6..7f59baf 100755
--- a/service/modules/machine.py
+++ b/service/modules/machine.py
@@ -15,8 +15,8 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 
 from server.codes import *
+from baseobj import FieldValidator
 
-import baseobj
 import profile
 import cobbler
 import provisioning
@@ -67,12 +67,11 @@ class Machine(web_svc.AuthWebSvc):
              'processor_count','memory', 'kernel_options', 'kickstart_metadata', 
              'list_group', 'mac_address', 'is_container', 'puppet_node_diff', 'netboot_enabled', 'is_locked')
         required = ('profile_id')
-        self.__validate(args, required)
+        self.validate(args, required)
         session = db.open_session()
         try:
             machine = db.Machine()
-            for key in (required+optional):
-                setattr(machine, key, args.get(key, None))
+            machine.update(args)
             # TODO: generate the registration token here and make it actually random and decent.
             machine.registration_token = regtoken.RegToken().generate(token)
             machine.netboot_enabled = 1 # initially, allow PXE, until it registers
@@ -167,16 +166,11 @@ class Machine(web_svc.AuthWebSvc):
             ('hostname', 'ip_address', 'registration_token', 'architecture', 'processor_speed', 
              'processor_count','memory', 'kernel_options', 'kickstart_metadata', 'profile_id', 
              'list_group', 'mac_address', 'is_container', 'puppet_node_diff', 'netboot_enabled', 'is_locked')
-        self.__validate(args, required)
+        self.validate(args, required)
         session = db.open_session()
         try:
-            objectid = args['id']
-            machine = session.get(db.Machine, objectid)
-            if machine is None:
-                 raise NoSuchObjectException(comment=objectid)
-            for key in optional:
-                current = getattr(user, key)
-                setattr(machine, key, args.get(key, current))
+            machine = db.Machine.get(session, args['id'])
+            machine.update(args)
             session.save(machine)
             session.flush()
         finally:
@@ -199,12 +193,7 @@ class Machine(web_svc.AuthWebSvc):
         FieldValidator(args).verify_required(required)
         session = db.open_session()
         try:
-            objectid = args['id']
-            machine = session.get(db.Machine, objectid)
-            if machine is None:
-                raise NoSuchObjectException(comment=objectid)
-            session.delete(machine)
-            session.flush()
+            db.Machine.delete(session, args['id'])
             return success()
         finally:
             session.close()
@@ -239,8 +228,8 @@ class Machine(web_svc.AuthWebSvc):
         try:
             result = []
             offset, limit = self.offset_and_limit(args)
-            for machine in session.query(db.Machine).select(offset=offset, limit=limit):
-                result.append(machine.data())
+            for machine in db.Machine.list(session, offset, limit):
+                result.append(self.expand(machine))
             return success(result)
         finally:
             session.close()
@@ -271,7 +260,6 @@ class Machine(web_svc.AuthWebSvc):
             - puppet_node_diff (optional)
             - netboot_enabled (optional)
             - is_locked (optional)
-        # TODO: nested structures.
         """
         required = ('hostname',)
         FieldValidator(args).verify_required(required)
@@ -282,7 +270,7 @@ class Machine(web_svc.AuthWebSvc):
             offset, limit = self.offset_and_limit(args)
             query = session.query(db.Machine)
             for machine in query.select_by(hostname == hostname, offset=offset, limit=limit):
-                result.append(machine.data())
+                result.append(self.expand(machine))
             return success(result)
         finally:
             session.close()
@@ -312,8 +300,6 @@ class Machine(web_svc.AuthWebSvc):
             - profile_id
             - puppet_node_diff (optional)
             - netboot_enabled (optional)
-            - is_locked (optional)
-        # TODO: nested structures.
         """
         required = ('registration_token',)
         FieldValidator(args).verify_required(required)
@@ -324,7 +310,7 @@ class Machine(web_svc.AuthWebSvc):
             offset, limit = self.offset_and_limit(args)
             query = session.query(db.Machine)
             for machine in query.select_by(registration_token == regtoken, offset=offset, limit=limit):
-                result.append(machine.data())
+                result.append(self.expand(machine))
             return success(result)
         finally:
             session.close()
@@ -355,31 +341,33 @@ class Machine(web_svc.AuthWebSvc):
             - puppet_node_diff (optional)
             - netboot_enabled (optional)
             - is_locked (optional)
-        # TODO: nested structures.
         """
         required = ('id',)
         FieldValidator(args).verify_required(required)
         session = db.open_session()
         try:
-            objectid = args['id']
-            machine = session.get(db.Machine, objectid)
-            if machine is None:
-                raise NoSuchObjectException(comment=machineid)
+            machine = db.Machine.get(session, args['id'])
             return success(machine.data())
         finally:
             session.close()
 
             
-    def __validate(self, args, required):
-        validator = FieldValidator(args)
-        validator.verify_required(required)
-        validator.verify_enum('architecture', VALID_ARCHS)
-        validator.verify_int('processor_speed', 'processor_count', 'memory')
-        validator.verify_printable(
+    def validate(self, args, required):
+        vdr = FieldValidator(args)
+        vdr.verify_required(required)
+        vdr.verify_enum('architecture', VALID_ARCHS)
+        vdr.verify_int('processor_speed', 'processor_count', 'memory')
+        vdr.verify_printable(
                'kernel_options', 'kickstart_metadata', 'list_group', 
                'list_group', 'puppet_node_diff')
 
 
+    def expand(self, machine):
+        result = machine.data()
+        result['profile'] = machine.profile.data()
+        return result
+
+
 methods = Machine()
 register_rpc = methods.register_rpc
 
diff --git a/service/modules/profile.py b/service/modules/profile.py
index df27115..61fef8c 100755
--- a/service/modules/profile.py
+++ b/service/modules/profile.py
@@ -15,8 +15,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 from server.codes import *
 from server import config_data
-
-import baseobj
+from baseobj import FieldValidator
 import distribution
 import cobbler
 import provisioning
@@ -58,12 +57,11 @@ class Profile(web_svc.AuthWebSvc):
             ('distribution_id', 'virt_storage_size', 'virt_ram', 'kickstart_metadata',
              'kernel_options', 'puppet_classes')
         required = ('name', 'version', 'valid_targets', 'is_container')
-        self.__validate(args, required)
+        self.validate(args, required)
         session = db.open_session()
         try:
             profile = db.Proflie()
-            for key in (required+optional):
-                setattr(profile, key, args.get(key, None))
+            profile.update(args)
             session.save(profile)
             session.flush()
             self.cobbler_sync(profile.data())
@@ -98,15 +96,11 @@ class Profile(web_svc.AuthWebSvc):
         optional =\
             ('name', 'version', 'valid_targets', 'is_container', 'distribution_id', 'virt_storage_size', 
              'virt_ram', 'kickstart_metadata', 'kernel_options', 'puppet_classes')
-        self.__validate(args, required)
+        self.validate(args, required)
         session = db.open_session()
         try:
-            profileid = args['id']
-            profile = session.get(db.Profile, profileid)
-            if profile is None:
-                 raise NoSuchObjectException(comment=profileid)
-            for key in optional:
-                setattr(profile, key, args.get(key, None))
+            profile = db.Profile.get(session, args['id'])
+            profile.update(args)
             session.save(profile)
             session.flush()
             return success()
@@ -125,12 +119,7 @@ class Profile(web_svc.AuthWebSvc):
         FieldValidator(args).verify_required(required)
         session = db.open_session()
         try:
-            profileid = args['id']
-            profile = session.get(db.Profile, profileid)
-            if profile is None:
-                 raise NoSuchObjectException(comment=profileid)
-            session.delete(profile)
-            session.flush()
+            db.Profile.delete(session, args['id'])
             return success()
         finally:
             session.close()
@@ -154,16 +143,15 @@ class Profile(web_svc.AuthWebSvc):
             - valid_targets (optional)
             - is_container (optional)
             - puppet_classes (optional)
-        # TODO: paging.
-        # TODO: nested structures.  
         """
         required = ('id',)
         FieldValidator(args).verify_required(required)
         session = db.open_session()
         try:
             result = []
-            for profile in session.query(db.Profile).select():
-                result.append(profile.data())
+            offset, limit = self.offset_and_limit(args)
+            for profile in db.Profile.list(session, offset, limit):
+                result.append(self.expand(profile))
             return success(result)
         finally:
             session.close()
@@ -187,18 +175,14 @@ class Profile(web_svc.AuthWebSvc):
             - kernel_options (optional)
             - valid_targets (optional)
             - is_container (optional)
-            - puppet_classes (optional)
-        # TODO: nested structures.            
+            - puppet_classes (optional)          
         """
         required = ('id',)
         FieldValidator(args).verify_required(required)
         session = db.open_session()
         try:
-            profileid = args['id']
-            profile = session.get(db.Profile, profileid)
-            if profile is None:
-                 raise NoSuchObjectException(comment=profileid)
-            return success(profile.data())
+            profile = db.Profile.get(session, args['id'])
+            return success(self.expand(profile))
         finally:
             session.close()
 
@@ -222,7 +206,6 @@ class Profile(web_svc.AuthWebSvc):
             - valid_targets (optional)
             - is_container (optional)
             - puppet_classes (optional)
-        # TODO: nested structures. 
         """
         required = ('name',)
         FieldValidator(args).verify_required(required)
@@ -232,17 +215,24 @@ class Profile(web_svc.AuthWebSvc):
             profile = session.query(db.Profile).selectfirst_by(name == name)
             if profile is None:
                  raise NoSuchObjectException(comment=name)
-            return success(profile.data())
+            return success(self.expand(profile))
         finally:
             session.close()
-            
-    def __validate(self, args, required):
-        validator = FieldValidator(args)
-        validator.verify_required(required)
-        validator.verify_printable('name', 'version')
-        validator.verify_int('virt_storage_size', 'virt_ram', 'kernel_options', 'puppet_classes')
-        validator.verify_enum('valid_targets', VALID_TARGETS)
-        validator.verify_enum('is_container', VALID_CONTAINERS)
+
+
+    def validate(self, args, required):
+        vdr = FieldValidator(args)
+        vdr.verify_required(required)
+        vdr.verify_printable('name', 'version')
+        vdr.verify_int('virt_storage_size', 'virt_ram', 'kernel_options', 'puppet_classes')
+        vdr.verify_enum('valid_targets', VALID_TARGETS)
+        vdr.verify_enum('is_container', VALID_CONTAINERS)
+
+
+    def expand(self, profile):
+        result = profile.data()
+        result['distribution'] = profile.distribution.data()
+        return result
 
 
 methods = Profile()
diff --git a/service/modules/regtoken.py b/service/modules/regtoken.py
index 47f7088..2614787 100755
--- a/service/modules/regtoken.py
+++ b/service/modules/regtoken.py
@@ -14,8 +14,8 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 """
 
 from server.codes import *
+from baseobj import FieldValidator
 
-import baseobj
 import profile
 import machine
 import deployment
@@ -38,6 +38,7 @@ class RegToken(web_svc.AuthWebSvc):
         }
         web_svc.AuthWebSvc.__init__(self)
 
+
     def generate(self, token):
          """
          token is base64 encoded short string from /dev/urandom
@@ -66,8 +67,7 @@ class RegToken(web_svc.AuthWebSvc):
          session = db.open_session()
          try:
              regtoken = db.RegToken()
-             for key in (required+optional):
-                 setattr(regtoken, key, args.get(key, None))
+             regtoken.update(args)
              session.save(regtoken)
              session.flush()
              return success(regtoken.id)
@@ -86,12 +86,7 @@ class RegToken(web_svc.AuthWebSvc):
          FieldValidator(args).verify_required(required)
          session = db.open_session()
          try:
-             objectid = args['id']
-             rt = session.get(db.RegToken, objectid)
-             if rt is None:
-                 raise NoSuchObjectException(comment=objectid)
-             session.delete(rt)
-             session.flush()
+             db.RegToken.delete(session, args['id'])
              return success()
          finally:
              session.close()
@@ -109,7 +104,6 @@ class RegToken(web_svc.AuthWebSvc):
              - token
              - profile_id (optional)
              - uses_remaining (optional)
-         # TODO: nested structures.
          """
          required = ('token',)
          FieldValidator(args).verify_required(required)
@@ -119,7 +113,7 @@ class RegToken(web_svc.AuthWebSvc):
              offset, limit = self.offset_and_limit(args)
              query = session.query(db.RegToken)
              for rt in query.select_by(token=args['token'], offset=offset, limit=limit):
-                 result.append(rt.data())
+                 result.append(self.expand(rt))
              return success(result)
          finally:
              session.close()
@@ -136,38 +130,31 @@ class RegToken(web_svc.AuthWebSvc):
              - token
              - profile_id (optional)
              - uses_remaining (optional)
-         # TODO: nested structures.
          """
          session = db.open_session()
          try:
              result = []
              offset, limit = self.offset_and_limit(args)
-             for rt in  session.query(db.RegToken).select(offset=offset, limit=limit):
-                 result.append(rt.data())
+             for rt in  db.RegToken.list(session, offset, limit):
+                 result.append(self.expand(rt))
              return success(result)
          finally:
              session.close()
 
 
-    def check(self, regtoken):
+    def check(self, tokenstr):
         """
         Validates a regtoken as being valid. If it fails, raise an
         approriate exception.
         """
-
-        st = """
-        SELECT
-              id, uses_remaining
-        FROM
-              regtokens
-        WHERE
-              token=:regtoken
-        """
-
-        # FIXME: this really should use the db_util stuff to have one less place
-        # to make schema changes.
-        self.db.cursor.execute(st, {'regtoken': regtoken})
-        x = self.db.cursor.fetchone()
+        token = None
+        session = db.open_session()
+        try:
+            token = session.query(db.RegToken).selectfirst_by(token=tokenstr)
+            if token is None:
+                return false
+        finally:
+             session.close()
 
         is_specific_token = False
 
@@ -175,11 +162,11 @@ class RegToken(web_svc.AuthWebSvc):
             
             # no generic regtoken was used, but a new machine or deployment might
             # be using a regtoken by way of kickstart, so those tables must also be checked
-            machine_obj = machine.Machine()
-            machines = machine_obj.get_by_regtoken(regtoken, { "registration_token" : regtoken})
+            machine = machine.Machine()
+            machines = machine.get_by_regtoken(tokenstr, { "registration_token" : tokenstr})
             if len(machines.data) < 0:
-                deployment_obj = deployment.Deployment()
-                deployments = deployment_obj.get_by_regtoken(regtoken, { "registration_token" : regtoken })
+                deployment = deployment.Deployment()
+                deployments = deployment.get_by_regtoken(tokenstr, { "registration_token" : tokenstr })
                 if len(deployments.data) < 0:
                     raise RegTokenInvalidException(comment="regtoken not found in regtoken.check")
                 else:
@@ -188,12 +175,11 @@ class RegToken(web_svc.AuthWebSvc):
                 is_specific_token = True
 
         if not is_specific_token:
-            (id, uses) = x
-            if uses == 0:
+            if token.uses_remaining == 0:
                 raise RegTokenExhaustedException(comment="regtoken max uses reached")
 
-            if uses is not None:
-                self.__decrement_uses_remaining(id, uses)
+            if token.uses_remaining is not None:
+                self.__decrement_uses_remaining(token.id, token.uses_remaining)
 
         # we don't really need to check this, since failure will raise exceptions
         return True
@@ -212,22 +198,23 @@ class RegToken(web_svc.AuthWebSvc):
              - token
              - profile_id (optional)
              - uses_remaining (optional)
-         # TODO: nested structures.
          """
          required = ('id',)
          FieldValidator(args).verify_required(required)
          session = db.open_session()
          try:
-             result = []
-             objectid = args['id']
-             rt = session.get(db.RegToken, objectid)
-             if rt is None:
-                 raise NoSuchObjectException(comment=objectid)
-             return success(rt.data())
+             rt = db.RegToken.get(session, args['id'])
+             return success(self.expand(rt))
          finally:
              session.close()
 
 
+    def expand(self, regtoken):
+        result = regtoken.data()
+        if regtoken.profile:
+            result['profile'] = regtoken.profile.data()
+        return result
+
 
 methods = RegToken()
 register_rpc = methods.register_rpc
diff --git a/service/modules/schema_version.py b/service/modules/schema_version.py
index 8dc74b8..9bf4d29 100644
--- a/service/modules/schema_version.py
+++ b/service/modules/schema_version.py
@@ -118,7 +118,7 @@ class SchemaVersion(web_svc.AuthWebSvc):
         web_svc.AuthWebSvc.__init__(self)
 
         # FIXME: could go in the baseclass...
-        self.db.db_schema = self.DB_SCHEMA
+        #self.db.db_schema = self.DB_SCHEMA
 
     def add(self, token, args):
 
diff --git a/service/modules/task.py b/service/modules/task.py
index 0b0f4fd..618472f 100755
--- a/service/modules/task.py
+++ b/service/modules/task.py
@@ -15,8 +15,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 from server.codes import *
 from server import db
-
-import baseobj
+from baseobj import FieldValidator
 import web_svc
 
 class Task(web_svc.AuthWebSvc):
@@ -55,8 +54,7 @@ class Task(web_svc.AuthWebSvc):
          session = db.open_session()
          try:
              task = db.Task()
-             for key in (required+optional):
-                 setattr(task, key, args.get(key, None))
+             task.update(args)
              session.save(task)
              session.flush()
              return success(task.id)
@@ -82,13 +80,8 @@ class Task(web_svc.AuthWebSvc):
          validator.verity_enum('action_type', VALID_TASK_STATES)
          session = db.open_session()
          try:
-             objectid = args['id']
-             task = session.get(db.Task, objectid)
-             if task is None:
-                 raise NoSuchObjectException(comment=objectid)
-             for key in optional:
-                 current = getattr(task, key)
-                 setattr(task, key, args.get(key, current))
+             task = db.Task.get(session, args['id'])
+             task.update(args)
              session.save(task)
              session.flush()
              return success()
@@ -107,12 +100,7 @@ class Task(web_svc.AuthWebSvc):
          FieldValidator(args).verify_required(required)
          session = db.open_session()
          try:
-             objectid = args['id']
-             task = session.get(db.Task, objectid)
-             if task is None:
-                 raise NoSuchObjectException(comment=objectid)
-             session.delete(task)
-             session.flush()
+             db.Task.delete(session, args['id'])
              return success()
          finally:
              session.close()
@@ -132,14 +120,13 @@ class Task(web_svc.AuthWebSvc):
              - deployment_id
              - state
              - time
-         # TODO: nested structures.
          """
          session = db.open_session()
          try:
              result = []
              offset, limit = self.offset_and_limit(args)
-             for task in session.query(db.Task).select(offset=offset, limit=limit):
-                 result.append(task.data())
+             for task in db.Task.list(session, offset, limit):
+                 result.append(self.expand(task))
              return success(result)
          finally:
              session.close()
@@ -151,19 +138,24 @@ class Task(web_svc.AuthWebSvc):
          @param args: A dictionary of task attributes.
              - id
          @type args: dict
-         # TODO: nested structures.
          """
          required = ('id',)
          FieldValidator(args).verify_required(required)
          session = db.open_session()
          try:
-             objectid = args['id']
-             task = session.get(db.Task, objectid)
-             if task is None:
-                 raise NoSuchObjectException(comment=objectid)
-             return success(task.data())
+             task = db.Task.get(session, args['id'])
+             return success(self.expand(task))
          finally:
              session.close()
+
+
+    def expand(self, task):
+        result = task.data()
+        result['user'] = task.user.data()
+        result['machine'] = task.machine.data()
+        result['deployment'] = task.deployment.data()
+        return result
+        
  
  
 methods = Task()
diff --git a/service/modules/upgrade_log_message.py b/service/modules/upgrade_log_message.py
index 26fd1a8..3a19cf1 100644
--- a/service/modules/upgrade_log_message.py
+++ b/service/modules/upgrade_log_message.py
@@ -98,15 +98,6 @@ class UpgradeLogMessageData(baseobj.BaseObject):
 
 
 class UpgradeLogMessage(web_svc.AuthWebSvc):
-
-    DB_SCHEMA = {
-        "table" : "upgrade_log_messages",
-        "fields" : UpgradeLogMessageData.FIELDS,
-        "add"    : [ "action", "message_type", "message_timestamp", "message" ],
-        "edit"   : [ ]
-        }
-
-
     def __init__(self):
         self.methods = {"upgrade_log_message_add"    : self.add,
                         "upgrade_log_message_delete" : self.delete,
@@ -115,45 +106,133 @@ class UpgradeLogMessage(web_svc.AuthWebSvc):
                         "upgrade_log_message_get"    : self.get}
         web_svc.AuthWebSvc.__init__(self)
 
-        # FIXME: could go in the baseclass...
-        self.db.db_schema = self.DB_SCHEMA
 
     def add(self, token, args):
+        """
+        Create a message.
+        @param args: A dictionary of message attributes.
+            - action (optional)
+            - message_type
+            - message (optional)
+        @type args: dict
+        """
+        optional = ('action', 'message')
+        required = ('message_type')
+        validator = FieldValidator(args)
+        validator.verify_required(required)
+        session = db.open_session()
+        try:
+            msg = db.UpgradeLogMessage()
+            for key in (required+optional):
+                setattr(msg, key, args.get(key, None))
+            session.save(msg)
+            session.flush()
+            return success(msg.id)
+        finally:
+            session.close()
+
+    def edit(self, token, args):
+        """
+        Edit a message.
+        @param args: A dictionary of message attributes.
+        @type args: dict
+            - id
+            - action (optional)
+            - message_type (optional)
+            - message (optional)
+        """
+        required = ('id',)
+        validator.verify_required(required)
+        session = db.open_session()
+        try:
+            objectid = args['id']
+            msg = session.get(db.UpgradeLogMessage, objectid)
+            if msg is None:
+                raise NoSuchObjectException(comment=objectid)
+            for key in optional:
+                current = getattr(msg, key)
+                setattr(msg, key, args.get(key, current))
+            session.save(msg)
+            session.flush()
+            return success()
+        finally:
+            session.close()
 
-        u = UpgradeLogMessageData.produce(args,OP_ADD)
-        result = self.db.simple_add(u.to_datastruct())
-        return result
-
-    def edit(self, token, args): 
-
-        u = UpgradeLogMessageData.produce(args,OP_EDIT)
-        # TODO: make this work w/ u.to_datastruct() 
-        result = self.db.simple_edit(args)
-        return result
 
     def delete(self, token, args):
-
-        u = UpgradeLogMessageData.produce(args,OP_DELETE) # force validation
-        return self.db.simple_delete(u.to_datastruct())
-
-
-    def list(self, token, args):
         """
-        Return a list of upgrade log messages.  The dist_args list is currently *NOT*
-        used.  Ideally we need to include LIMIT information here for
-        GUI pagination when we start worrying about hundreds of systems.
+        Delete a message.
+        @param args: A dictionary of message attributes.
+        @type args: dict
+            - id
+            - action (optional)
+            - message_type (optional)
+            - message (optional)
         """
+        required = ('id',)
+        FieldValidator(args).verify_required(required)
+        session = db.open_session()
+        try:
+            objectid = args['id']
+            msg = session.get(db.UpgradeLogMessage, objectid)
+            if msg is None:
+                raise NoSuchObjectException(comment=objectid)
+            session.delete(msg)
+            session.flush()
+            return success()
+        finally:
+            session.close()
 
-        return self.db.simple_list(args)
+
+    def list(self, token, args):
+         """
+         Get all messages.
+         @param args: A dictionary of message attributes.
+         @type args: dict
+         @return: A list of messages.
+         @rtype: [dict,]  
+            - id
+            - action (optional)
+            - message_type (optional)
+            - message_timestamp
+            - message (optional)
+         """
+         session = db.open_session()
+         try:
+             result = []
+             offset, limit = self.offset_and_limit(args)
+             for msg in session.query(db.UpgradeLogMessage).select(offset=offset, limit=limit):
+                 result.append(msg.date())
+             return success(result)
+         finally:
+             session.close()
 
 
     def get(self, token, args):
-        """
-        Return a specific upgrade log message record.  Only the "id" is required in dist_args.
-        """
-        
-        u = UpgradeLogMessageData.produce(args,OP_GET) # force validation
-        return self.db.simple_get(u.to_datastruct())
+         """
+         Get a message by id.
+         @param args: A dictionary of message attributes.
+             - id
+         @type args: dict
+         @return: A messages.
+         @rtype: dict
+            - id
+            - action (optional)
+            - message_type (optional)
+            - message_timestamp
+            - message (optional)
+         """
+         required = ('id',)
+         FieldValidator(args).verify_required(required)
+         session = db.open_session()
+         try:
+             objectid = args['id']
+             msg = session.get(db.UpgradeLogMessage, objectid)
+             if msg is None:
+                 raise NoSuchObjectException(comment=objectid)
+             return success(msg.data())
+         finally:
+             session.close()
 
 
 methods = UpgradeLogMessage()
diff --git a/service/modules/user.py b/service/modules/user.py
index 3862888..ae8435b 100755
--- a/service/modules/user.py
+++ b/service/modules/user.py
@@ -58,8 +58,7 @@ class User(web_svc.AuthWebSvc):
          self.__lock.acquire()
          try:
              user = db.User()
-             for key in (required+optional):
-                 setattr(user, key, args.get(key, None))
+             user.update(args)
              session.save(user)
              session.flush()
              return success(user.id)
@@ -89,21 +88,15 @@ class User(web_svc.AuthWebSvc):
          session = db.open_session()
          self.__lock.acquire()
          try:
-             objectid = args['id']
-             user = session.get(db.User, objectid)
-             if user is None:
-                 raise NoSuchObjectException(comment=objectid)
-             for key in optional:
-                 current = getattr(user, key)
-                 setattr(user, key, args.get(key, current))
+             user = db.User.get(session, args['id'])
+             user.update(args)
              session.save(user)
              session.flush()
-             return success(args)
+             return success()
          finally:
              self.__lock.release()
              session.close()
 
-
     def delete(self, token, args):
          """
          Deletes a user.
@@ -116,14 +109,7 @@ class User(web_svc.AuthWebSvc):
          session = db.open_session()
          self.__lock.acquire()
          try:
-             objectid = args['id']
-             user = session.get(db.User, objectid)
-             if user is None:
-                 raise NoSuchObjectException(comment=objectid)
-             if user.username == 'admin':
-                 return success()
-             session.delete(user)
-             session.flush()
+             db.User.delete(session, args['id'])
              return success()
          finally:
              self.__lock.release()
@@ -149,7 +135,7 @@ class User(web_svc.AuthWebSvc):
          try:
              result = []
              offset, limit = self.offset_and_limit(args)
-             for user in session.query(db.User).select(offset=offset, limit=limit):
+             for user in db.User.list(session, offset, limit):
                  result.append(user.data())
              return success(result)
          finally:
@@ -166,10 +152,7 @@ class User(web_svc.AuthWebSvc):
          FieldValidator(args).verify_required(required)
          session = db.open_session()
          try:
-             objectid = args['id']
-             user = session.get(db.User, objectid)
-             if user is None:
-                 raise NoSuchObjectException(comment=objectid)
+             user = db.User.get(session, args['id'])
              return success(user.data())
          finally:
              session.close()
diff --git a/service/modules/web_svc.py b/service/modules/web_svc.py
index 830d4bb..09ced2f 100755
--- a/service/modules/web_svc.py
+++ b/service/modules/web_svc.py
@@ -35,10 +35,6 @@ class WebSvc(object):
         config_result = config_obj.get()
         self.config = config_result
         self.__init_log()
-        self.__init_db()
-
-    def __init_db(self):
-        self.db = db_util.DbUtil()
         
     def __init_log(self):
         # lets see what happens when we c&p the stuff from server.py 
diff --git a/service/server/db.py b/service/server/db.py
index 7f17623..3ce6c5b 100644
--- a/service/server/db.py
+++ b/service/server/db.py
@@ -32,7 +32,7 @@ import traceback
 import threading
 from sqlalchemy import *
 from datetime import datetime
-from codes import SQLException
+from codes import SQLException, NoSuchObjectException
 
 tables =\
 (
@@ -173,7 +173,7 @@ tables =\
     Table('upgrade_log_messages',
         Column('id', Integer, Sequence('uglogmsgid'), primary_key=True),
         Column('action', String(50)),
-        Column('message_type', String(50)),
+        Column('message_type', String(50), nullable=False),
         Column('message_timestamp',
                DateTime,
                default=datetime.utcnow()),
@@ -195,6 +195,7 @@ indexes =\
 class Base(object):
     def fields(self):
         return ormbindings.get(self.__class__, ())
+    
     def data(self, filter=[]):
         result = {}
         for key in self.fields():
@@ -204,6 +205,36 @@ class Base(object):
             if value is not None:
                 result[key] = value
         return result
+    
+    def update(self, args, filter=('id',)):
+        for key in self.fields():
+            if key in filter: continue
+            if key in args:
+                setattr(self, key, args[key])
+    
+    def __delete(self, session, id):
+            session.delete(self.get(session, id))
+            session.flush()
+                        
+    def __get(self, session, id):
+        result = session.get(self, id)
+        if result is None:
+            comment = '%s(id=%s) not-found' % (self, id)
+            raise NoSuchObjectException(comment=comment)
+        return result
+    
+    def __list(self, session, offset=0, limit=0):
+        result = []        
+        query = session.query(self)
+        if limit > 0:
+            result = query.select(offset=offset, limit=limit)
+        else:
+            result = query.select()
+        return result
+    
+    get = classmethod(__get)
+    delete = classmethod(__delete)
+    list = classmethod(__list)
 
 
 class User(Base):
@@ -315,7 +346,6 @@ class Database:
         Create all tables, indexes and constraints that have not
         yet been created.
         """
-        # TODO: create the database here?
         for t in tables:
             t.create(checkfirst=True)
     
@@ -381,32 +411,41 @@ def open_session():
 
 if __name__ == '__main__':
     database = Database('postgres://jortel:jortel@localhost/virtfactory')
-    #database.drop()
-    #database.create()
+    database.drop()
+    database.create()
         
-    ssn = open_session()
     try:
+        args =\
+            {'username':'jortel', 'password':'mypassword', 
+             'first':'Elvis', 'last':'Prestley', 'description':'a desc', 'email':'king at hell.com'}
+            
+        ssn = open_session()
+            
         user = User()
-        user.username = 'jortel'
-        user.password = 'mypassword'
-        user.first = 'Elvis'
-        user.last = 'Prestley'
-        user.description = 'The King.'
-        user.email = 'elvis at redhat.com'
+        user.update(args)
         ssn.save(user)
+        ssn.flush()
         
+
+        user = User.get(ssn, 1)
         session = Session()
         session.session_token = 'my token'
         user.sessions.append(session)
-
         ssn.save(session)
         ssn.flush()
-        
-        for user in ssn.query(User).select(limit=10, offset=0):
-            print user.last
 
-        ssn.delete(user)
-        ssn.flush()
+        try:
+            fetched = User.get(ssn, 1)
+            print '______fetched= %s %s %s ________' % (fetched.last, fetched.middle, fetched.first)
+            fetched.update({'middle':'Dog'})
+            ssn.save(fetched)
+            ssn.flush()
+            for fetched in User.list(ssn):
+                print '______(list) fetched= %s %s %s ________' % (fetched.last, fetched.middle, fetched.first)
+        except Exception, e:
+            print '___failed___%s'  % e.comment
+
+        User.delete(ssn, 1)
     finally:
-        ssn.close()
+        pass
         

hooks/update
---
Git Source Code Management System
hooks/update refs/heads/master \
  7ee012548d91756d1e3793cb8c394e55e7110f43 \
  c60eb873f5e6e80f270babf573fd4164af486db2




More information about the Et-mgmt-commits-list mailing list