Skip to content

Commit 7909902

Browse files
committed
proxmox_subnet: Merge state present and update
1 parent 1d6480b commit 7909902

File tree

1 file changed

+29
-77
lines changed

1 file changed

+29
-77
lines changed

plugins/modules/proxmox_subnet.py

Lines changed: 29 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,18 @@
2626
- Desired state of the network configuration.
2727
- Choices include present (create), absent (delete), or update (modify).
2828
type: str
29-
choices: ['present', 'absent', 'update']
29+
choices: ['present', 'absent']
3030
default: present
31-
force:
31+
update:
3232
description:
33-
- If true it will create subnet when state is update but subnet is missing and update the subnet when state is present and subnet already exists
33+
- If O(state=present) then it will update the subnet if needed.
3434
type: bool
35-
default: False
35+
default: True
3636
subnet:
3737
description:
3838
- subnet CIDR.
3939
type: str
4040
required: true
41-
type:
42-
description:
43-
- Type of network configuration.
44-
- Currently only supports 'subnet'.
45-
type: str
46-
choices: ['subnet']
47-
default: subnet
4841
vnet:
4942
description:
5043
- The virtual network to which the subnet belongs.
@@ -113,18 +106,6 @@
113106
subnet: 10.10.2.0/24
114107
zone: ans1
115108
state: present
116-
117-
- name: Update a subnet
118-
community.proxmox.proxmox_subnet:
119-
api_user: "{{ pc.proxmox.api_user }}"
120-
api_token_id: "{{ pc.proxmox.api_token_id }}"
121-
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
122-
api_host: "{{ pc.proxmox.api_host }}"
123-
validate_certs: no
124-
vnet: test
125-
subnet: 10.10.2.0/24
126-
zone: ans1
127-
state: update
128109
dhcp_range:
129110
- start: 10.10.2.5
130111
end: 10.10.2.50
@@ -156,19 +137,18 @@
156137
"""
157138

158139
from ansible.module_utils.basic import AnsibleModule
140+
from ansible_collections.community.proxmox.plugins.module_utils.proxmox_sdn import ProxmoxSdnAnsible
159141
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
160142
proxmox_auth_argument_spec,
161-
ansible_to_proxmox_bool,
162-
ProxmoxAnsible
143+
ansible_to_proxmox_bool
163144
)
164145

165146

166147
def get_proxmox_args():
167148
return dict(
168-
state=dict(type="str", choices=["present", "absent", "update"], default='present', required=False),
169-
force=dict(type="bool", default=False, required=False),
149+
state=dict(type="str", choices=["present", "absent"], default='present', required=False),
150+
update=dict(type="bool", default=True),
170151
subnet=dict(type="str", required=True),
171-
type=dict(type="str", choices=['subnet'], default='subnet', required=False),
172152
vnet=dict(type="str", required=True),
173153
zone=dict(type="str", required=False),
174154
dhcp_dns_server=dict(type="str", required=False),
@@ -196,25 +176,24 @@ def get_ansible_module():
196176
return AnsibleModule(
197177
argument_spec=module_args,
198178
required_if=[
199-
('state', 'present', ['subnet', 'type', 'vnet', 'zone']),
200-
('state', 'update', ['zone', 'vnet', 'subnet']),
179+
('state', 'present', ['subnet', 'vnet', 'zone']),
201180
('state', 'absent', ['zone', 'vnet', 'subnet']),
202181
]
203182
)
204183

205184

206-
class ProxmoxSubnetAnsible(ProxmoxAnsible):
185+
class ProxmoxSubnetAnsible(ProxmoxSdnAnsible):
207186
def __init__(self, module):
208187
super(ProxmoxSubnetAnsible, self).__init__(module)
209188
self.params = module.params
210189

211190
def run(self):
212191
state = self.params.get("state")
213-
force = self.params.get("force")
192+
update = self.params.get("update")
214193

215194
subnet_params = {
216195
'subnet': self.params.get('subnet'),
217-
'type': self.params.get('type'),
196+
'type': 'subnet',
218197
'vnet': self.params.get('vnet'),
219198
'dhcp-dns-server': self.params.get('dhcp_dns_server'),
220199
'dhcp-range': self.get_dhcp_range(),
@@ -225,9 +204,7 @@ def run(self):
225204
}
226205

227206
if state == 'present':
228-
self.subnet_present(force=force, **subnet_params)
229-
elif state == 'update':
230-
self.subnet_update(force=force, **subnet_params)
207+
self.subnet_present(update=update, **subnet_params)
231208
elif state == 'absent':
232209
self.subnet_absent(**subnet_params)
233210

@@ -237,71 +214,46 @@ def get_dhcp_range(self):
237214
dhcp_range = [f"start-address={x['start']},end-address={x['end']}" for x in self.params.get('dhcp_range')]
238215
return dhcp_range
239216

240-
def subnet_present(self, force, **subnet_params):
217+
def subnet_present(self, update, **subnet_params):
241218
vnet_name = subnet_params['vnet']
242219
lock = subnet_params['lock-token']
243-
subnet = subnet_params['subnet']
220+
subnet_cidr = subnet_params['subnet']
244221
subnet_id = f"{self.params['zone']}-{subnet_params['subnet'].replace('/', '-')}"
245222

246223
try:
247224
vnet = getattr(self.proxmox_api.cluster().sdn().vnets(), vnet_name)
248225

249226
# Check if subnet already present
250227
if subnet_id in [x['subnet'] for x in vnet().subnets().get()]:
251-
if force:
252-
self.subnet_update(force=False, **subnet_params)
228+
if update:
229+
subnet = getattr(vnet().subnets(), subnet_id)
230+
subnet_params['digest'] = subnet.get()['digest']
231+
subnet_params['delete'] = self.params.get('delete')
232+
del subnet_params['type']
233+
del subnet_params['subnet']
234+
235+
subnet.put(**subnet_params)
236+
self.apply_sdn_changes_and_release_lock(lock=lock)
237+
self.module.exit_json(
238+
changed=True, subnet=subnet_id, msg=f'Updated subnet {subnet_id}'
239+
)
253240
else:
254241
self.release_lock(lock=lock)
255242
self.module.exit_json(
256-
changed=False, subnet=subnet_id, msg=f'subnet {subnet_id} already present and force is false.'
243+
changed=False, subnet=subnet_id, msg=f'subnet {subnet_id} already present and update is false.'
257244
)
258245
else:
259246
vnet.subnets().post(**subnet_params)
260247
self.apply_sdn_changes_and_release_lock(lock=lock)
261248
self.module.exit_json(
262-
changed=True, subnet=subnet_id, msg=f'Created new subnet {subnet}'
249+
changed=True, subnet=subnet_id, msg=f'Created new subnet {subnet_cidr}'
263250
)
264251
except Exception as e:
265252
self.rollback_sdn_changes_and_release_lock(lock=lock)
266253
self.module.fail_json(
267254
msg=f'Failed to create subnet. Rolling back all changes : {e}'
268255
)
269256

270-
def subnet_update(self, force, **subnet_params):
271-
lock = subnet_params['lock-token']
272-
vnet_id = subnet_params['vnet']
273-
subnet_id = f"{self.params['zone']}-{subnet_params['subnet'].replace('/', '-')}"
274-
275-
try:
276-
vnet = getattr(self.proxmox_api.cluster().sdn().vnets(), vnet_id)
277-
278-
# Check if subnet already present
279-
if subnet_id in [x['subnet'] for x in vnet().subnets().get()]:
280-
subnet = getattr(vnet().subnets(), subnet_id)
281-
subnet_params['digest'] = subnet.get()['digest']
282-
subnet_params['delete'] = self.params.get('delete')
283-
del subnet_params['type']
284-
del subnet_params['subnet']
285-
286-
subnet.put(**subnet_params)
287-
self.apply_sdn_changes_and_release_lock(lock=lock)
288-
self.module.exit_json(
289-
changed=True, subnet=subnet_id, msg=f'Updated subnet {subnet_id}'
290-
)
291-
else:
292-
if force:
293-
self.subnet_present(force=False, **subnet_params)
294-
else:
295-
self.release_lock(lock=lock)
296-
self.module.exit_json(
297-
changed=False, subnet=subnet_id, msg=f'subnet {subnet_id} not present and force is false.'
298-
)
299-
except Exception as e:
300-
self.rollback_sdn_changes_and_release_lock(lock=lock)
301-
self.module.fail_json(
302-
msg=f'Failed to update subnet. Rolling back all changes. : {e}'
303-
)
304-
305257
def subnet_absent(self, **subnet_params):
306258
vnet_id = subnet_params['vnet']
307259
lock = subnet_params['lock-token']

0 commit comments

Comments
 (0)