Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_pim_rp_address.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17 #
18 from __future__ import absolute_import, division, print_function
19
20
21 __metaclass__ = type
22
23
24 DOCUMENTATION = """
25 module: nxos_pim_rp_address
26 extends_documentation_fragment:
27 - cisco.nxos.nxos
28 short_description: Manages configuration of an PIM static RP address instance.
29 description:
30 - Manages configuration of an Protocol Independent Multicast (PIM) static rendezvous
31 point (RP) address instance.
32 version_added: 1.0.0
33 author: Gabriele Gerbino (@GGabriele)
34 notes:
35 - Tested against NXOSv 7.3.(0)D1(1) on VIRL
36 - Unsupported for Cisco MDS
37 - C(state=absent) is currently not supported on all platforms.
38 options:
39 rp_address:
40 description:
41 - Configures a Protocol Independent Multicast (PIM) static rendezvous point (RP)
42 address. Valid values are unicast addresses.
43 required: true
44 type: str
45 group_list:
46 description:
47 - Group range for static RP. Valid values are multicast addresses.
48 type: str
49 prefix_list:
50 description:
51 - Prefix list policy for static RP. Valid values are prefix-list policy names.
52 type: str
53 route_map:
54 description:
55 - Route map policy for static RP. Valid values are route-map policy names.
56 type: str
57 bidir:
58 description:
59 - Group range is treated in PIM bidirectional mode.
60 type: bool
61 state:
62 description:
63 - Specify desired state of the resource.
64 default: present
65 choices:
66 - present
67 - absent
68 type: str
69 """
70 EXAMPLES = """
71 - cisco.nxos.nxos_pim_rp_address:
72 rp_address: 10.1.1.20
73 state: present
74 """
75
76 RETURN = """
77 commands:
78 description: commands sent to the device
79 returned: always
80 type: list
81 sample: ["router bgp 65535", "vrf test", "router-id 192.0.2.1"]
82 """
83
84
85 import re
86
87 from ansible.module_utils.basic import AnsibleModule
88 from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import (
89 CustomNetworkConfig,
90 )
91
92 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import (
93 get_config,
94 load_config,
95 )
96
97
98 def get_existing(module, args, gl):
99 existing = {}
100 config = str(get_config(module))
101 address = module.params["rp_address"]
102
103 pim_address_re = r"ip pim rp-address (?P<value>.*)$"
104 for line in re.findall(pim_address_re, config, re.M):
105 values = line.split()
106 if values[0] != address:
107 continue
108 if gl and "group-list" not in line:
109 continue
110 elif not gl and "group-list" in line:
111 if "224.0.0.0/4" not in line: # ignore default group-list
112 continue
113
114 existing["bidir"] = existing.get("bidir") or "bidir" in line
115 if len(values) > 2:
116 value = values[2]
117 if values[1] == "route-map":
118 existing["route_map"] = value
119 elif values[1] == "prefix-list":
120 existing["prefix_list"] = value
121 elif values[1] == "group-list":
122 if value != "224.0.0.0/4": # ignore default group-list
123 existing["group_list"] = value
124
125 return existing
126
127
128 def state_present(module, existing, proposed, candidate):
129 address = module.params["rp_address"]
130 command = "ip pim rp-address {0}".format(address)
131 if module.params["group_list"] and not proposed.get("group_list"):
132 command += " group-list " + module.params["group_list"]
133 if module.params["prefix_list"]:
134 if not proposed.get("prefix_list"):
135 command += " prefix-list " + module.params["prefix_list"]
136 if module.params["route_map"]:
137 if not proposed.get("route_map"):
138 command += " route-map " + module.params["route_map"]
139 commands = build_command(proposed, command)
140 if commands:
141 candidate.add(commands, parents=[])
142
143
144 def build_command(param_dict, command):
145 for param in ["group_list", "prefix_list", "route_map"]:
146 if param_dict.get(param):
147 command += " {0} {1}".format(param.replace("_", "-"), param_dict.get(param))
148 if param_dict.get("bidir"):
149 command += " bidir"
150 return [command]
151
152
153 def state_absent(module, existing, candidate):
154 address = module.params["rp_address"]
155
156 commands = []
157 command = "no ip pim rp-address {0}".format(address)
158 if module.params["group_list"] == existing.get("group_list"):
159 commands = build_command(existing, command)
160 elif not module.params["group_list"]:
161 commands = [command]
162
163 if commands:
164 candidate.add(commands, parents=[])
165
166
167 def get_proposed(pargs, existing):
168 proposed = {}
169
170 for key, value in pargs.items():
171 if key != "rp_address":
172 if str(value).lower() == "true":
173 value = True
174 elif str(value).lower() == "false":
175 value = False
176
177 if existing.get(key) != value:
178 proposed[key] = value
179
180 return proposed
181
182
183 def main():
184 argument_spec = dict(
185 rp_address=dict(required=True, type="str"),
186 group_list=dict(required=False, type="str"),
187 prefix_list=dict(required=False, type="str"),
188 route_map=dict(required=False, type="str"),
189 bidir=dict(required=False, type="bool"),
190 state=dict(choices=["present", "absent"], default="present", required=False),
191 )
192
193 module = AnsibleModule(
194 argument_spec=argument_spec,
195 mutually_exclusive=[
196 ["group_list", "route_map"],
197 ["group_list", "prefix_list"],
198 ["route_map", "prefix_list"],
199 ],
200 supports_check_mode=True,
201 )
202
203 warnings = list()
204 result = {"changed": False, "commands": [], "warnings": warnings}
205
206 state = module.params["state"]
207
208 args = ["rp_address", "group_list", "prefix_list", "route_map", "bidir"]
209
210 proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args)
211
212 if module.params["group_list"]:
213 existing = get_existing(module, args, True)
214 proposed = get_proposed(proposed_args, existing)
215
216 else:
217 existing = get_existing(module, args, False)
218 proposed = get_proposed(proposed_args, existing)
219
220 candidate = CustomNetworkConfig(indent=3)
221 if state == "present" and (proposed or not existing):
222 state_present(module, existing, proposed, candidate)
223 elif state == "absent" and existing:
224 state_absent(module, existing, candidate)
225
226 if candidate:
227 candidate = candidate.items_text()
228 result["commands"] = candidate
229 result["changed"] = True
230 msgs = load_config(module, candidate, True)
231 if msgs:
232 for item in msgs:
233 if item:
234 if isinstance(item, dict):
235 err_str = item["clierror"]
236 else:
237 err_str = item
238 if "No policy was configured" in err_str:
239 if state == "absent":
240 addr = module.params["rp_address"]
241 new_cmd = "no ip pim rp-address {0}".format(addr)
242 load_config(module, new_cmd)
243
244 module.exit_json(**result)
245
246
247 if __name__ == "__main__":
248 main()