def run_module(): module_args = dict(name=dict(type='str', required=True), kind=dict(type='str', required=False, choices=['Native', 'Master', 'Slave'], default='Master'), masters=dict(type='list', required=False, default=[]), dnssec=dict(type='bool', required=False, default=True), api_rectify=dict(type='bool', required=False, default=True), state=dict(type='str', required=False, choices=['present', 'absent'], default='present'), pdns_api_url=dict( type='str', required=False, default='http://127.0.0.1:8001/api/v1'), pdns_api_key=dict(type='str', required=True), strict_ssl_checking=dict(type='bool', required=False, default=True), server_id=dict(type='int', required=False, default=0)) module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) result = dict(changed=False, reponse='') api_client = powerdns.PDNSApiClient( api_endpoint=module.params['pdns_api_url'], api_key=module.params['pdns_api_key'], verify=module.params['strict_ssl_checking']) api = powerdns.PDNSEndpoint(api_client) api_server = api.servers[module.params['server_id']] zone = api_server.get_zone(module.params['name']) if module.params['state'] == 'present' and zone == None: changed = True elif module.params['state'] == 'present' and zone != None: changed = not ( zone.details['kind'] == module.params['kind'] and set(zone.details['masters']) == set(module.params['masters']) and zone.details['dnssec'] == module.params['dnssec'] and zone.details['api_rectify'] == module.params['api_rectify']) else: changed = zone != None if module.check_mode: return result if module.params['state'] == 'present' and changed == True: api_server.delete_zone(module.params['name']) # *sigh* api_server.create_zone(name=module.params['name'], kind=module.params['kind'], nameservers=[], masters=module.params['masters']) elif module.params['state'] == 'absent' and zone != None: api_server.delete_zone(module.params['name']) module.exit_json(**result)
def main(numzones, numworkers): PDNS_API = "http://172.20.0.4:8081/api/v1" PDNS_KEY = "foobar" api_client = powerdns.PDNSApiClient(api_endpoint=PDNS_API, api_key=PDNS_KEY) api = powerdns.PDNSEndpoint(api_client) with concurrent.futures.ThreadPoolExecutor(max_workers=numworkers) as executor: for i in range(numzones): executor.submit(add_rand_zone, api)
def _handle_message(self, body): jbody = json.loads(body["oslo.message"]) event_type = jbody["event_type"] log.info("Event type: {}".format(event_type)) project = jbody["_context_project_name"] project_id = jbody["_context_project_id"] hostaddr_internal = "" if event_type in [EVENT_CREATE, EVENT_DELETE, EVENT_IP_UPDATE]: log.info("Have changes for project {}".format(project)) api_client = powerdns.PDNSApiClient(api_endpoint=PDNS_API, api_key=PDNS_KEY) api = powerdns.PDNSEndpoint(api_client) internal_zone = api.servers[0].get_zone( '{}.internal.dev.seeburger.de.'.format(project)) external_zone = api.servers[0].get_zone( '{}.dev.seeburger.de.'.format(project)) if event_type == EVENT_CREATE: server_id = jbody["payload"]["instance_id"] hostname = jbody["payload"]["hostname"] hostaddr = jbody["payload"]["fixed_ips"][0]["address"] log.info( "Adding {}.{}.internal.seeburger.de. \tA \t{} \t{}".format( hostname, project, TTL, hostaddr)) user = jbody["_context_user_name"] user_id = jbody["_context_user_id"] # server_id = jbody["payload"]["instance_id"] log.debug( "Instance ID: {}, User: {}, User ID: {}, Project: {}, Project ID: {}" .format(server_id, user, user_id, project, project_id)) server = self.nova.servers.get(server_id) try: self.nova.servers.set_meta_item(server, "project", project) self.nova.servers.set_meta_item(server, "project_id", project_id) self.nova.servers.set_meta_item(server, "user", user) self.nova.servers.set_meta_item(server, "user_id", user_id) self.nova.servers.set_meta_item(server, "ip", hostaddr) self.nova.servers.set_meta_item(server, "hostname", hostname) except Exception as e: log.warn("Exception {} thrown".format(e)) # Delete old A records, which may have existed and create a new one in the # Zone of the internal and externat domain internal_zone.delete_record( [powerdns.RRSet(hostname, 'A', [])]) external_zone.delete_record( [powerdns.RRSet(hostname, 'A', [])]) internal_zone.create_records( [powerdns.RRSet(hostname, 'A', [(hostaddr, False)], TTL)]) elif event_type == EVENT_DELETE and SKIP_DELETE: log.info( "Delete request received, but SKIP_DELETE is set to {}, so skipping" .format(SKIP_DELETE)) elif event_type == EVENT_DELETE and not SKIP_DELETE: server_id = jbody["payload"]["instance_id"] hostname = jbody["payload"]["hostname"] hostaddr = "" log.info("Deleting {}.{}[.internal].seeburger.de".format( hostname, project)) # As the instance vanished, delete all remaining know A records. internal_zone.delete_record( [powerdns.RRSet(hostname, 'A', [])]) external_zone.delete_record( [powerdns.RRSet(hostname, 'A', [])]) elif event_type == EVENT_IP_UPDATE: hostaddr = jbody["payload"]["floatingip"][ "floating_ip_address"] ip = jbody["payload"]["floatingip"]["fixed_ip_address"] log.info("Hostaddr {}".format(hostaddr)) log.info("Ip {}".format(ip)) if ip == None: log.info( "Disaccotiated floating ip {}. Do nothing for now...". format(hostaddr)) return if not hostaddr.isspace(): server = self.get_server_for_ip(ip, project_id) hostaddr_internal = ip hostname = server.name #nsupdate_script = NSUPDATE_ADD_PUBLIC log.info("Adding records for hostname {}".format(hostname)) # Add or update A records fir internal and external domain. log.info("Adding address...") external_zone.create_records([ powerdns.RRSet(hostname, 'A', [(hostaddr, False)], TTL) ]) internal_zone.create_records([ powerdns.RRSet(hostname, 'A', [(hostaddr_internal, False)], TTL) ]) log.info("Added {}.{}.dev.seeburger.de {}".format( hostname, project, hostaddr)) else: log.error( "Unknown event type: {} - Do nothing".format(event_type)) return
import powerdns pdnsclient = powerdns.PDNSApiClient( **{ "api_key": "changeme", "api_endpoint": "http://localhost:7080/api/v1" }) pdnsextern = powerdns.PDNSEndpoint(pdnsclient) zone_list = pdnsextern.get_server("localhost").get_zone_list() iagle_net_zone = zone_list[0] #iagle_net_zone.set("type", "Zone") print(iagle_net_zone.get_rrset_list()[3].get("name")) # iagle_net_zone.get_rrset_list()[3].delete() iagle_net_zone.save()
def _init_powerdns(self): self.api_client = powerdns.PDNSApiClient(api_endpoint=self.api_url, api_key=self.api_key, timeout=self.timeout) self._api = powerdns.PDNSEndpoint(self.api_client)
import os import logging from typing import Sequence import powerdns import pdns_updater.logger as logging logger = logging.getLogger(__name__) PDNS_API_URL = os.getenv("PDNS_API_URL", "http://localhost:8081/api/v1") PDNS_API_KEY = os.getenv("PDNS_API_KEY", "changeme") api_client = powerdns.PDNSApiClient(api_endpoint=PDNS_API_URL, api_key=PDNS_API_KEY) api = powerdns.PDNSEndpoint(api_client) def get_or_create_zone(zone_domain): if zone_domain.endswith(".") == False: zone_domain += "." zone = api.servers[0].get_zone(zone_domain) if zone is None: logger.info(f"Will create zone: {zone_domain}") zone_nameservers = [ "ns1." + zone_domain, ]
def run(self, timeout): super(PowerDNSClient, self).run() self.api_client = powerdns.PDNSApiClient(api_endpoint=self.api_url, api_key=self.api_key, timeout=timeout) self.api = powerdns.PDNSEndpoint(self.api_client)
def __init__(self, api, key, zone): self.api_client = powerdns.PDNSApiClient(api_endpoint=api, api_key=key) self.api = powerdns.PDNSEndpoint(self.api_client) self.zone_name = zone self.zone = self.api.servers[0].get_zone(zone)
def __init__(self): api_client = powerdns.PDNSApiClient(api_endpoint=settings.PDNS_APIURL, api_key=settings.PDNS_APIKEY) self.api = powerdns.PDNSEndpoint(api_client)