Commits

Paul McLanahan committed 912f435

Added discovery and writing of domain id to config file if not present.

Comments (0)

Files changed (1)

             if domain['DOMAIN'] == dname:
                 return domain['DOMAINID']
 
-    def set_record_value(self, dname, rid, value='[remote_addr]'):
+    def set_record_value(self, did, rid, value='[remote_addr]'):
         rdata = {
-            'DomainID': self.get_domain_id(dname),
+            'DomainID': did,
             'ResourceID': rid,
             'Target': value,
         }
     if not os.path.exists(CONFIG_FILE):
         print_result('no config file')
 
-    config = RawConfigParser()
-    config.read(cfg_file)
+    config = RawConfigParser({'domain_id':None})
+    with open(cfg_file) as f:
+        config.readfp(f)
     key = config.get('linode', 'api_key')
     domain = config.get('linode', 'domain_name')
     resource = config.get('linode', 'resource_id')
+    domain_id = config.get('linode', 'domain_id')
     logging.debug('domain=%s, names=%r, key=%s', domain, resource, key)
-    sh = Linode(key)
-    sh.set_record_value(domain, resource)
+    api = Linode(key)
+    if domain_id is None:
+        domain_id = api.get_domain_id(domain)
+        config.set('linode', 'domain_id', domain_id)
+        with open(cfg_file, 'w') as f:
+            config.write(f)
+    api.set_record_value(domain_id, resource)
     logging.info('set %s to current ip', domain)
     print_result()