Commits

Peter Sanchez committed b836fc9

Adding Host Template support

Comments (0)

Files changed (1)

         self.xml_skip = ('id', 'created-at', 'updated-at',)
         self.element = 'zone_templates'
     
+    def __setattr__(self, name, val):
+        if name == 'host-templates' and isinstance(val, (list, tuple)):
+            # Add host-templates as NSHostTemplate objects
+            self.vals[name] = [
+                NSHostTemplate(
+                    self.user,
+                    self.key,
+                    data=zdata['host-template'],
+                    is_secure=self.is_secure,
+                    is_debug=self.is_debug,
+                ) for zdata in val
+            ]
+        else:
+            super(NSZoneTemplate, self).__setattr__(name, val)
+    
     def to_xml(self):
         return self._to_xml({'zone-template': self.get_xml_data()})
 
             is_debug=self.is_debug
         )
         zone.reload()
-        return zone
+        return zone
+
+
+class NSHostTemplate(ZerigoDNS):
+    ''' Class to manage host templates 
+    '''
+    def __init__(self, *args, **kwargs):
+        super(NSHostTemplate, self).__init__(*args, **kwargs)
+        self.xml_skip = ('id', 'created-at', 'updated-at',)
+        self.element = 'host_templates'
+
+    def _get_zone_template_id(self, zone_id):
+        return zone_id.id if isinstance(zone_id, NSZoneTemplate) else zone_id
+
+    def to_xml(self):
+        return self._to_xml({'host-template': self.get_xml_data()})
+
+    def build_blank_template(self):
+        self.load(self.get_blank()['host-template'])
+    
+    def create(self, data, zone_id=None, load_blank=False):
+        if data.has_key('host-template'):
+            data = data.get('host-template')
+        if zone_id is not None:
+            data['zone_template_id'] = self._get_zone_template_id(zone_id)
+        host = NSHostTemplate(
+            self.user,
+            self.key,
+            data=data,
+            is_secure=self.is_secure,
+            is_debug=self.is_debug,
+        )
+        if load_blank:
+            host.build_blank_host()
+            host.load(data, reset=False)
+        host.save()
+        return host