diff --git a/.gitmodules b/.gitmodules index 9854add..a52a791 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "inwx"] path = inwx - url = https://github.com/inwx/python2.7-client.git + url = https://github.com/inwx/python-client.git diff --git a/README.md b/README.md index 0273ea3..00b051b 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,6 @@ A file `conf.cfg` with inwx settings must e exist in the repository root folder like: ``` [live] -url = https://api.domrobot.com/xmlrpc/ username = [USERNAME] password = [PASSWORD] shared_secret = your_shared_secret diff --git a/configuration.py b/configuration.py new file mode 100644 index 0000000..0b979ea --- /dev/null +++ b/configuration.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# -*- encoding: UTF8 -*- + +# author: Philipp Klaus, philipp.klaus →AT→ gmail.com +# modified by Pascal Gollor, pascal@pgollor.de since 2021-03-19 + +# This file is part of python-inwx-xmlrpc. +# +# python-inwx-xmlrpc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# python-inwx-xmlrpc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with python-inwx-xmlrpc. If not, see . + +########################################################################### +###### This file helps to handle the INI-style .cfg files. ###### +###### You only need it if you want to test the example script. ###### +###### It is not needed if you just want to use the inwx class. ###### + +import sys + +if sys.version_info.major == 3: + import configparser +else: + import ConfigParser as configparser + +from os.path import expanduser + +def get_account_data(print_errors = False, config_file = 'conf.cfg', config_section = 'ote'): + """ + boolean print_errors: Print errors to stdout instead of raising an exception. + string config_file: The name of the configuration file. + string config_section: The name of the section to read in the configuration file. + """ + config = open_config_file(print_errors, config_file) + try: + username = config.get(config_section, 'username') + password = config.get(config_section, 'password') + shared_secret = config.get(config_section, 'shared_secret') + except Exception as err: + message = 'Error: Please make sure your config file %s contains the section %s with the entries "username", "password" and "shared_secret".' % (config_file, config_section) + if print_errors: + print(message) + sys.exit(2) + else: + raise NameError(message) + return (username, password, shared_secret) + +def open_config_file(print_errors, config_file): + config = configparser.ConfigParser() + try: + if config.read(config_file) == []: raise Exception() + except: + message = "Error: Please make sure you adopted the config file: %s" % config_file + if print_errors: + print(message) + sys.exit(2) + else: + raise NameError(message) + return config diff --git a/update-record.py b/update-record.py index f4a7c91..89ec598 100755 --- a/update-record.py +++ b/update-record.py @@ -1,12 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- encoding: UTF8 -*- # author: Pascal Gollor (https://gitbucket.pgollor.de) -# author: InterNetworX, info →AT→ inwx.de -from inwx.inwx import domrobot#, prettyprint -from inwx.configuration import get_account_data +from INWX.Domrobot import ApiClient +from configuration import get_account_data import argparse @@ -18,7 +17,6 @@ h = domain.split('.') if (len(h) < 3): raise ValueError("invalid subdomain") - # end if sub = h[0] for i in range(1, len(h)-2): sub += '.' + h[i] @@ -26,29 +24,37 @@ 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}) + # get config and login + username, password, shared_secret = get_account_data(True, config_file=args.config_file, config_section=args.config_section) + debug_mode = False + api_url = ApiClient.API_LIVE_URL + if (args.config_section == "ote"): + debug_mode = True + api_url = ApiClient.API_OTE_URL + api_client = ApiClient(api_url=api_url, debug_mode=debug_mode) + ret = api_client.login(username, password) + if (ret['code'] != 1000): + raise RuntimeError('Api login error. Code: ' + str(ret['code']) + ' Message: ' + ret['msg']) if (verbose): - print("login: " + str(ret)) - # end if + print("login: ", ret) # check domain - ret = inwx_conn.nameserver.list({'domain': domain}) + ret = api_client.call_api(api_method='nameserver.list', method_params={'domain': domain}) if (verbose): - print('list: ' + str(ret)) - # end if + print('list: ', ret) + if (ret['code'] != 1000): + raise RuntimeError('Api call error. Code: ' + str(ret['code']) + ' Message: ' + ret['msg']) 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}) + ret = api_client.call_api(api_method='nameserver.info', method_params={'domain': domain}) + if (ret['code'] != 1000): + raise RuntimeError('Api call error. Code: ' + str(ret['code']) + ' Message: ' + ret['msg']) records = ret['resData']['record'] for r in records: @@ -56,7 +62,6 @@ subId = r['id'] if (verbose): print(r) - # end if break # end if # end for @@ -64,26 +69,31 @@ if (args.delete): if (subId >= 0): if (verbose): - print ("delete id: " + str(subId)) - # end if - ret = inwx_conn.nameserver.deleteRecord({'id': subId}) + print ("delete id: ", subId) + ret = api_client.call_api(api_method='nameserver.deleteRecord', method_params={'id': subId}) + if (ret['code'] != 1000): + raise RuntimeError('Api call error. Code: ' + str(ret['code']) + ' Message: ' + ret['msg']) else: ret = None # end if else: if (not args.update or subId < 0): - ret = inwx_conn.nameserver.createRecord({'domain': domain, 'name': sub, 'type': args.record_type, 'content': str(args.content), 'ttl': args.ttl}) + ret = api_client.call_api(api_method='nameserver.createRecord', method_params={'domain': domain, 'name': sub, 'type': args.record_type, 'content': str(args.content), 'ttl': args.ttl}) + if (ret['code'] != 1000): + raise RuntimeError('Api call error. Code: ' + str(ret['code']) + ' Message: ' + ret['msg']) if (verbose): - print('create: ' + str(ret)) - # end if + print('create: ', ret) else: - ret = inwx_conn.nameserver.updateRecord({'id': subId, 'type': args.record_type, 'content': str(args.content), 'ttl': args.ttl}) + ret = api_client.call_api(api_method='nameserver.updateRecord', method_params={'id': subId, 'type': args.record_type, 'content': str(args.content), 'ttl': args.ttl}) + if (ret['code'] != 1000): + raise RuntimeError('Api call error. Code: ' + str(ret['code']) + ' Message: ' + ret['msg']) if (verbose): - print('update: ' + str(ret)) - # end if + print('update: ', ret) # end if # end if + api_client.logout() + # end main @@ -118,6 +128,8 @@ main(args) except NameError as e: print(e.args[0]) + except RuntimeError as e: + print(e.args[0]) # end try # end if