#!/usr/bin/env python
'''
    gandi api to update an ip address
'''

import xmlrpclib
import urllib2
import sys
import ConfigParser

def main():
    '''
        gandi api to update an ip address
    '''

    config = ConfigParser.ConfigParser()
    config.read('/etc/gandi-api-key')
    api_key = config.get('general', 'key')
    zone_name = config.get('general', 'zone_name')
    ttl = config.getint('general', 'ttl')

    domains_to_update = [v for _, v in config.items('domain')]

    _update_domain(api_key, zone_name, domains_to_update, ttl)

def _create_api_client(api_key):
    '''
        gandi api to update an ip address
    '''

    api = xmlrpclib.ServerProxy('https://rpc.gandi.net/xmlrpc/')
    _ = api.version.info(api_key)
    return api

def _get_current_address():
    '''
        gandi api to update an ip address
    '''

    return urllib2.urlopen('http://icanhazip.com').read()

def _get_zone_for_name(api, api_key, zone_name):
    '''
        gandi api to update an ip address
    '''

    zone_id = [
                z['id'] for z in api.domain.zone.list(api_key)
                if z['name'] == zone_name
    ]
    return zone_id.pop()

def _update_domain(api_key, zone_name, domain_list, ttl):
    '''
        gandi api to update an ip address
    '''

    api = _create_api_client(api_key)
    zone_id = _get_zone_for_name(api, api_key, zone_name)
    ip_address = _get_current_address()

    zone_version = api.domain.zone.version.new(api_key, zone_id)

    for domain_name in domain_list:
        print >> sys.stderr, "delete %s in version %s with result = %s" % (
                domain_name,
                zone_version,
                api.domain.zone.record.delete(
                    api_key,
                    zone_id,
                    zone_version,
                    {"type": "A", "name": domain_name}
                )
        )

        print >> sys.stderr, "add %s in version %s with result = %s" % (
                domain_name,
                zone_version,
                api.domain.zone.record.add(
                    api_key,
                    zone_id,
                    zone_version,
                    {
                        "type": "A",
                        "name": domain_name,
                        "ttl": ttl,
                        "value": ip_address
                    }
                )
        )

if __name__ == 'main':
    main()

@@ -830,25 +830,24 @@
 ess%0A    '''%0A
-%0A
     return u
@@ -2371,16 +2371,249 @@
      )%0A%0A
+    print %3E%3E sys.stderr, %22active version %25s with result = %25s%22 %25 (%0A            zone_version,%0A            api.domain.zone.version.set(%0A                api_key,%0A                zone_id,%0A                zone_version%0A            )%0A    )%0A%0A
 if __nam
@@ -2624,22 +2624,36 @@
 == '
+__
 main
+__
 ':%0A    
+sys.exit(
 main()
+)
 %0A

active the zone