|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright: (c) 2025, Daniel Chiquito (@dchiquito) <daniel.chiquito@gmail.com> |
| 4 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | + |
| 6 | +__metaclass__ = type |
| 7 | + |
| 8 | +DOCUMENTATION = r""" |
| 9 | +--- |
| 10 | +module: netbox_data_source |
| 11 | +short_description: Creates or removes data sources from NetBox |
| 12 | +description: |
| 13 | + - Creates or removes data sources from NetBox |
| 14 | +author: |
| 15 | + - Daniel Chiquito (@dchiquito) |
| 16 | +requirements: |
| 17 | + - pynetbox |
| 18 | +version_added: "3.22.0" |
| 19 | +extends_documentation_fragment: |
| 20 | + - netbox.netbox.common |
| 21 | +options: |
| 22 | + data: |
| 23 | + type: dict |
| 24 | + description: |
| 25 | + - Defines the data source configuration |
| 26 | + suboptions: |
| 27 | + name: |
| 28 | + description: |
| 29 | + - Name of the data source |
| 30 | + required: true |
| 31 | + type: str |
| 32 | + type: |
| 33 | + description: |
| 34 | + - The origin of the data source |
| 35 | + choices: |
| 36 | + - local |
| 37 | + - git |
| 38 | + - amazon-s3 |
| 39 | + required: false |
| 40 | + type: str |
| 41 | + source_url: |
| 42 | + description: |
| 43 | + - URL of the data source to be created |
| 44 | + required: false |
| 45 | + type: str |
| 46 | + enabled: |
| 47 | + description: |
| 48 | + - Whether or not this data source can be synced |
| 49 | + required: false |
| 50 | + type: bool |
| 51 | + description: |
| 52 | + description: |
| 53 | + - Description of the data source |
| 54 | + required: false |
| 55 | + type: str |
| 56 | + ignore_rules: |
| 57 | + description: |
| 58 | + - Patterns (one per line) matching files to ignore when syncing |
| 59 | + required: false |
| 60 | + type: str |
| 61 | + sync_interval: |
| 62 | + description: |
| 63 | + - The interval in seconds between syncs |
| 64 | + required: false |
| 65 | + choices: |
| 66 | + - 1 |
| 67 | + - 60 |
| 68 | + - 720 |
| 69 | + - 1440 |
| 70 | + - 10080 |
| 71 | + - 43200 |
| 72 | + type: int |
| 73 | + comments: |
| 74 | + description: |
| 75 | + - Comments about the data source |
| 76 | + required: false |
| 77 | + type: str |
| 78 | + required: true |
| 79 | +""" |
| 80 | + |
| 81 | +EXAMPLES = r""" |
| 82 | +- name: "Test NetBox modules" |
| 83 | + connection: local |
| 84 | + hosts: localhost |
| 85 | + gather_facts: false |
| 86 | +
|
| 87 | + tasks: |
| 88 | + - name: "Create a new data source with only required information" |
| 89 | + netbox.netbox.netbox_data_source: |
| 90 | + netbox_url: http://netbox.local |
| 91 | + netbox_token: thisIsMyToken |
| 92 | + data: |
| 93 | + name: "Data Source 1" |
| 94 | + type: "local" |
| 95 | + source_url: "/tmp/data-source.txt" |
| 96 | + enabled: true |
| 97 | + state: present |
| 98 | + - name: "Update that data source with other fields" |
| 99 | + netbox.netbox.netbox_data_source: |
| 100 | + netbox_url: http://netbox.local |
| 101 | + netbox_token: thisIsMyToken |
| 102 | + data: |
| 103 | + name: "Data Source 1" |
| 104 | + type: "amazon-s3" |
| 105 | + source_url: "path/to/bucket" |
| 106 | + enabled: false |
| 107 | + description: "My first data source" |
| 108 | + ignore_rules: ".*\nfoo.txt\n*.yml" |
| 109 | + sync_interval: 1440 |
| 110 | + comments: "Some commentary on this data source" |
| 111 | + state: present |
| 112 | + - name: "Delete the data source" |
| 113 | + netbox.netbox.netbox_data_source: |
| 114 | + netbox_url: http://netbox.local |
| 115 | + netbox_token: thisIsMyToken |
| 116 | + data: |
| 117 | + name: "Data Source 1" |
| 118 | + state: absent |
| 119 | +""" |
| 120 | + |
| 121 | +RETURN = r""" |
| 122 | +data_source: |
| 123 | + description: Serialized object as created or already existent within NetBox |
| 124 | + returned: on creation |
| 125 | + type: dict |
| 126 | +msg: |
| 127 | + description: Message indicating failure or info about what has been achieved |
| 128 | + returned: always |
| 129 | + type: str |
| 130 | +""" |
| 131 | + |
| 132 | + |
| 133 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( |
| 134 | + NetboxAnsibleModule, |
| 135 | + NETBOX_ARG_SPEC, |
| 136 | +) |
| 137 | +from ansible_collections.netbox.netbox.plugins.module_utils.netbox_core import ( |
| 138 | + NetboxCoreModule, |
| 139 | + NB_DATA_SOURCES, |
| 140 | +) |
| 141 | +from copy import deepcopy |
| 142 | + |
| 143 | + |
| 144 | +def main(): |
| 145 | + """ |
| 146 | + Main entry point for module execution |
| 147 | + """ |
| 148 | + argument_spec = deepcopy(NETBOX_ARG_SPEC) |
| 149 | + argument_spec.update( |
| 150 | + dict( |
| 151 | + data=dict( |
| 152 | + type="dict", |
| 153 | + required=True, |
| 154 | + options=dict( |
| 155 | + name=dict(required=True, type="str"), |
| 156 | + type=dict( |
| 157 | + required=False, |
| 158 | + choices=["local", "git", "amazon-s3"], |
| 159 | + type="str", |
| 160 | + ), |
| 161 | + source_url=dict(required=False, type="str"), |
| 162 | + enabled=dict(required=False, type="bool"), |
| 163 | + description=dict(required=False, type="str"), |
| 164 | + ignore_rules=dict(required=False, type="str"), |
| 165 | + sync_interval=dict( |
| 166 | + required=False, |
| 167 | + choices=[1, 60, 60 * 12, 60 * 24, 60 * 24 * 7, 60 * 24 * 30], |
| 168 | + type="int", |
| 169 | + ), |
| 170 | + comments=dict(required=False, type="str"), |
| 171 | + ), |
| 172 | + ), |
| 173 | + ) |
| 174 | + ) |
| 175 | + |
| 176 | + required_if = [ |
| 177 | + ("state", "present", ["name", "type", "source_url", "enabled"]), |
| 178 | + ("state", "absent", ["name"]), |
| 179 | + ] |
| 180 | + |
| 181 | + module = NetboxAnsibleModule( |
| 182 | + argument_spec=argument_spec, supports_check_mode=True, required_if=required_if |
| 183 | + ) |
| 184 | + |
| 185 | + netbox_data_source = NetboxCoreModule(module, NB_DATA_SOURCES) |
| 186 | + netbox_data_source.run() |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == "__main__": # pragma: no cover |
| 190 | + main() |
0 commit comments