1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Sites import Site, Sites
5 from PLC.Auth import Auth
7 from PLC.TagTypes import TagTypes
8 from PLC.SiteTags import SiteTags
9 from PLC.Methods.AddSiteTag import AddSiteTag
10 from PLC.Methods.UpdateSiteTag import UpdateSiteTag
12 related_fields = Site.related_fields.keys()
13 can_update = lambda (field, value): field in \
14 ['name', 'abbreviated_name', 'login_base',
15 'is_public', 'latitude', 'longitude', 'url',
16 'max_slices', 'max_slivers', 'enabled', 'ext_consortium_id'] + \
19 class UpdateSite(Method):
21 Updates a site. Only the fields specified in update_fields are
22 updated, all other fields are left untouched.
24 PIs can only update sites they are a member of. Only admins can
25 update max_slices, max_slivers, and login_base.
27 Returns 1 if successful, faults otherwise.
30 roles = ['admin', 'pi']
32 site_fields = dict(filter(can_update, Site.fields.items() + Site.related_fields.items()))
36 Mixed(Site.fields['site_id'],
37 Site.fields['login_base']),
41 returns = Parameter(int, '1 if successful')
43 def call(self, auth, site_id_or_login_base, site_fields):
44 site_fields = dict(filter(can_update, site_fields.items()))
46 # Get site information
47 sites = Sites(self.api, [site_id_or_login_base])
49 raise PLCInvalidArgument, "No such site"
52 if site['peer_id'] is not None:
53 raise PLCInvalidArgument, "Not a local site"
55 # Authenticated function
56 assert self.caller is not None
58 # If we are not an admin, make sure that the caller is a
60 if 'admin' not in self.caller['roles']:
61 if site['site_id'] not in self.caller['site_ids']:
62 raise PLCPermissionDenied, "Not allowed to modify specified site"
64 # Remove admin only fields
65 for key in 'max_slices', 'max_slivers', 'login_base':
66 if key in site_fields:
69 # Make requested associations
70 for field in related_fields:
71 if field in site_fields:
72 site.associate(auth, field, site_fields[field])
73 site_fields.pop(field)
75 site.update(site_fields)
76 site.update_last_updated(False)
80 self.event_objects = {'Site': [site['site_id']]}
81 self.message = 'Site %d updated: %s' % \
82 (site['site_id'], ", ".join(site_fields.keys()))
84 # Update Site HRN if login_base changed
85 if 'login_base' in site_fields:
86 root_auth = self.api.config.PLC_HRN_ROOT
88 tagvalue = '.'.join([root_auth, site['login_base']])
89 # check if the tagtype instance exists
90 tag_types = TagTypes(self.api,{'tagname':tagname})
92 site_tags=SiteTags(self.api,{'tagname':tagname,'site_id':site['site_id']})
94 AddSiteTag(self.api).__call__(auth,int(site['site_id']),tagname,tagvalue)
96 UpdateSiteTag(self.api).__call__(auth,site_tags[0]['site_tag_id'],tagvalue)