Skip to content

Commit 0d5378b

Browse files
authored
Add module netbox_provider_network - closes #528 (#653)
* Add module netbox_provider_network * Add provider_network to circuit termination
1 parent 4b38639 commit 0d5378b

File tree

6 files changed

+267
-1
lines changed

6 files changed

+267
-1
lines changed

plugins/module_utils/netbox_circuits.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414

1515
NB_PROVIDERS = "providers"
16+
NB_PROVIDER_NETWORKS = "provider_networks"
1617
NB_CIRCUIT_TYPES = "circuit_types"
1718
NB_CIRCUIT_TERMINATIONS = "circuit_terminations"
1819
NB_CIRCUITS = "circuits"
@@ -31,6 +32,7 @@ def run(self):
3132
- circuit_terminations
3233
- circuits
3334
- providers
35+
- provider_networks
3436
"""
3537
# Used to dynamically set key when returning results
3638
endpoint_name = ENDPOINT_NAME_MAPPING[self.endpoint]

plugins/module_utils/netbox_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333

3434
# Used to map endpoints to applications dynamically
3535
API_APPS_ENDPOINTS = dict(
36-
circuits=["circuits", "circuit_types", "circuit_terminations", "providers"],
36+
circuits=[
37+
"circuits",
38+
"circuit_types",
39+
"circuit_terminations",
40+
"providers",
41+
"provider_networks",
42+
],
3743
dcim=[
3844
"cables",
3945
"console_ports",
@@ -120,6 +126,7 @@
120126
primary_ip4="address",
121127
primary_ip6="address",
122128
provider="slug",
129+
provider_network="name",
123130
rack="name",
124131
rack_group="slug",
125132
rack_role="slug",
@@ -197,6 +204,7 @@
197204
"primary_ip4": "ip_addresses",
198205
"primary_ip6": "ip_addresses",
199206
"provider": "providers",
207+
"provider_network": "provider_networks",
200208
"rack": "racks",
201209
"rack_group": "rack_groups",
202210
"rack_role": "rack_roles",
@@ -267,6 +275,7 @@
267275
"power_port_templates": "power_port_template",
268276
"prefixes": "prefix",
269277
"providers": "provider",
278+
"provider_networks": "provider_network",
270279
"racks": "rack",
271280
"rack_groups": "rack_group",
272281
"rack_roles": "rack_role",
@@ -362,6 +371,7 @@
362371
"primary_ip4": set(["address", "vrf"]),
363372
"primary_ip6": set(["address", "vrf"]),
364373
"provider": set(["slug"]),
374+
"provider_network": set(["name"]),
365375
"rack": set(["name", "site"]),
366376
"rack_group": set(["slug"]),
367377
"rack_role": set(["slug"]),

plugins/modules/netbox_circuit_termination.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
- The site the circuit termination will be assigned to
5555
required: false
5656
type: raw
57+
provider_network:
58+
description:
59+
- The provider_network the circuit termination will be assigned to
60+
required: false
61+
type: raw
5762
port_speed:
5863
description:
5964
- The speed of the port (Kbps)
@@ -158,6 +163,7 @@ def main():
158163
circuit=dict(required=True, type="raw"),
159164
term_side=dict(required=True, choices=["A", "Z"]),
160165
site=dict(required=False, type="raw"),
166+
provider_network=dict(required=False, type="raw"),
161167
port_speed=dict(required=False, type="int"),
162168
upstream_speed=dict(required=False, type="int"),
163169
xconnect_id=dict(required=False, type="str"),
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright: (c) 2019, Mikhail Yohman (@FragmentedPacket) <mikhail.yohman@gmail.com>
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
7+
from __future__ import absolute_import, division, print_function
8+
9+
__metaclass__ = type
10+
11+
DOCUMENTATION = r"""
12+
---
13+
module: netbox_provider_network
14+
short_description: Create, update or delete provider networks within NetBox
15+
description:
16+
- Creates, updates or removes provider networks from NetBox
17+
notes:
18+
- Tags should be defined as a YAML list
19+
- This should be ran with connection C(local) and hosts C(localhost)
20+
author:
21+
- Martin Rødvand (@rodvand)
22+
requirements:
23+
- pynetbox
24+
version_added: '3.4.0'
25+
extends_documentation_fragment:
26+
- netbox.netbox.common
27+
options:
28+
data:
29+
type: dict
30+
description:
31+
- Defines the provider network configuration
32+
suboptions:
33+
provider:
34+
description:
35+
- The name of the provider
36+
required: true
37+
type: raw
38+
name:
39+
description:
40+
- The name of the provider network
41+
required: true
42+
type: str
43+
description:
44+
description:
45+
- Description related to the provider network
46+
required: false
47+
type: str
48+
comments:
49+
description:
50+
- Comments related to the provider network
51+
required: false
52+
type: str
53+
tags:
54+
description:
55+
- Any tags that the provdier_network may need to be associated with
56+
required: false
57+
type: list
58+
elements: raw
59+
custom_fields:
60+
description:
61+
- must exist in NetBox
62+
required: false
63+
type: dict
64+
required: true
65+
"""
66+
67+
EXAMPLES = r"""
68+
- name: "Test NetBox modules"
69+
connection: local
70+
hosts: localhost
71+
gather_facts: False
72+
73+
tasks:
74+
- name: Create provider network within NetBox with only required information
75+
netbox_provider:
76+
netbox_url: http://netbox.local
77+
netbox_token: thisIsMyToken
78+
data:
79+
provider: Test Provider
80+
name: Test Provider Network
81+
state: present
82+
83+
- name: Update provider with other fields
84+
netbox_provider:
85+
netbox_url: http://netbox.local
86+
netbox_token: thisIsMyToken
87+
data:
88+
provider: Test Provider
89+
name: Test Provider Network
90+
description: Describe a Provider Network
91+
comments: "A Provider Network"
92+
state: present
93+
94+
- name: Delete provider within netbox
95+
netbox_provider:
96+
netbox_url: http://netbox.local
97+
netbox_token: thisIsMyToken
98+
data:
99+
provider: test Provider
100+
name: Test Provider Network
101+
state: absent
102+
"""
103+
104+
RETURN = r"""
105+
provider_network:
106+
description: Serialized object as created or already existent within NetBox
107+
returned: success (when I(state=present))
108+
type: dict
109+
msg:
110+
description: Message indicating failure or info about what has been achieved
111+
returned: always
112+
type: str
113+
"""
114+
115+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
116+
NetboxAnsibleModule,
117+
NETBOX_ARG_SPEC,
118+
)
119+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_circuits import (
120+
NetboxCircuitsModule,
121+
NB_PROVIDER_NETWORKS,
122+
)
123+
from copy import deepcopy
124+
125+
126+
def main():
127+
"""
128+
Main entry point for module execution
129+
"""
130+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
131+
argument_spec.update(
132+
dict(
133+
data=dict(
134+
type="dict",
135+
required=True,
136+
options=dict(
137+
provider=dict(required=True, type="raw"),
138+
name=dict(required=True, type="str"),
139+
description=dict(required=False, type="str"),
140+
comments=dict(required=False, type="str"),
141+
tags=dict(required=False, type="list", elements="raw"),
142+
custom_fields=dict(required=False, type="dict"),
143+
),
144+
),
145+
),
146+
)
147+
148+
required_if = [
149+
("state", "present", ["provider", "name"]),
150+
("state", "absent", ["provider", "name"]),
151+
]
152+
153+
module = NetboxAnsibleModule(
154+
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
155+
)
156+
157+
netbox_provider_network = NetboxCircuitsModule(module, NB_PROVIDER_NETWORKS)
158+
netbox_provider_network.run()
159+
160+
161+
if __name__ == "__main__": # pragma: no cover
162+
main()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595
- name: "NETBOX_PROVIDER TESTS"
9696
include_tasks: "netbox_provider.yml"
9797

98+
- name: "NETBOX_PROVIDER_NETWORK TESTS"
99+
include_tasks: "netbox_provider_network.yml"
100+
98101
- name: "NETBOX_CIRCUIT_TYPE TESTS"
99102
include_tasks: "netbox_circuit_type.yml"
100103

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
##
3+
##
4+
### NETBOX_PROVIDER
5+
##
6+
##
7+
- name: "NETBOX_PROVIDER_NETWORK 1: Create provider network within NetBox with only required information"
8+
netbox.netbox.netbox_provider_network:
9+
netbox_url: http://localhost:32768
10+
netbox_token: 0123456789abcdef0123456789abcdef01234567
11+
data:
12+
provider: Test Provider
13+
name: Test Provider Network One
14+
state: present
15+
register: test_one
16+
17+
- name: "NETBOX_PROVIDER_NETWORK 1: ASSERT - Necessary info creation"
18+
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['provider_network']['name'] == "Test Provider Network One"
24+
- test_one['msg'] == "provider_network Test Provider Network One created"
25+
26+
- name: "NETBOX_PROVIDER_NETWORK 2: Duplicate"
27+
netbox.netbox.netbox_provider_network:
28+
netbox_url: http://localhost:32768
29+
netbox_token: 0123456789abcdef0123456789abcdef01234567
30+
data:
31+
provider: Test Provider
32+
name: Test Provider Network One
33+
state: present
34+
register: test_two
35+
36+
- name: "NETBOX_PROVIDER_NETWORK 2: ASSERT - Create duplicate"
37+
assert:
38+
that:
39+
- not test_two['changed']
40+
- test_two['provider_network']['name'] == "Test Provider Network One"
41+
- test_two['msg'] == "provider_network Test Provider Network One already exists"
42+
43+
- name: "NETBOX_PROVIDER_NETWORK 3: Update provider network with other fields"
44+
netbox.netbox.netbox_provider_network:
45+
netbox_url: http://localhost:32768
46+
netbox_token: 0123456789abcdef0123456789abcdef01234567
47+
data:
48+
provider: Test Provider
49+
name: Test Provider Network One
50+
description: Describe a Provider Network
51+
comments: "A provider network"
52+
state: present
53+
register: test_three
54+
55+
- name: "NETBOX_PROVIDER_NETWORK 3: ASSERT - Updated"
56+
assert:
57+
that:
58+
- test_three is changed
59+
- test_three['diff']['after']['comments'] == "A provider network"
60+
- test_three['diff']['after']['description'] == "Describe a Provider Network"
61+
- test_three['provider_network']['name'] == "Test Provider Network One"
62+
- test_three['provider_network']['comments'] == "A provider network"
63+
- test_three['provider_network']['description'] == "Describe a Provider Network"
64+
- test_three['msg'] == "provider_network Test Provider Network One updated"
65+
66+
- name: "NETBOX_PROVIDER_NETWORK 4: Delete provider within netbox"
67+
netbox.netbox.netbox_provider_network:
68+
netbox_url: http://localhost:32768
69+
netbox_token: 0123456789abcdef0123456789abcdef01234567
70+
data:
71+
provider: Test Provider
72+
name: Test Provider Network One
73+
state: absent
74+
register: test_four
75+
76+
- name: "NETBOX_PROVIDER_NETWORK 4 : ASSERT - Delete"
77+
assert:
78+
that:
79+
- test_four is changed
80+
- test_four['provider_network']['name'] == "Test Provider Network One"
81+
- test_four['provider_network']['comments'] == "A provider network"
82+
- test_four['provider_network']['description'] == "Describe a Provider Network"
83+
- test_four['msg'] == "provider_network Test Provider Network One deleted"

0 commit comments

Comments
 (0)