Skip to content

Commit 812a0d2

Browse files
authored
Feature #743: Added module for ASN (#947)
* Added ASN module * Added tests for ASN module
1 parent 3100164 commit 812a0d2

File tree

9 files changed

+431
-3
lines changed

9 files changed

+431
-3
lines changed

plugins/module_utils/netbox_ipam.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
NB_AGGREGATES = "aggregates"
22+
NB_ASNS = "asns"
2223
NB_IP_ADDRESSES = "ip_addresses"
2324
NB_PREFIXES = "prefixes"
2425
NB_IPAM_ROLES = "roles"
@@ -148,6 +149,7 @@ def run(self):
148149
to create/update/delete the endpoint objects
149150
Supported endpoints:
150151
- aggregates
152+
- asns
151153
- ipam_roles
152154
- ip_addresses
153155
- l2vpns
@@ -182,6 +184,8 @@ def run(self):
182184
name = data.get("address")
183185
elif self.endpoint in ["aggregates", "prefixes"]:
184186
name = data.get("prefix")
187+
elif self.endpoint == "asns":
188+
name = data.get("asn")
185189
else:
186190
name = data.get("name")
187191

plugins/module_utils/netbox_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
],
8787
ipam=[
8888
"aggregates",
89+
"asns",
8990
"ip_addresses",
9091
"l2vpns",
9192
"l2vpn_terminations",
@@ -112,6 +113,7 @@
112113

113114
# Used to normalize data for the respective query types used to find endpoints
114115
QUERY_TYPES = dict(
116+
asn="asn",
115117
circuit="cid",
116118
circuit_termination="circuit",
117119
circuit_type="slug",
@@ -287,6 +289,7 @@
287289

288290
ENDPOINT_NAME_MAPPING = {
289291
"aggregates": "aggregate",
292+
"asns": "asn",
290293
"cables": "cable",
291294
"circuit_terminations": "circuit_termination",
292295
"circuit_types": "circuit_type",
@@ -360,6 +363,7 @@
360363

361364
ALLOWED_QUERY_PARAMS = {
362365
"aggregate": set(["prefix", "rir"]),
366+
"asn": set(["asn"]),
363367
"assigned_object": set(["name", "device", "virtual_machine"]),
364368
"bridge": set(["name", "device"]),
365369
"circuit": set(["cid"]),

plugins/modules/netbox_asn.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2023, Andrii Konts (@andrii-konts) <andrew.konts@uk2group.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+
DOCUMENTATION = r"""
11+
---
12+
module: netbox_asn
13+
short_description: Create, update or delete ASNs within NetBox
14+
description:
15+
- Creates, updates or removes ASNs from NetBox
16+
notes:
17+
- Tags should be defined as a YAML list
18+
- This should be ran with connection C(local) and hosts C(localhost)
19+
author:
20+
- Andrii Konts (@andrii-konts)
21+
requirements:
22+
- pynetbox
23+
version_added: '3.12.0'
24+
extends_documentation_fragment:
25+
- netbox.netbox.common
26+
options:
27+
data:
28+
type: dict
29+
description:
30+
- Defines the ASN configuration
31+
suboptions:
32+
asn:
33+
description:
34+
- 32-bit autonomous system number
35+
required: true
36+
type: int
37+
rir:
38+
description:
39+
- RIR
40+
required: false
41+
type: raw
42+
tenant:
43+
description:
44+
- Tenant
45+
required: false
46+
type: raw
47+
description:
48+
description:
49+
- Description
50+
required: false
51+
type: str
52+
tags:
53+
description:
54+
- Any tags that the ASN may need to be associated with
55+
required: false
56+
type: list
57+
elements: raw
58+
custom_fields:
59+
description:
60+
- Must exist in NetBox
61+
required: false
62+
type: dict
63+
required: true
64+
"""
65+
66+
EXAMPLES = r"""
67+
- name: "Test NetBox modules"
68+
connection: local
69+
hosts: localhost
70+
gather_facts: False
71+
72+
tasks:
73+
- name: Create ASN within NetBox with only required information
74+
netbox.netbox.netbox_asn:
75+
netbox_url: http://netbox.local
76+
netbox_token: thisIsMyToken
77+
data:
78+
asn: 1111111111
79+
rir: RFC1111
80+
description: test ASN
81+
state: present
82+
83+
- name: Delete ASN within netbox
84+
netbox.netbox.netbox_asn:
85+
netbox_url: http://netbox.local
86+
netbox_token: thisIsMyToken
87+
data:
88+
asn: 1111111111
89+
state: absent
90+
91+
"""
92+
93+
RETURN = r"""
94+
asn:
95+
description: Serialized object as created or already existent within NetBox
96+
returned: success (when I(state=present))
97+
type: dict
98+
msg:
99+
description: Message indicating failure or info about what has been achieved
100+
returned: always
101+
type: str
102+
"""
103+
104+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
105+
NetboxAnsibleModule,
106+
NETBOX_ARG_SPEC,
107+
)
108+
109+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_ipam import (
110+
NetboxIpamModule,
111+
NB_ASNS,
112+
)
113+
114+
from copy import deepcopy
115+
116+
117+
def main():
118+
"""
119+
Main entry point for module execution
120+
"""
121+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
122+
argument_spec.update(
123+
dict(
124+
data=dict(
125+
type="dict",
126+
required=True,
127+
options=dict(
128+
asn=dict(required=True, type="int"),
129+
rir=dict(required=False, type="raw"),
130+
tenant=dict(required=False, type="raw"),
131+
description=dict(required=False, type="str"),
132+
tags=dict(required=False, type="list", elements="raw"),
133+
custom_fields=dict(required=False, type="dict"),
134+
),
135+
),
136+
)
137+
)
138+
required_if = [("state", "present", ["rir"])]
139+
module = NetboxAnsibleModule(
140+
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
141+
)
142+
netbox_cable = NetboxIpamModule(module, NB_ASNS)
143+
netbox_cable.run()
144+
145+
146+
if __name__ == "__main__":
147+
main()

tests/integration/targets/v3.2/tasks/main.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,7 @@
234234
tags:
235235
- netbox_webhook
236236
tags:
237-
- netbox_webhook
237+
- netbox_webhook
238+
239+
- name: "NETBOX_ASN TESTS"
240+
include_tasks: "netbox_asn.yml"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
##
3+
##
4+
### NETBOX_ASN
5+
##
6+
##
7+
- name: "ASN 1: Test ASN creation"
8+
netbox.netbox.netbox_asn:
9+
netbox_url: http://localhost:32768
10+
netbox_token: 0123456789abcdef0123456789abcdef01234567
11+
data:
12+
asn: 1111111111
13+
rir: Example RIR
14+
state: present
15+
register: test_one
16+
17+
- name: "ASN 1: ASSERT - Necessary info creation"
18+
ansible.builtin.assert:
19+
that:
20+
- test_one is changed
21+
- test_one['diff']['before']['state'] == "absent"
22+
- test_one['diff']['after']['state'] == "present"
23+
- test_one['asn']['asn'] == 1111111111
24+
- test_one['asn']['rir'] == 1
25+
- test_one['msg'] == "asn 1111111111 created"
26+
27+
- name: "ASN 2: Create duplicate"
28+
netbox.netbox.netbox_asn:
29+
netbox_url: http://localhost:32768
30+
netbox_token: 0123456789abcdef0123456789abcdef01234567
31+
data:
32+
asn: 1111111111
33+
rir: Example RIR
34+
state: present
35+
register: test_two
36+
37+
- name: "ASN 2: ASSERT - Create duplicate"
38+
ansible.builtin.assert:
39+
that:
40+
- not test_two['changed']
41+
- test_two['asn']['asn'] == 1111111111
42+
- test_two['asn']['rir'] == 1
43+
- test_two['msg'] == "asn 1111111111 already exists"
44+
45+
- name: "ASN 3: Update ASN with other fields"
46+
netbox.netbox.netbox_asn:
47+
netbox_url: http://localhost:32768
48+
netbox_token: 0123456789abcdef0123456789abcdef01234567
49+
data:
50+
asn: 1111111111
51+
rir: Example RIR
52+
tenant: Test Tenant
53+
description: Test description
54+
tags:
55+
- "Schnozzberry"
56+
state: present
57+
register: test_three
58+
59+
- name: "ASN 3: ASSERT - Update ASN with other fields"
60+
ansible.builtin.assert:
61+
that:
62+
- test_three is changed
63+
- test_three['diff']['after']['tenant'] == 1
64+
- test_three['diff']['after']['description'] == "Test description"
65+
- test_three['diff']['after']['tags'][0] == 4
66+
- test_three['asn']['asn'] == 1111111111
67+
- test_three['asn']['rir'] == 1
68+
- test_three['asn']['tenant'] == 1
69+
- test_three['asn']['description'] == "Test description"
70+
- test_three['asn']['tags'][0] == 4
71+
- test_three['msg'] == "asn 1111111111 updated"
72+
73+
- name: "ASN 4: ASSERT - Delete"
74+
netbox.netbox.netbox_asn:
75+
netbox_url: http://localhost:32768
76+
netbox_token: 0123456789abcdef0123456789abcdef01234567
77+
data:
78+
asn: 1111111111
79+
state: absent
80+
register: test_four
81+
82+
- name: "ASN 4: ASSERT - Delete"
83+
ansible.builtin.assert:
84+
that:
85+
- test_four is changed
86+
- test_four['diff']['before']['state'] == "present"
87+
- test_four['diff']['after']['state'] == "absent"
88+
- test_four['msg'] == "asn 1111111111 deleted"

tests/integration/targets/v3.3/tasks/main.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,7 @@
263263
tags:
264264
- netbox_module_type
265265
tags:
266-
- netbox_module_type
266+
- netbox_module_type
267+
268+
- name: "NETBOX_ASN TESTS"
269+
include_tasks: "netbox_asn.yml"

0 commit comments

Comments
 (0)