Created
July 22, 2020 12:55
-
-
Save TruePack/b42e213875f8ff7244470d7915a7ef2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import json | |
import urllib | |
import logging | |
from http.client import HTTPSConnection | |
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 = 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(f'Response => {response.status} {response.reason}\n{body}') | |
except Exception as exc: | |
logger.error(f'Error while accessing {DOMAINS_API_HOST} => {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