|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# -*- coding: utf-8 -*- |
| 4 | +# (c) Christian Meißner 2021 |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from __future__ import absolute_import, division, print_function |
| 20 | +__metaclass__ = type |
| 21 | + |
| 22 | +DOCUMENTATION = ''' |
| 23 | +--- |
| 24 | +module: tag |
| 25 | +version_added: 1.4.0 |
| 26 | +short_description: Manage tags |
| 27 | +description: |
| 28 | + - create, update and delete tags |
| 29 | +author: |
| 30 | + - "Christian Meißner (@cmeissner)" |
| 31 | +options: |
| 32 | + name: |
| 33 | + description: Name of the given tag |
| 34 | + type: str |
| 35 | + required: true |
| 36 | + aliases: [type] |
| 37 | + show_tag: |
| 38 | + description: Show tag or not |
| 39 | + type: bool |
| 40 | + required: false |
| 41 | + default: no |
| 42 | + bg_color: |
| 43 | + description: |
| 44 | + - Background color of the given tag |
| 45 | + - Can be a valid color name or a hex code |
| 46 | + type: str |
| 47 | + required: true |
| 48 | + fg_color: |
| 49 | + description: |
| 50 | + - Foreground color of the given tag |
| 51 | + - Can be a valid color name or a hex code |
| 52 | + type: str |
| 53 | + required: true |
| 54 | + compress_range: |
| 55 | + description: Compress range or not |
| 56 | + type: bool |
| 57 | + required: false |
| 58 | + default: no |
| 59 | + update_tags: |
| 60 | + description: Update tags or not |
| 61 | + type: bool |
| 62 | + required: false |
| 63 | + default: no |
| 64 | + is_locked: |
| 65 | + description: Lock tag or not |
| 66 | + type: bool |
| 67 | + required: false |
| 68 | + default: no |
| 69 | +requirements: |
| 70 | + - colour |
| 71 | + - inflection |
| 72 | + - ipaddress |
| 73 | + - phpypam>=1.0.0 |
| 74 | +extends_documentation_fragment: |
| 75 | + - codeaffen.phpipam.phpipam |
| 76 | + - codeaffen.phpipam.phpipam.entity_state |
| 77 | +''' |
| 78 | + |
| 79 | +EXAMPLES = ''' |
| 80 | +- name: "Create tag" |
| 81 | + codeaffen.phpipam.tag: |
| 82 | + username: "admin" |
| 83 | + password: "s3cr3t" |
| 84 | + server_url: "https://ipam.example.com" |
| 85 | + name: "my tag" |
| 86 | + bg_color: #ffffff |
| 87 | + fg_color: #000000 |
| 88 | + state: present |
| 89 | +
|
| 90 | +- name: "Remove tag" |
| 91 | + codeaffen.phpipam.tag: |
| 92 | + username: "admin" |
| 93 | + password: "s3cr3t" |
| 94 | + server_url: "https://ipam.example.com" |
| 95 | + name: "my tag" |
| 96 | + state: absent |
| 97 | +''' |
| 98 | + |
| 99 | +import traceback |
| 100 | +from ansible_collections.codeaffen.phpipam.plugins.module_utils.phpipam_helper import PhpipamEntityAnsibleModule, missing_required_lib |
| 101 | +try: |
| 102 | + from colour import Color |
| 103 | + HAS_COLOUR = True |
| 104 | +except ImportError: |
| 105 | + HAS_COLOUR = False |
| 106 | + COLOUR_IMP_ERR = traceback.format_exc() |
| 107 | + |
| 108 | + |
| 109 | +class PhpipamToolsTagsModule(PhpipamEntityAnsibleModule): |
| 110 | + pass |
| 111 | + |
| 112 | + |
| 113 | +def main(): |
| 114 | + module = PhpipamToolsTagsModule( |
| 115 | + phpipam_spec=dict( |
| 116 | + id=dict(type='int', invisible=True, phpipam_name='id'), |
| 117 | + name=dict(type='str', required=True, aliases=['type'], phpipam_name='type'), |
| 118 | + show_tag=dict(type='bool', required=False, default=False, phpipam_name='showtag'), |
| 119 | + bg_color=dict(type='str', required=True, phpipam_name='bgcolor'), |
| 120 | + fg_color=dict(type='str', required=True, phpipam_name='fgcolor'), |
| 121 | + compress_range=dict(type='bool', default=False), |
| 122 | + update_tags=dict(type='bool', required=False, default=False, phpipam_name='updateTag'), |
| 123 | + is_locked=dict(type='bool', default='no'), |
| 124 | + ) |
| 125 | + ) |
| 126 | + |
| 127 | + def get_color_code(color): |
| 128 | + try: |
| 129 | + c = Color(color) |
| 130 | + return c.get_hex() |
| 131 | + except ValueError: |
| 132 | + module.fail_json(msg="Invalid color: {}".format(color)) |
| 133 | + |
| 134 | + if not HAS_COLOUR: |
| 135 | + module.fail_json(msg=missing_required_lib("colour"), exception=COLOUR_IMP_ERR) |
| 136 | + |
| 137 | + module_params = module.phpipam_params |
| 138 | + |
| 139 | + module_params['bg_color'] = get_color_code(module_params['bg_color']) |
| 140 | + module_params['fg_color'] = get_color_code(module_params['fg_color']) |
| 141 | + module_params['locked'] = 'yes' if module_params['is_locked'] else 'no' |
| 142 | + module_params['compress'] = 'yes' if module_params['compress_range'] else 'no' |
| 143 | + |
| 144 | + del(module_params['is_locked']) |
| 145 | + del(module_params['compress_range']) |
| 146 | + |
| 147 | + with module.api_connection(): |
| 148 | + module.run() |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + main() |
0 commit comments