Skip to content

Commit 0741d0c

Browse files
committed
proxmox_firewall: Split into seprate proxmox_firewall_info
- Also add get methods in module_utils.
1 parent a5419bd commit 0741d0c

File tree

3 files changed

+362
-190
lines changed

3 files changed

+362
-190
lines changed

plugins/module_utils/proxmox_sdn.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88

99
__metaclass__ = type
1010

11+
import traceback
12+
13+
PROXMOXER_IMP_ERR = None
14+
try:
15+
from proxmoxer import ProxmoxResource
16+
from proxmoxer import __version__ as proxmoxer_version
17+
HAS_PROXMOXER = True
18+
except ImportError:
19+
HAS_PROXMOXER = False
20+
PROXMOXER_IMP_ERR = traceback.format_exc()
21+
1122
from typing import List, Dict
1223

1324
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
@@ -99,3 +110,47 @@ def get_zones(self, zone_type: str = None) -> List[Dict]:
99110
self.module.fail_json(
100111
msg=f'Failed to retrieve zone information from cluster: {e}'
101112
)
113+
114+
def get_aliases(self, firewall_obj: ProxmoxResource | None) -> List[Dict]:
115+
"""Get aliases for IP/CIDR at given firewall endpoint level
116+
117+
:param firewall_obj: Firewall endpoint as a ProxmoxResource e.g. self.proxmox_api.cluster().firewall
118+
If it is None it'll return empty list
119+
:return: List of aliases and corresponding IP/CIDR
120+
"""
121+
if firewall_obj is None:
122+
return list()
123+
try:
124+
return firewall_obj().aliases().get()
125+
except Exception as e:
126+
self.module.fail_json(
127+
msg='Failed to retrieve aliases'
128+
)
129+
130+
def get_fw_rules(self, rules_obj: ProxmoxResource, pos: int = None) -> List[Dict]:
131+
"""Get firewall rules at given rules endpoint level
132+
133+
:param rules_obj: Firewall Rules endpoint as a ProxmoxResource e.g. self.proxmox_api.cluster().firewall().rules
134+
:param pos: Rule position if it is None it'll return all rules
135+
:return: Firewall rules as a list of dict
136+
"""
137+
if pos is not None:
138+
rules_obj = getattr(rules_obj(), str(pos))
139+
try:
140+
return rules_obj.get()
141+
except Exception as e:
142+
self.module.fail_json(
143+
msg=f'Failed to retrieve firewall rules: {e}'
144+
)
145+
146+
def get_groups(self) -> List:
147+
"""Get firewall security groups
148+
149+
:return: list of groups
150+
"""
151+
try:
152+
return [x['group'] for x in self.proxmox_api.cluster().firewall().groups().get()]
153+
except Exception as e:
154+
self.module.fail_json(
155+
msg=f'Failed to retrieve firewall security groups: {e}'
156+
)

plugins/modules/proxmox_firewall.py

Lines changed: 3 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,6 @@
209209
"""
210210

211211
EXAMPLES = r"""
212-
- name: Get Cluster level firewall rules
213-
community.proxmox.proxmox_firewall:
214-
api_user: "{{ pc.proxmox.api_user }}"
215-
api_token_id: "{{ pc.proxmox.api_token_id }}"
216-
api_token_secret: "{{ vault.proxmox.api_token_secret }}"
217-
api_host: "{{ pc.proxmox.api_host }}"
218-
validate_certs: no
219-
level: cluster
220-
221212
- name: Create firewall rules at cluster level
222213
community.proxmox.proxmox_firewall:
223214
api_user: "{{ pc.proxmox.api_user }}"
@@ -343,154 +334,14 @@
343334
type: str
344335
sample:
345336
test
346-
347-
groups:
348-
description: list of firewall security groups
349-
returned: on success
350-
type: list
351-
elements: str
352-
sample:
353-
[ "test" ]
354-
355-
aliases:
356-
description:
357-
- list of alias present at given level
358-
- aliases are only available for cluster and VM level so if any other level it'll be empty list
359-
returned: on success
360-
type: list
361-
elements: dict
362-
sample:
363-
[
364-
{
365-
"cidr": "10.10.1.0/24",
366-
"digest": "978391f460484e8d4fb3ca785cfe5a9d16fe8b1f",
367-
"ipversion": 4,
368-
"name": "test1"
369-
},
370-
{
371-
"cidr": "10.10.2.0/24",
372-
"digest": "978391f460484e8d4fb3ca785cfe5a9d16fe8b1f",
373-
"ipversion": 4,
374-
"name": "test2"
375-
},
376-
{
377-
"cidr": "10.10.3.0/24",
378-
"digest": "978391f460484e8d4fb3ca785cfe5a9d16fe8b1f",
379-
"ipversion": 4,
380-
"name": "test3"
381-
}
382-
]
383-
384-
firewall_rules:
385-
description: List of firewall rules.
386-
returned: on success
387-
type: list
388-
elements: dict
389-
sample:
390-
[
391-
{
392-
"action": "ACCEPT",
393-
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9",
394-
"dport": "53",
395-
"enable": 1,
396-
"ipversion": 4,
397-
"log": "nolog",
398-
"pos": 0,
399-
"proto": "udp",
400-
"source": "192.168.1.0/24",
401-
"type": "in"
402-
},
403-
{
404-
"action": "ACCEPT",
405-
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9",
406-
"dport": "53",
407-
"enable": 1,
408-
"ipversion": 4,
409-
"log": "nolog",
410-
"pos": 1,
411-
"proto": "tcp",
412-
"source": "192.168.1.0/24",
413-
"type": "in"
414-
},
415-
{
416-
"action": "ACCEPT",
417-
"dest": "192.168.1.0/24",
418-
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9",
419-
"enable": 1,
420-
"ipversion": 4,
421-
"log": "nolog",
422-
"pos": 2,
423-
"type": "out"
424-
},
425-
{
426-
"action": "ACCEPT",
427-
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9",
428-
"enable": 1,
429-
"ipversion": 4,
430-
"log": "nolog",
431-
"pos": 3,
432-
"source": "192.168.1.0/24",
433-
"type": "in"
434-
},
435-
{
436-
"action": "ACCEPT",
437-
"dest": "+sdn/test2-gateway",
438-
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9",
439-
"enable": 1,
440-
"iface": "test2",
441-
"log": "nolog",
442-
"macro": "DNS",
443-
"pos": 4,
444-
"type": "in"
445-
},
446-
{
447-
"action": "ACCEPT",
448-
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9",
449-
"enable": 1,
450-
"iface": "test2",
451-
"log": "nolog",
452-
"macro": "DHCPfwd",
453-
"pos": 5,
454-
"type": "in"
455-
},
456-
{
457-
"action": "ACCEPT",
458-
"dest": "+sdn/test2-all",
459-
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9",
460-
"dport": "68",
461-
"enable": 1,
462-
"log": "nolog",
463-
"pos": 6,
464-
"proto": "udp",
465-
"source": "+sdn/test2-gateway",
466-
"sport": "67",
467-
"type": "out"
468-
},
469-
{
470-
"action": "DROP",
471-
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9",
472-
"enable": 1,
473-
"log": "nolog",
474-
"pos": 7,
475-
"type": "in"
476-
},
477-
{
478-
"action": "DROP",
479-
"digest": "b5ddaed23b415b9368706fc9edc83d037526aae9",
480-
"enable": 1,
481-
"log": "nolog",
482-
"pos": 8,
483-
"type": "out"
484-
}
485-
]
486337
"""
487338

488339
from ansible.module_utils.basic import AnsibleModule
340+
from ansible_collections.community.proxmox.plugins.module_utils.proxmox_sdn import ProxmoxSdnAnsible
489341
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
490342
proxmox_auth_argument_spec,
491343
ansible_to_proxmox_bool,
492-
compare_list_of_dicts,
493-
ProxmoxAnsible
344+
compare_list_of_dicts
494345
)
495346

496347

@@ -562,7 +413,7 @@ def get_ansible_module():
562413
)
563414

564415

565-
class ProxmoxFirewallAnsible(ProxmoxAnsible):
416+
class ProxmoxFirewallAnsible(ProxmoxSdnAnsible):
566417
def __init__(self, module):
567418
super(ProxmoxFirewallAnsible, self).__init__(module)
568419
self.params = module.params
@@ -646,27 +497,7 @@ def run(self):
646497
self.delete_group(group_name=group)
647498
if aliases is not None:
648499
self.delete_aliases(firewall_obj=firewall_obj, level=level, aliases=aliases)
649-
else:
650-
rules = self.get_fw_rules(rules_obj, pos=self.params.get('pos'))
651-
groups = self.get_groups()
652-
aliases = self.get_aliases(firewall_obj=firewall_obj, level=level)
653-
self.module.exit_json(
654-
changed=False,
655-
firewall_rules=rules,
656-
groups=groups,
657-
aliases=aliases,
658-
msg='successfully retrieved firewall rules and groups'
659-
)
660500

661-
def get_aliases(self, firewall_obj, level):
662-
if firewall_obj is None or level not in ['cluster', 'vm']:
663-
return list()
664-
try:
665-
return firewall_obj().aliases().get()
666-
except Exception as e:
667-
self.module.fail_json(
668-
msg='Failed to retrieve aliases'
669-
)
670501

671502
def create_aliases(self, firewall_obj, level, aliases, force=False):
672503
if firewall_obj is None or level not in ['cluster', 'vm']:
@@ -795,24 +626,6 @@ def delete_group(self, group_name):
795626
msg=f'Failed to delete security group {group_name}: {e}'
796627
)
797628

798-
def get_fw_rules(self, rules_obj, pos=None):
799-
if pos is not None:
800-
rules_obj = getattr(rules_obj(), str(pos))
801-
try:
802-
return rules_obj.get()
803-
except Exception as e:
804-
self.module.fail_json(
805-
msg=f'Failed to retrieve firewall rules: {e}'
806-
)
807-
808-
def get_groups(self):
809-
try:
810-
return [x['group'] for x in self.proxmox_api.cluster().firewall().groups().get()]
811-
except Exception as e:
812-
self.module.fail_json(
813-
msg=f'Failed to retrieve firewall security groups: {e}'
814-
)
815-
816629
def delete_fw_rule(self, rules_obj, pos):
817630
try:
818631
for item in self.get_fw_rules(rules_obj):

0 commit comments

Comments
 (0)