Commit 34410d70 authored by Sigmund Augdal's avatar Sigmund Augdal

Refactor code to create a powerrest client based on values in a config file...

Refactor code to create a powerrest client based on values in a config file into a reusable function
Also fixed an issue in create_dns_record.py caused by earlier refactoring
parent fbbe7186
......@@ -47,14 +47,7 @@ def main():
ca_cert=conf.get('etcd', 'cacert'), protocol='https')
vmm_client = odata.VMMClient(conf.get('spf', 'vmm_baseurl'), conf.get('spf', 'username'),
conf.get('spf', 'password'))
pdns_url = conf.get('pdns', 'url')
pdns_username = None
if conf.has_option("pdns", "username"):
pdns_username = conf.get("pdns", "username")
pdns_password = None
if conf.has_option("pdns", "password"):
pdns_password = conf.get("pdns", "password")
dns = dns_rr.PowerRESTClient(pdns_url, pdns_username, pdns_password)
dns = dns_rr.client_from_conf(conf)
nics = set()
for nic in vmm_client.query_collection("VirtualNetworkAdapters"):
......
......@@ -27,6 +27,17 @@ def reverse_dns6(ipv6_string):
return r + suffix
def client_from_conf(conf):
pdns_url = conf.get('pdns', 'url')
pdns_username = None
if conf.has_option("pdns", "username"):
pdns_username = conf.get("pdns", "username")
pdns_password = None
if conf.has_option("pdns", "password"):
pdns_password = conf.get("pdns", "password")
return PowerRESTClient(pdns_url, pdns_username, pdns_password)
class PowerRESTClient(object):
def __init__(self, pdns_url, username=None, password=None):
self.pdns_url = pdns_url
......
......@@ -2,14 +2,18 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import argparse
from nova_router import dns_rr
try:
from configparser import SafeConfigParser # pylint: disable=F0401
except ImportError:
from ConfigParser import SafeConfigParser # pylint: disable=F0401
DESCRIPTION = "Add a DNS record"
def parse_args():
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument('--url', default="http://srv-pdns.bs.unsi.no:9090/v1",
help="PowerREST url to use")
parser.add_argument('--config', default="nova.cfg",
help="Config file to use")
parser.add_argument('--ttl', default='1800', help="Time to live for record")
parser.add_argument('record', help="Name of record to create")
parser.add_argument('type', help="Record type to create")
......@@ -20,13 +24,16 @@ def parse_args():
def main(args):
domain = args.record.split(".", 1)[1]
domain_id = dns_rr.get_domain_id(args.url, domain)
conf = SafeConfigParser()
conf.read(args.config)
dns = dns_rr.client_from_conf(conf)
domain_id = dns.get_domain_id(domain)
if not domain_id:
print("Unknown domain {}".format(domain))
return
record = dict(name=args.record, type=args.type, content=args.content, domain_id=domain_id,
ttl=int(args.ttl))
if not dns_rr.PowerRESTClient(args.url).create_record(record):
if not dns.create_record(record):
print("Failed to create record")
print("Created record in domain id {}".format(domain_id))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment