-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from peteeckel/feature/ipam-coupling
IPAM Coupling by @jean1
- Loading branch information
Showing
18 changed files
with
890 additions
and
56 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
from extras.plugins import PluginConfig | ||
import logging | ||
|
||
logger = logging.getLogger("netbox.config") | ||
|
||
__version__ = "0.19.4" | ||
|
||
|
@@ -11,6 +14,7 @@ class DNSConfig(PluginConfig): | |
version = __version__ | ||
author = "Peter Eckel" | ||
author_email = "[email protected]" | ||
middleware = ["netbox_dns.middleware.IpamCouplingMiddleware"] | ||
required_settings = [] | ||
default_settings = { | ||
"zone_default_ttl": 86400, | ||
|
@@ -20,7 +24,7 @@ class DNSConfig(PluginConfig): | |
"zone_soa_retry": 7200, | ||
"zone_soa_expire": 2592000, | ||
"zone_soa_minimum": 3600, | ||
"feature_ipam_integration": False, | ||
"feature_ipam_coupling": False, | ||
"tolerate_underscores_in_hostnames": False, | ||
"tolerate_leading_underscore_types": [ | ||
"TXT", | ||
|
@@ -32,5 +36,32 @@ class DNSConfig(PluginConfig): | |
} | ||
base_url = "netbox-dns" | ||
|
||
def ready(self): | ||
# | ||
# Check if required custom field exist for IPAM coupling | ||
# | ||
if self.default_settings["feature_ipam_coupling"]: | ||
from extras.models import CustomField | ||
from ipam.models import IPAddress | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
objtype = ContentType.objects.get_for_model(IPAddress) | ||
required_cf = ("ipaddress_dns_record_name", "ipaddress_dns_zone_id") | ||
|
||
if CustomField.objects.filter( | ||
name__in=required_cf, content_types=objtype | ||
).count() < len(required_cf): | ||
logger.warning( | ||
"'feature_ipam_coupling' is enabled, but the required custom" | ||
" fields for IPAM-DNS coupling are missing. Please run the" | ||
" Django management command 'setup_coupling' to create the" | ||
" custom fields." | ||
) | ||
|
||
super().ready() | ||
|
||
|
||
# | ||
# Initialize plugin config | ||
# | ||
config = DNSConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from django.core.management.base import BaseCommand, CommandError | ||
|
||
from django.contrib.contenttypes.models import ContentType | ||
from extras.models import CustomField | ||
from extras.choices import CustomFieldTypeChoices | ||
from ipam.models import IPAddress | ||
from netbox_dns.models import Record, Zone | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Setup IPAddress custom fields needed for IPAM-DNS coupling" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--remove", action="store_true", help="Remove custom fields" | ||
) | ||
|
||
def handle(self, *model_names, **options): | ||
ipaddress_object_type = ContentType.objects.get_for_model(IPAddress) | ||
zone_object_type = ContentType.objects.get_for_model(Zone) | ||
record_object_type = ContentType.objects.get_for_model(Record) | ||
customfields = ("ipaddress_dns_record_name", "ipaddress_dns_zone_id") | ||
|
||
if options["remove"]: | ||
for cf in customfields: | ||
try: | ||
CustomField.objects.get( | ||
name=cf, content_types=ipaddress_object_type | ||
).delete() | ||
except: | ||
self.stderr.write(f"Custom field '{cf}' does not exist!") | ||
else: | ||
self.stdout.write(f"Custom field '{cf}' removed") | ||
|
||
else: | ||
msg = "" | ||
for cf in customfields: | ||
try: | ||
CustomField.objects.get( | ||
name=cf, content_types=ipaddress_object_type | ||
) | ||
except: | ||
pass | ||
else: | ||
msg += f"custom fields '{cf}' already exists, " | ||
if msg != "": | ||
raise CommandError( | ||
"\n".join( | ||
( | ||
"Can't setup IPAM-DNS coupling:", | ||
msg, | ||
"Remove them with NetBox command:", | ||
"python manage.py setup_coupling --remove", | ||
) | ||
) | ||
) | ||
|
||
cf_name = CustomField.objects.create( | ||
name="ipaddress_dns_record_name", | ||
label="Name", | ||
type=CustomFieldTypeChoices.TYPE_TEXT, | ||
required=False, | ||
group_name="DNS", | ||
) | ||
cf_name.content_types.set([ipaddress_object_type]) | ||
cf_zone = CustomField.objects.create( | ||
name="ipaddress_dns_zone_id", | ||
label="Zone", | ||
type=CustomFieldTypeChoices.TYPE_OBJECT, | ||
object_type=zone_object_type, | ||
required=False, | ||
group_name="DNS", | ||
) | ||
cf_zone.content_types.set([ipaddress_object_type]) | ||
self.stdout.write(f"Custom fields for IPAM-DNS coupling added") |
Oops, something went wrong.