1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
from django.contrib.contenttypes.models import ContentType
from django.utils.text import slugify
from dcim.choices import DeviceStatusChoices, InterfaceModeChoices, InterfaceTypeChoices, SiteStatusChoices
from dcim.models import Cable, CableTermination, Device, DeviceRole, DeviceType, Interface, Manufacturer, Site
from extras.models import Tag
from extras.scripts import *
from ipam.models import IPAddress, Prefix, VLAN, VLANGroup
# Used for getting existing types/objects from Netbox.
DISTRIBUTION_SWITCH_DEVICE_ROLE = 'distribution-switch' # match the name or the slug
ROUTER_DEVICE_ROLE = 'router'
CORE_DEVICE_ROLE = 'core'
ACCESS_SWITCH_DEVICE_ROLE = DeviceRole.objects.get(name='Access Switch')
DEFAULT_SITE = Site.objects.first() # TODO: pick default site ?
class CreateSwitch(Script):
class Meta:
name = "Create Switch"
description = "Provision a new switch"
field_order = ['site_name', 'switch_count', 'switch_model']
switch_name = StringVar(
description="Switch name"
)
device_type = ObjectVar(
description="Device model",
model=DeviceType,
)
role = ObjectVar(
description="Device role",
model=DeviceRole,
default=ACCESS_SWITCH_DEVICE_ROLE.id,
)
site = ObjectVar(
description = "Site",
model=Site,
default=DEFAULT_SITE.id,
)
destination_device = ObjectVar(
description = "Destination/uplink",
model=Device,
query_params={
'role': [DISTRIBUTION_SWITCH_DEVICE_ROLE, ROUTER_DEVICE_ROLE, CORE_DEVICE_ROLE],
},
)
destination_interfaces = MultiObjectVar(
description="Destination interface(s)",
model=Interface,
query_params={
'device_id': '$destination_device',
# ignore interfaces aleady cabled https://github.com/netbox-community/netbox/blob/v3.4.5/netbox/dcim/filtersets.py#L1225
'cabled': False,
}
)
vlan_group = ObjectVar(
label="VLAN Group",
description="VLAN Group",
model=VLANGroup,
)
vlan_id = IntegerVar(
label="VLAN ID",
description="Auto-assigned if not specified. Make sure it is available if you provide it.",
required=False,
default='',
)
mgmt_vlan = ObjectVar(
description="Management VLAN",
model=VLAN,
query_params={
'vid': [666, 667],
}
)
mgmt_prefix_v4 = ObjectVar(
description="IPv4 Prefix to assign a management IP Address from",
model=Prefix,
query_params={
'family': 4,
'vlan_id': '$mgmt_vlan'
}
)
mgmt_prefix_v6 = ObjectVar(
description="IPv6 Prefix to assign a management IP Address from",
model=Prefix,
query_params={
'family': 6,
'vlan_id': '$mgmt_vlan'
}
)
tags = MultiObjectVar(
description="Tags to be sent to Gondul. These are used for templating, so be sure what they do.",
model=Tag,
)
def run(self, data, commit):
mgmt_vlan = data['mgmt_vlan']
# Create the new switch
switch = Device(
name=data['switch_name'],
device_type=data['device_type'],
device_role=data['role'],
site=data['site'],
)
switch.save()
self.log_success(f"Created new switch: <a href=\"{switch.get_absolute_url()}\">{switch}</a>")
vlan_group = data['vlan_group']
vid = vlan_group.get_next_available_vid()
# use provided vid if specified.
if data['vlan_id']:
vid = data['vlan_id']
vlan = VLAN.objects.create(
name=switch.name,
group=vlan_group,
vid=vid
)
vlan.save()
mgmt_vlan_interface = Interface.objects.create(
device=switch,
name=f"vlan.{mgmt_vlan.vid}",
type=InterfaceTypeChoices.TYPE_VIRTUAL,
mode=InterfaceModeChoices.MODE_TAGGED,
)
mgmt_vlan_interface.tagged_vlans.add(mgmt_vlan.id)
uplink_ae = Interface.objects.create(
device=switch,
name="ae0",
description=data['destination_device'].name,
type=InterfaceTypeChoices.TYPE_LAG,
mode=InterfaceModeChoices.MODE_TAGGED,
)
uplink_ae.tagged_vlans.add(mgmt_vlan.id)
uplink_vlan = Interface.objects.create(
device=switch,
name="ae0.0",
description=data['destination_device'].name,
type=InterfaceTypeChoices.TYPE_VIRTUAL,
parent=uplink_ae,
)
destination_ae = Interface.objects.create(
device=data['destination_device'],
name=f"ae{vlan.vid}",
description=switch.name,
type=InterfaceTypeChoices.TYPE_LAG,
mode=InterfaceModeChoices.MODE_TAGGED,
)
destination_ae.tagged_vlans.add(mgmt_vlan.id)
destination_vlan = Interface.objects.create(
device=data['destination_device'],
name=f"vlan.{vid}",
description=switch.name,
type=InterfaceTypeChoices.TYPE_VIRTUAL,
parent=destination_ae,
)
self.log_success("Created AE and VLAN interfaces for both ends")
v4_mgmt_addr = IPAddress.objects.create(
address=data['mgmt_prefix_v4'].get_first_available_ip(),
)
v6_mgmt_addr = IPAddress.objects.create(
address=data['mgmt_prefix_v6'].get_first_available_ip(),
)
mgmt_vlan_interface.ip_addresses.add(v4_mgmt_addr)
mgmt_vlan_interface.ip_addresses.add(v6_mgmt_addr)
switch.primary_ip4 = v4_mgmt_addr
switch.primary_ip6 = v6_mgmt_addr
switch.save()
num_uplinks = len(data['destination_interfaces'])
interfaces = list(Interface.objects.filter(device=switch).exclude(type=InterfaceTypeChoices.TYPE_VIRTUAL).exclude(type=InterfaceTypeChoices.TYPE_LAG))
interface_type = ContentType.objects.get_for_model(Interface)
for uplink_num in range(0, num_uplinks):
# mark last ports as uplinks
a_interface = data['destination_interfaces'][::-1][uplink_num]
b_interface = interfaces[(uplink_num * -1) -1]
# Configure uplink as AE0
b_interface.lag = uplink_ae
b_interface.save()
# Configure downlink on destination
a_interface.lag = destination_ae
a_interface.save()
cable = Cable.objects.create()
a = CableTermination.objects.create(
cable=cable,
cable_end='A',
termination_id=a_interface.id,
termination_type=interface_type,
)
b = CableTermination.objects.create(
cable_end='B',
cable=cable,
termination_id=b_interface.id,
termination_type=interface_type,
)
cable = Cable.objects.get(id=cable.id)
# https://github.com/netbox-community/netbox/discussions/10199
cable._terminations_modified = True
cable.save()
self.log_success(f"Cabled {data['destination_device']} {a_interface} to {switch} {b_interface}")
|