def _delete_recordset_if_empty(context, domain_id, recordset_id): recordset = get_central_api().find_recordset(context, { 'id': recordset_id }) # Make sure it's the right recordset if len(recordset.records) == 0: get_central_api().delete_recordset(context, domain_id, recordset_id)
def update_domain(domain_id): context = flask.request.environ.get('context') values = flask.request.json domain = get_central_api().get_domain(context, domain_id) domain = domain_schema.filter(domain) domain.update(values) domain_schema.validate(domain) domain = get_central_api().update_domain(context, domain_id, values) return flask.jsonify(domain_schema.filter(domain))
def update_server(server_id): context = flask.request.environ.get('context') values = flask.request.json server = get_central_api().get_server(context, server_id) server = server_schema.filter(server) server.update(values) server_schema.validate(server) server = get_central_api().update_server(context, server_id, values=values) return flask.jsonify(server_schema.filter(server))
def update_tsigkey(tsigkey_id): context = flask.request.environ.get('context') values = flask.request.json tsigkey = get_central_api().get_tsigkey(context, tsigkey_id) tsigkey = tsigkey_schema.filter(tsigkey) tsigkey.update(values) tsigkey_schema.validate(tsigkey) tsigkey = get_central_api().update_tsigkey(context, tsigkey_id, values=values) return flask.jsonify(tsigkey_schema.filter(tsigkey))
def delete_record(domain_id, record_id): context = flask.request.environ.get("context") # NOTE: We need to ensure the domain actually exists, otherwise we may # return a record not found instead of a domain not found get_central_api().get_domain(context, domain_id) # Find the record criterion = {"domain_id": domain_id, "id": record_id} record = get_central_api().find_record(context, criterion) get_central_api().delete_record(context, domain_id, record["recordset_id"], record_id) return flask.Response(status=200)
def delete_record(domain_id, record_id): context = flask.request.environ.get('context') # NOTE: We need to ensure the domain actually exists, otherwise we may # return a record not found instead of a domain not found get_central_api().get_domain(context, domain_id) # Find the record criterion = {'domain_id': domain_id, 'id': record_id} record = get_central_api().find_record(context, criterion) get_central_api().delete_record(context, domain_id, record['recordset_id'], record_id) return flask.Response(status=200)
def get_record(domain_id, record_id): context = flask.request.environ.get("context") # NOTE: We need to ensure the domain actually exists, otherwise we may # return an record not found instead of a domain not found get_central_api().get_domain(context, domain_id) criterion = {"domain_id": domain_id, "id": record_id} record = get_central_api().find_record(context, criterion) recordset = get_central_api().get_recordset(context, domain_id, record["recordset_id"]) record = _format_record_v1(record, recordset) return flask.jsonify(record_schema.filter(record))
def create_record(domain_id): context = flask.request.environ.get('context') values = flask.request.json record_schema.validate(values) if values['type'] == 'SOA': raise exceptions.BadRequest('SOA records cannot be manually created.') recordset = _find_or_create_recordset(context, domain_id, values['name'], values['type'], values.get('ttl', None)) record = objects.Record(**_extract_record_values(values)) record = get_central_api().create_record(context, domain_id, recordset['id'], record) record = _format_record_v1(record, recordset) response = flask.jsonify(record_schema.filter(record)) response.status_int = 201 response.location = flask.url_for('.get_record', domain_id=domain_id, record_id=record['id']) return response
def get_record(domain_id, record_id): context = flask.request.environ.get('context') # NOTE: We need to ensure the domain actually exists, otherwise we may # return an record not found instead of a domain not found get_central_api().get_domain(context, domain_id) criterion = {'domain_id': domain_id, 'id': record_id} record = get_central_api().find_record(context, criterion) recordset = get_central_api().get_recordset(context, domain_id, record['recordset_id']) record = _format_record_v1(record, recordset) return flask.jsonify(record_schema.filter(record))
def _find_or_create_recordset(context, domain_id, name, type, ttl): try: recordset = _find_recordset(context, domain_id, name, type) except exceptions.RecordSetNotFound: values = {"name": name, "type": type, "ttl": ttl} recordset = get_central_api().create_recordset(context, domain_id, objects.RecordSet(**values)) return recordset
def get_records(domain_id): context = flask.request.environ.get('context') # NOTE: We need to ensure the domain actually exists, otherwise we may # return an empty records array instead of a domain not found get_central_api().get_domain(context, domain_id) records = get_central_api().find_records(context, {'domain_id': domain_id}) recordsets = _fetch_domain_recordsets(context, domain_id) def _inner(record): recordset = recordsets[record['recordset_id']] return _format_record_v1(record, recordset) records = [_inner(r) for r in records] return flask.jsonify(records_schema.filter({'records': records}))
def update_tsigkey(tsigkey_id): context = flask.request.environ.get('context') values = flask.request.json # Fetch the existing resource tsigkey = get_central_api().get_tsigkey(context, tsigkey_id) # Prepare a dict of fields for validation tsigkey_data = tsigkey_schema.filter(tsigkey) tsigkey_data.update(values) # Validate the new set of data tsigkey_schema.validate(tsigkey_data) # Update and persist the resource tsigkey.update(values) tsigkey = get_central_api().update_tsigkey(context, tsigkey) return flask.jsonify(tsigkey_schema.filter(tsigkey))
def update_server(server_id): context = flask.request.environ.get("context") values = flask.request.json # Fetch the existing resource server = get_central_api().get_server(context, server_id) # Prepare a dict of fields for validation server_data = server_schema.filter(server) server_data.update(values) # Validate the new set of data server_schema.validate(server_data) # Update and persist the resource server.update(values) server = get_central_api().update_server(context, server) return flask.jsonify(server_schema.filter(server))
def update_domain(domain_id): context = flask.request.environ.get('context') values = flask.request.json # Fetch the existing resource domain = get_central_api().get_domain(context, domain_id) # Prepare a dict of fields for validation domain_data = domain_schema.filter(domain) domain_data.update(values) # Validate the new set of data domain_schema.validate(domain_data) # Update and persist the resource domain.update(values) domain = get_central_api().update_domain(context, domain) return flask.jsonify(domain_schema.filter(domain))
def delete_record(domain_id, record_id): context = flask.request.environ.get('context') # NOTE: We need to ensure the domain actually exists, otherwise we may # return a record not found instead of a domain not found get_central_api().get_domain(context, domain_id) # Find the record criterion = {'domain_id': domain_id, 'id': record_id} record = get_central_api().find_record(context, criterion) # Cannot delete a managed record via the API. if record['managed'] is True: raise exceptions.BadRequest('Managed records may not be deleted') get_central_api().delete_record( context, domain_id, record['recordset_id'], record_id) _delete_recordset_if_empty(context, domain_id, record['recordset_id']) return flask.Response(status=200)
def _find_or_create_recordset(context, domain_id, name, type, ttl): try: recordset = _find_recordset(context, domain_id, name, type) except exceptions.RecordSetNotFound: recordset = get_central_api().create_recordset(context, domain_id, { 'name': name, 'type': type, 'ttl': ttl, }) return recordset
def create_server(): context = flask.request.environ.get("context") values = flask.request.json server_schema.validate(values) server = get_central_api().create_server(context, server=Server(**values)) response = flask.jsonify(server_schema.filter(server)) response.status_int = 201 response.location = flask.url_for(".get_server", server_id=server["id"]) return response
def create_server(): context = flask.request.environ.get('context') values = flask.request.json server_schema.validate(values) server = get_central_api().create_server(context, server=Server(**values)) response = flask.jsonify(server_schema.filter(server)) response.status_int = 201 response.location = flask.url_for('.get_server', server_id=server['id']) return response
def create_domain(): context = flask.request.environ.get('context') values = flask.request.json domain_schema.validate(values) domain = get_central_api().create_domain(context, Domain(**values)) response = flask.jsonify(domain_schema.filter(domain)) response.status_int = 201 response.location = flask.url_for('.get_domain', domain_id=domain['id']) return response
def create_server(): context = flask.request.environ.get('context') values = flask.request.json server_schema.validate(values) server = get_central_api().create_server(context, values=flask.request.json) response = flask.jsonify(server_schema.filter(server)) response.status_int = 201 response.location = flask.url_for('.get_server', server_id=server['id']) return response
def create_tsigkey(): context = flask.request.environ.get('context') values = flask.request.json tsigkey_schema.validate(values) tsigkey = get_central_api().create_tsigkey( context, tsigkey=TsigKey(**values)) response = flask.jsonify(tsigkey_schema.filter(tsigkey)) response.status_int = 201 response.location = flask.url_for('.get_tsigkey', tsigkey_id=tsigkey['id']) return response
def get_limits(): context = flask.request.environ.get('context') absolute_limits = get_central_api().get_absolute_limits(context) return flask.jsonify(limits_schema.filter({ "limits": { "absolute": { "maxDomains": absolute_limits['domains'], "maxDomainRecords": absolute_limits['domain_records'] } } }))
def create_tsigkey(): context = flask.request.environ.get('context') values = flask.request.json tsigkey_schema.validate(values) tsigkey = get_central_api().create_tsigkey(context, tsigkey=TsigKey(**values)) response = flask.jsonify(tsigkey_schema.filter(tsigkey)) response.status_int = 201 response.location = flask.url_for('.get_tsigkey', tsigkey_id=tsigkey['id']) return response
def _find_or_create_recordset(context, domain_id, name, type, ttl): try: recordset = _find_recordset(context, domain_id, name, type) except exceptions.RecordSetNotFound: # Create an empty recordset values = { 'name': name, 'type': type, 'ttl': ttl, } recordset = get_central_api().create_recordset( context, domain_id, objects.RecordSet(**values)) return recordset
def update_record(domain_id, record_id): context = flask.request.environ.get('context') values = flask.request.json # NOTE: We need to ensure the domain actually exists, otherwise we may # return an record not found instead of a domain not found get_central_api().get_domain(context, domain_id) # Find the record criterion = {'domain_id': domain_id, 'id': record_id} record = get_central_api().find_record(context, criterion) # Find the associated recordset recordset = get_central_api().get_recordset( context, domain_id, record['recordset_id']) # Filter out any extra fields from the fetched record record = record_schema.filter(record) # Ensure all the API V1 fields are in place record = _format_record_v1(record, recordset) # Name and Type can't be updated on existing records if 'name' in values and record['name'] != values['name']: raise exceptions.InvalidOperation('The name field is immutable') if 'type' in values and record['type'] != values['type']: raise exceptions.InvalidOperation('The type field is immutable') # TTL Updates should be applied to the RecordSet update_recordset = False if 'ttl' in values and record['ttl'] != values['ttl']: update_recordset = True # Apply the updated fields to the record record.update(values) # Validate the record record_schema.validate(record) # Update the record record = get_central_api().update_record( context, domain_id, recordset['id'], record_id, _extract_record_values(values)) # Update the recordset (if necessary) if update_recordset: recordset = get_central_api().update_recordset( context, domain_id, recordset['id'], _extract_recordset_values(values)) # Format and return the response record = _format_record_v1(record, recordset) return flask.jsonify(record_schema.filter(record))
def create_domain(): valid_attributes = ['name', 'email', 'ttl', 'description'] context = flask.request.environ.get('context') values = load_values(flask.request, valid_attributes) domain_schema.validate(values) domain = get_central_api().create_domain(context, Domain(**values)) response = flask.jsonify(domain_schema.filter(domain)) response.status_int = 201 response.location = flask.url_for('.get_domain', domain_id=domain['id']) return response
def update_record(domain_id, record_id): context = flask.request.environ.get('context') values = flask.request.json # NOTE: We need to ensure the domain actually exists, otherwise we may # return an record not found instead of a domain not found get_central_api().get_domain(context, domain_id) # Find the record criterion = {'domain_id': domain_id, 'id': record_id} record = get_central_api().find_record(context, criterion) # Find the associated recordset recordset = get_central_api().get_recordset(context, domain_id, record['recordset_id']) # Filter out any extra fields from the fetched record record = record_schema.filter(record) # Ensure all the API V1 fields are in place record = _format_record_v1(record, recordset) # Name and Type can't be updated on existing records if 'name' in values and record['name'] != values['name']: raise exceptions.InvalidOperation('The name field is immutable') if 'type' in values and record['type'] != values['type']: raise exceptions.InvalidOperation('The type field is immutable') # TTL Updates should be applied to the RecordSet update_recordset = False if 'ttl' in values and record['ttl'] != values['ttl']: update_recordset = True # Apply the updated fields to the record record.update(values) # Validate the record record_schema.validate(record) # Update the record record = get_central_api().update_record(context, domain_id, recordset['id'], record_id, _extract_record_values(values)) # Update the recordset (if necessary) if update_recordset: recordset = get_central_api().update_recordset( context, domain_id, recordset['id'], _extract_recordset_values(values)) # Format and return the response record = _format_record_v1(record, recordset) return flask.jsonify(record_schema.filter(record))
def create_record(domain_id): context = flask.request.environ.get("context") values = flask.request.json record_schema.validate(values) recordset = _find_or_create_recordset(context, domain_id, values["name"], values["type"], values.get("ttl", None)) record = objects.Record(**_extract_record_values(values)) record = get_central_api().create_record(context, domain_id, recordset["id"], record) record = _format_record_v1(record, recordset) response = flask.jsonify(record_schema.filter(record)) response.status_int = 201 response.location = flask.url_for(".get_record", domain_id=domain_id, record_id=record["id"]) return response
def create_record(domain_id): context = flask.request.environ.get('context') values = flask.request.json record_schema.validate(values) recordset = _find_or_create_recordset(context, domain_id, values['name'], values['type'], values.get('ttl', None)) record = get_central_api().create_record(context, domain_id, recordset['id'], _extract_record_values(values)) record = _format_record_v1(record, recordset) response = flask.jsonify(record_schema.filter(record)) response.status_int = 201 response.location = flask.url_for('.get_record', domain_id=domain_id, record_id=record['id']) return response
def update_record(domain_id, record_id): context = flask.request.environ.get('context') values = flask.request.json # NOTE: We need to ensure the domain actually exists, otherwise we may # return a record not found instead of a domain not found get_central_api().get_domain(context, domain_id) # Fetch the existing resource # NOTE(kiall): We use "find_record" rather than "get_record" as we do not # have the recordset_id. criterion = {'domain_id': domain_id, 'id': record_id} record = get_central_api().find_record(context, criterion) # Find the associated recordset recordset = get_central_api().get_recordset( context, domain_id, record.recordset_id) # Cannot manually update a SOA record if recordset['type'] == 'SOA': raise exceptions.BadRequest('SOA records cannot be manually updated.') # Prepare a dict of fields for validation record_data = record_schema.filter(_format_record_v1(record, recordset)) record_data.update(values) # Validate the new set of data record_schema.validate(record_data) # Update and persist the resource record.update(_extract_record_values(values)) record = get_central_api().update_record(context, record) # Update the recordset resource (if necessary) recordset.update(_extract_recordset_values(values)) if len(recordset.obj_what_changed()) > 0: recordset = get_central_api().update_recordset(context, recordset) # Format and return the response record = _format_record_v1(record, recordset) return flask.jsonify(record_schema.filter(record))
def delete_record(domain_id, record_id): context = flask.request.environ.get('context') # NOTE: We need to ensure the domain actually exists, otherwise we may # return a record not found instead of a domain not found get_central_api().get_domain(context, domain_id) # Find the record criterion = {'domain_id': domain_id, 'id': record_id} record = get_central_api().find_record(context, criterion) # SOA records cannot be deleted recordset = get_central_api().get_recordset(context, domain_id, record['recordset_id']) if recordset['type'] == 'SOA': raise exceptions.BadRequest('SOA records cannot be manually deleted.') get_central_api().delete_record( context, domain_id, record['recordset_id'], record_id) _delete_recordset_if_empty(context, domain_id, record['recordset_id']) return flask.Response(status=200)
def delete_tsigkey(tsigkey_id): context = flask.request.environ.get('context') get_central_api().delete_tsigkey(context, tsigkey_id) return flask.Response(status=200)
def central_api(self): return api.get_central_api()
def get_domain(domain_id): context = flask.request.environ.get('context') domain = get_central_api().get_domain(context, domain_id) return flask.jsonify(domain_schema.filter(domain))
def get_domains(): context = flask.request.environ.get('context') domains = get_central_api().find_domains(context) return flask.jsonify(domains_schema.filter({'domains': domains}))
def get_servers(): context = flask.request.environ.get('context') servers = get_central_api().find_servers(context) return flask.jsonify(servers_schema.filter({'servers': servers}))
def delete_domain(domain_id): context = flask.request.environ.get('context') get_central_api().delete_domain(context, domain_id) return flask.Response(status=200)
def _fetch_domain_recordsets(context, domain_id): criterion = {'domain_id': domain_id} recordsets = get_central_api().find_recordsets(context, criterion) return dict((r['id'], r) for r in recordsets)
def _find_recordset(context, domain_id, name, type): return get_central_api().find_recordset(context, { 'domain_id': domain_id, 'name': name, 'type': type, })
def get_tsigkeys(): context = flask.request.environ.get('context') tsigkeys = get_central_api().find_tsigkeys(context) return flask.jsonify(tsigkeys_schema.filter({'tsigkeys': tsigkeys}))
def get_tsigkey(tsigkey_id): context = flask.request.environ.get('context') tsigkey = get_central_api().get_tsigkey(context, tsigkey_id) return flask.jsonify(tsigkey_schema.filter(tsigkey))
def delete_server(server_id): context = flask.request.environ.get('context') get_central_api().delete_server(context, server_id) return flask.Response(status=200)
def get_server(server_id): context = flask.request.environ.get('context') server = get_central_api().get_server(context, server_id) return flask.jsonify(server_schema.filter(server))
def get_domain_servers(domain_id): context = flask.request.environ.get('context') servers = get_central_api().get_domain_servers(context, domain_id) return flask.jsonify(servers_schema.filter({'servers': servers}))