implement AddPerson, DeletePerson
[plcapi.git] / PLC / Sites.py
1 from types import StringTypes
2 import string
3
4 from PLC.Faults import *
5 from PLC.Logger import logger
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.NovaTable import NovaObject, NovaTable
8 from PLC.Slices import Slice, Slices
9 #from PLC.Persons import Person, Persons
10
11 class Site(NovaObject):
12     """
13     Representation of a row in the sites table. To use, optionally
14     instantiate with a dict of values. Update as you would a
15     dict. Commit to the database with sync().
16     """
17
18     fields = {
19         'enabled': Parameter(bool, "Has been enabled"),
20         'id': Parameter(str, "Site identifier"),
21         'name': Parameter(str, "Full site name", max = 254),
22         'description': Parameter(str, "Description", max = 254),
23         #'max_slices': Parameter(int, "Maximum number of slices that the site is able to create"),
24         #'max_slivers': Parameter(int, "Maximum number of slivers that the site is able to create"),
25         'person_ids': Parameter([int], "List of account identifiers"),
26         'slice_ids': Parameter([int], "List of slice identifiers"),
27         'pcu_ids': Parameter([int], "List of PCU identifiers"),
28         'node_ids': Parameter([int], "List of site node identifiers"),
29         }
30
31     def sync(self, insert=False, validate=True):
32         NovaObject.sync(self, insert, validate)
33         if insert == True or id not in self:
34             self.object = self.api.client_shell.keystone.tenants.create(**self)
35
36 class Sites(NovaTable):
37     """
38     Representation of row(s) from the sites table in the
39     database.
40     """
41
42     def __init__(self, api, site_filter = None, columns = None):
43         self.api = api 
44         if not site_filter:
45             sites = self.api.client_shell.keystone.tenants.findall()
46         elif isinstance(site_filter, StringTypes):
47             sites = [self.api.client_shell.keystone.tenants.find(id=site_filter)]
48         elif isinstance(site_filter, dict):
49             sites = self.api.client_shell.keystone.tenants.findall(**site_filter)
50         elif isinstance(site_filter, (list, tuple, set)):
51             sites = self.api.client_shell.keystone.tenants.findall()
52             sites = [site for site in sites if site.id in site_filter]
53         else:
54             raise PLCInvalidArgument, "Wrong site filter %s" % site_filter         
55
56         for site in sites:
57             site = Site(self.api, object = site)
58             self.append(site)