Skip to content

Commit e55b977

Browse files
authored
Fix #93 - trouble creating subnet with a vrf (#94)
* Implement find_vrf method * Fix phpipam_name in vrf module * Create test case to reproduce the issue * Add `ansible-lint` to dev requirements * Add changelog entry
1 parent 4de4704 commit e55b977

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- fix \#93 - trouble creating subnet with a vrf

plugins/module_utils/phpipam_helper.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,28 @@ def find_vlan(self, number, domain='default'):
188188

189189
return result
190190

191+
def find_vrf(self, vrf, section=None):
192+
# As vrfs should link to subnets in a clean way the vrf module only allow a single vrf with a given name
193+
# We also need to guarantee that a vrf belongs to the same section as the related subnet
194+
# This method implements the logic to guarantee all that suff in a clean way.
195+
result = self.find_by_key('vrfs', vrf)
196+
197+
if result:
198+
# If we resolve a vrf for a subnet we need to check if the vrf belongs to the same section
199+
if section:
200+
_section_id = self.find_entity('sections', '/' + section)
201+
202+
if _section_id is None:
203+
self.fail_json(msg="Found no results while searching for section '{0}' ".format(section))
204+
205+
if _section_id['id'] not in result['sections']:
206+
self.fail_json(msg="Found no vrf '{0}' that belongs to section '{1}' ".format(vrf, section))
207+
208+
if 'vrfId' in result:
209+
result['id'] = result['vrfId']
210+
211+
return result
212+
191213
def find_by_key(self, controller, value, key='name', controller_path='/'):
192214
"""
193215
Some controllers don't provide the ability to search for entities by uri
@@ -219,8 +241,10 @@ def find_current_entity(self):
219241
entity = self.find_by_key(self.controller_uri, self.phpipam_params['name'], key='type')
220242
elif self.controller_name == 'vlan':
221243
entity = self.find_vlan(self.phpipam_params['vlan_id'], self.phpipam_params['routing_domain'])
244+
elif self.controller_name == 'vrf':
245+
entity = self.find_vrf(self.phpipam_params['name'])
222246
# l2domains needs to be singular because it is derived from class name
223-
elif 'tools' in self.controller_uri or self.controller_name in ['l2domain', 'vrf']:
247+
elif 'tools' in self.controller_uri or self.controller_name == 'l2domain':
224248
entity = self.find_by_key(self.controller_uri, self.phpipam_params['name'])
225249
else:
226250
entity = self.find_entity(self.controller_uri, '/' + self.phpipam_params['name'])
@@ -248,8 +272,10 @@ def _resolve_entity(self, key):
248272
result = self.find_by_key(controller=controller, value=self.phpipam_params[key], key='type')
249273
elif controller == 'vlan':
250274
result = self.find_vlan(self.phpipam_params[key], self.phpipam_params['routing_domain'])
275+
elif controller == 'vrf':
276+
result = self.find_vrf(self.phpipam_params['vrf'], self.phpipam_params['section'])
251277
# l2domains needs to be plural because it is derived from either controller parameter in entity_spec or controller_uri (which is pluralized)
252-
elif 'tools' in controller or controller in ['l2domains', 'vrf']:
278+
elif 'tools' in controller or controller == 'l2domains':
253279
result = self.find_by_key(controller=controller, value=self.phpipam_params[key])
254280
else:
255281
if entity_spec.get('type') == 'entity':

plugins/modules/vrf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class PhpipamVrfModule(PhpipamEntityAnsibleModule):
8181
def main():
8282
module = PhpipamVrfModule(
8383
phpipam_spec=dict(
84-
id=dict(type='int', invisible=True, phpipam_name='vrfId'),
84+
id=dict(type='int', invisible=True, phpipam_name='id'),
8585
name=dict(type='str', required=True),
8686
description=dict(type='str'),
8787
distinguisher=dict(type='str', phpipam_name='rd'),

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ansible
22
# ansible_runner<2.0; python_version < '3.6' # pyup: ignore , disabled due to a flaw in versions < 2.1.0 , please reenable locally if absolutely needed
33
ansible_runner; python_version >= '3.6' # pyup: ignore
4+
ansible-lint
45
colour
56
coverage
67
geopy
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
- name: Subnet VRF mapping test
2+
hosts: localhost
3+
collections:
4+
- codeaffen.phpipam
5+
gather_facts: false
6+
vars_files:
7+
- vars/server.yml
8+
- vars/vrf.yml
9+
- vars/subnet.yml
10+
tasks:
11+
- name: Create vrf
12+
ansible.builtin.include_tasks: tasks/vrf.yml
13+
vars:
14+
name: create vrf
15+
vrf: "{{ base_vrf_data }}"
16+
- name: Create vars/subnet.yml
17+
ansible.builtin.include_tasks: tasks/subnet.yml
18+
vars:
19+
name: create subnet with vrf
20+
override:
21+
vrf: "{{ base_vrf_data.name }}"
22+
subnet: "{{ base_subnet_data | combine(override) }}"

0 commit comments

Comments
 (0)