- make Add() calling convention consistent among all functions that
[plcapi.git] / PLC / Methods / UpdateSliceAttributeType.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.SliceAttributeTypes import SliceAttributeType, SliceAttributeTypes
5 from PLC.Auth import PasswordAuth
6
7 can_update = lambda (field, value): field in \
8              ['name', 'description', 'min_role_id']
9
10 class UpdateSliceAttributeType(Method):
11     """
12     Updates the parameters of an existing attribute with the values in
13     attribute_type_fields.
14
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     attribute_type_fields = dict(filter(can_update, SliceAttributeType.fields.items()))
21     for field in attribute_type_fields.values():
22         field.optional = True
23
24     accepts = [
25         PasswordAuth(),
26         Mixed(SliceAttributeType.fields['attribute_type_id'],
27               SliceAttributeType.fields['name']),
28         attribute_type_fields
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, attribute_type_id_or_name, attribute_type_fields):
34         attribute_type_fields = dict(filter(can_update, attribute_type_fields.items()))
35
36         attribute_types = SliceAttributeTypes(self.api, [attribute_type_id_or_name]).values()
37         if not attribute_types:
38             raise PLCInvalidArgument, "No such attribute"
39         attribute_type = attribute_types[0]
40
41         attribute_type.update(attribute_type_fields)
42
43         attribute_type.sync()
44
45         return 1