1. Peter Sanchez
  2. zerigodns

Commits

Peter Sanchez  committed c86cd60

Working on NSHost. Have it mostly functional

  • Participants
  • Parent commits 2284d4c
  • Branches default

Comments (0)

Files changed (1)

File zerigodns.py

View file
         }
     
     def send_request(self, method='GET', id=None, path=None, 
-                        request=None, headers={}, opts={}, success_code=200):
+                                request=None, headers={}, opts={}):
         ''' Construct and send request to Zerigo servers.
         '''
         self.errors = []
         )
         return ip_hash.get('ipv4')
     
+    def get_blank(self):
+        api_path = '%s/new.xml' % self.element
+        return self.parse(self.send_request(path=api_path))
+    
     def has_errors(self):
         return (len(self.errors) > 0)
     
     def destroy(self):
         if not hasattr(self, 'id'):
             raise ZerigoError('Cannot destroy without a valid id')
-        self.send_request(method='DELETE', id=int(self.id))
+        self.send_request(method='DELETE', id=self.id)
         return (not self.has_errors())
     
     def save(self):
             # Update current instance
             result = self.send_request(
                 method='PUT',
-                id=int(self.id),
+                id=self.id,
                 request=data,
             )
 
         self.count = int(data['count'])
         return self.count
     
-    def get_blank_zone(self):
-        api_path = 'zones/new.xml'
-        return self.parse(self.send_request(path=api_path))
-    
     def build_blank_zone(self):
-        self.load(self.get_blank_zone()['zone'])
+        self.load(self.get_blank()['zone'])
     
     def create(self, data, load_blank=False):
         if data.has_key('zone'):
     def reload(self):
         if not hasattr(self, 'id'):
             raise ZerigoError('Cannot retrieve zone without a valid id')
-        xml = self.send_request(method='GET', id=int(self.id))
+        xml = self.send_request(method='GET', id=self.id)
         data = self.parse(xml)
         self.load(data['zone'])
         return self
     def __init__(self, *args, **kwargs):
         super(NSHost, self).__init__(*args, **kwargs)
         self.xml_skip = ('id', 'created-at', 'updated-at',)
-        self.element = 'hosts'
+        self.element = 'hosts'
+    
+    def to_xml(self):
+        return self._to_xml({'host': self.get_xml_data()})
+    
+    def build_blank_host(self):
+        self.load(self.get_blank()['host'])
+    
+    def create(self, data, zone_id=None, load_blank=False):
+        if data.has_key('host'):
+            data = data.get('host')
+        if zone_id is not None:
+            data['zone_id'] = \
+                zone_id.id if isinstance(zone_id, NSZone) else zone_id
+        host = NSHost(
+            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
+    
+    def find_all(self, zone_id, opts={}):
+        opts['zone_id'] = zone_id
+        return self.hosts(opts=opts)
+
+    def hosts(self, opts={}):
+        if not opts.has_key('zone_id'):
+            opts['zone_id'] = self.zone_id
+        xml = self.send_request(method='GET', opts=opts)
+        data = self.parse(xml)
+        return [
+            NSHost(
+                self.user,
+                self.key,
+                data=zdata['host'],
+                is_secure=self.is_secure,
+                is_debug=self.is_debug,
+            ) for zdata in data.get('hosts', [])
+        ]
+    
+    def find(self, id, zone_id):
+        host = NSHost(
+            self.user,
+            self.key,
+            data={'id': id, 'zone_id': zone_id,},
+            is_secure=self.is_secure,
+            is_debug=self.is_debug,
+        )
+        host.reload()
+        return host
+    
+    def reload(self):
+        if not hasattr(self, 'id'):
+            raise ZerigoError('Cannot retrieve host without a valid id')
+        xml = self.send_request(method='GET', id=self.id)
+        data = self.parse(xml)
+        self.load(data['host'])
+        return self