Skip to content

Commit

Permalink
Merge pull request #201 from innogames/powerdns_managed_models
Browse files Browse the repository at this point in the history
Add Domain and Record models
  • Loading branch information
kofrezo committed Dec 3, 2021
2 parents 4b17abb + 985211f commit 665cbe4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
54 changes: 53 additions & 1 deletion serveradmin/powerdns/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
from django.db import models

# Create your models here.

class Domain(models.Model):
"""PowerDNS Domain
Model to access domain in the PowerDNS db.
See https://doc.powerdns.com/authoritative/backends/generic-postgresql.html#default-schema
"""
TYPES = [
('MASTER', 'MASTER'),
('SLAVE', 'SLAVE'),
('NATIVE', 'NATIVE'),
]

id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255, null=False)
master = models.CharField(max_length=128, default=None)
type = models.CharField(max_length=6, null=False, choices=TYPES)

class Meta:
managed = False
db_table = 'domains'


class Record(models.Model):
"""PowerDNS Record
Model to access records in the PowerDNS db.
See https://doc.powerdns.com/authoritative/backends/generic-postgresql.html#default-schema
"""
TYPES = [
('A', 'A'),
('AAAA', 'AAAA'),
('CNAME', 'CNAME'),
('TXT', 'TXT'),
('SSHFP', 'SSHFP'),
('SOA', 'SOA'),
('MX', 'MX'),
('PTR', 'PTR'),
('NS', 'NS'),
]

id = models.BigIntegerField(primary_key=True)
domain_id = models.ForeignKey('Domain', on_delete=models.CASCADE)
name = models.CharField(max_length=255, default=None)
type = models.CharField(max_length=10, default=None, choices=TYPES)
content = models.CharField(max_length=65535, default=None)
ttl = models.IntegerField()

class Meta:
managed = False
db_table = 'records'
22 changes: 22 additions & 0 deletions serveradmin/powerdns/routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class PowerDNSRouter:
"""
A router to control all database operations on models in the
auth and contenttypes applications.
"""
route_app_labels = {'powerdns'}

def db_for_read(self, model, **hints):
"""
Attempts to read powerdns models go to the pdns db.
"""
if model._meta.app_label in self.route_app_labels:
return 'pdns'
return None

def db_for_write(self, model, **hints):
"""
Attempts to write powerdns models go to the pdns db.
"""
if model._meta.app_label in self.route_app_labels:
return 'pdns'
return None
2 changes: 2 additions & 0 deletions serveradmin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@
'HOST': env('POSTGRES_PDNS_HOST', default=None),
'PORT': env('POSTGRES_PDNS_PORT', default=5432),
'OPTIONS': {
'connect_timeout': 1,
'client_encoding': 'UTF8',
},
},
}
DATABASE_ROUTERS = ['serveradmin.powerdns.routers.PowerDNSRouter']

MIDDLEWARE = [
'django.middleware.common.CommonMiddleware',
Expand Down

0 comments on commit 665cbe4

Please sign in to comment.