Newer
Older
certbot-dns-inwx / update-record.py
#!/usr/bin/env python
# -*- encoding: UTF8 -*-

# author: Pascal Gollor (https://gitbucket.pgollor.de)
# author: InterNetworX, info →AT→ inwx.de

#############################################################################
###### This is an example of how to use the inwx class #######

from inwx.inwx import domrobot#, prettyprint
from inwx.configuration import get_account_data
import argparse


def main(args):
	verbose = args.verbose
	domain = args.domain

	# split domain
	h = domain.split('.')
	if (len(h) != 3):
		raise ValueError("invalid subdomain")
	# end if
	sub = h[0]
	domain = h[1] + '.' + h[2]

	if (args.debug):
		print(args)
	# end if

	api_url, username, password, shared_secret = get_account_data(True, config_file=args.config_file, config_section=args.config_section)
	inwx_conn = domrobot(api_url, False)
	ret = inwx_conn.account.login({'lang': args.language, 'user': username, 'pass': password})
	if (verbose):
		print("login: " + str(ret))
	# end if


	# check domain
	ret = inwx_conn.nameserver.list({'domain': domain})
	if (verbose):
		print('list: ' + str(ret))
	# end if
	if (ret['resData']['count'] == 0):
		raise RuntimeError("You are not user of this domain.")
	# end if


	# looking if subdomain exists
	subId = -1
	ret = inwx_conn.nameserver.info({'domain': domain})
	records = ret['resData']['record']

	for r in records:
		if (r['name'] == args.domain):
			subId = r['id']
			if (verbose):
				print(r)
			# end if
			break
		# end if
	# end for

	if (verbose):
		print('subdomain id: ' + str(subId))
	# end if

	if (subId < 0):
		ret = inwx_conn.nameserver.createRecord({'domain': domain, 'name': sub, 'type': args.record_type, 'content': str(args.content), 'ttl': args.ttl})
	else:
		ret = inwx_conn.nameserver.updateRecord({'id': subId, 'type': args.record_type, 'content': str(args.content), 'ttl': args.ttl})
	# end if

	if (verbose):
		print('update/create: ' + str(ret))
	# end if

# end main


if __name__ == '__main__':
	parser = argparse.ArgumentParser(description='inwx subdomain update')

	parser.add_argument('domain', metavar='domain', help='full domain like subdomain.example.com')
	parser.add_argument('content', metavar='content', help='ip or string to fill into sub domain entry')
	parser.add_argument('-r', '--record-type', metavar='type', required=True, choices=['A', 'AAAA', 'TXT', 'CNAME'], help='record type (A, AAAA, TXT, CNAME)')

	parser.add_argument('-c', '--config_file', metavar='path', default='./conf.cfg', help='path to configuration file')
	parser.add_argument('-t', '--ttl', default=3600, type=int, help='TTL (time to live) of the nameserver record in seconds (default 3600)')
	parser.add_argument('-s', '--config_section', metavar='section', default='ote', choices=['live', 'ote'], help='configuration section (live, ote) default: ote')
	parser.add_argument('-l', '--language', metavar='lang', default='en', choices=['de', 'en'], help='answer language (de, en)')
	parser.add_argument('-v', '--verbose', action='store_true', help='verbose')
	parser.add_argument('-d', '--debug', action='store_true', help='debug')

	args = parser.parse_args()

	if (args.debug):
		main(args)
	else:
		try:
			main(args)
		except NameError as e:
			print(e.args[0])
		# end try
	# end if

# end if