@@ -183,7 +183,6 @@ def get_ansible_module():
183
183
required_if = [
184
184
('state' , 'present' , ['subnet' , 'vnet' , 'zone' ]),
185
185
('state' , 'absent' , ['zone' , 'vnet' , 'subnet' ]),
186
- # ('dhcp_range_update_mode', 'overwrite', ['dhcp_range'])
187
186
]
188
187
)
189
188
@@ -235,12 +234,12 @@ def run(self):
235
234
'dhcp-range' : get_dhcp_range (dhcp_range = self .params .get ('dhcp_range' )),
236
235
'dnszoneprefix' : self .params .get ('dnszoneprefix' ),
237
236
'gateway' : self .params .get ('gateway' ),
238
- 'lock-token' : self . params . get ( 'lock_token' ) or self . get_global_sdn_lock () ,
237
+ 'lock-token' : None ,
239
238
'snat' : ansible_to_proxmox_bool (self .params .get ('snat' ))
240
239
}
241
240
242
241
if state == 'present' :
243
- self .subnet_present (update = update , ** subnet_params )
242
+ self .subnet_present (** subnet_params )
244
243
elif state == 'absent' :
245
244
self .subnet_absent (** subnet_params )
246
245
@@ -253,7 +252,6 @@ def get_subnets(self, vnet_name):
253
252
def update_subnet (self , ** subnet_params ):
254
253
new_subnet = copy .deepcopy (subnet_params )
255
254
subnet_id = f"{ self .params ['zone' ]} -{ new_subnet ['subnet' ].replace ('/' , '-' )} "
256
- lock = subnet_params ['lock-token' ]
257
255
vnet_name = new_subnet ['vnet' ]
258
256
dhcp_range_update_mode = self .params .get ('dhcp_range_update_mode' )
259
257
@@ -293,9 +291,7 @@ def update_subnet(self, **subnet_params):
293
291
if dhcp_range_update_mode == 'append' :
294
292
if partial_overlap :
295
293
self .module .fail_json (
296
- msg = f"There are overlapping DHCP ranges. this is not allowed. "
297
- f"Existing range - { existing_subnet ['dhcp-range' ]} "
298
- f"New Range - { new_dhcp_range } "
294
+ msg = f"There are partially overlapping DHCP ranges. this is not allowed."
299
295
)
300
296
301
297
if len (new_dhcp ) > 0 :
@@ -312,7 +308,7 @@ def update_subnet(self, **subnet_params):
312
308
"dhcp_range_update_mode is set to append, but you didn't provide any DHCP ranges for the subnet. "
313
309
"Existing ranges will be ignored."
314
310
)
315
-
311
+
316
312
elif dhcp_range_update_mode == 'overwrite' :
317
313
update_dhcp = True
318
314
self .module .warn (
@@ -328,37 +324,35 @@ def update_subnet(self, **subnet_params):
328
324
self .module .warn (f"{ subnet_params } , { update_dhcp } " )
329
325
if self .params .get ('update' ):
330
326
try :
327
+ subnet_params ['lock-token' ] = self .get_global_sdn_lock ()
331
328
subnet = getattr (self .proxmox_api .cluster ().sdn ().vnets (vnet_name ).subnets (), subnet_id )
332
329
subnet_params ['digest' ] = subnet .get ()['digest' ]
333
330
del subnet_params ['type' ]
334
331
del subnet_params ['subnet' ]
335
332
336
333
subnet .put (** subnet_params )
337
- self .apply_sdn_changes_and_release_lock (lock = lock )
334
+ self .apply_sdn_changes_and_release_lock (lock = subnet_params [ ' lock-token' ] )
338
335
self .module .exit_json (
339
336
changed = True , subnet = subnet_id , msg = f'Updated subnet { subnet_id } '
340
337
)
341
338
except Exception as e :
342
- self .rollback_sdn_changes_and_release_lock (lock = lock )
339
+ self .rollback_sdn_changes_and_release_lock (lock = subnet_params [ ' lock-token' ] )
343
340
self .module .fail_json (
344
341
msg = f'Failed to update subnet. Rolling back all changes : { e } '
345
342
)
346
343
else :
347
- self .release_lock (lock = lock )
348
344
self .module .fail_json (
349
345
msg = f"Subnet { subnet_id } needs to be updated but update is false."
350
346
)
351
347
else :
352
- self .release_lock (lock = lock )
353
348
self .module .exit_json (
354
349
changed = False ,
355
350
subnet = subnet_id ,
356
351
msg = f'subnet { subnet_id } is already present with correct parameters.'
357
352
)
358
353
359
- def subnet_present (self , update , ** subnet_params ):
354
+ def subnet_present (self , ** subnet_params ):
360
355
vnet_name = subnet_params ['vnet' ]
361
- lock = subnet_params ['lock-token' ]
362
356
subnet_cidr = subnet_params ['subnet' ]
363
357
subnet_id = f"{ self .params ['zone' ]} -{ subnet_params ['subnet' ].replace ('/' , '-' )} "
364
358
@@ -370,46 +364,45 @@ def subnet_present(self, update, **subnet_params):
370
364
if subnet_id in [x ['subnet' ] for x in existing_subnets ]:
371
365
self .update_subnet (** subnet_params )
372
366
else :
367
+ subnet_params ['lock-token' ] = self .get_global_sdn_lock ()
373
368
vnet .subnets ().post (** subnet_params )
374
- self .apply_sdn_changes_and_release_lock (lock = lock )
369
+ self .apply_sdn_changes_and_release_lock (lock = subnet_params [ ' lock-token' ] )
375
370
self .module .exit_json (
376
371
changed = True , subnet = subnet_id , msg = f'Created new subnet { subnet_cidr } '
377
372
)
378
373
except Exception as e :
379
- self .rollback_sdn_changes_and_release_lock (lock = lock )
374
+ self .rollback_sdn_changes_and_release_lock (lock = subnet_params [ ' lock-token' ] )
380
375
self .module .fail_json (
381
376
msg = f'Failed to create subnet. Rolling back all changes : { e } '
382
377
)
383
378
384
379
def subnet_absent (self , ** subnet_params ):
385
380
vnet_id = subnet_params ['vnet' ]
386
- lock = subnet_params ['lock-token' ]
387
381
subnet_id = f"{ self .params ['zone' ]} -{ subnet_params ['subnet' ].replace ('/' , '-' )} "
388
382
389
383
params = {
390
384
'subnet' : subnet_id ,
391
385
'vnet' : vnet_id ,
392
- 'lock-token' : lock
386
+ 'lock-token' : None
393
387
}
394
388
395
389
try :
396
390
vnet = getattr (self .proxmox_api .cluster ().sdn ().vnets (), vnet_id )
397
391
398
392
# Check if subnet already present
399
393
if subnet_id in [x ['subnet' ] for x in vnet ().subnets ().get ()]:
400
- subnet = getattr ( vnet (). subnets (), subnet_id )
401
- subnet .delete (** params )
402
- self .apply_sdn_changes_and_release_lock (lock = lock )
394
+ params [ 'lock-token' ] = self . get_global_sdn_lock ( )
395
+ vnet (). subnets ( subnet_id ) .delete (** params )
396
+ self .apply_sdn_changes_and_release_lock (lock = params [ ' lock-token' ] )
403
397
self .module .exit_json (
404
398
changed = True , subnet = subnet_id , msg = f'Deleted subnet { subnet_id } '
405
399
)
406
400
else :
407
- self .release_lock (lock = lock )
408
401
self .module .exit_json (
409
402
changed = False , subnet = subnet_id , msg = f'subnet { subnet_id } already not present.'
410
403
)
411
404
except Exception as e :
412
- self .rollback_sdn_changes_and_release_lock (lock = lock )
405
+ self .rollback_sdn_changes_and_release_lock (lock = params [ ' lock-token' ] )
413
406
self .module .fail_json (
414
407
msg = f'Failed to delete subnet. Rolling back all changes. : { e } '
415
408
)
0 commit comments