Update fsij_list remove 2006 list, adding fsij-meeting.
[fsij-server-admin.git] / gandi_tool.py
1 import xmlrpclib
2 import subprocess
3 import json
4
5 class gandi_tool(object):
6     def __init__(self, url, encrypted_key_file, domain):
7         self.api = xmlrpclib.ServerProxy(url)
8         command=['gpg', '--decrypt', '-o', '-',  encrypted_key_file]
9         p = subprocess.Popen(command, stdout=subprocess.PIPE)
10         keydata_string = p.communicate()[0]
11         keydata = json.loads(keydata_string)
12         self.apikey = keydata['key']    # validation check: is it 24-char?
13         self.domain = domain
14
15     def api_version(self):
16         return self.api.version.info(self.apikey)
17
18     def info(self):
19         return self.api.domain.info(self.apikey, self.domain)
20
21     def forward_count(self):
22         return self.api.domain.forward.count(self.apikey, self.domain)
23
24     def forward_list(self, opts=None):
25         if opts:
26             return self.api.domain.forward.list(self.apikey, self.domain, opts)
27         else:
28             return self.api.domain.forward.list(self.apikey, self.domain)
29
30     def forward_create(self, src, params):
31         return self.api.domain.forward.create(self.apikey, self.domain, src, params)
32
33     def forward_update(self, src, params):
34         return self.api.domain.forward.update(self.apikey, self.domain, src, params)
35
36     def forward_delete(self, src):
37         return self.api.domain.forward.delete(self.apikey, self.domain, src)
38
39
40 if __name__ == "__main__":
41     url='https://rpc.gandi.net/xmlrpc/'
42     encrypted_key_file='api-key-info.asc'
43     domain='fsij.org'
44
45     g=gandi_tool(url, encrypted_key_file, domain)
46     print("API Version: %s" % g.api_version()['api_version'])
47     info=g.info()
48     print("Domain owner handle: %s" % info['contacts']['owner']['handle'])
49     print(g.forward_count())
50     print(g.forward_list())
51     if g.forward_list({ 'source' : 'gniibe' }):
52         print(g.forward_delete('gniibe'))
53     print(g.forward_create('gniibe', {'destinations': ['gniibe@gniibe.org']}))
54     print(g.forward_list({ 'source' : 'gniibe' }))
55     print(g.forward_update('gniibe', {'destinations': ['fsij@gniibe.org']}))
56     if g.forward_delete('gniibe'):
57         print 'Done'