fix parameter types. added validate_last_updated(). added add_person(), delete_person
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Fri, 5 Oct 2012 04:01:24 +0000 (00:01 -0400)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Fri, 5 Oct 2012 04:01:24 +0000 (00:01 -0400)
PLC/Sites.py

index 777fe6a..6147cd0 100644 (file)
@@ -1,12 +1,13 @@
 from types import StringTypes
+from datetime import datetime
 import string
 
 from PLC.Faults import *
 from PLC.Logger import logger
 from PLC.Parameter import Parameter, Mixed
-from PLC.NovaTable import NovaObject, NovaTable
-from PLC.Storage.AlchemyObj import AlchemyObj
+from PLC.Storage.AlchemyObject import AlchemyObj
 from PLC.Slices import Slice, Slices
+from PLC.Persons import Person, Persons
 from PLC.SitePersons import SitePerson, SitePersons
 from PLC.Addresses import Address, Addresses
 from PLC.PCUs import PCU, PCUs
@@ -34,10 +35,10 @@ class Site(AlchemyObj):
         'latitude': Parameter(float, "Decimal latitude of the site", min = -90.0, max = 90.0, nullok = True),
         'longitude': Parameter(float, "Decimal longitude of the site", min = -180.0, max = 180.0, nullok = True),
         'url': Parameter(str, "URL of a page that describes the site", max = 254, nullok = True),
-        'date_created': Parameter(int, "Date and time when site entry was created, in seconds since UNIX epoch", ro = True),
-        'last_updated': Parameter(int, "Date and time when site entry was last updated, in seconds since UNIX epoch", ro = True),
-        'max_slices': Parameter(int, "Maximum number of slices that the site is able to create"),
-        'max_slivers': Parameter(int, "Maximum number of slivers that the site is able to create"),
+        'date_created': Parameter(datetime, "Date and time when site entry was created, in seconds since UNIX epoch", ro = True, default=datetime.now()), 
+        'last_updated': Parameter(datetime, "Date and time when site entry was last updated, in seconds since UNIX epoch", ro = True, nullok=True), 
+        'max_slices': Parameter(int, "Maximum number of slices that the site is able to create", default=10),
+        'max_slivers': Parameter(int, "Maximum number of slivers that the site is able to create", default=1000),
         'person_ids': Parameter([int], "List of account identifiers", joined=True),
         'slice_ids': Parameter([int], "List of slice identifiers", joined=True),
         'address_ids': Parameter([int], "List of address identifiers", joined=True),
@@ -49,17 +50,59 @@ class Site(AlchemyObj):
         'ext_consortium_id': Parameter(int, "external consortium id", nullok = True) 
         }
 
+    def validate_last_updated(self, last_updated):
+        # always return current timestamp 
+        last_updated = datetime.now()
+        return last_updated
+
+    def add_person(self, person_filter, role=None):
+        assert 'site_id' in self
+        assert 'tenant_id' in self
+        if not role:
+            role = 'user'
+        tenant = self.api.client_shell.keystone.tenants.find(id=self['tenant_id']) 
+        persons = Persons(self.api, person_filter)
+        for person in persons:
+            keystone_user = self.api.client_shell.keystone.users.find(id=person['keystone_id'])
+            tenant.add_user(keystone_user, role)  
+            site_person = SitePerson(self.api, {'site_id': self['id'], 
+                                                'person_id': person['person_id']}) 
+            site_person.sync()
+
+
+    def delete_person(self, person_filter, role=None):
+        assert 'site_id' in self
+        assert 'tenant_id' in self
+        if not role:
+            role = 'user'
+        tenant = self.api.client_shell.keystone.tenants.find(id=self['tenant_id'])
+        persons = Persons(self.api, person_filter)
+        for person in persons:
+            keystone_user = self.api.client_shell.keystone.users.find(id=person['keystone_id'])
+            tenant.remove_user(keystone_user, role)
+            site_persons = SitePersons(self.api, {'site_id': self['id'],
+                                                'person_id': person['person_id']})
+            for site_person in site_persons:
+                site_person.delete()    
+          
+
     def sync(self, commit=True, validate=True):
         """
         Add or update the site.
         """
+        assert 'login_base' in self
         # sync the nova record and the plc record
         AlchemyObj.sync(self, commit=commit, validate=validate)     
-        nova_fields = ['enabled', 'name', 'description']
+        nova_fields = ['enabled', 'description']
         nova_can_update = lambda (field, value): field in nova_fields
         nova_site = dict(filter(nova_can_update, self.items()))
+        nova_site['tenant_name'] = self['login_base']
         if 'site_id' not in self:
-            self.object = self.api.client_shell.keystone.tenants.create(**nova_site)
+            tenants = self.api.client_shell.keystone.tenants.findall(name=self['login_base'])
+            if not tenants:
+                self.object = self.api.client_shell.keystone.tenants.create(**nova_site)
+            else:
+                self.object = tenants[0]
             self['tenant_id'] = self.object.id
             AlchemyObj.insert(self, dict(self)) 
         else: