-rw-r--r-- | gandi_tool.py | 82 |
diff --git a/gandi_tool.py b/gandi_tool.py index 594dd0a..a5a5dfa 100644 --- a/gandi_tool.py +++ b/gandi_tool.py @@ -1,31 +1,57 @@ -url='https://rpc.gandi.net/xmlrpc/' - import xmlrpclib -api = xmlrpclib.ServerProxy(url) - import subprocess -command=['gpg', '--decrypt', '-o', '-', 'api-key-info.asc' ] -p = subprocess.Popen(command, stdout=subprocess.PIPE) -keydata_string = p.communicate()[0] - import json -keydata = json.loads(keydata_string) -apikey = keydata['key'] -# validation check: is it 24-char? - -version = api.version.info(apikey) -print("API Version: %s", version) -print(api.domain.info(apikey, 'fsij.org')) - -print(api.domain.forward.count(apikey, 'fsij.org')) -print(api.domain.forward.list(apikey, 'fsij.org')) - -# >>> api.domain.forward.create(apikey, 'mydomain.net', 'admin', -# ... {'destinations': ['stephanie@example.com']}) -# {'destinations': ['stephanie@example.com'], 'source': 'admin'} -# -# >>> api.domain.forward.update(apikey, 'mydomain.net', 'admin', ... {'destinations': ['stephanie@example.com', 'steph@example.com']}) -# {'destinations': ['stephanie@example.com', 'steph@example.com'], 'source': 'admin'} -# -# >>> api.domain.forward.delete(apikey, 'mydomain.net', 'admin') -# True + +class gandi_tool(object): + def __init__(self, url, encrypted_key_file, domain): + self.api = xmlrpclib.ServerProxy(url) + command=['gpg', '--decrypt', '-o', '-', encrypted_key_file] + p = subprocess.Popen(command, stdout=subprocess.PIPE) + keydata_string = p.communicate()[0] + keydata = json.loads(keydata_string) + self.apikey = keydata['key'] # validation check: is it 24-char? + self.domain = domain + + def api_version(self): + return self.api.version.info(self.apikey) + + def info(self): + return self.api.domain.info(self.apikey, self.domain) + + def forward_count(self): + return self.api.domain.forward.count(self.apikey, self.domain) + + def forward_list(self, opts=None): + if opts: + return self.api.domain.forward.list(self.apikey, self.domain, opts) + else: + return self.api.domain.forward.list(self.apikey, self.domain) + + def forward_create(self, src, params): + return self.api.domain.forward.create(self.apikey, self.domain, src, params) + + def forward_update(self, src, params): + return self.api.domain.forward.update(self.apikey, self.domain, src, params) + + def forward_delete(self, src): + return self.api.domain.forward.delete(self.apikey, self.domain, src) + + +if __name__ == "__main__": + url='https://rpc.gandi.net/xmlrpc/' + encrypted_key_file='api-key-info.asc' + domain='fsij.org' + + g=gandi_tool(url, encrypted_key_file, domain) + print("API Version: %s" % g.api_version()['api_version']) + info=g.info() + print("Domain owner handle: %s" % info['contacts']['owner']['handle']) + print(g.forward_count()) + print(g.forward_list()) + if g.forward_list({ 'source' : 'gniibe' }): + print(g.forward_delete('gniibe')) + print(g.forward_create('gniibe', {'destinations': ['gniibe@gniibe.org']})) + print(g.forward_list({ 'source' : 'gniibe' })) + print(g.forward_update('gniibe', {'destinations': ['fsij@gniibe.org']})) + if g.forward_delete('gniibe'): + print 'Done' |