def _update_records(self, zone, name, rtype, ttl, contents): assert isinstance(contents, list) zone = self._encode_name(zone) name = self._encode_name(name) contents = [self._encode_content(rtype, c) for c in contents] rrset = powerdns.RRSet(name, rtype, contents, ttl) zone = self._server.get_zone(zone) zone.create_records([rrset])
def add_rand_records(api, zonename, num_records): logging.error("Entering add rand records!") zone = api.servers[0].get_zone(zonename) logging.error(zone.details) records = [] for i in range(num_records): r = rand_string() ip = rand_ipv4() zone.create_records([powerdns.RRSet(r, 'A', [(ip, False)])])
def add_record(dns_names: Sequence[str], host_ips: Sequence[str], dns_domain: str = None): records = [] for dns_name in dns_names: domain = extract_domain(dns_name) if dns_domain is None else dns_domain zone = get_or_create_zone(domain) logger.info(f"Add/update records for {dns_name} ({host_ips})") if dns_name.endswith(".") == False: dns_name += "." records.append(powerdns.RRSet(dns_name, 'A', host_ips)) return zone.create_records(records)
def add_rand_zone(api): tlds = ["com", "org", "net", "se"] tld = random.choice(tlds) domain = rand_string() full_name = f"{domain}.{tld}." serial = str(date.today().strftime("%Y%m%d00")) soa = f"ns0.{full_name} admin.{full_name} {serial} 28800 7200 604800 86400" soa_name = f"test.python-powerdns.{full_name}" soa_r = powerdns.RRSet(name=full_name, rtype="SOA", records=[(soa, False)], ttl=86400) try: zone = api.servers[0].create_zone(name=full_name, kind="Native", rrsets=[soa_r], nameservers=[f"ns1.{full_name}", f"ns2.{full_name}"]) except: log.error("Error in api connection") log.error(sys.exc_info()[0]) log.info(f"{zone},{str(zone.details)}")
def add(self, name, rtype, values, ttl=3600): """Add a new record to the zone. :param name: label name :type name: str :param rtype: type of record :type rtype: str :param values: values of label :type values: list :param ttl: time to live :type ttl: int """ if name.endswith('.'): extended_name = name else: extended_name = '{name}.'.format(name=name) logging.info( 'Adding record: %-30s %3s %20s %4d', extended_name, rtype, values, ttl) items = [(item, False) for item in values] rrset = powerdns.RRSet(extended_name, rtype, items, ttl=ttl) self.zone.create_records([rrset])
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
def _handle_message(self, body): #log.debug(body) jbody = json.loads(body["oslo.message"]) event_type = jbody["event_type"] log.debug(event_type) if event_type == EVENT_CREATE or event_type == EVENT_DELETE: instancename = jbody["payload"]["hostname"] if FQDN(str(instancename)).is_valid: log.debug('Instancename is a valide FQDN') fqdn = instancename + '.' zone = self.suggested_zone(fqdn) hostname = fqdn.replace(zone.name, "")[:-1] if event_type == EVENT_CREATE: fixed_ips0 = jbody["payload"]["fixed_ips"][0]["address"] fixed_ips1 = jbody["payload"]["fixed_ips"][1]["address"] ips0 = ipaddress.ip_address(fixed_ips0) if ips0.version == 4: ipv4addr = fixed_ips0 ipv6addr = fixed_ips1 else: ipv4addr = fixed_ips1 ipv6addr = fixed_ips0 log.info("Adding {} {} {}".format(instancename, ipv4addr, ipv6addr)) fq_ptr_v4 = self.get_reverse_pointer(ipv4addr) fq_ptr_v6 = self.get_reverse_pointer(ipv6addr) ptr_v4_zone = self.suggested_zone(fq_ptr_v4) ptr_v6_zone = self.suggested_zone(fq_ptr_v6) ptr_v4_name = fq_ptr_v4.replace(ptr_v4_zone.name, "")[:-1] ptr_v6_name = fq_ptr_v6.replace(ptr_v6_zone.name, "")[:-1] if '.dev.' in fqdn: ttl = 300 else: ttl = 3600 zone.create_records([ powerdns.RRSet(hostname, 'A', [(ipv4addr, False)], ttl), powerdns.RRSet(hostname, 'AAAA', [(ipv6addr, False)], ttl), ]) ptr_v4_zone.create_records([ powerdns.RRSet(ptr_v4_name, 'PTR', [(instancename + '.', False)], ttl) ]) ptr_v6_zone.create_records([ powerdns.RRSet(ptr_v6_name, 'PTR', [(instancename + '.', False)], ttl) ]) if event_type == EVENT_DELETE: log.info("Deleting {}".format(instancename)) result = api.servers[0].search(instancename) i = 0 while i < len(result): if result[i]['type'] == 'A' or result[i]['type'] == 'AAAA': fq_ptr = self.get_reverse_pointer(result[i]['content']) ptr_zone = self.suggested_zone(fq_ptr) ptr_name = fq_ptr.replace(ptr_zone.name, "")[:-1] log.info("Deleting {}".format(ptr_name)) ptr_zone.delete_records([ powerdns.RRSet(ptr_name, 'PTR', []) ]) i += 1 zone.delete_records([ powerdns.RRSet(hostname, 'A', []), powerdns.RRSet(hostname, 'AAAA', []), ])