def get_domain_fromname(name, firsttry=True): """A method to utilize the PDNS Admin of domains to determine which this is in.""" name_split = name.split('.') name_split.reverse() test = '' testlst = [] for item in name_split: if item != '': if test == '': test = "%s" % (item) else: test = "%s.%s" % (item, test) testlst.append(test) testlst.reverse() print pformat(testlst) retval = None for test in testlst: if not retval: # show("get_domain_fromname of testing if string is a domain : '%s'" % (test), level=6) mdl = db.session.query(Domain)\ .filter(Domain.name == test)\ .first() if mdl: retval = mdl.name if not retval and firsttry: dom_ = Domain() dom_.update() retval = get_domain_fromname(name, firsttry=False) # raise RuntimeError("Issue getting domain name from name %s" % (name)) return retval
def api_login_delete_zone(domain_name): pdns_api_url = Setting().get('pdns_api_url') pdns_api_key = Setting().get('pdns_api_key') pdns_version = Setting().get('pdns_version') api_uri_with_prefix = utils.pdns_api_extended_uri(pdns_version) api_full_uri = api_uri_with_prefix + '/servers/localhost/zones' api_full_uri += '/' + domain_name headers = {} headers['X-API-Key'] = pdns_api_key domain = Domain.query.filter(Domain.name == domain_name) if not domain: abort(404) if g.user.role.name not in ['Administrator', 'Operator']: user_domains_obj_list = g.user.get_domains() user_domains_list = [item.name for item in user_domains_obj_list] if domain_name not in user_domains_list: raise DomainAccessForbidden() msg_str = "Sending request to powerdns API {0}" logging.debug(msg_str.format(domain_name)) try: resp = utils.fetch_remote( urljoin(pdns_api_url, api_full_uri), method='DELETE', headers=headers, accept='application/json; q=1' ) if resp.status_code == 204: logging.debug("Request to powerdns API successful") history = History( msg='Delete domain {0}'.format(domain_name), detail='', created_by=g.user.username ) history.add() domain = Domain() domain.update() except Exception as e: logging.error('Error: {0}'.format(e)) abort(500) return resp.content, resp.status_code, resp.headers.items()
def api_login_create_zone(): pdns_api_url = Setting().get('pdns_api_url') pdns_api_key = Setting().get('pdns_api_key') pdns_version = Setting().get('pdns_version') api_uri_with_prefix = utils.pdns_api_extended_uri(pdns_version) api_full_uri = api_uri_with_prefix + '/servers/localhost/zones' headers = {} headers['X-API-Key'] = pdns_api_key msg_str = "Sending request to powerdns API {0}" msg = msg_str.format(request.get_json(force=True)) logging.debug(msg) resp = utils.fetch_remote( urljoin(pdns_api_url, api_full_uri), method='POST', data=request.get_json(force=True), headers=headers, accept='application/json; q=1' ) if resp.status_code == 201: logging.debug("Request to powerdns API successful") data = request.get_json(force=True) history = History( msg='Add domain {0}'.format(data['name'].rstrip('.')), detail=json.dumps(data), created_by=g.user.username ) history.add() if g.user.role.name not in ['Administrator', 'Operator']: logging.debug("User is ordinary user, assigning created domain") domain = Domain(name=data['name'].rstrip('.')) domain.update() domain.grant_privileges([g.user.username]) domain = Domain() domain.update() return resp.content, resp.status_code, resp.headers.items()
def api_create_zone(server_id): resp = helper.forward_request() if resp.status_code == 201: logging.debug("Request to powerdns API successful") data = request.get_json(force=True) history = History(msg='Add domain {0}'.format( data['name'].rstrip('.')), detail=json.dumps(data), created_by=g.apikey.description) history.add() if g.apikey.role.name not in ['Administrator', 'Operator']: logging.debug("Apikey is user key, assigning created domain") domain = Domain(name=data['name'].rstrip('.')) g.apikey.domains.append(domain) domain = Domain() domain.update() return resp.content, resp.status_code, resp.headers.items()
def sync_domains(): domain = Domain() domain.update() return 'Finished synchronization in background', 200
def api_zone_forward(server_id, zone_id): resp = helper.forward_request() domain = Domain() domain.update() return resp.content, resp.status_code, resp.headers.items()