Skip to content

Commit ebab6a3

Browse files
authored
Fix #80 - Can't add VLAN to subnet (#81)
* fix testcase for subnet module * Fix vlan to id resolve mechanism As reported in #80 it is currently not possible to resolve a vlan number to its id. This relies on a different structure of vlan entities. Vlan entities has `vlanId` instead of `id` as usual. We decided to use the same approach as for device_type to add `vlanId` as `id` to the resolved entity. * Add changlog entry * Add vlan domain mapping test * Fix domain resolution in vlan module * Cleanup conditions for selecting the correct find_* method * Add comment for later understanding
1 parent 3bc4c8a commit ebab6a3

File tree

7 files changed

+84
-6
lines changed

7 files changed

+84
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- fix \#80 - Can't add VLAN to subnet through to phpipam implementation differences in different entities

plugins/module_utils/phpipam_helper.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ def find_device_type(self, device_type):
158158
return result
159159

160160
def find_vlan(self, vlan):
161-
return self.find_by_key(self.controller_uri, vlan)
161+
result = self.find_by_key('vlan', vlan, key='number')
162+
if result and 'vlanId' in result:
163+
result['id'] = result['vlanId']
164+
return result
162165

163166
def find_by_key(self, controller, value, key='name'):
164167
"""
@@ -189,7 +192,10 @@ def find_current_entity(self):
189192
entity = self.find_device_type(self.phpipam_params['name'])
190193
elif self.controller_name == 'tools/tags':
191194
entity = self.find_by_key(self.controller_uri, self.phpipam_params['name'], key='type')
192-
elif 'tools' in self.controller_uri or self.controller_name in ['vlan', 'l2domain', 'vrf']:
195+
elif self.controller_name == 'vlan':
196+
entity = self.find_vlan(self.phpipam_params['vlan_id'])
197+
# l2domains needs to be singular because it is derived from class name
198+
elif 'tools' in self.controller_uri or self.controller_name in ['l2domain', 'vrf']:
193199
entity = self.find_by_key(self.controller_uri, self.phpipam_params['name'])
194200
else:
195201
entity = self.find_entity(self.controller_uri, '/' + self.phpipam_params['name'])
@@ -215,7 +221,10 @@ def _resolve_entity(self, key):
215221
result = self.find_device_type(self.phpipam_params[key])
216222
elif controller == 'tools/tags':
217223
result = self.find_by_key(controller=controller, value=self.phpipam_params[key], key='type')
218-
elif 'tools' in controller or controller in ['vlan', 'l2domains', 'vrf']:
224+
elif controller == 'vlan':
225+
result = self.find_vlan(self.phpipam_params['vlan'])
226+
# l2domains needs to be plural because it is derived from either controller parameter in entity_spec or controller_uri (which is pluralized)
227+
elif 'tools' in controller or controller in ['l2domains', 'vrf']:
219228
result = self.find_by_key(controller=controller, value=self.phpipam_params[key])
220229
else:
221230
if entity_spec.get('type') == 'entity':
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
- hosts: localhost
2+
collections:
3+
- codeaffen.phpipam
4+
gather_facts: false
5+
vars_files:
6+
- vars/server.yml
7+
- vars/domain.yml
8+
- vars/vlan.yml
9+
tasks:
10+
- name: create domains for mapping test
11+
include: tasks/domain.yml
12+
vars:
13+
name: create {{ loop_domain_data['name'] }}
14+
domain: "{{ loop_domain_data }}"
15+
loop: "{{ vlan_mapping_base_domain_data }}"
16+
loop_control:
17+
loop_var: loop_domain_data
18+
19+
- name: create vlans for mapping test
20+
include: tasks/vlan.yml
21+
vars:
22+
name: create vlan {{ loop_vlan_data['name'] }}
23+
vlan: "{{ loop_vlan_data }}"
24+
loop: "{{ vlan_mapping_base_vlan_data }}"
25+
loop_control:
26+
loop_var: loop_vlan_data
27+
28+
- name: delete vlans
29+
include: tasks/vlan.yml
30+
vars:
31+
name: delete vlan {{ vlan['name'] }}
32+
override:
33+
state: absent
34+
vlan: "{{ loop_vlan_data | combine(override) }}"
35+
loop: "{{ vlan_mapping_base_vlan_data }}"
36+
loop_control:
37+
loop_var: loop_vlan_data
38+
39+
- name: delete domains
40+
include: tasks/domain.yml
41+
vars:
42+
name: delete {{ loop_domain_data['name'] }}
43+
override:
44+
state: absent
45+
domain: "{{ loop_domain_data | combine(override) }}"
46+
loop: "{{ vlan_mapping_base_domain_data }}"
47+
loop_control:
48+
loop_var: loop_domain_data

tests/test_playbooks/tasks/subnet.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
description: "{{ subnet.description | default(omit) }}"
1212
section: "{{ subnet.section | default(omit) }}"
1313
linked_subnet: "{{ subnet.linked_subnet | default(omit) }}"
14-
vlan_id: "{{ subnet.vlan_id | default(omit) }}"
15-
vrf_id: "{{ subnet.vrf_id | default(omit) }}"
14+
vlan: "{{ subnet.vlan | default(omit) }}"
15+
vrf: "{{ subnet.vrf | default(omit) }}"
1616
master_subnet.cidr: "{{ subnet.master_subnet.cidr | default(omit) }}"
1717
nameserver: "{{ subnet.nameserver | default(omit) }}"
1818
show_as_name: "{{ subnet.show_as_name | default(omit) }}"
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
---
22
base_domain_data:
3-
name: my routing domain
3+
name: routing domain
44
sections:
55
- Customers
6+
7+
vlan_mapping_base_domain_data:
8+
- name: routing domain 1
9+
sections:
10+
- Customers
11+
- name: routing domain 2
12+
sections:
13+
- Customers

tests/test_playbooks/vars/subnet.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
base_subnet_data:
33
cidr: 10.0.0.0/24
44
section: "Customers"
5+
vlan: 2001
56

67
subnets:
78
- cidr: 192.0.2.0/24

tests/test_playbooks/vars/vlan.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@ base_vlan_data:
33
name: ansible_test
44
description: test vlan
55
vlan_id: 1337
6+
7+
vlan_mapping_base_vlan_data:
8+
- name: vlan 1
9+
description: domain vlan mappinng test 1
10+
vlan_id: 1001
11+
routing_domain: routing domain 1
12+
- name: vlan 2
13+
description: domain vlan mappinng test 2
14+
vlan_id: 1002
15+
routing_domain: routing domain 2

0 commit comments

Comments
 (0)