
    Vh0                      z    d dl mZmZmZ eZdZdZdZd dl	m
Z
 ddlmZmZ  G d d	e      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functionu  
---
module: firewall_rule
short_description: Manages firewall rules on Vultr
description:
  - Create and remove firewall rules.
version_added: "1.0.0"
author: "René Moser (@resmo)"
options:
  group:
    description:
      - Name of the firewall group.
    required: true
    type: str
  ip_type:
    description:
      - IP address version
    choices: [ v4, v6 ]
    type: str
    default: v4
  protocol:
    description:
      - Protocol of the firewall rule.
    choices: [ icmp, tcp, udp, gre, esp, ah ]
    type: str
    default: tcp
  subnet:
    description:
      - The network or IP, e.g. 192.0.2.123 or 0.0.0.0.
      - Mutally exclusive with I(source).
    type: str
  subnet_size:
    description:
      - The number of bits for the netmask in CIDR notation, e.g. C(32).
    type: int
  port:
    description:
      - Single port or port range, e.g. C(80) or C(8000:8080).
      - Required if I(protocol) is tcp or udp and I(state=present).
    aliases: [ port_range ]
    type: str
  source:
    description:
      - Possible values are C(cloudflare) or a loadbalancer label.
      - Mutally exclusive with I(subnet).
    type: str
  notes:
    description:
      - Notes of the firewall rule.
    type: str
  state:
    description:
      - State of the firewall rule.
    default: present
    choices: [ present, absent ]
    type: str
extends_documentation_fragment:
  - vultr.cloud.vultr_v2
a_  
- name: Ensure a firewall rule is present
  vultr.cloud.firewall_rule:
    group: web
    port: 80
    protocol: tcp
    ip_type: v4
    subnet: "0.0.0.0"
    subnet_size: 0
    notes: "open HTTP to the world"

- name: Ensure a firewall rule with port range is present
  vultr.cloud.firewall_rule:
    group: apps
    port: "8000:8999"
    protocol: tcp
    ip_type: v4
    subnet: "10.10.10.0"
    subnet_size: 24

- name: Ensure a firewall rule is absent
  vultr.cloud.firewall_rule:
    group: apps
    port: "443"
    protocol: tcp
    ip_type: v6
    subnet: "::"
    subnet_size: 0
    state: absent
a  
---
vultr_api:
  description: Response from Vultr API with a few additions/modification.
  returned: success
  type: dict
  contains:
    api_timeout:
      description: Timeout used for the API requests.
      returned: success
      type: int
      sample: 60
    api_retries:
      description: Amount of max retries for the API requests.
      returned: success
      type: int
      sample: 5
    api_retry_max_delay:
      description: Exponential backoff delay in seconds between retries up to this max delay value.
      returned: success
      type: int
      sample: 12
    api_endpoint:
      description: Endpoint used for the API requests.
      returned: success
      type: str
      sample: "https://api.vultr.com/v2"
vultr_firewall_rule:
  description: Response from Vultr API.
  returned: success
  type: dict
  contains:
    id:
      description: ID of the firewall rule.
      returned: success
      type: int
      sample: 1
    action:
      description: Action of the firewall rule.
      returned: success
      type: str
      sample: accept
    protocol:
      description: Protocol of the firewall rule.
      returned: success
      type: str
      sample: tcp
    port:
      description: Port or port range of the firewall rule.
      returned: success
      type: str
      sample: "80"
    source:
      description: Source string of the firewall rule.
      returned: success
      type: str
      sample: cloudflare
    notes:
      description: Supplied description of the firewall rule.
      returned: success
      type: str
      sample: my rule
    subnet:
      description: Subnet of the firewall rule.
      returned: success
      type: str
      sample: 0.0.0.0
    subnet_size:
      description: Size of the subnet of the firewall rule.
      returned: success
      type: int
      sample: 0
    ip_type:
      description: IP type of the firewall rule.
      returned: success
      type: str
      sample: v4
)AnsibleModule   )AnsibleVultrvultr_argument_specc                   *    e Zd Zd Zd Zd Zd Zd Zy)AnsibleVultrFirewallRulec                 .    | j                  ddddd      S )Ndescriptiongroupz
/firewallsfirewall_groupsTkey_name	param_keypath
result_keyfail_not_foundquery_filter_list_by_nameselfs    m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/vultr/cloud/plugins/modules/firewall_rule.pyget_firewall_groupz+AnsibleVultrFirewallRule.get_firewall_group   s)    --"( . 
 	
    c                 .    | j                  ddddd      S )Nlabelsourcez/load-balancersload_balancersTr   r   r   s    r   get_load_balancerz*AnsibleVultrFirewallRule.get_load_balancer   s)    --"' . 
 	
r   c                    | j                   | j                         d   z  | _         | j                  j                  j	                  d      }|/|dk7  r*| j                         d   | j                  j                  d<   | j                  j                  j	                  d      dvr| j                  j                  j	                  d      | j                  j                  d| j                  j                  j	                  d      d| j                  j                  j	                  d      d	       d | j                  j                  d<   y y y )
Nidr   
cloudflareprotocol)tcpudpportzSetting a port (z3) only affects protocols TCP/UDP, but protocol is: z. Ignoring.)resource_pathr   moduleparamsgetr!   warn)r   r   s     r   	configurez"AnsibleVultrFirewallRule.configure   s   !//$2I2I2KD2QQ ##''1&L"8+/+A+A+CD+IDKKx( KK"":.
 ""&&v.:KK;;%%))&14;;3E3E3I3I*3UW *.DKKv& ;r   c                     t               }| j                         D ]N  }dD ]@  }| j                  j                  j	                  |      }|+|j	                  |      |k7  s@ n |}|sM |S  |S )N)ip_typer%   r(   r   subnetsubnet_size)dict
query_listr*   r+   r,   )r   resultresourcekeyparams        r   queryzAnsibleVultrFirewallRule.query   s    ) 	H " **..s3=<<$-"  "-	, r   c                     |S )N )r   r6   s     r   updatezAnsibleVultrFirewallRule.update   s    r   N)__name__
__module____qualname__r   r!   r.   r9   r<   r;   r   r   r   r      s    

.04r   r   c                     t               } | j                  t        t        d      t        dd      t        ddg      t        d      t        d      t        d      t        dg dd	
      t        dddgd
      t        dddgd
      	             t        | dddd      }t	        |ddddg d      }|j
                  j                  d      dk(  r|j                          y |j                          y )Nstr)typeT)rB   required
port_range)rB   aliasesint)icmpr&   r'   greespahr&   )rB   choicesdefaultv4v6presentabsent)	notesr   r(   r1   r2   r   r%   r0   state))r   r1   ))r1   r2   )argument_specrequired_one_ofmutually_exclusiverequired_togethersupports_check_modevultr_firewall_rulez/firewalls/%s/rulesfirewall_rulez
##unused##)rQ   r(   r1   r2   r   r%   r0   )r*   	namespacer)   ressource_result_key_singularresource_key_nameresource_create_param_keysrR   )	r	   r<   r3   r   r   r+   r,   rP   rO   )rS   r*   vultrs      r   mainr_     s    ')ME"ED15<.9U#%(U#B
 edD\4HEIx+@)T	
$ #/26 F %'+&5&$
E" }}!X-r   __main__N)
__future__r   r   r   rB   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   module_utils.vultr_v2r   r	   r   r_   r=   r;   r   r   <module>rh      s\    A @:x>M
^ 5 EF| FR0f zF r   