Set Site/Slice HRN when new Site/Slice is created
[plcapi.git] / PLC / Methods / AddSite.py
index 171670d..2150a91 100644 (file)
@@ -2,12 +2,19 @@ from PLC.Faults import *
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
 from PLC.Sites import Site, Sites
-from PLC.Auth import PasswordAuth
+from PLC.Auth import Auth
+
+from PLC.Methods.AddSiteTag import AddSiteTag
+
+can_update = lambda (field, value): field in \
+             ['name', 'abbreviated_name', 'login_base',
+              'is_public', 'latitude', 'longitude', 'url',
+              'max_slices', 'max_slivers', 'enabled', 'ext_consortium_id']
 
 class AddSite(Method):
     """
     Adds a new site, and creates a node group for that site. Any
-    fields specified in optional_vals are used, otherwise defaults are
+    fields specified in site_fields are used, otherwise defaults are
     used.
 
     Returns the new site_id (> 0) if successful, faults otherwise.
@@ -15,29 +22,29 @@ class AddSite(Method):
 
     roles = ['admin']
 
-    can_update = lambda (field, value): field in \
-                 ['is_public', 'latitude', 'longitude', 'url',
-                  'organization_id', 'ext_consortium_id']
-    update_fields = dict(filter(can_update, Site.fields.items()))
+    site_fields = dict(filter(can_update, Site.fields.items()))
 
     accepts = [
-        PasswordAuth(),
-        Site.fields['name'],
-        Site.fields['abbreviated_name'],
-        Site.fields['login_base'],
-        update_fields
+        Auth(),
+        site_fields
         ]
 
     returns = Parameter(int, 'New site_id (> 0) if successful')
 
-    def call(self, auth, name, abbreviated_name, login_base, optional_vals = {}):
-        if filter(lambda field: field not in self.update_fields, optional_vals):
-            raise PLCInvalidArgument, "Invalid field specified"
-
-        site = Site(self.api, optional_vals)
-        site['name'] = name
-        site['abbreviated_name'] = abbreviated_name
-        site['login_base'] = login_base
+    def call(self, auth, site_fields):
+        site_fields = dict(filter(can_update, site_fields.items()))
+        site = Site(self.api, site_fields)
         site.sync()
 
+        # Logging variables
+        self.event_objects = {'Site': [site['site_id']]}
+        self.message = 'Site %d created' % site['site_id']
+
+        # Set Site HRN
+        root_auth = self.api.config.PLC_HRN_ROOT
+        tagname = 'hrn'
+        tagvalue = '.'.join([root_auth, site['login_base']])
+        AddSiteTag(self.api).__call__(auth,site['site_id'],tagname,tagvalue)
+
+
         return site['site_id']