Skip to content

Instantly share code, notes, and snippets.

Last active October 4, 2019 09:56
Show Gist options
  • Save clickfreak/d810af912daf3385f1b7 to your computer and use it in GitHub Desktop.
Save clickfreak/d810af912daf3385f1b7 to your computer and use it in GitHub Desktop.
Import bind-zone file to Selectel DNS via API
usage: [-h] --key <key> --name <name> --zone <zone>
Selectel domains-api bind-zone import helper
optional arguments:
-h, --help show this help message and exit
required named arguments:
--key <key> Your API Key
--name <name> Domain name for creation
--zone <zone> Path to bind-zone file for import
Result JSON will be printed to stdout
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import json
import urllib
import logging
import httplib
import argparse
DOMAINS_API_URL = '/domains/v1/'
logger = logging.getLogger(__name__)
def main():
parser = argparse.ArgumentParser(
description='Selectel domains-api bind-zone import helper',
epilog='Result JSON will be printed to stdout'
required = parser.add_argument_group('required named arguments')
required.add_argument('--key', metavar='<key>', dest='key',
help='Your API Key', required=True)
required.add_argument('--name', metavar='<name>', dest='name',
help='Domain name for creation', required=True)
required.add_argument('--zone', metavar='<zone>', dest='zone',
help='Path to bind-zone file for import', required=True)
if len(sys.argv) == 1:
args = parser.parse_args()
if not os.path.exists( or not os.access(, os.R_OK):
logger.error('Zone file does not exist or not accessible')
zone = open(
params = json.dumps({'name':, 'bind_zone': zone})
headers = {'Content-type': 'application/json', 'X-Token': args.key}
conn = httplib.HTTPSConnection(DOMAINS_API_HOST, 443)
conn.request('POST', DOMAINS_API_URL, params, headers)
response = conn.getresponse()
body =
body = json.loads(body)
if isinstance(body, dict):
body = json.dumps(body, indent=4)'Response => {0} {1}\n{2}'.format(response.status, response.reason, body))
except Exception as exc:
logger.error('Error while accessing {0} => {1}'.format(DOMAINS_API_HOST, str(exc)))
if __name__ == '__main__':
format='[%(asctime)s] [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment