|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright: (c) 2024, Philipp Rintz (@p-rintz) <[email protected]> |
| 4 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | + |
| 6 | +from __future__ import absolute_import, division, print_function |
| 7 | + |
| 8 | +__metaclass__ = type |
| 9 | + |
| 10 | +DOCUMENTATION = r""" |
| 11 | +--- |
| 12 | +module: netbox_custom_field_choice_set |
| 13 | +short_description: Creates, updates or deletes custom field choice sets within Netbox |
| 14 | +description: |
| 15 | + - Creates, updates or removes custom fields choice sets from Netbox |
| 16 | +notes: |
| 17 | + - This should be run with connection C(local) and hosts C(localhost) |
| 18 | +author: |
| 19 | + - Philipp Rintz (@p-rintz) |
| 20 | +requirements: |
| 21 | + - pynetbox |
| 22 | +version_added: "3.6.0" |
| 23 | +extends_documentation_fragment: |
| 24 | + - netbox.netbox.common |
| 25 | +options: |
| 26 | + data: |
| 27 | + type: dict |
| 28 | + description: |
| 29 | + - Defines the choice set |
| 30 | + suboptions: |
| 31 | + name: |
| 32 | + description: |
| 33 | + - Name of the choice set |
| 34 | + required: true |
| 35 | + type: str |
| 36 | + description: |
| 37 | + description: |
| 38 | + - Description of the choice set |
| 39 | + required: false |
| 40 | + type: str |
| 41 | + extra_choices: |
| 42 | + description: |
| 43 | + - List of available choices in the choice set |
| 44 | + required: false |
| 45 | + default: [] |
| 46 | + type: list |
| 47 | + elements: list |
| 48 | + base_choices: |
| 49 | + description: |
| 50 | + - Selection of base choice to use in the choice set |
| 51 | + required: false |
| 52 | + type: str |
| 53 | + choices: |
| 54 | + - IATA |
| 55 | + - ISO_3166 |
| 56 | + - UN_LOCODE |
| 57 | + order_alphabetically: |
| 58 | + description: |
| 59 | + - Order the choices alphabetically |
| 60 | + required: false |
| 61 | + type: bool |
| 62 | + required: true |
| 63 | +""" |
| 64 | + |
| 65 | +EXAMPLES = r""" |
| 66 | +- name: "Test Netbox custom_field_choice_set module" |
| 67 | + connection: local |
| 68 | + hosts: localhost |
| 69 | + tasks: |
| 70 | + - name: Create a choice set with choices |
| 71 | + netbox.netbox.netbox_custom_field_choice_set: |
| 72 | + netbox_url: http://netbox.local |
| 73 | + netbox_token: thisIsMyToken |
| 74 | + data: |
| 75 | + name: "ChoiceSetName" |
| 76 | + description: "Choice Set Description" |
| 77 | + extra_choices: |
| 78 | + - ['choice1', 'label1'] |
| 79 | + - ['choice2', 'label2'] |
| 80 | +
|
| 81 | + - name: Create a choice set with a base choice |
| 82 | + netbox.netbox.netbox_custom_field_choice_set: |
| 83 | + netbox_url: http://netbox.local |
| 84 | + netbox_token: thisIsMyToken |
| 85 | + data: |
| 86 | + name: "ChoiceSetName" |
| 87 | + description: "Choice Set Description" |
| 88 | + order_alphabetically: true |
| 89 | + base_choices: "IATA" |
| 90 | +""" |
| 91 | + |
| 92 | +RETURN = r""" |
| 93 | +custom_field_choice_set: |
| 94 | + description: Serialized object as created/existent/updated/deleted within NetBox |
| 95 | + returned: always |
| 96 | + type: dict |
| 97 | +msg: |
| 98 | + description: Message indicating failure or info about what has been achieved |
| 99 | + returned: always |
| 100 | + type: str |
| 101 | +""" |
| 102 | + |
| 103 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( |
| 104 | + NetboxAnsibleModule, |
| 105 | + NETBOX_ARG_SPEC, |
| 106 | +) |
| 107 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_extras import ( |
| 108 | + NetboxExtrasModule, |
| 109 | + NB_CUSTOM_FIELD_CHOICE_SETS, |
| 110 | +) |
| 111 | +from copy import deepcopy |
| 112 | + |
| 113 | + |
| 114 | +def main(): |
| 115 | + """ |
| 116 | + Main entry point for module execution |
| 117 | + """ |
| 118 | + argument_spec = deepcopy(NETBOX_ARG_SPEC) |
| 119 | + argument_spec.update( |
| 120 | + dict( |
| 121 | + data=dict( |
| 122 | + type="dict", |
| 123 | + required=True, |
| 124 | + options=dict( |
| 125 | + name=dict(required=True, type="str"), |
| 126 | + description=dict(required=False, type="str"), |
| 127 | + base_choices=dict( |
| 128 | + required=False, |
| 129 | + type="str", |
| 130 | + choices=[ |
| 131 | + "IATA", |
| 132 | + "ISO_3166", |
| 133 | + "UN_LOCODE", |
| 134 | + ], |
| 135 | + ), |
| 136 | + extra_choices=dict( |
| 137 | + required=False, |
| 138 | + default=[], |
| 139 | + type="list", |
| 140 | + elements="list", |
| 141 | + ), |
| 142 | + order_alphabetically=dict(required=False, type="bool"), |
| 143 | + ), |
| 144 | + ) |
| 145 | + ) |
| 146 | + ) |
| 147 | + |
| 148 | + required_if = [ |
| 149 | + ("state", "present", ["name"]), |
| 150 | + ("state", "absent", ["name"]), |
| 151 | + ] |
| 152 | + |
| 153 | + module = NetboxAnsibleModule( |
| 154 | + argument_spec=argument_spec, supports_check_mode=True, required_if=required_if |
| 155 | + ) |
| 156 | + |
| 157 | + netbox_custom_field_choice_set = NetboxExtrasModule( |
| 158 | + module, NB_CUSTOM_FIELD_CHOICE_SETS |
| 159 | + ) |
| 160 | + netbox_custom_field_choice_set.run() |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": # pragma: no cover |
| 164 | + main() |
0 commit comments