- implement AddressType and Address manipulation
authorMark Huang <mlhuang@cs.princeton.edu>
Fri, 6 Oct 2006 19:36:19 +0000 (19:36 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Fri, 6 Oct 2006 19:36:19 +0000 (19:36 +0000)
PLC/Methods/AddAddress.py [new file with mode: 0644]
PLC/Methods/AddAddressType.py [new file with mode: 0644]
PLC/Methods/AddAddressTypeToAddress.py [new file with mode: 0644]
PLC/Methods/DeleteAddress.py [new file with mode: 0644]
PLC/Methods/DeleteAddressType.py [new file with mode: 0644]
PLC/Methods/DeleteAddressTypeFromAddress.py [new file with mode: 0644]
PLC/Methods/GetAddressTypes.py [new file with mode: 0644]
PLC/Methods/GetAddresses.py [new file with mode: 0644]
PLC/Methods/UpdateAddress.py [new file with mode: 0644]
PLC/Methods/UpdateAddressType.py [new file with mode: 0644]
PLC/Methods/__init__.py

diff --git a/PLC/Methods/AddAddress.py b/PLC/Methods/AddAddress.py
new file mode 100644 (file)
index 0000000..bdced15
--- /dev/null
@@ -0,0 +1,52 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Addresses import Address, Addresses
+from PLC.Auth import PasswordAuth
+from PLC.Sites import Site, Sites
+
+class AddAddress(Method):
+    """
+    Adds a new address to a site. Fields specified in
+    address_fields are used; some are not optional.
+
+    PIs may only add addresses to their own sites.
+
+    Returns the new address_id (> 0) if successful, faults otherwise.
+    """
+
+    roles = ['admin', 'pi']
+
+    can_update = lambda (field, value): field in \
+                 ['line1', 'line2', 'line3',
+                  'city', 'state', 'postalcode', 'country']
+    update_fields = dict(filter(can_update, Address.fields.items()))
+
+    accepts = [
+        PasswordAuth(),
+        Mixed(Site.fields['site_id'],
+              Site.fields['login_base']),
+        update_fields
+        ]
+
+    returns = Parameter(int, 'New address_id (> 0) if successful')
+
+    def call(self, auth, site_id_or_login_base, address_fields = {}):
+        if filter(lambda field: field not in self.update_fields, address_fields):
+            raise PLCInvalidArgument, "Invalid field specified"
+
+        # Get associated site details
+        sites = Sites(self.api, [site_id_or_login_base]).values()
+        if not sites:
+            raise PLCInvalidArgument, "No such site"
+        site = sites[0]
+
+        if 'admin' not in self.caller['roles']:
+            if site['site_id'] not in self.caller['site_ids']:
+                raise PLCPermissionDenied, "Address must be associated with one of your sites"
+
+        address = Address(self.api, address_fields)
+        address['site_id'] = site['site_id']
+        address.sync()
+
+        return address['address_id']
diff --git a/PLC/Methods/AddAddressType.py b/PLC/Methods/AddAddressType.py
new file mode 100644 (file)
index 0000000..25f9d42
--- /dev/null
@@ -0,0 +1,33 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.AddressTypes import AddressType, AddressTypes
+from PLC.Auth import PasswordAuth
+
+class AddAddressType(Method):
+    """
+    Adds a new address type. Fields specified in address_type_fields
+    are used.
+
+    Returns the new address_type_id (> 0) if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    can_update = lambda (field, value): field in ['description']
+    update_fields = dict(filter(can_update, AddressType.fields.items()))
+
+    accepts = [
+        PasswordAuth(),
+        AddressType.fields['name'],
+        update_fields
+        ]
+
+    returns = Parameter(int, 'New address_type_id (> 0) if successful')
+
+    def call(self, auth, name, address_type_fields = {}):
+        address_type = AddressType(self.api, address_type_fields)
+        address_type['name'] = name
+        address_type.sync()
+
+        return address_type['address_type_id']
diff --git a/PLC/Methods/AddAddressTypeToAddress.py b/PLC/Methods/AddAddressTypeToAddress.py
new file mode 100644 (file)
index 0000000..64f532a
--- /dev/null
@@ -0,0 +1,45 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.AddressTypes import AddressType, AddressTypes
+from PLC.Addresses import Address, Addresses
+from PLC.Auth import PasswordAuth
+
+class AddAddressTypeToAddress(Method):
+    """
+    Adds an address type to the specified address.
+
+    PIs may only update addresses of their own sites.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin', 'pi']
+
+    accepts = [
+        PasswordAuth(),
+        Mixed(AddressType.fields['address_type_id'],
+              AddressType.fields['name']),
+        Address.fields['address_id']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, address_type_id_or_name, address_id):
+        address_types = AddressTypes(self.api, [address_type_id_or_name]).values()
+        if not address_types:
+            raise PLCInvalidArgument, "No such address type"
+        address_type = address_types[0]
+
+        addresses = Addresses(self.api, [address_id]).values()
+        if not addresses:
+            raise PLCInvalidArgument, "No such address"
+        address = addresses[0]
+
+        if 'admin' not in self.caller['roles']:
+            if address['site_id'] not in self.caller['site_ids']:
+                raise PLCPermissionDenied, "Address must be associated with one of your sites"
+
+        address.add_address_type(address_type)
+
+        return 1
diff --git a/PLC/Methods/DeleteAddress.py b/PLC/Methods/DeleteAddress.py
new file mode 100644 (file)
index 0000000..363896f
--- /dev/null
@@ -0,0 +1,38 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Addresses import Address, Addresses
+from PLC.Auth import PasswordAuth
+
+class DeleteAddress(Method):
+    """
+    Deletes an address.
+
+    PIs may only delete addresses from their own sites.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin', 'pi']
+
+    accepts = [
+        PasswordAuth(),
+        Address.fields['address_id'],
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, address_id):
+        # Get associated address details
+        addresses = Addresses(self.api, [address_id]).values()
+        if not addresses:
+            raise PLCInvalidArgument, "No such address"
+        address = addresses[0]
+
+        if 'admin' not in self.caller['roles']:
+            if address['site_id'] not in self.caller['site_ids']:
+                raise PLCPermissionDenied, "Address must be associated with one of your sites"
+
+        address.delete()
+
+        return 1
diff --git a/PLC/Methods/DeleteAddressType.py b/PLC/Methods/DeleteAddressType.py
new file mode 100644 (file)
index 0000000..0ccb168
--- /dev/null
@@ -0,0 +1,32 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.AddressTypes import AddressType, AddressTypes
+from PLC.Auth import PasswordAuth
+
+class DeleteAddressType(Method):
+    """
+    Deletes an address type.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    accepts = [
+        PasswordAuth(),
+        Mixed(AddressType.fields['address_type_id'],
+              AddressType.fields['name'])
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, address_type_id_or_name):
+        address_types = AddressTypes(self.api, [address_type_id_or_name]).values()
+        if not address_types:
+            raise PLCInvalidArgument, "No such address type"
+        address_type = address_types[0]
+
+        address_type.delete()
+
+        return 1
diff --git a/PLC/Methods/DeleteAddressTypeFromAddress.py b/PLC/Methods/DeleteAddressTypeFromAddress.py
new file mode 100644 (file)
index 0000000..266a9ff
--- /dev/null
@@ -0,0 +1,45 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.AddressTypes import AddressType, AddressTypes
+from PLC.Addresses import Address, Addresses
+from PLC.Auth import PasswordAuth
+
+class DeleteAddressTypeFromAddress(Method):
+    """
+    Deletes an address type from the specified address.
+
+    PIs may only update addresses of their own sites.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin', 'pi']
+
+    accepts = [
+        PasswordAuth(),
+        Mixed(AddressType.fields['address_type_id'],
+              AddressType.fields['name']),
+        Address.fields['address_id']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, address_type_id_or_name, address_id):
+        address_types = AddressTypes(self.api, [address_type_id_or_name]).values()
+        if not address_types:
+            raise PLCInvalidArgument, "No such address type"
+        address_type = address_types[0]
+
+        addresses = Addresses(self.api, [address_id]).values()
+        if not addresses:
+            raise PLCInvalidArgument, "No such address"
+        address = addresses[0]
+
+        if 'admin' not in self.caller['roles']:
+            if address['site_id'] not in self.caller['site_ids']:
+                raise PLCPermissionDenied, "Address must be associated with one of your sites"
+
+        address.remove_address_type(address_type)
+
+        return 1
diff --git a/PLC/Methods/GetAddressTypes.py b/PLC/Methods/GetAddressTypes.py
new file mode 100644 (file)
index 0000000..0aaef9b
--- /dev/null
@@ -0,0 +1,27 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.AddressTypes import AddressType, AddressTypes
+from PLC.Auth import PasswordAuth
+
+class GetAddressTypes(Method):
+    """
+    Get an array of structs containing details about valid address
+    types. If address_type_id_or_name_list is specified, only the
+    specified address types will be queried.
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech']
+
+    accepts = [
+        PasswordAuth(),
+        [Mixed(AddressType.fields['address_type_id'],
+               AddressType.fields['name'])]
+        ]
+
+    returns = [AddressType.fields]
+
+    def call(self, auth, address_type_id_or_name_list = None):
+        address_types = AddressTypes(self.api, address_type_id_or_name_list).values()
+
+        return address_types
diff --git a/PLC/Methods/GetAddresses.py b/PLC/Methods/GetAddresses.py
new file mode 100644 (file)
index 0000000..eea3885
--- /dev/null
@@ -0,0 +1,41 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Addresses import Address, Addresses
+from PLC.Sites import Site, Sites
+from PLC.Auth import PasswordAuth
+
+class GetAddresses(Method):
+    """
+    Get an array of structs containing the addresses of the specified
+    site. If address_id_list is specified, only the specified
+    addresses will be queried.
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech']
+
+    accepts = [
+        PasswordAuth(),
+        Mixed(Site.fields['site_id'],
+              Site.fields['login_base']),
+        [Address.fields['address_id']],
+        ]
+
+    returns = [Address.fields]
+
+    def call(self, auth, site_id_or_login_base, address_id_list = None):
+        sites = Sites(self.api, [site_id_or_login_base]).values()
+        if not sites:
+            raise PLCInvalidArgument, "No such site"
+        site = sites[0]
+
+        if address_id_list is None:
+            address_id_list = site['address_ids']
+        else:
+            if set(address_id_list).intersection(site['address_ids']) != \
+               set(address_id_list):
+                raise PLCInvalidArgument, "Invalid address ID(s)"
+
+        addresses = Addresses(self.api, address_id_list).values()
+
+        return addresses
diff --git a/PLC/Methods/UpdateAddress.py b/PLC/Methods/UpdateAddress.py
new file mode 100644 (file)
index 0000000..2428b2f
--- /dev/null
@@ -0,0 +1,49 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Addresses import Address, Addresses
+from PLC.Auth import PasswordAuth
+
+class UpdateAddress(Method):
+    """
+    Updates the parameters of an existing address with the values in
+    address_fields.
+
+    PIs may only update addresses of their own sites.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin', 'pi']
+
+    can_update = lambda (field, value): field in \
+                 ['line1', 'line2', 'line3',
+                  'city', 'state', 'postalcode', 'country']
+    update_fields = dict(filter(can_update, Address.fields.items()))
+
+    accepts = [
+        PasswordAuth(),
+        Address.fields['address_id'],
+        update_fields
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, address_id, address_fields):
+        if filter(lambda field: field not in self.update_fields, address_fields):
+            raise PLCInvalidArgument, "Invalid field specified"
+
+        # Get associated address details
+        addresses = Addresses(self.api, [address_id]).values()
+        if not addresses:
+            raise PLCInvalidArgument, "No such address"
+        address = addresses[0]
+
+        if 'admin' not in self.caller['roles']:
+            if address['site_id'] not in self.caller['site_ids']:
+                raise PLCPermissionDenied, "Address must be associated with one of your sites"
+
+        address.update(address_fields)
+        address.sync()
+
+        return 1
diff --git a/PLC/Methods/UpdateAddressType.py b/PLC/Methods/UpdateAddressType.py
new file mode 100644 (file)
index 0000000..f8ad7c4
--- /dev/null
@@ -0,0 +1,41 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.AddressTypes import AddressType, AddressTypes
+from PLC.Auth import PasswordAuth
+
+class UpdateAddressType(Method):
+    """
+    Updates the parameters of an existing address type with the values
+    in address_type_fields.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    can_update = lambda (field, value): field in ['name', 'description']
+    update_fields = dict(filter(can_update, AddressType.fields.items()))
+
+    accepts = [
+        PasswordAuth(),
+        Mixed(AddressType.fields['address_type_id'],
+              AddressType.fields['name']),
+        update_fields
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, address_type_id_or_name, address_type_fields):
+        if filter(lambda field: field not in self.update_fields, address_type_fields):
+            raise PLCInvalidArgument, "Invalid field specified"
+
+        address_types = AddressTypes(self.api, [address_type_id_or_name]).values()
+        if not address_types:
+            raise PLCInvalidArgument, "No such address type"
+        address_type = address_types[0]
+
+        address_type.update(address_type_fields)
+        address_type.sync()
+
+        return 1
index 9ca0644..9063405 100644 (file)
@@ -1 +1 @@
-methods = 'AddAttribute AddNodeGroup AddNodeNetwork AddNode AddNodeToNodeGroup AddPerson AddPersonToSite AddSite AddSliceAttribute AddSlice AdmAddNodeGroup AdmAddNodeNetwork AdmAddNode AdmAddNodeToNodeGroup AdmAddPerson AdmAddPersonToSite AdmAddSite AdmAuthCheck AdmDeleteNodeGroup AdmDeleteNodeNetwork AdmDeleteNode AdmDeletePerson AdmDeleteSite AdmGetAllNodeNetworkBandwidthLimits AdmGetAllNodeNetworks AdmGetAllRoles AdmGetNodeGroupNodes AdmGetNodeGroups AdmGetNodes AdmGetPersonAddresses AdmGetPersonRoles AdmGetPersonSites AdmGetPersons AdmGetSiteNodes AdmGetSitePersons AdmGetSites AdmGrantRoleToPerson AdmIsPersonInRole AdmRemoveNodeFromNodeGroup AdmRemovePersonFromSite AdmRevokeRoleFromPerson AdmSetPersonEnabled AdmSetPersonPrimarySite AdmUpdateNodeGroup AdmUpdateNodeNetwork AdmUpdateNode AdmUpdatePerson AdmUpdateSite AuthCheck DeleteAttribute DeleteNodeGroup DeleteNodeNetwork DeleteNode DeletePerson DeleteSite DeleteSliceAttribute DeleteSlice GetAllNodeNetworkBandwidthLimits GetAllNodeNetworks GetAllRoles GetAttributes GetNodeGroupNodes GetNodeGroups GetNodes GetPersonAddresses GetPersonRoles GetPersonSites GetPersons GetSiteNodes GetSitePersons GetSites GetSliceAttributes GetSlices GrantRoleToPerson IsPersonInRole RemoveNodeFromNodeGroup RemovePersonFromSite RevokeRoleFromPerson SetPersonEnabled SetPersonPrimarySite UpdateAttribute UpdateNodeGroup UpdateNodeNetwork UpdateNode UpdatePerson UpdateSite UpdateSliceAttribute UpdateSlice  system.listMethods  system.methodHelp  system.methodSignature  system.multicall'.split()
+methods = 'AddAddress AddAddressType AddAddressTypeToAddress AddAttribute AddNodeGroup AddNodeNetwork AddNode AddNodeToNodeGroup AddPerson AddPersonToSite AddRoleToPerson AddSite AddSliceAttribute AddSlice AdmAddNodeGroup AdmAddNodeNetwork AdmAddNode AdmAddNodeToNodeGroup AdmAddPerson AdmAddPersonToSite AdmAddSite AdmAuthCheck AdmDeleteNodeGroup AdmDeleteNodeNetwork AdmDeleteNode AdmDeletePerson AdmDeleteSite AdmGetAllNodeNetworkBandwidthLimits AdmGetAllNodeNetworks AdmGetAllRoles AdmGetNodeGroupNodes AdmGetNodeGroups AdmGetNodes AdmGetPersonRoles AdmGetPersonSites AdmGetPersons AdmGetSiteNodes AdmGetSitePersons AdmGetSites AdmGrantRoleToPerson AdmIsPersonInRole AdmRemoveNodeFromNodeGroup AdmRemovePersonFromSite AdmRevokeRoleFromPerson AdmSetPersonEnabled AdmSetPersonPrimarySite AdmUpdateNodeGroup AdmUpdateNodeNetwork AdmUpdateNode AdmUpdatePerson AdmUpdateSite Authenticate DeleteAddress DeleteAddressTypeFromAddress DeleteAddressType DeleteAttribute DeleteNodeGroup DeleteNodeNetwork DeleteNode DeletePerson DeleteSite DeleteSliceAttribute DeleteSlice GetAddresses GetAddressTypes GetAttributes GetNodeGroups GetNodeNetworkBandwidthLimits GetNodeNetworks GetNodes GetPersons GetSites GetSliceAttributes GetSlices RemoveNodeFromNodeGroup RemovePersonFromSite RemoveRoleFromPerson SetPersonPrimarySite UpdateAddress UpdateAddressType UpdateAttribute UpdateNodeGroup UpdateNodeNetwork UpdateNode UpdatePerson UpdateSite UpdateSliceAttribute UpdateSlice  system.listMethods  system.methodHelp  system.methodSignature  system.multicall'.split()