Skip to content

Commit 9861478

Browse files
cjy008bsanders
authored andcommitted
FEATURE: Ansible module for getting network info
1 parent 8bb5395 commit 9861478

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# @copyright@
2+
# Copyright (c) 2006 - 2020 Teradata
3+
# All rights reserved. Stacki(r) v5.x stacki.com
4+
# https://github.com/Teradata/stacki/blob/master/LICENSE.txt
5+
# @copyright@
6+
7+
from ansible.module_utils.basic import AnsibleModule
8+
from ansible.module_utils.stacki import run_stack_command, StackCommandError
9+
10+
DOCUMENTATION = """
11+
module: stacki_network_info
12+
short_description: Return data about networks in Stacki
13+
description:
14+
- If name is supplied, returns information about the single network
15+
- If name is not supplied, returns information about all the networks in the system
16+
options:
17+
name:
18+
description:
19+
- The name of the network
20+
required: false
21+
"""
22+
23+
EXAMPLES = """
24+
- name: Get info on all the networks
25+
stacki_network_info:
26+
register: result
27+
28+
- name: Get the primary netwrok info
29+
stacki_network_info:
30+
name: primary
31+
register: result
32+
"""
33+
34+
RETURN = """
35+
networks:
36+
description:
37+
- List of networks
38+
returned: on success
39+
type: complex
40+
contains:
41+
network:
42+
description:
43+
- Name of the network
44+
type: str
45+
46+
address:
47+
description:
48+
- Address of the network
49+
type: str
50+
51+
mask:
52+
description:
53+
- Mask of the network
54+
type: str
55+
56+
gateway:
57+
description:
58+
- Gateway of the network
59+
type: str
60+
61+
mtu:
62+
description:
63+
- MTU of the network
64+
type: int
65+
66+
zone:
67+
description:
68+
- Zone of the network
69+
type: str
70+
71+
dns:
72+
description:
73+
- Is DNS enabled on the network
74+
type: boolean
75+
76+
pxe:
77+
description:
78+
- Is PXE enabled on the network
79+
type: boolean
80+
"""
81+
82+
83+
def main():
84+
argument_spec = dict(
85+
name=dict(type="str", required=False, default=None)
86+
)
87+
88+
module = AnsibleModule(
89+
argument_spec=argument_spec,
90+
supports_check_mode=True
91+
)
92+
93+
results = {
94+
"changed": False,
95+
"networks": []
96+
}
97+
98+
if module.check_mode:
99+
module.exit_json(**results)
100+
101+
args = []
102+
if module.params["name"]:
103+
args.append(module.params["name"])
104+
105+
try:
106+
results["networks"] = run_stack_command("list.network", args)
107+
108+
except StackCommandError as e:
109+
module.fail_json(msg=e.message, **results)
110+
111+
module.exit_json(**results)
112+
113+
114+
if __name__ == "__main__":
115+
main()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class TestStackiNetworkInfo:
2+
3+
def test_no_network_name(self, run_ansible_module):
4+
result = run_ansible_module("stacki_network_info")
5+
6+
assert result.status == "SUCCESS"
7+
assert result.data["changed"] is False
8+
assert len(result.data["networks"]) == 1
9+
10+
def test_network_name(self, run_ansible_module):
11+
network_name = "private"
12+
result = run_ansible_module("stacki_network_info", name=network_name)
13+
14+
assert result.status == "SUCCESS"
15+
assert result.data["changed"] is False
16+
assert len(result.data["networks"]) == 1
17+
assert result.data["networks"][0]["network"] == network_name
18+
19+
def test_invalid_network_name(self, run_ansible_module):
20+
network_name = "fake_network_name"
21+
result = run_ansible_module("stacki_network_info", name=network_name)
22+
23+
assert "FAIL" in result.status
24+
assert result.data["changed"] is False
25+
26+
assert "error" in result.data["msg"]
27+
assert "not a valid network" in result.data["msg"]

0 commit comments

Comments
 (0)