|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# © 2020 Nokia |
| 4 | +# Licensed under the GNU General Public License v3.0 only |
| 5 | +# SPDX-License-Identifier: GPL-3.0-only |
| 6 | + |
| 7 | +from __future__ import absolute_import, division, print_function |
| 8 | + |
| 9 | +__metaclass__ = type |
| 10 | + |
| 11 | +ANSIBLE_METADATA = { |
| 12 | + "metadata_version": "1.1", |
| 13 | + "status": ["preview"], |
| 14 | + "supported_by": "community", |
| 15 | +} |
| 16 | + |
| 17 | +DOCUMENTATION = r""" |
| 18 | +--- |
| 19 | +module: netbox_front_port |
| 20 | +short_description: Create, update or delete front ports within Netbox |
| 21 | +description: |
| 22 | + - Creates, updates or removes front ports from Netbox |
| 23 | +notes: |
| 24 | + - Tags should be defined as a YAML list |
| 25 | + - This should be ran with connection C(local) and hosts C(localhost) |
| 26 | +author: |
| 27 | + - Tobias Groß (@toerb) |
| 28 | +requirements: |
| 29 | + - pynetbox |
| 30 | +version_added: '0.2.3' |
| 31 | +options: |
| 32 | + netbox_url: |
| 33 | + description: |
| 34 | + - URL of the Netbox instance resolvable by Ansible control host |
| 35 | + required: true |
| 36 | + type: str |
| 37 | + netbox_token: |
| 38 | + description: |
| 39 | + - The token created within Netbox to authorize API access |
| 40 | + required: true |
| 41 | + type: str |
| 42 | + data: |
| 43 | + type: dict |
| 44 | + required: true |
| 45 | + description: |
| 46 | + - Defines the front port configuration |
| 47 | + suboptions: |
| 48 | + device: |
| 49 | + description: |
| 50 | + - The device the front port is attached to |
| 51 | + required: true |
| 52 | + type: raw |
| 53 | + name: |
| 54 | + description: |
| 55 | + - The name of the front port |
| 56 | + required: true |
| 57 | + type: str |
| 58 | + type: |
| 59 | + description: |
| 60 | + - The type of the front port |
| 61 | + choices: |
| 62 | + - 8p8c |
| 63 | + - 110-punch |
| 64 | + - bnc |
| 65 | + - mrj21 |
| 66 | + - fc |
| 67 | + - lc |
| 68 | + - lc-apc |
| 69 | + - lsh |
| 70 | + - lsh-apc |
| 71 | + - mpo |
| 72 | + - mtrj |
| 73 | + - sc |
| 74 | + - sc-apc |
| 75 | + - st |
| 76 | + required: true |
| 77 | + type: str |
| 78 | + rear_port: |
| 79 | + description: |
| 80 | + - The rear_port the front port is attached to |
| 81 | + required: true |
| 82 | + type: raw |
| 83 | + rear_port_position: |
| 84 | + description: |
| 85 | + - The position of the rear port this front port is connected to |
| 86 | + required: false |
| 87 | + type: int |
| 88 | + description: |
| 89 | + description: |
| 90 | + - Description of the front port |
| 91 | + required: false |
| 92 | + type: str |
| 93 | + tags: |
| 94 | + description: |
| 95 | + - Any tags that the front port may need to be associated with |
| 96 | + required: false |
| 97 | + type: list |
| 98 | + state: |
| 99 | + description: |
| 100 | + - Use C(present) or C(absent) for adding or removing. |
| 101 | + choices: [ absent, present ] |
| 102 | + default: present |
| 103 | + type: str |
| 104 | + query_params: |
| 105 | + description: |
| 106 | + - This can be used to override the specified values in ALLOWED_QUERY_PARAMS that is defined |
| 107 | + - in plugins/module_utils/netbox_utils.py and provides control to users on what may make |
| 108 | + - an object unique in their environment. |
| 109 | + required: false |
| 110 | + type: list |
| 111 | + validate_certs: |
| 112 | + description: |
| 113 | + - If C(no), SSL certificates will not be validated. This should only be used on personally controlled sites using self-signed certificates. |
| 114 | + default: true |
| 115 | + type: raw |
| 116 | +""" |
| 117 | + |
| 118 | +EXAMPLES = r""" |
| 119 | +- name: "Test Netbox modules" |
| 120 | + connection: local |
| 121 | + hosts: localhost |
| 122 | + gather_facts: False |
| 123 | +
|
| 124 | + tasks: |
| 125 | + - name: Create front port within Netbox with only required information |
| 126 | + netbox_front_port: |
| 127 | + netbox_url: http://netbox.local |
| 128 | + netbox_token: thisIsMyToken |
| 129 | + data: |
| 130 | + name: Test Front Port |
| 131 | + device: Test Device |
| 132 | + type: bnc |
| 133 | + rear_port: Test Rear Port |
| 134 | + state: present |
| 135 | +
|
| 136 | + - name: Update front port with other fields |
| 137 | + netbox_front_port: |
| 138 | + netbox_url: http://netbox.local |
| 139 | + netbox_token: thisIsMyToken |
| 140 | + data: |
| 141 | + name: Test Front Port |
| 142 | + device: Test Device |
| 143 | + type: bnc |
| 144 | + rear_port: Test Rear Port |
| 145 | + rear_port_position: 5 |
| 146 | + description: front port description |
| 147 | + state: present |
| 148 | +
|
| 149 | + - name: Delete front port within netbox |
| 150 | + netbox_front_port: |
| 151 | + netbox_url: http://netbox.local |
| 152 | + netbox_token: thisIsMyToken |
| 153 | + data: |
| 154 | + name: Test Front Port |
| 155 | + device: Test Device |
| 156 | + type: bnc |
| 157 | + rear_port: Test Rear Port |
| 158 | + state: absent |
| 159 | +""" |
| 160 | + |
| 161 | +RETURN = r""" |
| 162 | +front_port: |
| 163 | + description: Serialized object as created or already existent within Netbox |
| 164 | + returned: success (when I(state=present)) |
| 165 | + type: dict |
| 166 | +msg: |
| 167 | + description: Message indicating failure or info about what has been achieved |
| 168 | + returned: always |
| 169 | + type: str |
| 170 | +""" |
| 171 | + |
| 172 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( |
| 173 | + NetboxAnsibleModule, |
| 174 | + NETBOX_ARG_SPEC, |
| 175 | +) |
| 176 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_dcim import ( |
| 177 | + NetboxDcimModule, |
| 178 | + NB_FRONT_PORTS, |
| 179 | +) |
| 180 | +from copy import deepcopy |
| 181 | + |
| 182 | + |
| 183 | +def main(): |
| 184 | + """ |
| 185 | + Main entry point for module execution |
| 186 | + """ |
| 187 | + argument_spec = deepcopy(NETBOX_ARG_SPEC) |
| 188 | + argument_spec.update( |
| 189 | + dict( |
| 190 | + data=dict( |
| 191 | + type="dict", |
| 192 | + required=True, |
| 193 | + options=dict( |
| 194 | + device=dict(required=True, type="raw"), |
| 195 | + name=dict(required=True, type="str"), |
| 196 | + type=dict( |
| 197 | + required=True, |
| 198 | + choices=[ |
| 199 | + "8p8c", |
| 200 | + "110-punch", |
| 201 | + "bnc", |
| 202 | + "mrj21", |
| 203 | + "fc", |
| 204 | + "lc", |
| 205 | + "lc-apc", |
| 206 | + "lsh", |
| 207 | + "lsh-apc", |
| 208 | + "mpo", |
| 209 | + "mtrj", |
| 210 | + "sc", |
| 211 | + "sc-apc", |
| 212 | + "st", |
| 213 | + ], |
| 214 | + type="str", |
| 215 | + ), |
| 216 | + rear_port=dict(required=True, type="raw"), |
| 217 | + rear_port_position=dict(required=False, type="int"), |
| 218 | + description=dict(required=False, type="str"), |
| 219 | + tags=dict(required=False, type="list"), |
| 220 | + ), |
| 221 | + ), |
| 222 | + ) |
| 223 | + ) |
| 224 | + |
| 225 | + required_if = [ |
| 226 | + ("state", "present", ["device", "name", "type", "rear_port"]), |
| 227 | + ("state", "absent", ["device", "name", "type", "rear_port"]), |
| 228 | + ] |
| 229 | + |
| 230 | + module = NetboxAnsibleModule( |
| 231 | + argument_spec=argument_spec, supports_check_mode=True, required_if=required_if |
| 232 | + ) |
| 233 | + |
| 234 | + netbox_front_port = NetboxDcimModule(module, NB_FRONT_PORTS) |
| 235 | + netbox_front_port.run() |
| 236 | + |
| 237 | + |
| 238 | +if __name__ == "__main__": # pragma: no cover |
| 239 | + main() |
0 commit comments