Skip to content

Commit 6f37333

Browse files
Apply suggestions from code review
Added suggestions from @IamLunchbox Co-authored-by: IamLunchbox <[email protected]>
1 parent 0b01684 commit 6f37333

File tree

4 files changed

+59
-65
lines changed

4 files changed

+59
-65
lines changed

plugins/module_utils/proxmox.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ def compare_list_of_dicts(existing_list, new_list, uid, params_to_ignore=None):
7878
Use case - for firewall rules we will be getting a list of rules from user.
7979
We want to filter out which rules needs to be updated and which rules are completely new and needs to be created
8080
81-
@param existing_list: Existing values example - list of existing rules
82-
@param new_list: New values example - list of rules passed to module
83-
@param uid: unique identifier in dict. It should always be present in both lists - in case of firewall rules it's pos
84-
@param params_to_ignore: list of params we want to ignore which are present in existing_list's dict.
81+
:param existing_list: Existing values example - list of existing rules
82+
:param new_list: New values example - list of rules passed to module
83+
:param uid: unique identifier in dict. It should always be present in both lists - in case of firewall rules it's pos
84+
:param params_to_ignore: list of params we want to ignore which are present in existing_list's dict.
8585
In case of firewall rules we want to ignore ['digest', 'ipversion']
8686
87-
@return: returns 2 list items 1st is the list of items which are completely new and needs to be created
87+
:return: returns 2 list items 1st is the list of items which are completely new and needs to be created
8888
2nd is a list of items which needs to be updated
8989
"""
9090
if params_to_ignore is None:

plugins/module_utils/proxmox_sdn.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def get_aliases(self, firewall_obj):
104104
"""Get aliases for IP/CIDR at given firewall endpoint level
105105
106106
:param firewall_obj: Firewall endpoint as a ProxmoxResource e.g. self.proxmox_api.cluster().firewall
107-
If it is None it'll return empty list
107+
If it is None it'll return an empty list
108108
:return: List of aliases and corresponding IP/CIDR
109109
"""
110110
if firewall_obj is None:
@@ -123,14 +123,13 @@ def get_fw_rules(self, rules_obj, pos=None):
123123
:param pos: Rule position if it is None it'll return all rules
124124
:return: Firewall rules as a list of dict
125125
"""
126-
if pos is not None:
127-
rules_obj = getattr(rules_obj(), str(pos))
128-
try:
129-
return rules_obj.get()
130-
except Exception as e:
131-
self.module.fail_json(
132-
msg=f'Failed to retrieve firewall rules: {e}'
133-
)
126+
if pos:
127+
try:
128+
return rules_obj(str(pos)).get()
129+
except Exception as e:
130+
self.module.fail_json(
131+
msg=f'Failed to retrieve firewall rules: {e}'
132+
)
134133

135134
def get_groups(self):
136135
"""Get firewall security groups

plugins/modules/proxmox_firewall.py

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
short_description: Manage firewall rules in Proxmox
1515
version_added: "1.4.0"
1616
description:
17-
- create/update/delete FW rules at cluster/group/vnet/node/vm level
18-
- Create/delete firewall security groups
19-
- get firewall rules at cluster/group/vnet/node/vm level
17+
- Create/update/delete firewall rules at cluster/group/vnet/node/vm level.
18+
- Create/delete firewall security groups.
2019
author: 'Jana Hoch <[email protected]> (!UNKNOWN)'
2120
attributes:
2221
check_mode:
@@ -26,16 +25,15 @@
2625
options:
2726
state:
2827
description:
29-
- create/update/delete firewall rules or security group
30-
- if state is not provided then it will just list firewall rules at level
28+
- Create/update/delete firewall rules or security group.
3129
type: str
3230
choices:
3331
- present
3432
- absent
3533
default: present
3634
update:
3735
description:
38-
- If O(state=present) and if 1 or more rule/alias already exists it will update them
36+
- If O(state=present) and if one or more rule/alias already exists it will update them.
3937
type: bool
4038
default: true
4139
level:
@@ -52,22 +50,22 @@
5250
node:
5351
description:
5452
- Name of the node.
55-
- only needed when level is node.
53+
- Only needed when O(level=node).
5654
type: str
5755
vmid:
5856
description:
5957
- ID of the VM to which the rule applies.
60-
- only needed when level is vm.
58+
- Only needed when O(level=vm).
6159
type: int
6260
vnet:
6361
description:
6462
- Name of the virtual network for the rule.
65-
- only needed when level is vnet.
63+
- Only needed when O(level=vnet).
6664
type: str
6765
pos:
6866
description:
6967
- Position of the rule in the list.
70-
- only needed if deleting rule or trying to list it
68+
- Only needed if O(state=absent).
7169
type: int
7270
group_conf:
7371
description:
@@ -77,7 +75,7 @@
7775
group:
7876
description:
7977
- Name of the group to which the rule belongs.
80-
- only needed when level is group or group_conf is True.
78+
- Only needed when O(level=group) or O(group_conf=true).
8179
type: str
8280
comment:
8381
description:
@@ -86,23 +84,23 @@
8684
type: str
8785
aliases:
8886
description:
89-
- List of aliases
90-
- Alias can only be created/updated/deleted at cluster or VM level
87+
- List of aliases.
88+
- Alias can only be created/updated/deleted at cluster or VM level.
9189
type: list
9290
elements: dict
9391
suboptions:
9492
name:
95-
description: Alias name
93+
description: Alias name.
9694
type: str
9795
required: true
9896
cidr:
9997
description:
100-
- CIDR for alias
101-
- only needed when O(state=present) or O(state=update)
98+
- CIDR for alias.
99+
- Only needed when O(state=present) or O(state=update).
102100
type: str
103101
required: false
104102
comment:
105-
description: Comment for Alias
103+
description: Comment for alias.
106104
type: str
107105
required: false
108106
rules:
@@ -215,7 +213,7 @@
215213
api_token_id: "{{ pc.proxmox.api_token_id }}"
216214
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
217215
api_host: "{{ pc.proxmox.api_host }}"
218-
validate_certs: no
216+
validate_certs: false
219217
level: cluster
220218
state: present
221219
rules:
@@ -224,43 +222,43 @@
224222
source: 1.1.1.1
225223
log: nolog
226224
pos: 9
227-
enable: True
225+
enable: true
228226
- type: out
229227
action: ACCEPT
230228
source: 1.0.0.1
231229
pos: 10
232-
enable: True
230+
enable: true
233231
234232
- name: Update Cluster level firewall rules
235233
community.proxmox.proxmox_firewall:
236234
api_user: "{{ pc.proxmox.api_user }}"
237235
api_token_id: "{{ pc.proxmox.api_token_id }}"
238236
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
239237
api_host: "{{ pc.proxmox.api_host }}"
240-
validate_certs: no
238+
validate_certs: false
241239
level: cluster
242240
state: present
243-
update: True
241+
update: true
244242
rules:
245243
- type: out
246244
action: ACCEPT
247245
source: 8.8.8.8
248246
log: nolog
249247
pos: 9
250-
enable: False
248+
enable: false
251249
- type: out
252250
action: ACCEPT
253251
source: 8.8.4.4
254252
pos: 10
255-
enable: False
253+
enable: false
256254
257255
- name: Delete cluster level firewall rule at pos 10
258256
community.proxmox.proxmox_firewall:
259257
api_user: "{{ pc.proxmox.api_user }}"
260258
api_token_id: "{{ pc.proxmox.api_token_id }}"
261259
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
262260
api_host: "{{ pc.proxmox.api_host }}"
263-
validate_certs: no
261+
validate_certs: false
264262
level: cluster
265263
state: absent
266264
pos: 10
@@ -271,8 +269,8 @@
271269
api_token_id: "{{ pc.proxmox.api_token_id }}"
272270
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
273271
api_host: "{{ pc.proxmox.api_host }}"
274-
validate_certs: no
275-
group_conf: True
272+
validate_certs: false
273+
group_conf: true
276274
state: present
277275
group: test
278276
@@ -282,8 +280,8 @@
282280
api_token_id: "{{ pc.proxmox.api_token_id }}"
283281
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
284282
api_host: "{{ pc.proxmox.api_host }}"
285-
validate_certs: no
286-
group_conf: True
283+
validate_certs: false
284+
group_conf: true
287285
state: absent
288286
group: test
289287
@@ -293,7 +291,7 @@
293291
api_token_id: "{{ pc.proxmox.api_token_id }}"
294292
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
295293
api_host: "{{ pc.proxmox.api_host }}"
296-
validate_certs: no
294+
validate_certs: false
297295
state: present
298296
aliases:
299297
- name: test1
@@ -307,9 +305,9 @@
307305
api_token_id: "{{ pc.proxmox.api_token_id }}"
308306
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
309307
api_host: "{{ pc.proxmox.api_host }}"
310-
validate_certs: no
308+
validate_certs: false
311309
state: present
312-
update: True
310+
update: true
313311
aliases:
314312
- name: test1
315313
cidr: '10.10.1.0/28'
@@ -322,7 +320,7 @@
322320
api_token_id: "{{ pc.proxmox.api_token_id }}"
323321
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
324322
api_host: "{{ pc.proxmox.api_host }}"
325-
validate_certs: no
323+
validate_certs: false
326324
state: absent
327325
aliases:
328326
- name: test1
@@ -433,7 +431,7 @@ def validate_params(self):
433431
return True
434432
else:
435433
self.module.fail_json(
436-
msg="When State is absent either group_conf should be true or pos/aliases must be present but not both"
434+
msg="When state is absent either group_conf should be true or pos/aliases must be present but not both"
437435
)
438436

439437
def run(self):
@@ -447,7 +445,7 @@ def run(self):
447445
group = self.params.get("group")
448446
group_conf = self.params.get("group_conf")
449447

450-
if rules is not None:
448+
if rules:
451449
for rule in rules:
452450
rule['icmp-type'] = rule.get('icmp_type')
453451
rule['enable'] = ansible_to_proxmox_bool(rule.get('enable'))
@@ -479,20 +477,20 @@ def run(self):
479477
if state == "present":
480478
if group_conf:
481479
self.group_present(group=group, comment=self.params.get('comment'))
482-
if rules is not None:
480+
if rules:
483481
self.fw_rules_present(rules_obj=rules_obj, rules=rules, update=update)
484-
if aliases is not None:
482+
if aliases:
485483
self.aliases_present(firewall_obj=firewall_obj, level=level, aliases=aliases, update=update)
486484
elif state == "absent":
487485
if self.params.get('pos'):
488486
self.fw_rule_absent(rules_obj=rules_obj, pos=self.params.get('pos'))
489487
if group_conf:
490488
self.group_absent(group_name=group)
491-
if aliases is not None:
489+
if aliases:
492490
self.aliases_absent(firewall_obj=firewall_obj, aliases=aliases)
493491

494492
def aliases_present(self, firewall_obj, level, aliases, update):
495-
if firewall_obj is None or level not in ['cluster', 'vm']:
493+
if not firewall_obj or level not in ['cluster', 'vm']:
496494
self.module.fail_json(
497495
msg='Aliases can only be created at cluster or VM level'
498496
)

plugins/modules/proxmox_firewall_info.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
short_description: Manage firewall rules in Proxmox
1515
version_added: "1.4.0"
1616
description:
17-
- create/update/delete FW rules at cluster/group/vnet/node/vm level
18-
- Create/delete firewall security groups
19-
- get firewall rules at cluster/group/vnet/node/vm level
17+
- Get firewall rules at cluster/group/vnet/node/vm level.
2018
author: 'Jana Hoch <[email protected]> (!UNKNOWN)'
2119
options:
2220
level:
@@ -33,27 +31,26 @@
3331
node:
3432
description:
3533
- Name of the node.
36-
- only needed when level is node.
34+
- Only needed when O(level=node).
3735
type: str
3836
vmid:
3937
description:
4038
- ID of the VM to which the rule applies.
41-
- only needed when level is vm.
39+
- Only needed when O(level=vm).
4240
type: int
4341
vnet:
4442
description:
4543
- Name of the virtual network for the rule.
46-
- only needed when level is vnet.
44+
- Only needed when O(level=vnet).
4745
type: str
4846
pos:
4947
description:
5048
- Position of the rule in the list.
51-
- only needed if deleting rule or trying to list it
5249
type: int
5350
group:
5451
description:
5552
- Name of the group to which the rule belongs.
56-
- only needed when level is group or group_conf is True.
53+
- Only needed when O(level=group).
5754
type: str
5855
extends_documentation_fragment:
5956
- community.proxmox.proxmox.actiongroup_proxmox
@@ -69,7 +66,7 @@
6966
api_token_id: "{{ pc.proxmox.api_token_id }}"
7067
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
7168
api_host: "{{ pc.proxmox.api_host }}"
72-
validate_certs: no
69+
validate_certs: false
7370
level: cluster
7471
"""
7572

@@ -78,7 +75,7 @@
7875
description:
7976
- List of firewall security groups.
8077
- This will always be given for cluster level regardless of the level passed.
81-
- Because only at cluster level we can have firewall security groups
78+
- Because only at cluster level we can have firewall security groups.
8279
returned: on success
8380
type: list
8481
elements: str
@@ -87,8 +84,8 @@
8784
8885
aliases:
8986
description:
90-
- list of alias present at given level
91-
- aliases are only available for cluster and VM level so if any other level it'll be empty list
87+
- List of alias present at given level.
88+
- Aliases are only available for cluster and VM level so if any other level it'll be empty list.
9289
returned: on success
9390
type: list
9491
elements: dict

0 commit comments

Comments
 (0)