Skip to content

Commit b8b1ddd

Browse files
cmeissnerlush
andauthored
Create tag module (#60)
* update requirements for tag module * create tag module * remove fixed flake8 excludes * update find_current_entity to handle tags * add tests for tag module * add tag module documentation * add changelog fragment for tag module Co-authored-by: Alx Dll <porous@web.de>
1 parent 0132ba4 commit b8b1ddd

File tree

11 files changed

+586
-1
lines changed

11 files changed

+586
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ info:
3030
@echo " $(foreach PLUGIN_TYPE,$(PLUGIN_TYPES), $(PLUGIN_TYPE):\n $(foreach PLUGIN,$(basename $(notdir $(_$(PLUGIN_TYPE)))), - $(PLUGIN)\n)\n)"
3131

3232
lint: $(MANIFEST)
33-
flake8 --ignore=E402,W503 --max-line-length=160 plugins/ tests/
33+
flake8 plugins/ tests/
3434

3535
$(MANIFEST): $(NAMESPACE)-$(NAME)-$(VERSION).tar.gz
3636
ansible-galaxy collection install -p build/collections $< --force

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ A last option to read the docs is the docs folder in this repository.
4545

4646
The following dependencies have to be fulfiled by the Ansible controller.
4747

48+
* colour
4849
* inflection
4950
* ipaddress
5051
* phpypam>=1.0.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- add `tag_module` to `create`, `update` and `delete` tags

docs/plugins/tag_module.rst

Lines changed: 369 additions & 0 deletions
Large diffs are not rendered by default.

plugins/module_utils/phpipam_helper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ def find_current_entity(self):
190190
entity = self.find_device(self.phpipam_params['hostname'])
191191
elif self.controller_name == 'tools/device_type':
192192
entity = self.find_device_type(self.phpipam_params['name'])
193+
elif self.controller_name == 'tools/tags':
194+
entity = self.find_by_key(self.controller_uri, self.phpipam_params['name'], key='type')
193195
elif 'tools' in self.controller_uri or self.controller_name in ['vlan', 'l2domain', 'vrf']:
194196
entity = self.find_by_key(self.controller_uri, self.phpipam_params['name'])
195197
else:

plugins/modules/tag.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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()

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ansible
2+
colour
23
wheel
34
jinja2 # pyup: ignore
45
PyYAML~=5.3

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
colour
12
inflection
23
ipaddress
34
phpypam

tests/test_playbooks/tag.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
- hosts: localhost
3+
collections:
4+
- codeaffen.phpipam
5+
gather_facts: false
6+
vars_files:
7+
- vars/server.yml
8+
- vars/tag.yml
9+
tasks:
10+
- name: create tag
11+
include: tasks/tag.yml
12+
vars:
13+
name: create tag
14+
tag: "{{ base_tag_data }}"
15+
16+
- name: create tag again, no change
17+
include: tasks/tag.yml
18+
vars:
19+
name: create tag again, no change
20+
tag: "{{ base_tag_data }}"
21+
22+
- name: update tag
23+
include: tasks/tag.yml
24+
vars:
25+
name: update tag
26+
override:
27+
bg_color: yellow
28+
tag: "{{ base_tag_data | combine(override) }}"
29+
30+
- name: delete tag
31+
include: tasks/tag.yml
32+
vars:
33+
type: delete tag
34+
override:
35+
state: absent
36+
tag: "{{ base_tag_data | combine(override) }}"

tests/test_playbooks/tasks/tag.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
- name: "Ensure state of tag: {{ name }}"
3+
tag:
4+
server_url: "{{ phpipam_server_url }}"
5+
app_id: "{{ phpipam_app_id }}"
6+
username: "{{ phpipam_username }}"
7+
password: "{{ phpipam_password }}"
8+
name: "{{ tag.name }}"
9+
show_tag: "{{ tag.show_tag | default(omit) }}"
10+
bg_color: "{{ tag.bg_color | default('white') }}"
11+
fg_color: "{{ tag.fg_color | default('black') }}"
12+
compress_range: "{{ tag.compress_range | default(omit) }}"
13+
update_tags: "{{ tag.update_tags | default(omit) }}"
14+
locked: "{{ tag.locked | default(omit) }}"
15+
state: "{{ tag.state | default('present') }}"

0 commit comments

Comments
 (0)