| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2023, Eric Girard <@netgirard> |
| 4 |
# Copyright: (c) 2024, Gaspard Micol (@gmicol) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE 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 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"} |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
--- |
| 15 |
module: aci_interface_policy_storm_control |
| 16 |
short_description: Manage Storm Control interface policies (stormctrl:IfPol) |
| 17 |
description: |
| 18 |
- Manage Storm Control interface policies on Cisco ACI fabrics. |
| 19 |
options: |
| 20 |
storm_control_policy: |
| 21 |
description: |
| 22 |
- The Storm Control interface policy name. |
| 23 |
type: str |
| 24 |
aliases: [ storm_control, storm_control_name, name ] |
| 25 |
description: |
| 26 |
description: |
| 27 |
- The description for the Storm interface policy name. |
| 28 |
type: str |
| 29 |
aliases: [ descr ] |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Use C(present) or C(absent) for adding or removing. |
| 33 |
- Use C(query) for listing an object or multiple objects. |
| 34 |
type: str |
| 35 |
choices: [ absent, present, query ] |
| 36 |
default: present |
| 37 |
name_alias: |
| 38 |
description: |
| 39 |
- The alias for the current object. This relates to the nameAlias field in ACI. |
| 40 |
type: str |
| 41 |
storm_control_action: |
| 42 |
description: |
| 43 |
- The storm control action to take when triggered. |
| 44 |
type: str |
| 45 |
choices: [drop, shutdown] |
| 46 |
storm_control_soak_action: |
| 47 |
description: |
| 48 |
- The number of instances before triggering shutdown. |
| 49 |
type: int |
| 50 |
all_types_configuration: |
| 51 |
description: |
| 52 |
- The rates configuration for all packets type. |
| 53 |
type: dict |
| 54 |
aliases: [ all_types ] |
| 55 |
suboptions: |
| 56 |
rate: |
| 57 |
description: |
| 58 |
- The rate for all packet types. |
| 59 |
type: str |
| 60 |
burst_rate: |
| 61 |
description: |
| 62 |
- The burst rate of all packet types. |
| 63 |
type: str |
| 64 |
rate_type: |
| 65 |
description: |
| 66 |
- The type of rate of all packet types. |
| 67 |
- Choice between percentage of the bandiwth C(percentage) or packet per second C(pps) |
| 68 |
type: str |
| 69 |
choices: [ percentage, pps ] |
| 70 |
required: true |
| 71 |
broadcast_configuration: |
| 72 |
description: |
| 73 |
- The rates configuration of broadcast packets. |
| 74 |
type: dict |
| 75 |
aliases: [ broadcast ] |
| 76 |
suboptions: |
| 77 |
rate: |
| 78 |
description: |
| 79 |
- The rate for broadcast packets. |
| 80 |
type: str |
| 81 |
burst_rate: |
| 82 |
description: |
| 83 |
- The burst rate of broadcast packets. |
| 84 |
type: str |
| 85 |
rate_type: |
| 86 |
description: |
| 87 |
- The type of rate of all packet types. |
| 88 |
- Choice between percentage of the bandiwth C(percentage) or packet per second C(pps) |
| 89 |
type: str |
| 90 |
choices: [ percentage, pps ] |
| 91 |
required: true |
| 92 |
multicast_configuration: |
| 93 |
description: |
| 94 |
- The rates configuration of multicast packets. |
| 95 |
type: dict |
| 96 |
aliases: [ multicast ] |
| 97 |
suboptions: |
| 98 |
rate: |
| 99 |
description: |
| 100 |
- The rate for multicast packets. |
| 101 |
type: str |
| 102 |
burst_rate: |
| 103 |
description: |
| 104 |
- The burst rate of multicast packets. |
| 105 |
type: str |
| 106 |
rate_type: |
| 107 |
description: |
| 108 |
- The type of rate of all packet types. |
| 109 |
- Choice between percentage of the bandiwth C(percentage) or packet per second C(pps) |
| 110 |
type: str |
| 111 |
choices: [ percentage, pps ] |
| 112 |
required: true |
| 113 |
unicast_configuration: |
| 114 |
description: |
| 115 |
- The rates configuration of unicast packets. |
| 116 |
type: dict |
| 117 |
aliases: [ unicast ] |
| 118 |
suboptions: |
| 119 |
rate: |
| 120 |
description: |
| 121 |
- The rate for unicast packets. |
| 122 |
type: str |
| 123 |
burst_rate: |
| 124 |
description: |
| 125 |
- The burst rate of unicast packets. |
| 126 |
type: str |
| 127 |
rate_type: |
| 128 |
description: |
| 129 |
- The type of rate of all packet types. |
| 130 |
- Choice between percentage of the bandiwth C(percentage) or packet per second C(pps) |
| 131 |
type: str |
| 132 |
choices: [ percentage, pps ] |
| 133 |
required: true |
| 134 |
extends_documentation_fragment: |
| 135 |
- cisco.aci.aci |
| 136 |
- cisco.aci.annotation |
| 137 |
- cisco.aci.owner |
| 138 |
|
| 139 |
seealso: |
| 140 |
- name: APIC Management Information Model reference |
| 141 |
description: More information about the internal APIC class B(stormctrl:IfPol). |
| 142 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 143 |
author: |
| 144 |
- Eric Girard (@netgirard) |
| 145 |
- Gaspard Micol (@gmicol) |
| 146 |
""" |
| 147 |
|
| 148 |
EXAMPLES = r""" |
| 149 |
- name: Add a new Storm Control Interface Policy |
| 150 |
cisco.aci.aci_interface_policy_storm_control: |
| 151 |
host: apic |
| 152 |
username: admin |
| 153 |
password: SomeSecretPassword |
| 154 |
storm_control_policy: my_storm_control_policy |
| 155 |
description: My Storm Control Policy |
| 156 |
all_types_configuration: |
| 157 |
rate: 80 |
| 158 |
burst_rate: 100 |
| 159 |
rate_type: percentage |
| 160 |
storm_control_action: shutdown |
| 161 |
storm_control_soak_action: 5 |
| 162 |
state: present |
| 163 |
delegate_to: localhost |
| 164 |
|
| 165 |
- name: Query a Storm Control Interface Policy |
| 166 |
cisco.aci.aci_interface_policy_storm_control: |
| 167 |
host: apic |
| 168 |
username: admin |
| 169 |
password: SomeSecretPassword |
| 170 |
storm_control_policy: my_storm_control_policy |
| 171 |
state: query |
| 172 |
delegate_to: localhost |
| 173 |
|
| 174 |
- name: Query all Storm Control Interface Policies |
| 175 |
cisco.aci.aci_interface_policy_storm_control: |
| 176 |
host: apic |
| 177 |
username: admin |
| 178 |
password: SomeSecretPassword |
| 179 |
state: query |
| 180 |
delegate_to: localhost |
| 181 |
|
| 182 |
- name: Delete a Storm Control Interface Policy |
| 183 |
cisco.aci.aci_interface_policy_storm_control: |
| 184 |
host: apic |
| 185 |
username: admin |
| 186 |
password: SomeSecretPassword |
| 187 |
tenant: my_tenant |
| 188 |
storm_control_policy: my_storm_control_policy |
| 189 |
state: absent |
| 190 |
delegate_to: localhost |
| 191 |
""" |
| 192 |
|
| 193 |
RETURN = r""" |
| 194 |
current: |
| 195 |
description: The existing configuration from the APIC after the module has finished |
| 196 |
returned: success |
| 197 |
type: list |
| 198 |
sample: |
| 199 |
[ |
| 200 |
{ |
| 201 |
"cdpIfPol": { |
| 202 |
"attributes": { |
| 203 |
"adminSt": "disabled", |
| 204 |
"annotation": "", |
| 205 |
"descr": "Ansible Created CDP Test Policy", |
| 206 |
"dn": "uni/infra/cdpIfP-Ansible_CDP_Test_Policy", |
| 207 |
"name": "Ansible_CDP_Test_Policy", |
| 208 |
"nameAlias": "", |
| 209 |
"ownerKey": "", |
| 210 |
"ownerTag": "" |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
] |
| 215 |
error: |
| 216 |
description: The error information as returned from the APIC |
| 217 |
returned: failure |
| 218 |
type: dict |
| 219 |
sample: |
| 220 |
{ |
| 221 |
"code": "122", |
| 222 |
"text": "unknown managed object class foo" |
| 223 |
} |
| 224 |
raw: |
| 225 |
description: The raw output returned by the APIC REST API (xml or json) |
| 226 |
returned: parse error |
| 227 |
type: str |
| 228 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 229 |
sent: |
| 230 |
description: The actual/minimal configuration pushed to the APIC |
| 231 |
returned: info |
| 232 |
type: list |
| 233 |
sample: |
| 234 |
{ |
| 235 |
"fvTenant": { |
| 236 |
"attributes": { |
| 237 |
"descr": "Production environment" |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
previous: |
| 242 |
description: The original configuration from the APIC before the module has started |
| 243 |
returned: info |
| 244 |
type: list |
| 245 |
sample: |
| 246 |
[ |
| 247 |
{ |
| 248 |
"fvTenant": { |
| 249 |
"attributes": { |
| 250 |
"descr": "Production", |
| 251 |
"dn": "uni/tn-production", |
| 252 |
"name": "production", |
| 253 |
"nameAlias": "", |
| 254 |
"ownerKey": "", |
| 255 |
"ownerTag": "" |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
] |
| 260 |
proposed: |
| 261 |
description: The assembled configuration from the user-provided parameters |
| 262 |
returned: info |
| 263 |
type: dict |
| 264 |
sample: |
| 265 |
{ |
| 266 |
"fvTenant": { |
| 267 |
"attributes": { |
| 268 |
"descr": "Production environment", |
| 269 |
"name": "production" |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
filter_string: |
| 274 |
description: The filter string used for the request |
| 275 |
returned: failure or debug |
| 276 |
type: str |
| 277 |
sample: ?rsp-prop-include=config-only |
| 278 |
method: |
| 279 |
description: The HTTP method used for the request to the APIC |
| 280 |
returned: failure or debug |
| 281 |
type: str |
| 282 |
sample: POST |
| 283 |
response: |
| 284 |
description: The HTTP response from the APIC |
| 285 |
returned: failure or debug |
| 286 |
type: str |
| 287 |
sample: OK (30 bytes) |
| 288 |
status: |
| 289 |
description: The HTTP status from the APIC |
| 290 |
returned: failure or debug |
| 291 |
type: int |
| 292 |
sample: 200 |
| 293 |
url: |
| 294 |
description: The HTTP url used for the request to the APIC |
| 295 |
returned: failure or debug |
| 296 |
type: str |
| 297 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 298 |
""" |
| 299 |
|
| 300 |
from ansible.module_utils.basic import AnsibleModule |
| 301 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ( |
| 302 |
ACIModule, |
| 303 |
aci_argument_spec, |
| 304 |
aci_annotation_spec, |
| 305 |
aci_owner_spec, |
| 306 |
storm_control_policy_rate_spec, |
| 307 |
) |
| 308 |
|
| 309 |
|
| 310 |
def get_rates_configuration(module, configuration, percentage, pps, burst_percentage, burst_pps): |
| 311 |
if configuration is None: |
| 312 |
return {} |
| 313 |
rate = configuration.get("rate") |
| 314 |
burst_rate = configuration.get("burst_rate") |
| 315 |
rate_type = configuration.get("rate_type") |
| 316 |
|
| 317 |
if rate_type == "percentage": |
| 318 |
for rate_name, rate_value in dict(rate=rate, burst_rate=burst_rate).items(): |
| 319 |
if rate_value is None or not (0 <= float(rate_value) <= 100): |
| 320 |
module.fail_json( |
| 321 |
msg="If argument rate_type is percentage, the {0} needs to be a value between 0 and 100 inclusive, got {1}".format( |
| 322 |
rate_name, |
| 323 |
rate_value, |
| 324 |
) |
| 325 |
) |
| 326 |
return { |
| 327 |
percentage: "{0:.6f}".format(float(rate)), |
| 328 |
pps: "unspecified", |
| 329 |
burst_percentage: "{0:.6f}".format(float(burst_rate)), |
| 330 |
burst_pps: "unspecified", |
| 331 |
} |
| 332 |
elif rate_type == "pps": |
| 333 |
return {pps: rate, burst_pps: burst_rate} |
| 334 |
|
| 335 |
|
| 336 |
def main(): |
| 337 |
argument_spec = aci_argument_spec() |
| 338 |
argument_spec.update(aci_annotation_spec()) |
| 339 |
argument_spec.update(aci_owner_spec()) |
| 340 |
argument_spec.update( |
| 341 |
storm_control_policy=dict( |
| 342 |
type="str", required=False, aliases=["name", "storm_control", "storm_control_name"] |
| 343 |
), # Not required for querying all objects |
| 344 |
description=dict(type="str", aliases=["descr"]), |
| 345 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 346 |
name_alias=dict(type="str"), |
| 347 |
all_types_configuration=dict(type="dict", options=storm_control_policy_rate_spec(), aliases=["all_types"]), |
| 348 |
broadcast_configuration=dict(type="dict", options=storm_control_policy_rate_spec(), aliases=["broadcast"]), |
| 349 |
multicast_configuration=dict(type="dict", options=storm_control_policy_rate_spec(), aliases=["multicast"]), |
| 350 |
unicast_configuration=dict(type="dict", options=storm_control_policy_rate_spec(), aliases=["unicast"]), |
| 351 |
storm_control_action=dict(type="str", choices=["drop", "shutdown"]), |
| 352 |
storm_control_soak_action=dict(type="int"), |
| 353 |
) |
| 354 |
|
| 355 |
module = AnsibleModule( |
| 356 |
argument_spec=argument_spec, |
| 357 |
supports_check_mode=True, |
| 358 |
required_if=[ |
| 359 |
["state", "absent", ["storm_control_policy"]], |
| 360 |
["state", "present", ["storm_control_policy"]], |
| 361 |
], |
| 362 |
mutually_exclusive=[ |
| 363 |
("all_types_configuration", "broadcast_configuration"), |
| 364 |
("all_types_configuration", "multicast_configuration"), |
| 365 |
("all_types_configuration", "unicast_configuration"), |
| 366 |
], |
| 367 |
) |
| 368 |
|
| 369 |
aci = ACIModule(module) |
| 370 |
|
| 371 |
storm_control_policy = module.params.get("storm_control_policy") |
| 372 |
description = module.params.get("description") |
| 373 |
state = module.params.get("state") |
| 374 |
name_alias = module.params.get("name_alias") |
| 375 |
storm_control_action = module.params.get("storm_control_action") |
| 376 |
storm_control_soak_action = module.params.get("storm_control_soak_action") |
| 377 |
all_types_configuration = module.params.get("all_types_configuration") |
| 378 |
broadcast_configuration = module.params.get("broadcast_configuration") |
| 379 |
multicast_configuration = module.params.get("multicast_configuration") |
| 380 |
unicast_configuration = module.params.get("unicast_configuration") |
| 381 |
|
| 382 |
rates_input = {} |
| 383 |
|
| 384 |
if all_types_configuration is not None: |
| 385 |
rates_input.update(get_rates_configuration(module, all_types_configuration, "rate", "ratePps", "burstRate", "burstPps")) |
| 386 |
storm_control_types = "Invalid" |
| 387 |
elif any([broadcast_configuration, multicast_configuration, unicast_configuration]): |
| 388 |
rates_input.update(get_rates_configuration(module, broadcast_configuration, "bcRate", "bcRatePps", "bcBurstRate", "bcBurstPps")) |
| 389 |
rates_input.update(get_rates_configuration(module, multicast_configuration, "mcRate", "mcRatePps", "mcBurstRate", "mcBurstPps")) |
| 390 |
rates_input.update(get_rates_configuration(module, unicast_configuration, "uucRate", "uucRatePps", "uucBurstRate", "uucBurstPps")) |
| 391 |
storm_control_types = "Valid" |
| 392 |
|
| 393 |
aci.construct_url( |
| 394 |
root_class=dict( |
| 395 |
aci_class="infraInfra", |
| 396 |
aci_rn="infra", |
| 397 |
), |
| 398 |
subclass_1=dict( |
| 399 |
aci_class="stormctrlIfPol", |
| 400 |
aci_rn="stormctrlifp-{0}".format(storm_control_policy), |
| 401 |
module_object=storm_control_policy, |
| 402 |
target_filter={"name": storm_control_policy}, |
| 403 |
), |
| 404 |
) |
| 405 |
|
| 406 |
aci.get_existing() |
| 407 |
|
| 408 |
if state == "present": |
| 409 |
class_config = dict( |
| 410 |
name=storm_control_policy, |
| 411 |
descr=description, |
| 412 |
nameAlias=name_alias, |
| 413 |
isUcMcBcStormPktCfgValid=storm_control_types, |
| 414 |
stormCtrlAction=storm_control_action, |
| 415 |
stormCtrlSoakInstCount=storm_control_soak_action, |
| 416 |
) |
| 417 |
class_config.update(rates_input) |
| 418 |
aci.payload( |
| 419 |
aci_class="stormctrlIfPol", |
| 420 |
class_config=class_config, |
| 421 |
) |
| 422 |
|
| 423 |
aci.get_diff(aci_class="stormctrlIfPol") |
| 424 |
|
| 425 |
aci.post_config() |
| 426 |
|
| 427 |
elif state == "absent": |
| 428 |
aci.delete_config() |
| 429 |
|
| 430 |
aci.exit_json() |
| 431 |
|
| 432 |
|
| 433 |
if __name__ == "__main__": |
| 434 |
main() |