
    Vh                        d dl mZ dZdZdZd dlmZ ddlmZ ddl	m
Z
 dd	lmZmZmZ  G d
 de      Zd Zedk(  r e        yy)    )annotationsa  
---
module: firewall_info
short_description: Gather infos about the Hetzner Cloud Firewalls.

description:
    - Gather facts about your Hetzner Cloud Firewalls.

author:
    - Jonas Lammler (@jooola)

options:
    id:
        description:
            - The ID of the Firewall you want to get.
            - The module will fail if the provided ID is invalid.
        type: int
    name:
        description:
            - The name for the Firewall you want to get.
        type: str
    label_selector:
        description:
            - The label selector for the Firewalls you want to get.
        type: str

extends_documentation_fragment:
    - hetzner.hcloud.hcloud
z
- name: Gather hcloud Firewall infos
  hetzner.hcloud.firewall_info:
  register: output

- name: Print the gathered infos
  debug:
    var: output
a  
hcloud_firewall_info:
    description: List of Firewalls.
    returned: always
    type: list
    elements: dict
    contains:
        id:
            description: Numeric identifier of the firewall.
            returned: always
            type: int
            sample: 1937415
        name:
            description: Name of the firewall.
            returned: always
            type: str
            sample: my-firewall
        labels:
            description: User-defined labels (key-value pairs).
            returned: always
            type: dict
        rules:
            description: List of rules the firewall contain.
            returned: always
            type: list
            elements: dict
            contains:
                description:
                    description: User defined description of this rule.
                    type: str
                    returned: always
                    sample: allow http from anywhere
                direction:
                    description: The direction of the firewall rule.
                    type: str
                    returned: always
                    sample: in
                protocol:
                    description: The protocol of the firewall rule.
                    type: str
                    returned: always
                    sample: tcp
                port:
                    description: The port or port range allowed by this rule.
                    type: str
                    returned: if RV(hcloud_firewall_info[].rules[].protocol=tcp) or RV(hcloud_firewall_info[].rules[].protocol=udp)
                    sample: "80"
                source_ips:
                    description: List of source CIDRs that are allowed within this rule.
                    type: list
                    elements: str
                    returned: always
                    sample: ["0.0.0.0/0", "::/0"]
                destination_ips:
                    description: List of destination CIDRs that are allowed within this rule.
                    type: list
                    elements: str
                    returned: always
                    sample: []
        applied_to:
            description: List of Resources the Firewall is applied to.
            returned: always
            type: list
            elements: dict
            contains:
                type:
                    description: Type of the resource.
                    type: str
                    choices: [server, label_selector]
                    sample: label_selector
                server:
                    description: ID of the server.
                    type: int
                    sample: 12345
                label_selector:
                    description: Label selector value.
                    type: str
                    sample: env=prod
                applied_to_resources:
                    description: List of Resources the Firewall label selector is applied to.
                    returned: if RV(hcloud_firewall_info[].applied_to[].type=label_selector)
                    type: list
                    elements: dict
                    contains:
                        type:
                            description: Type of resource referenced.
                            type: str
                            choices: [server]
                            sample: server
                        server:
                            description: ID of the Server.
                            type: int
                            sample: 12345
)AnsibleModule   )AnsibleHCloud)HCloudException)BoundFirewallFirewallResourceFirewallRulec                  X     e Zd ZU dZdZded<   d Zd	dZd
dZd Z	e
 fd       Z xZS )AnsibleHCloudFirewallInfohcloud_firewall_infoNzlist[BoundFirewall] | Nonec                ^   g }| j                   D ]  }||j                  t        |j                        |j                  |j
                  |j                  D cg c]  }| j                  |       c}|j                  D cg c]  }| j                  |       c}d        |S c c}w c c}w )N)idnamelabelsrules
applied_to)
r   appendstrr   r   r   r   _prepare_result_ruler   _prepare_result_applied_to)selftmpfirewallruleresources        p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/firewall_info.py_prepare_resultz)AnsibleHCloudFirewallInfo._prepare_result   s    11 	HJJhkk*$MM&ooJR..Y$d77=Y]e]p]p"qQY4#B#B8#L"q		 
 Z"qs   B% B*c                    |j                   |j                  |j                  |j                  |j                  |j
                  dS )Ndescription	directionprotocolport
source_ipsdestination_ipsr    )r   r   s     r   r   z.AnsibleHCloudFirewallInfo._prepare_result_rule   s:    ++II//#33
 	
    c                   |j                   |j                  t        |j                  j                        nd |j                  |j                  j
                  nd d}|j                  U|j                  D cg c]<  }|j                   |j                  t        |j                  j                        nd d> c}|d<   |S c c}w )N)typeserverlabel_selector)r)   r*   applied_to_resources)r)   r*   r   r   r+   selectorr,   )r   r   resultitems       r   r   z4AnsibleHCloudFirewallInfo._prepare_result_applied_to   s    MM191Lc(//,,-RVBJBYBYBeh55>>ko

 ((4 %99.
  !II59[[5Lc$++..1RV.F)* .s   8ACc                ^   	 | j                   j                  j                  d      O| j                  j                  j                  | j                   j                  j                  d            g| _        y | j                   j                  j                  d      O| j                  j                  j                  | j                   j                  j                  d            g| _        y | j                   j                  j                  d      O| j                  j                  j                  | j                   j                  j                  d            | _        y | j                  j                  j                         | _        y # t        $ r}| j                  |       Y d }~y d }~ww xY w)Nr   r   r+   )r+   )moduleparamsgetclient	firewalls	get_by_idr   get_by_nameget_allr   fail_json_hcloud)r   	exceptions     r   get_firewallsz'AnsibleHCloudFirewallInfo.get_firewalls   s?   	-{{!!%%d+7-1[[-B-B-L-LT[[M_M_McMcdhMi-j,k)##''/;-1[[-B-B-N-Nt{{OaOaOeOeflOm-n,o)##''(89E,0KK,A,A,I,I#';;#5#5#9#9:J#K -J -) -1KK,A,A,I,I,K) 	-!!),,	-s,   A3F 6A3F *A3F )F 	F,F''F,c           	     \    t        t        dddiddiddidt        |          d      S )Nr)   intr   )r   r   r+   T)argument_specsupports_check_mode )r   dictsuperbase_module_arguments)cls	__class__s    r   define_modulez'AnsibleHCloudFirewallInfo.define_module   sI     E?e_ & '/1	 !%
 	
r'   )r   r
   )r   r	   )__name__
__module____qualname__	representr   __annotations__r   r   r   r;   classmethodrF   __classcell__)rE   s   @r   r   r      s<    &I7;4;&
 -  	
 	
r'   r   c                     t         j                         } t        |       }|j                           | j                  di |j	                          y )Nr@   )r   rF   r;   	exit_json
get_result)r1   hclouds     r   mainrR      sB    &446F&v.F
F+v((*+r'   __main__N)
__future__r   DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   module_utils.hcloudr   module_utils.vendor.hcloudr   $module_utils.vendor.hcloud.firewallsr   r	   r
   r   rR   rG   r@   r'   r   <module>r\      s^    #<]
~ 5 / 8 L
 L
^, zF r'   