Skip to content

Commit

Permalink
Closes #2229: Allow mapping of ConfigContexts to tenant groups
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jul 27, 2018
1 parent 7476f52 commit a1e8397
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
7 changes: 4 additions & 3 deletions netbox/extras/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
ConfigContext, ExportTemplate, Graph, ImageAttachment, ObjectChange, ReportResult, TopologyMap, UserAction,
)
from extras.constants import *
from tenancy.api.serializers import NestedTenantSerializer
from tenancy.api.serializers import NestedTenantSerializer, NestedTenantGroupSerializer
from users.api.serializers import NestedUserSerializer
from utilities.api import (
ChoiceFieldSerializer, ContentTypeFieldSerializer, get_serializer_for_model, ValidatedModelSerializer,
Expand Down Expand Up @@ -136,13 +136,14 @@ class ConfigContextSerializer(ValidatedModelSerializer):
sites = NestedSiteSerializer(required=False, many=True)
roles = NestedDeviceRoleSerializer(required=False, many=True)
platforms = NestedPlatformSerializer(required=False, many=True)
tenant_groups = NestedTenantGroupSerializer(required=False, many=True)
tenants = NestedTenantSerializer(required=False, many=True)

class Meta:
model = ConfigContext
fields = [
'id', 'name', 'weight', 'description', 'is_active', 'regions', 'sites', 'roles', 'platforms', 'tenants',
'data',
'id', 'name', 'weight', 'description', 'is_active', 'regions', 'sites', 'roles', 'platforms',
'tenant_groups', 'tenants', 'data',
]


Expand Down
3 changes: 2 additions & 1 deletion netbox/extras/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ class ConfigContextForm(BootstrapMixin, forms.ModelForm):
class Meta:
model = ConfigContext
fields = [
'name', 'weight', 'description', 'is_active', 'regions', 'sites', 'roles', 'platforms', 'tenants', 'data',
'name', 'weight', 'description', 'is_active', 'regions', 'sites', 'roles', 'platforms', 'tenant_groups',
'tenants', 'data',
]


Expand Down
17 changes: 12 additions & 5 deletions netbox/extras/migrations/0014_configcontexts.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Generated by Django 2.0.6 on 2018-06-29 13:34
# Generated by Django 2.0.7 on 2018-07-27 19:44

import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('tenancy', '0005_change_logging'),
('dcim', '0060_change_logging'),
('dcim', '0061_platform_napalm_args'),
('extras', '0013_objectchange'),
]

Expand All @@ -19,13 +20,14 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, unique=True)),
('weight', models.PositiveSmallIntegerField(default=1000)),
('is_active', models.BooleanField(default=True)),
('description', models.CharField(blank=True, max_length=100)),
('is_active', models.BooleanField(default=True)),
('data', django.contrib.postgres.fields.jsonb.JSONField()),
('platforms', models.ManyToManyField(blank=True, related_name='_configcontext_platforms_+', to='dcim.Platform')),
('regions', models.ManyToManyField(blank=True, related_name='_configcontext_regions_+', to='dcim.Region')),
('roles', models.ManyToManyField(blank=True, related_name='_configcontext_roles_+', to='dcim.DeviceRole')),
('sites', models.ManyToManyField(blank=True, related_name='_configcontext_sites_+', to='dcim.Site')),
('tenant_groups', models.ManyToManyField(blank=True, related_name='_configcontext_tenant_groups_+', to='tenancy.TenantGroup')),
('tenants', models.ManyToManyField(blank=True, related_name='_configcontext_tenants_+', to='tenancy.Tenant')),
],
options={
Expand All @@ -35,11 +37,16 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='customfield',
name='obj_type',
field=models.ManyToManyField(help_text='The object(s) to which this field applies.', limit_choices_to={'model__in': ('provider', 'circuit', 'site', 'rack', 'devicetype', 'device', 'aggregate', 'prefix', 'ipaddress', 'vlan', 'vrf', 'service', 'tenant', 'cluster', 'virtualmachine')}, related_name='custom_fields', to='contenttypes.ContentType', verbose_name='Object(s)'),
field=models.ManyToManyField(help_text='The object(s) to which this field applies.', limit_choices_to={'model__in': ('provider', 'circuit', 'site', 'rack', 'devicetype', 'device', 'aggregate', 'prefix', 'ipaddress', 'vlan', 'vrf', 'service', 'secret', 'tenant', 'cluster', 'virtualmachine')}, related_name='custom_fields', to='contenttypes.ContentType', verbose_name='Object(s)'),
),
migrations.AlterField(
model_name='exporttemplate',
name='content_type',
field=models.ForeignKey(limit_choices_to={'model__in': ['provider', 'circuit', 'site', 'region', 'rack', 'rackgroup', 'manufacturer', 'devicetype', 'device', 'consoleport', 'powerport', 'interfaceconnection', 'virtualchassis', 'aggregate', 'prefix', 'ipaddress', 'vlan', 'vrf', 'service', 'secret', 'tenant', 'cluster', 'virtualmachine']}, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
),
migrations.AlterField(
model_name='webhook',
name='obj_type',
field=models.ManyToManyField(help_text='The object(s) to which this Webhook applies.', limit_choices_to={'model__in': ('provider', 'circuit', 'site', 'rack', 'rackgroup', 'device', 'interface', 'aggregate', 'prefix', 'ipaddress', 'vlan', 'vlangroup', 'vrf', 'service', 'tenant', 'tenantgroup', 'cluster', 'clustergroup', 'virtualmachine')}, related_name='webhooks', to='contenttypes.ContentType', verbose_name='Object types'),
field=models.ManyToManyField(help_text='The object(s) to which this Webhook applies.', limit_choices_to={'model__in': ('provider', 'circuit', 'site', 'rack', 'devicetype', 'device', 'virtualchassis', 'consoleport', 'consoleserverport', 'powerport', 'poweroutlet', 'interface', 'devicebay', 'inventoryitem', 'aggregate', 'prefix', 'ipaddress', 'vlan', 'vrf', 'service', 'secret', 'tenant', 'cluster', 'virtualmachine')}, related_name='webhooks', to='contenttypes.ContentType', verbose_name='Object types'),
),
]
5 changes: 5 additions & 0 deletions netbox/extras/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,11 @@ class ConfigContext(models.Model):
related_name='+',
blank=True
)
tenant_groups = models.ManyToManyField(
to='tenancy.TenantGroup',
related_name='+',
blank=True
)
tenants = models.ManyToManyField(
to='tenancy.Tenant',
related_name='+',
Expand Down
6 changes: 5 additions & 1 deletion netbox/extras/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def get_for_object(self, obj):
# `device_role` for Device; `role` for VirtualMachine
role = getattr(obj, 'device_role', None) or obj.role

# Get the group of the assigned tenant, if any
tenant_group = obj.tenant.group if obj.tenant else None

# Match against the directly assigned region as well as any parent regions.
region = getattr(obj.site, 'region', None)
if region:
Expand All @@ -24,7 +27,8 @@ def get_for_object(self, obj):
Q(regions__in=regions) | Q(regions=None),
Q(sites=obj.site) | Q(sites=None),
Q(roles=role) | Q(roles=None),
Q(tenants=obj.tenant) | Q(tenants=None),
Q(platforms=obj.platform) | Q(platforms=None),
Q(tenant_groups=tenant_group) | Q(tenant_groups=None),
Q(tenants=obj.tenant) | Q(tenants=None),
is_active=True,
).order_by('weight', 'name')
1 change: 1 addition & 0 deletions netbox/templates/extras/configcontext_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
{% render_field form.sites %}
{% render_field form.roles %}
{% render_field form.platforms %}
{% render_field form.tenant_groups %}
{% render_field form.tenants %}
</div>
</div>
Expand Down

0 comments on commit a1e8397

Please sign in to comment.