Skip to content

Commit b30bd19

Browse files
committed
proxmox_subnet: cleanup
1 parent e854ff2 commit b30bd19

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

plugins/modules/proxmox_subnet.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def get_subnets(self, vnet_name):
247247
try:
248248
return self.proxmox_api.cluster().sdn().vnets(vnet_name).subnets().get()
249249
except Exception as e:
250-
self.module.fail_json(f'Failed to retrieve subnet isubnet_paramsnfo {e}')
250+
self.module.fail_json(f'Failed to retrieve subnets {e}')
251251

252252
def update_subnet(self, **subnet_params):
253253
new_subnet = copy.deepcopy(subnet_params)
@@ -357,15 +357,14 @@ def subnet_present(self, **subnet_params):
357357
subnet_id = f"{self.params['zone']}-{subnet_params['subnet'].replace('/', '-')}"
358358

359359
try:
360-
vnet = getattr(self.proxmox_api.cluster().sdn().vnets(), vnet_name)
361360
existing_subnets = self.get_subnets(vnet_name)
362361

363362
# Check if subnet already present
364363
if subnet_id in [x['subnet'] for x in existing_subnets]:
365364
self.update_subnet(**subnet_params)
366365
else:
367366
subnet_params['lock-token'] = self.get_global_sdn_lock()
368-
vnet.subnets().post(**subnet_params)
367+
self.proxmox_api.cluster().sdn().vnets(vnet_name).subnets().post(**subnet_params)
369368
self.apply_sdn_changes_and_release_lock(lock=subnet_params['lock-token'])
370369
self.module.exit_json(
371370
changed=True, subnet=subnet_id, msg=f'Created new subnet {subnet_cidr}'
@@ -377,22 +376,21 @@ def subnet_present(self, **subnet_params):
377376
)
378377

379378
def subnet_absent(self, **subnet_params):
380-
vnet_id = subnet_params['vnet']
379+
vnet_name = subnet_params['vnet']
381380
subnet_id = f"{self.params['zone']}-{subnet_params['subnet'].replace('/', '-')}"
382381

383382
params = {
384383
'subnet': subnet_id,
385-
'vnet': vnet_id,
384+
'vnet': vnet_name,
386385
'lock-token': None
387386
}
388387

388+
existing_subnets = self.get_subnets(vnet_name)
389389
try:
390-
vnet = getattr(self.proxmox_api.cluster().sdn().vnets(), vnet_id)
391-
392390
# Check if subnet already present
393-
if subnet_id in [x['subnet'] for x in vnet().subnets().get()]:
391+
if subnet_id in [x['subnet'] for x in existing_subnets]:
394392
params['lock-token'] = self.get_global_sdn_lock()
395-
vnet().subnets(subnet_id).delete(**params)
393+
self.proxmox_api.cluster().sdn().vnets(vnet_name).subnets(subnet_id).delete(**params)
396394
self.apply_sdn_changes_and_release_lock(lock=params['lock-token'])
397395
self.module.exit_json(
398396
changed=True, subnet=subnet_id, msg=f'Deleted subnet {subnet_id}'

0 commit comments

Comments
 (0)