Skip to content

Commit 67b9415

Browse files
new_module: netbox_rir
1 parent 66d4e85 commit 67b9415

File tree

6 files changed

+242
-2
lines changed

6 files changed

+242
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- netbox_rack_group
3838
- netbox_rack_role
3939
- netbox_rack
40+
- netbox_rir
4041
- netbox_site
4142
- netbox_tenant
4243
- netbox_tenant_group

plugins/module_utils/netbox_ipam.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
NB_IP_ADDRESSES = "ip_addresses"
2828
NB_PREFIXES = "prefixes"
2929
NB_IPAM_ROLES = "roles"
30+
NB_RIRS = "rirs"
3031
NB_VLANS = "vlans"
3132
NB_VLAN_GROUPS = "vlan_groups"
3233
NB_VRFS = "vrfs"
@@ -126,6 +127,7 @@ def run(self):
126127
- ipam_roles
127128
- ip_addresses
128129
- prefixes
130+
- rirs
129131
- vlans
130132
- vlan_groups
131133
- vrfs

plugins/module_utils/netbox_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"sites",
3838
],
3939
extras=[],
40-
ipam=["ip_addresses", "prefixes", "roles", "vlans", "vlan_groups", "vrfs"],
40+
ipam=["ip_addresses", "prefixes", "rirs", "roles", "vlans", "vlan_groups", "vrfs"],
4141
secrets=[],
4242
tenancy=["tenants", "tenant_groups"],
4343
virtualization=["clusters"],
@@ -62,6 +62,7 @@
6262
rack_group="slug",
6363
rack_role="slug",
6464
region="slug",
65+
rir="slug",
6566
slug="slug",
6667
site="slug",
6768
tenant="name",
@@ -94,6 +95,7 @@
9495
rack_group="rack_groups",
9596
rack_role="rack_roles",
9697
region="regions",
98+
rir="rirs",
9799
site="sites",
98100
tagged_vlans="vlans",
99101
tenant="tenants",
@@ -117,6 +119,7 @@
117119
"racks": "rack",
118120
"rack_groups": "rack_group",
119121
"rack_roles": "rack_role",
122+
"rirs": "rir",
120123
"roles": "role",
121124
"sites": "site",
122125
"tenants": "tenant",
@@ -236,6 +239,7 @@
236239
"rack": set(["name", "site"]),
237240
"rack_group": set(["slug"]),
238241
"rack_role": set(["slug"]),
242+
"rir": set(["slug"]),
239243
"role": set(["slug"]),
240244
"site": set(["slug"]),
241245
"tagged_vlans": set(["name", "site", "vlan_group", "tenant"]),
@@ -277,6 +281,7 @@
277281
"ipam_roles",
278282
"rack_groups",
279283
"rack_roles",
284+
"rirs",
280285
"roles",
281286
"manufacturers",
282287
"platforms",

plugins/modules/netbox_rir.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2018, Mikhail Yohman (@FragmentedPacket) <mikhail.yohman@gmail.com>
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
6+
from __future__ import absolute_import, division, print_function
7+
8+
__metaclass__ = type
9+
10+
ANSIBLE_METADATA = {
11+
"metadata_version": "1.1",
12+
"status": ["preview"],
13+
"supported_by": "community",
14+
}
15+
16+
DOCUMENTATION = r"""
17+
---
18+
module: netbox_rir
19+
short_description: Create, update or delete RIRs within Netbox
20+
description:
21+
- Creates, updates or removes RIRs from Netbox
22+
notes:
23+
- Tags should be defined as a YAML list
24+
- This should be ran with connection C(local) and hosts C(localhost)
25+
author:
26+
- Mikhail Yohman (@FragmentedPacket)
27+
requirements:
28+
- pynetbox
29+
version_added: '0.1.0'
30+
options:
31+
netbox_url:
32+
description:
33+
- URL of the Netbox instance resolvable by Ansible control host
34+
required: true
35+
netbox_token:
36+
description:
37+
- The token created within Netbox to authorize API access
38+
required: true
39+
data:
40+
description:
41+
- Defines the RIR configuration
42+
suboptions:
43+
name:
44+
description:
45+
- The name of the RIR
46+
required: true
47+
is_private:
48+
description:
49+
- IP space managed by this RIR is considered private
50+
type: bool
51+
required: true
52+
state:
53+
description:
54+
- Use C(present) or C(absent) for adding or removing.
55+
choices: [ absent, present ]
56+
default: present
57+
validate_certs:
58+
description:
59+
- If C(no), SSL certificates will not be validated. This should only be used on personally controlled sites using self-signed certificates.
60+
default: 'yes'
61+
type: bool
62+
"""
63+
64+
EXAMPLES = r"""
65+
- name: "Test Netbox modules"
66+
connection: local
67+
hosts: localhost
68+
gather_facts: False
69+
70+
tasks:
71+
- name: Create RIR within Netbox with only required information
72+
netbox_rir:
73+
netbox_url: http://netbox.local
74+
netbox_token: thisIsMyToken
75+
data:
76+
name: Test RIR One
77+
state: present
78+
79+
- name: Update Test RIR One
80+
netbox_rir:
81+
netbox_url: http://netbox.local
82+
netbox_token: thisIsMyToken
83+
data:
84+
name: Test RIR One
85+
is_private: True
86+
state: present
87+
88+
- name: Delete RIR within netbox
89+
netbox_rir:
90+
netbox_url: http://netbox.local
91+
netbox_token: thisIsMyToken
92+
data:
93+
name: Test RIR One
94+
state: absent
95+
"""
96+
97+
RETURN = r"""
98+
rir:
99+
description: Serialized object as created or already existent within Netbox
100+
returned: success (when I(state=present))
101+
type: dict
102+
msg:
103+
description: Message indicating failure or info about what has been achieved
104+
returned: always
105+
type: str
106+
"""
107+
108+
from ansible.module_utils.basic import AnsibleModule
109+
from ansible_collections.fragmentedpacket.netbox_modules.plugins.module_utils.netbox_ipam import (
110+
NetboxIpamModule,
111+
NB_RIRS,
112+
)
113+
114+
115+
def main():
116+
"""
117+
Main entry point for module execution
118+
"""
119+
argument_spec = dict(
120+
netbox_url=dict(type="str", required=True),
121+
netbox_token=dict(type="str", required=True, no_log=True),
122+
data=dict(type="dict", required=True),
123+
state=dict(required=False, default="present", choices=["present", "absent"]),
124+
validate_certs=dict(type="bool", default=True),
125+
)
126+
127+
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
128+
129+
# Fail if name is not given
130+
if not module.params["data"].get("name"):
131+
module.fail_json(msg="missing name")
132+
133+
netbox_rir = NetboxIpamModule(module, NB_RIRS)
134+
netbox_rir.run()
135+
136+
137+
if __name__ == "__main__":
138+
main()

tests/integration/integration-tests.yml

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,6 @@
21912191
- test_six['vlan_group'] == None
21922192
- test_six['msg'] == "vlan_group VLAN Group One already absent"
21932193

2194-
21952194
##
21962195
##
21972196
### NETBOX_VLAN
@@ -2476,3 +2475,90 @@
24762475
- test_six['vrf']['description'] == "Updated description"
24772476
- test_six['vrf']['tags'][0] == "Schnozzberry"
24782477
- test_six['msg'] == "vrf Test VRF One deleted"
2478+
2479+
##
2480+
##
2481+
### NETBOX_RIR
2482+
##
2483+
##
2484+
- name: "TEST NETBOX_RIR"
2485+
connection: local
2486+
hosts: localhost
2487+
gather_facts: False
2488+
collections:
2489+
- fragmentedpacket.netbox_modules
2490+
2491+
tasks:
2492+
- name: "RIR 1: Necessary info creation"
2493+
netbox_rir:
2494+
netbox_url: http://localhost:32768
2495+
netbox_token: 0123456789abcdef0123456789abcdef01234567
2496+
data:
2497+
name: Test RIR One
2498+
state: present
2499+
register: test_one
2500+
2501+
- name: "RIR 1: ASSERT - Necessary info creation"
2502+
assert:
2503+
that:
2504+
- test_one is changed
2505+
- test_one['diff']['before']['state'] == "absent"
2506+
- test_one['diff']['after']['state'] == "present"
2507+
- test_one['rir']['name'] == "Test RIR One"
2508+
- test_one['rir']['slug'] == "test-rir-one"
2509+
- test_one['msg'] == "rir Test RIR One created"
2510+
2511+
- name: "RIR 2: Create duplicate"
2512+
netbox_rir:
2513+
netbox_url: http://localhost:32768
2514+
netbox_token: 0123456789abcdef0123456789abcdef01234567
2515+
data:
2516+
name: Test RIR One
2517+
state: present
2518+
register: test_two
2519+
2520+
- name: "RIR 2: ASSERT - Create duplicate"
2521+
assert:
2522+
that:
2523+
- not test_two['changed']
2524+
- test_two['rir']['name'] == "Test RIR One"
2525+
- test_two['rir']['slug'] == "test-rir-one"
2526+
- test_two['msg'] == "rir Test RIR One already exists"
2527+
2528+
- name: "RIR 3: ASSERT - Update"
2529+
netbox_rir:
2530+
netbox_url: http://localhost:32768
2531+
netbox_token: 0123456789abcdef0123456789abcdef01234567
2532+
data:
2533+
name: "Test RIR One"
2534+
is_private: true
2535+
state: present
2536+
register: test_three
2537+
2538+
- name: "RIR 3: ASSERT - Updated"
2539+
assert:
2540+
that:
2541+
- test_three is changed
2542+
- test_three['diff']['after']['is_private'] == true
2543+
- test_three['rir']['name'] == "Test RIR One"
2544+
- test_three['rir']['slug'] == "test-rir-one"
2545+
- test_three['rir']['is_private'] == true
2546+
- test_three['msg'] == "rir Test RIR One updated"
2547+
2548+
- name: "RIR 4: ASSERT - Delete"
2549+
netbox_rir:
2550+
netbox_url: http://localhost:32768
2551+
netbox_token: 0123456789abcdef0123456789abcdef01234567
2552+
data:
2553+
name: "Test RIR One"
2554+
state: absent
2555+
register: test_four
2556+
2557+
- name: "RIR 4: ASSERT - Delete"
2558+
assert:
2559+
that:
2560+
- test_four is changed
2561+
- test_four['rir']['name'] == "Test RIR One"
2562+
- test_four['rir']['slug'] == "test-rir-one"
2563+
- test_four['rir']['is_private'] == true
2564+
- test_four['msg'] == "rir Test RIR One deleted"

tests/unit/module_utils/test_netbox_base_class.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def test_normalize_data_returns_correct_data(mock_netbox_module):
161161
"rack_group": "RacK_group",
162162
"rack_role": "Test Rack Role",
163163
"region": "Test Region_1",
164+
"rir": "Test RIR_One",
164165
"prefix_role": "TEst Role-1",
165166
"slug": "Test to_slug",
166167
"site": "Test Site",
@@ -189,6 +190,7 @@ def test_normalize_data_returns_correct_data(mock_netbox_module):
189190
"rack_group": "rack_group",
190191
"rack_role": "test-rack-role",
191192
"region": "test-region_1",
193+
"rir": "test-rir_one",
192194
"prefix_role": "test-role-1",
193195
"slug": "test-to_slug",
194196
"site": "test-site",
@@ -230,6 +232,7 @@ def test_to_slug_returns_valid_slug(mock_netbox_module):
230232
("ip_addresses", "ipam"),
231233
("prefixes", "ipam"),
232234
("roles", "ipam"),
235+
("rirs", "ipam"),
233236
("vlans", "ipam"),
234237
("vlan_groups", "ipam"),
235238
("vrfs", "ipam"),
@@ -524,6 +527,11 @@ def test_change_choices_id(mock_netbox_module, endpoint, data, expected):
524527
{"name": "Test Rack Role", "slug": "test-rack-role"},
525528
{"slug": "test-rack-role"},
526529
),
530+
(
531+
"rir",
532+
{"name": "Test RIR One", "slug": "test-rir-one"},
533+
{"slug": "test-rir-one"},
534+
),
527535
(
528536
"site",
529537
{

0 commit comments

Comments
 (0)