def post(self): if self.auth.account.account_type != "senior_admin": abort(403, message="Insufficient privileges to create a location") location = request.form.get('location', None) location_len = len(location) if location_len < 1 or location_len > 2: abort( 400, message="location parameter must be 1 or two characters" ) try: existing_location = Location.get( Location.location == location ) abort(400, message="Location already in use") except peewee.DoesNotExist: pass location_description = request.form.get('location_description', None) new_location = Location() new_location.location = location new_location.location_description = location_description new_location.save() return {'status': 'ok', 'location': new_location.to_dict()}, 201
def post(self): if self.auth.account.account_type != "senior_admin": abort(403, message="Insufficient privileges to create a location") location = request.form.get('location', None) location_len = len(location) if location_len < 1 or location_len > 2: abort(400, message="location parameter must be 1 or two characters") try: existing_location = Location.get(Location.location == location) abort(400, message="Location already in use") except peewee.DoesNotExist: pass location_description = request.form.get('location_description', None) new_location = Location() new_location.location = location new_location.location_description = location_description new_location.save() # notify listeners of dns data change self.send_update_notification() return {'status': 'ok', 'location': new_location.to_dict()}, 201
def put(self, location_id): if self.auth.account.account_type != "senior_admin": abort(403, message="Insufficient privileges to modify a location") try: locationdb = ModelLocation.get( ModelLocation.location_id == location_id ) except peewee.DoesNotExist: abort(404, message="location not found") location = request.form.get('location', None) if location is not None: location_len = len(location) if (location_len < 1 or location_len > 2): abort( 400, message="location parameter must be 1 or two characters" ) location_description = request.form.get('location_description', None) # gotta supply something if location_description is None and location is None: abort( 400, message="You must supply location and or location_description" ) if location is not None: locationdb.location = location # check for existing duplicate try: existing_location = ModelLocation.get( ModelLocation.location == location, ModelLocation.location_id != locationdb.location_id ) abort(400, message="Location already in use") except peewee.DoesNotExist: pass if location_description is not None: locationdb.location_description = location_description locationdb.save() return {'status': 'ok', 'location': locationdb.to_dict()}
def get(self, location_id): try: location = ModelLocation.get( ModelLocation.location_id == location_id) except peewee.DoesNotExist: abort(404, message="location not found") return {'status': 'ok', 'location': location.to_dict()}
def request_form_to_type_model(self, request_form, TypeModel, domain): # switch on type for now common_types = [ "A", "A+PTR", "CNAME", "NS", "TXT", "PTR", "AAAA", "AAAA+PTR", "SPF" ] if TypeModel.record_type in common_types: if domain is not None: self.check_domain_suffix(domain.domain) TypeModel.values["name"] = request_form.get("name") TypeModel.values["value"] = request_form.get("value") TypeModel.values["ttl"] = request_form.get("ttl", 3600) elif TypeModel.record_type == "MX": if domain is not None: self.check_domain_suffix(domain.domain) TypeModel.values["name"] = request_form.get("name") TypeModel.values["value"] = request_form.get("value") TypeModel.values["distance"] = request_form.get("distance", 0) TypeModel.values["ttl"] = request_form.get("ttl", 3600) elif TypeModel.record_type == "SRV": if domain is not None: self.check_domain_suffix(domain.domain) TypeModel.values["name"] = request_form.get("name") TypeModel.values["value"] = request_form.get("value") TypeModel.values["distance"] = request_form.get("distance", 0) TypeModel.values["weight"] = request_form.get("weight") TypeModel.values["port"] = request_form.get("port") TypeModel.values["ttl"] = request_form.get("ttl", 3600) elif TypeModel.record_type == "SOA": TypeModel.values["email"] = request_form.get("email") TypeModel.values["nameserver"] = request_form.get("nameserver") TypeModel.values["ttl"] = request_form.get("ttl", 86400) TypeModel.values["refresh"] = request_form.get("refresh", 16374) TypeModel.values["retry"] = request_form.get("retry", 2048) TypeModel.values["expire"] = request_form.get("expire", 1048576) TypeModel.values["minimum"] = request_form.get("minimum", 2560) TypeModel.values["serial"] = request_form.get("serial", "") else: raise Exception("Unsupported record type") if TypeModel.record_type != "SOA": # Add location_id for non SOA records location_id = request_form.get("location_id", None) if location_id is not None and str(location_id).isdigit(): try: locationdb = ModelLocation.get( ModelLocation.location_id == location_id) except peewee.DoesNotExist: abort(400, message="location_id does not exist") # Map null/none to None type if str(location_id).lower() in ["null", "none", ""]: location_id = None TypeModel.values["location_id"] = location_id
def put(self, location_id): if self.auth.account.account_type != "senior_admin": abort(403, message="Insufficient privileges to modify a location") try: locationdb = ModelLocation.get( ModelLocation.location_id == location_id) except peewee.DoesNotExist: abort(404, message="location not found") location = request.form.get('location', None) if location is not None: location_len = len(location) if (location_len < 1 or location_len > 2): abort(400, message="location parameter must be 1 or two characters") location_description = request.form.get('location_description', None) # gotta supply something if location_description is None and location is None: abort( 400, message="You must supply location and or location_description") if location is not None: locationdb.location = location # check for existing duplicate try: existing_location = ModelLocation.get( ModelLocation.location == location, ModelLocation.location_id != locationdb.location_id) abort(400, message="Location already in use") except peewee.DoesNotExist: pass if location_description is not None: locationdb.location_description = location_description locationdb.save() # notify listeners of dns data change self.send_update_notification() return {'status': 'ok', 'location': locationdb.to_dict()}
def get(self, location_id): try: location = ModelLocation.get( ModelLocation.location_id == location_id ) except peewee.DoesNotExist: abort(404, message="location not found") return {'status': 'ok', 'location': location.to_dict()}
def get(self, prefix_id): try: prefixdb = ModelPrefix.get(ModelPrefix.prefix_id == prefix_id) location = ModelLocation.get( ModelLocation.location_id == prefixdb.location_id) except peewee.DoesNotExist: abort(404, message="prefix not found") return { 'status': 'ok', 'location_prefix': prefixdb.to_dict(), 'location': location.to_dict() }
def get(self, prefix_id): try: prefixdb = ModelPrefix.get(ModelPrefix.prefix_id == prefix_id) location = ModelLocation.get( ModelLocation.location_id == prefixdb.location_id ) except peewee.DoesNotExist: abort(404, message="prefix not found") return { 'status': 'ok', 'location_prefix': prefixdb.to_dict(), 'location': location.to_dict() }
def post(self): if self.auth.account.account_type != "senior_admin": abort( 403, message="Insufficient privileges to create a location prefix" ) location_id = self.get_location_id() # make sure the location exists try: locationdb = Location.get(Location.location_id == location_id) except peewee.DoesNotExist: abort(400, message="location_id does not exist") prefix_type = str(request.form.get('prefix_type', 'ipv4')).lower() if prefix_type not in ['ipv4', 'ipv6']: abort(400, message='prefix_type must be either ipv4 or ipv6') prefix = request.form.get('prefix', None) if prefix is None or not Validate().ip_prefix(prefix, prefix_type): abort( 400, message='invalid prefix for prefix_type ' + prefix_type ) prefix_description = request.form.get('prefix_description', None) new_prefix = LocationPrefix() new_prefix.location_id = location_id new_prefix.prefix = prefix new_prefix.prefix_type = prefix_type if prefix_description is not None: new_prefix.prefix_description = prefix_description try: new_prefix.save() except peewee.IntegrityError as e: abort( 400, message=str(e) ) # notify listeners of dns data change self.send_update_notification() return {'status': 'ok', 'location_prefix': new_prefix.to_dict()}
def delete(self, location_id): if self.auth.account.account_type != "senior_admin": abort(403, message="Insufficient privileges to delete a location") try: locationdb = ModelLocation.get( ModelLocation.location_id == location_id) except peewee.DoesNotExist: abort(404, message="location not found") locationdb.delete_prefixes() # update records that have this id to null (public) query = ModelRecord.update(location_id=None).where( ModelRecord.location_id == location_id) query.execute() locationdb.delete_instance() return {'status': 'ok'}
def post(self): if self.auth.account.account_type != "senior_admin": abort( 403, message="Insufficient privileges to create a location prefix" ) location_id = self.get_location_id() # make sure the location exists try: locationdb = Location.get(Location.location_id == location_id) except peewee.DoesNotExist: abort(400, message="location_id does not exist") prefix_type = str(request.form.get('prefix_type', 'ipv4')).lower() if prefix_type not in ['ipv4', 'ipv6']: abort(400, message='prefix_type must be either ipv4 or ipv6') prefix = request.form.get('prefix', None) if prefix is None or not Validate().ip_prefix(prefix, prefix_type): abort( 400, message='invalid prefix for prefix_type ' + prefix_type ) prefix_description = request.form.get('prefix_description', None) new_prefix = LocationPrefix() new_prefix.location_id = location_id new_prefix.prefix = prefix new_prefix.prefix_type = prefix_type if prefix_description is not None: new_prefix.prefix_description = prefix_description try: new_prefix.save() except peewee.IntegrityError as e: abort( 400, message=str(e) ) return {'status': 'ok', 'location_prefix': new_prefix.to_dict()}
def get(self): location_id = self.get_location_id() # make sure location exists try: location = Location.get(Location.location_id == location_id) except peewee.DoesNotExist: abort(404, message="location_id does not exist") query = LocationPrefix.select().where( LocationPrefix.location_id == location_id) prefixes = [] for prefix in query: prefixes.append(prefix.to_dict()) return { 'status': 'ok', 'location_prefixes': prefixes, 'location': location.to_dict() }
def delete(self, location_id): if self.auth.account.account_type != "senior_admin": abort(403, message="Insufficient privileges to delete a location") try: locationdb = ModelLocation.get( ModelLocation.location_id == location_id ) except peewee.DoesNotExist: abort(404, message="location not found") locationdb.delete_prefixes() # update records that have this id to null (public) query = ModelRecord.update(location_id=None).where( ModelRecord.location_id == location_id ) query.execute() locationdb.delete_instance() return {'status': 'ok'}
def get(self): location_id = self.get_location_id() # make sure location exists try: location = Location.get(Location.location_id == location_id) except peewee.DoesNotExist: abort(404, message="location_id does not exist") query = LocationPrefix.select().where( LocationPrefix.location_id == location_id ) prefixes = [] for prefix in query: prefixes.append(prefix.to_dict()) return { 'status': 'ok', 'location_prefixes': prefixes, 'location': location.to_dict() }
def request_form_to_type_model(self, request_form, TypeModel, domain): # switch on type for now common_types = [ "A", "A+PTR", "CNAME", "NS", "TXT", "PTR", "AAAA", "AAAA+PTR", "SPF" ] if TypeModel.record_type in common_types: if domain is not None: self.check_domain_suffix(domain.domain) TypeModel.values["name"] = request_form.get("name") TypeModel.values["value"] = request_form.get("value") TypeModel.values["ttl"] = request_form.get("ttl", 3600) elif TypeModel.record_type == "MX": if domain is not None: self.check_domain_suffix(domain.domain) TypeModel.values["name"] = request_form.get("name") TypeModel.values["value"] = request_form.get("value") TypeModel.values["distance"] = request_form.get("distance", 0) TypeModel.values["ttl"] = request_form.get("ttl", 3600) elif TypeModel.record_type == "SRV": if domain is not None: self.check_domain_suffix(domain.domain) TypeModel.values["name"] = request_form.get("name") TypeModel.values["value"] = request_form.get("value") TypeModel.values["distance"] = request_form.get("distance", 0) TypeModel.values["weight"] = request_form.get("weight") TypeModel.values["port"] = request_form.get("port") TypeModel.values["ttl"] = request_form.get("ttl", 3600) elif TypeModel.record_type == "SOA": TypeModel.values["email"] = request_form.get("email") TypeModel.values["nameserver"] = request_form.get("nameserver") TypeModel.values["ttl"] = request_form.get("ttl", 86400) TypeModel.values["refresh"] = request_form.get("refresh", 16374) TypeModel.values["retry"] = request_form.get("retry", 2048) TypeModel.values["expire"] = request_form.get("expire", 1048576) TypeModel.values["minimum"] = request_form.get("minimum", 2560) TypeModel.values["serial"] = request_form.get("serial", "") else: raise Exception("Unsupported record type") if TypeModel.record_type != "SOA": # Add location_id for non SOA records location_id = request_form.get("location_id", None) if location_id is not None and str(location_id).isdigit(): try: locationdb = ModelLocation.get( ModelLocation.location_id == location_id ) except peewee.DoesNotExist: abort( 400, message="location_id does not exist" ) # Map null/none to None type if str(location_id).lower() in ["null", "none", ""]: location_id = None TypeModel.values["location_id"] = location_id