Skip to content

Commit 73f121e

Browse files
Add slug key for sites, tenants and tenant_groups (#120)
1 parent ef43f5b commit 73f121e

File tree

8 files changed

+64
-17
lines changed

8 files changed

+64
-17
lines changed

plugins/module_utils/netbox_tenancy.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
from ansible_collections.netbox_community.ansible_modules.plugins.module_utils.netbox_utils import (
1111
NetboxModule,
1212
ENDPOINT_NAME_MAPPING,
13+
SLUG_REQUIRED,
1314
)
1415
except ImportError:
1516
import sys
1617

1718
sys.path.append(".")
18-
from netbox_utils import NetboxModule, ENDPOINT_NAME_MAPPING
19+
from netbox_utils import NetboxModule, ENDPOINT_NAME_MAPPING, SLUG_REQUIRED
1920

2021

2122
NB_TENANTS = "tenants"
@@ -46,9 +47,14 @@ def run(self):
4647
data = self.data
4748

4849
# Used for msg output
49-
name = data.get("name")
50-
51-
data["slug"] = self._to_slug(name)
50+
if data.get("name"):
51+
name = data["name"]
52+
elif data.get("slug"):
53+
name = data["slug"]
54+
55+
if self.endpoint in SLUG_REQUIRED:
56+
if not data.get("slug"):
57+
data["slug"] = self._to_slug(name)
5258

5359
object_query_params = self._build_query_params(endpoint_name, data)
5460
self.nb_object = self._nb_endpoint_get(nb_endpoint, object_query_params, name)

plugins/module_utils/netbox_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
rir="slug",
9393
slug="slug",
9494
site="slug",
95-
tenant="name",
95+
tenant="slug",
9696
tenant_group="slug",
9797
time_zone="timezone",
9898
virtual_machine="name",
@@ -217,8 +217,8 @@
217217
"services": set(["device", "virtual_machine", "name"]),
218218
"site": set(["slug"]),
219219
"tagged_vlans": set(["name", "site", "vlan_group", "tenant"]),
220-
"tenant": set(["name"]),
221-
"tenant_group": set(["name"]),
220+
"tenant": set(["slug"]),
221+
"tenant_group": set(["slug"]),
222222
"untagged_vlan": set(["name", "site", "vlan_group", "tenant"]),
223223
"virtual_machine": set(["name", "cluster"]),
224224
"vlan": set(["name", "site", "tenant", "vlan_group"]),
@@ -283,6 +283,9 @@
283283
"regions",
284284
"rirs",
285285
"roles",
286+
"sites",
287+
"tenants",
288+
"tenant_groups",
286289
"manufacturers",
287290
"platforms",
288291
"providers",
@@ -609,9 +612,6 @@ def _normalize_data(self, data):
609612
elif data_type == "timezone":
610613
if " " in v:
611614
data[k] = v.replace(" ", "_")
612-
if self.endpoint == "sites":
613-
site_slug = self._to_slug(data["name"])
614-
data["slug"] = site_slug
615615

616616
return data
617617

plugins/modules/netbox_site.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@
112112
description:
113113
- Comments for the site. This can be markdown syntax
114114
type: str
115+
slug:
116+
description:
117+
- URL-friendly unique shorthand
118+
type: str
115119
tags:
116120
description:
117121
- Any tags that the prefix may need to be associated with
@@ -178,6 +182,7 @@
178182
contact_name: Jenny
179183
contact_phone: 867-5309
180184
contact_email: jenny@changednumber.com
185+
slug: test-california
181186
comments: ### Placeholder
182187
state: present
183188
"""
@@ -237,6 +242,7 @@ def main():
237242
comments=dict(required=False, type="str"),
238243
tags=dict(required=False, type=list),
239244
custom_fields=dict(required=False, type=dict),
245+
slug=dict(required=False, type="str"),
240246
),
241247
),
242248
)

plugins/modules/netbox_tenant.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
description:
6161
- Comments for the tenant. This can be markdown syntax
6262
type: str
63+
slug:
64+
description:
65+
- URL-friendly unique shorthand
66+
type: str
6367
tags:
6468
description:
6569
- Any tags that the tenant may need to be associated with
@@ -115,6 +119,7 @@
115119
group: Very Special Tenants
116120
description: ABC Incorporated
117121
comments: '### This tenant is super cool'
122+
slug: tenant_abc
118123
tags:
119124
- tagA
120125
- tagB
@@ -158,6 +163,7 @@ def main():
158163
tenant_group=dict(required=False, type="raw"),
159164
description=dict(required=False, type="str"),
160165
comments=dict(required=False, type="str"),
166+
slug=dict(required=False, type="str"),
161167
tags=dict(required=False, type=list),
162168
custom_fields=dict(required=False, type=dict),
163169
),

plugins/modules/netbox_tenant_group.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
- Name of the tenant group to be created
4949
required: true
5050
type: str
51+
slug:
52+
description:
53+
- URL-friendly unique shorthand
54+
type: str
5155
required: true
5256
state:
5357
description:
@@ -75,7 +79,8 @@
7579
netbox_url: http://netbox.local
7680
netbox_token: thisIsMyToken
7781
data:
78-
name: Tenant Group ABC
82+
name: Tenant Group ABC
83+
slug: "tenant_group_abc"
7984
state: present
8085
8186
- name: Delete tenant within netbox

tests/integration/integration-tests.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,7 @@
11341134
contact_phone: 867-5309
11351135
contact_email: jenny@changednumber.com
11361136
comments: "### Placeholder"
1137+
slug: "test_california"
11371138
state: present
11381139
register: test_four
11391140

@@ -1160,6 +1161,7 @@
11601161
- test_four['site']['contact_phone'] == "867-5309"
11611162
- test_four['site']['contact_email'] == "jenny@changednumber.com"
11621163
- test_four['site']['comments'] == "### Placeholder"
1164+
- test_four['site']['slug'] == "test_california"
11631165

11641166
- name: "5 - Delete site within netbox"
11651167
netbox_site:
@@ -1271,6 +1273,7 @@
12711273
description: "ABC Incorporated"
12721274
comments: "### This tenant is super cool"
12731275
tenant_group: "Test Tenant Group"
1276+
slug: "tenant_abc"
12741277
tags:
12751278
- tagA
12761279
- tagB
@@ -1285,7 +1288,7 @@
12851288
- test_five['diff']['before']['state'] == "absent"
12861289
- test_five['diff']['after']['state'] == "present"
12871290
- test_five['tenant']['name'] == "Tenant ABC"
1288-
- test_five['tenant']['slug'] == "tenant-abc"
1291+
- test_five['tenant']['slug'] == "tenant_abc"
12891292
- test_five['tenant']['description'] == "ABC Incorporated"
12901293
- test_five['tenant']['comments'] == "### This tenant is super cool"
12911294
- test_five['tenant']['group'] == 1
@@ -1356,6 +1359,25 @@
13561359
- test_three['diff']['after']['state'] == "absent"
13571360
- test_three['msg'] == "tenant_group Test Tenant Group Two deleted"
13581361

1362+
- name: "4 - Test another tenant group creation"
1363+
netbox_tenant_group:
1364+
netbox_url: http://localhost:32768
1365+
netbox_token: 0123456789abcdef0123456789abcdef01234567
1366+
data:
1367+
name: "Test Tenant Group ABC"
1368+
slug: "test_tenant_group_four"
1369+
register: test_four
1370+
1371+
- name: "4 - ASSERT"
1372+
assert:
1373+
that:
1374+
- test_four is changed
1375+
- test_four['diff']['before']['state'] == "absent"
1376+
- test_four['diff']['after']['state'] == "present"
1377+
- test_four['tenant_group']['name'] == "Test Tenant Group ABC"
1378+
- test_four['tenant_group']['slug'] == "test_tenant_group_four"
1379+
- test_four['msg'] == "tenant_group Test Tenant Group ABC created"
1380+
13591381
##
13601382
##
13611383
### NETBOX_RACK

tests/unit/module_utils/test_data/build_query_params_no_child/data.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,20 +289,22 @@
289289
"parent": "tenant",
290290
"module_data": {
291291
"name": "Test Tenant",
292-
"description": "Test Description"
292+
"description": "Test Description",
293+
"slug": "test-tenant"
293294
},
294295
"expected": {
295-
"name": "Test Tenant"
296+
"slug": "test-tenant"
296297
}
297298
},
298299
{
299300
"parent": "tenant_group",
300301
"module_data": {
301302
"name": "Test Tenant Group",
302-
"description": "Test Description"
303+
"description": "Test Description",
304+
"slug": "test-tenant-group"
303305
},
304306
"expected": {
305-
"name": "Test Tenant Group"
307+
"slug": "test-tenant-group"
306308
}
307309
},
308310
{

tests/unit/module_utils/test_data/normalize_data/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228
"tenant": "Test Tenant"
229229
},
230230
"after": {
231-
"tenant": "Test Tenant"
231+
"tenant": "test-tenant"
232232
}
233233
},
234234
{

0 commit comments

Comments
 (0)