Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Import bind-zone file to Selectel DNS via API
usage: bind_upload.py [-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_HOST = 'api.selectel.ru'
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:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
if not os.path.exists(args.zone) or not os.access(args.zone, os.R_OK):
logger.error('Zone file does not exist or not accessible')
sys.exit(1)
zone = open(args.zone).read()
params = json.dumps({'name': args.name, 'bind_zone': zone})
headers = {'Content-type': 'application/json', 'X-Token': args.key}
try:
conn = httplib.HTTPSConnection(DOMAINS_API_HOST, 443)
conn.request('POST', DOMAINS_API_URL, params, headers)
response = conn.getresponse()
body = response.read()
try:
body = json.loads(body)
except:
pass
if isinstance(body, dict):
body = json.dumps(body, indent=4)
logger.info('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__':
logging.basicConfig(
level=logging.DEBUG,
format='[%(asctime)s] [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.