Skip to content

Commit 5b4a04a

Browse files
committed
Create tag module
* 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
1 parent b8cee06 commit 5b4a04a

File tree

12 files changed

+575
-1
lines changed

12 files changed

+575
-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/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Modules
2626
* :ref:`nameserver <ansible_collections.codeaffen.phpipam.nameserver_module>` -- Manage nameservers
2727
* :ref:`section <ansible_collections.codeaffen.phpipam.section_module>` -- Manage sections
2828
* :ref:`subnet <ansible_collections.codeaffen.phpipam.subnet_module>` -- Manage subnets
29+
* :ref:`tags <ansible_collections.codeaffen.phpipam.tags_module>` -- Manage tags
2930
* :ref:`vlan <ansible_collections.codeaffen.phpipam.vlan_module>` -- Manage vlans
3031
* :ref:`vrf <ansible_collections.codeaffen.phpipam.vrf_module>` -- Manage virtual routers and forwarders
3132

@@ -46,5 +47,6 @@ Modules
4647
nameserver_module
4748
section_module
4849
subnet_module
50+
tags_module
4951
vlan_module
5052
vrf_module

docs/plugins/tags_module.rst

Lines changed: 365 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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env python
2+
3+
# -*- coding: utf-8 -*-
4+
# (c) Christian Meißner 2020
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+
from sys import modules
21+
__metaclass__ = type
22+
23+
DOCUMENTATION = '''
24+
---
25+
module: tag
26+
version_added: 1.4.0
27+
short_description: Manage tags
28+
description:
29+
- create, update and delete tags
30+
author:
31+
- "Christian Meißner (@cmeissner)"
32+
options:
33+
name:
34+
description: Name of the given tag
35+
type: str
36+
required: true
37+
show_tag:
38+
description: Show tag or not
39+
type: bool
40+
required: false
41+
default: no
42+
bg_color:
43+
description: Background color of the given tag
44+
type: str
45+
required: true
46+
fg_color:
47+
description: Foreground color of the given tag
48+
type: str
49+
required: true
50+
compress_range:
51+
description: Compress range or not
52+
type: bool
53+
required: false
54+
default: no
55+
update_tags:
56+
description: Update tags or not
57+
type: bool
58+
required: false
59+
default: no
60+
is_locked:
61+
description: Lock tag or not
62+
type: bool
63+
required: false
64+
default: no
65+
extends_documentation_fragment:
66+
- codeaffen.phpipam.phpipam
67+
- codeaffen.phpipam.phpipam.entity_state
68+
'''
69+
70+
EXAMPLES = '''
71+
- name: "Create tag"
72+
codeaffen.phpipam.tag:
73+
username: "admin"
74+
password: "s3cr3t"
75+
server_url: "https://ipam.example.com"
76+
name: "my tag"
77+
bg_color: #ffffff
78+
fg_color: #000000
79+
state: present
80+
81+
- name: "Remove tag"
82+
codeaffen.phpipam.tag:
83+
username: "admin"
84+
password: "s3cr3t"
85+
server_url: "https://ipam.example.com"
86+
name: "my tag"
87+
state: absent
88+
'''
89+
90+
import traceback
91+
from ansible_collections.codeaffen.phpipam.plugins.module_utils.phpipam_helper import PhpipamEntityAnsibleModule, missing_required_lib
92+
try:
93+
from colour import Color
94+
HAS_COLOUR = True
95+
except ImportError:
96+
HAS_COLOUR = False
97+
COLOUR_IMP_ERR = traceback.format_exc()
98+
99+
100+
class PhpipamToolsTagsModule(PhpipamEntityAnsibleModule):
101+
pass
102+
103+
104+
def main():
105+
module = PhpipamToolsTagsModule(
106+
phpipam_spec=dict(
107+
id=dict(type='int', invisible=True, phpipam_name='id'),
108+
name=dict(type='str', required=True, phpipam_name='type'),
109+
show_tag=dict(type='bool', required=False, default=False, phpipam_name='showtag'),
110+
bg_color=dict(type='str', required=True, phpipam_name='bgcolor'),
111+
fg_color=dict(type='str', required=True, phpipam_name='fgcolor'),
112+
compress_range=dict(type='bool', default=False),
113+
update_tags=dict(type='bool', required=False, default=False, phpipam_name='updateTag'),
114+
is_locked=dict(type='bool', default='no'),
115+
)
116+
)
117+
118+
def get_color_code(color):
119+
try:
120+
c = Color(color)
121+
return c.get_hex()
122+
except ValueError:
123+
module.fail_json(msg="Invalid color: {}".format(color))
124+
125+
if not HAS_COLOUR:
126+
module.fail_json(msg=missing_required_lib("ipaddress"), exception=COLOUR_IMP_ERR)
127+
128+
module_params = module.phpipam_params
129+
130+
module_params['bg_color'] = get_color_code(module_params['bg_color'])
131+
module_params['fg_color'] = get_color_code(module_params['fg_color'])
132+
module_params['locked'] = 'yes' if module_params['is_locked'] else 'no'
133+
module_params['compress'] = 'yes' if module_params['compress_range'] else 'no'
134+
135+
del(module_params['is_locked'])
136+
del(module_params['compress_range'])
137+
138+
with module.api_connection():
139+
module.run()
140+
141+
142+
if __name__ == "__main__":
143+
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+
name: delete tag
34+
override:
35+
state: absent
36+
tag: "{{ base_tag_data | combine(override) }}"

0 commit comments

Comments
 (0)