
    VhV                     R    d Z dZdZddlmZ  G d de      Zd Zedk(  r e        y	y	)
a%  
---
module: security_group
short_description: Manage Neutron security groups of an OpenStack cloud.
author: OpenStack Ansible SIG
description:
  - Add or remove Neutron security groups to/from an OpenStack cloud.
options:
  description:
    description:
      - Long description of the purpose of the security group.
    type: str
  name:
    description:
      - Name that has to be given to the security group. This module
        requires that security group names be unique.
    required: true
    type: str
  project:
    description:
      - Unique name or ID of the project.
    type: str
  security_group_rules:
    description:
      - List of security group rules.
      - When I(security_group_rules) is not defined, Neutron might create this
        security group with a default set of rules.
      - Security group rules which are listed in I(security_group_rules)
        but not defined in this security group will be created.
      - When I(security_group_rules) is not set, existing security group rules
        which are not listed in I(security_group_rules) will be deleted.
      - When updating a security group, one has to explicitly list rules from
        Neutron's defaults in I(security_group_rules) if those rules should be
        kept. Rules which are not listed in I(security_group_rules) will be
        deleted.
    type: list
    elements: dict
    suboptions:
      description:
        description:
          - Description of the security group rule.
        type: str
      direction:
        description:
          - The direction in which the security group rule is applied.
          - Not all providers support C(egress).
        choices: ['egress', 'ingress']
        default: ingress
        type: str
      ether_type:
        description:
          - Must be IPv4 or IPv6, and addresses represented in CIDR must
            match the ingress or egress rules. Not all providers support IPv6.
        choices: ['IPv4', 'IPv6']
        default: IPv4
        type: str
      port_range_max:
        description:
          - The maximum port number in the range that is matched by the
            security group rule.
          - If the protocol is TCP, UDP, DCCP, SCTP or UDP-Lite this value must
            be greater than or equal to the I(port_range_min) attribute value.
          - If the protocol is ICMP, this value must be an ICMP code.
        type: int
      port_range_min:
        description:
          - The minimum port number in the range that is matched by the
            security group rule.
          - If the protocol is TCP, UDP, DCCP, SCTP or UDP-Lite this value must
            be less than or equal to the port_range_max attribute value.
          - If the protocol is ICMP, this value must be an ICMP type.
        type: int
      protocol:
        description:
          - The IP protocol can be represented by a string, an integer, or
            null.
          - Valid string or integer values are C(any) or C(0), C(ah) or C(51),
            C(dccp) or C(33), C(egp) or C(8), C(esp) or C(50), C(gre) or C(47),
            C(icmp) or C(1), C(icmpv6) or C(58), C(igmp) or C(2), C(ipip) or
            C(4), C(ipv6-encap) or C(41), C(ipv6-frag) or C(44), C(ipv6-icmp)
            or C(58), C(ipv6-nonxt) or C(59), C(ipv6-opts) or C(60),
            C(ipv6-route) or C(43), C(ospf) or C(89), C(pgm) or C(113), C(rsvp)
            or C(46), C(sctp) or C(132), C(tcp) or C(6), C(udp) or C(17),
            C(udplite) or C(136), C(vrrp) or C(112).
          - Additionally, any integer value between C([0-255]) is also valid.
          - The string any (or integer 0) means all IP protocols.
          - See the constants in neutron_lib.constants for the most up-to-date
            list of supported strings.
        type: str
      remote_group:
        description:
          - Name or ID of the security group to link.
          - Mutually exclusive with I(remote_ip_prefix).
        type: str
      remote_ip_prefix:
        description:
          - Source IP address(es) in CIDR notation.
          - When a netmask such as C(/32) is missing from I(remote_ip_prefix),
            then this module will fail on updates with OpenStack error message
            C(Security group rule already exists.).
          - Mutually exclusive with I(remote_group).
        type: str
  state:
    description:
      - Should the resource be present or absent.
    choices: [present, absent]
    default: present
    type: str
  stateful:
    description:
      - Should the resource be stateful or stateless.
    type: bool
extends_documentation_fragment:
  - openstack.cloud.openstack
az	  
security_group:
  description: Dictionary describing the security group.
  type: dict
  returned: On success when I(state) is C(present).
  contains:
    created_at:
      description: Creation time of the security group
      type: str
      sample: "yyyy-mm-dd hh:mm:ss"
    description:
      description: Description of the security group
      type: str
      sample: "My security group"
    id:
      description: ID of the security group
      type: str
      sample: "d90e55ba-23bd-4d97-b722-8cb6fb485d69"
    name:
      description: Name of the security group.
      type: str
      sample: "my-sg"
    project_id:
      description: Project ID where the security group is located in.
      type: str
      sample: "25d24fc8-d019-4a34-9fff-0a09fde6a567"
    revision_number:
      description: The revision number of the resource.
      type: int
    tenant_id:
      description: Tenant ID where the security group is located in. Deprecated
      type: str
      sample: "25d24fc8-d019-4a34-9fff-0a09fde6a567"
    security_group_rules:
      description: Specifies the security group rule list
      type: list
      sample: [
        {
          "id": "d90e55ba-23bd-4d97-b722-8cb6fb485d69",
          "direction": "ingress",
          "protocol": null,
          "ethertype": "IPv4",
          "description": null,
          "remote_group_id": "0431c9c5-1660-42e0-8a00-134bec7f03e2",
          "remote_ip_prefix": null,
          "tenant_id": "bbfe8c41dd034a07bebd592bf03b4b0c",
          "port_range_max": null,
          "port_range_min": null,
          "security_group_id": "0431c9c5-1660-42e0-8a00-134bec7f03e2"
        },
        {
          "id": "aecff4d4-9ce9-489c-86a3-803aedec65f7",
          "direction": "egress",
          "protocol": null,
          "ethertype": "IPv4",
          "description": null,
          "remote_group_id": null,
          "remote_ip_prefix": null,
          "tenant_id": "bbfe8c41dd034a07bebd592bf03b4b0c",
          "port_range_max": null,
          "port_range_min": null,
          "security_group_id": "0431c9c5-1660-42e0-8a00-134bec7f03e2"
        }
      ]
    stateful:
      description: Indicates if the security group is stateful or stateless.
      type: bool
    tags:
      description: The list of tags on the resource.
      type: list
    updated_at:
      description: Update time of the security group
      type: str
      sample: "yyyy-mm-dd hh:mm:ss"
a  
- name: Create a security group
  openstack.cloud.security_group:
    cloud: mordred
    state: present
    name: foo
    description: security group for foo servers

- name: Create a stateless security group
  openstack.cloud.security_group:
    cloud: mordred
    state: present
    stateful: false
    name: foo
    description: stateless security group for foo servers

- name: Update the existing 'foo' security group description
  openstack.cloud.security_group:
    cloud: mordred
    state: present
    name: foo
    description: updated description for the foo security group

- name: Create a security group for a given project
  openstack.cloud.security_group:
    cloud: mordred
    state: present
    name: foo
    project: myproj

- name: Create (or update) a security group with security group rules
  openstack.cloud.security_group:
    cloud: mordred
    state: present
    name: foo
    security_group_rules:
      - ether_type: IPv6
        direction: egress
      - ether_type: IPv4
        direction: egress

- name: Create (or update) security group without security group rules
  openstack.cloud.security_group:
    cloud: mordred
    state: present
    name: foo
    security_group_rules: []
    )OpenStackModulec                   `   e Zd Z e e        ed       e        edd e e        edddg       eddd	g       ed
       ed
       e        e        e                    edddg       ed            Z ed      Zd Zd Zd Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zy) SecurityGroupModuleT)requiredlistdictingressegress)defaultchoicesIPv4IPv6int)type)description	direction
ether_typeport_range_maxport_range_minprotocolremote_groupremote_ip_prefix)r   elementsoptionspresentabsentbool)r   nameprojectsecurity_group_rulesstatestateful)supports_check_modec                 f   | j                   d   }| j                         }| j                  j                  r"| j	                  | j                  ||             |dk(  r6|s4| j                         }| j	                  d|j                  d             y |dk(  rT|rR| j                  |      }|r| j                  ||      }| j	                  t        |      |j                  d             y |dk(  r&|r$| j                  |       | j	                  d       y |dk(  r|s| j	                  d       y y y )	Nr!   )changedr   TF)computed)r%   security_groupr   )params_findansible
check_mode	exit_json_will_change_createto_dict_build_update_updater   _delete)selfr!   r'   updates       r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/openstack/cloud/plugins/modules/security_group.pyrunzSecurityGroupModule.run  s   G$<<""NN4#4#4UN#KNLIn!\\^NNN-55u5E  G iN''7F!%nf!ENNV-55u5E  G h>LL(NN4N(h~NN5N) (6    c                 J    i | j                  |      | j                  |      S N)_build_update_security_group"_build_update_security_group_rulesr3   r'   s     r5   r0   z!SecurityGroupModule._build_update<  s6    G//?G55nEG 	Gr7   c                      i }dD cg c](  } j                   |    j                   |   |   k7  r|* }}|r! j                  dj                  |             t         fddD              }|r||d<   |S c c}w )N zCannot update parameters {0}msgc              3      K   | ]9  }j                   |   (j                   |   |   k7  r|j                   |   f ; y wr9   r(   ).0kr'   r3   s     r5   	<genexpr>zCSecurityGroupModule._build_update_security_group.<locals>.<genexpr>O  sK      C![[^7"kk!nq0AA dkk!n- Cs   ?A)r   
attributes)r(   	fail_jsonformatr   )r3   r'   r4   rD   non_updateable_keysrF   s   ``    r5   r:   z0SecurityGroupModule._build_update_security_groupA  s    
 +- HQ"&++a."<#';;q>^A5F#F  ! H H NN= &': ;  =  C#2C C

 #-F< !Hs   -A9c                 d   
  j                   d   i S 
 fd}d 
i }i }g }g } j                  |      D ]1  } |||j                        }|r	|||d   <   !|j                  |       3 |j                  D ])  }	|	d   |j	                         vs|j                  |	       + |r||d<   |r||d<   |S )Nr    c                     |D cg c]  } | |      r| }}t        |      dkD  rj                  d       y t        |      dk(  r|d   S y c c}w )N   zRFound more a single matching security group rule which match the given parameters.r?   r   )lenrG   )	prototyper    rmatchesis_security_group_rule_matchr3   s       r5   find_security_group_rule_matchz^SecurityGroupModule._build_update_security_group_rules.<locals>.find_security_group_rule_matcha  sl    "6 FQ6y!D  FG F7|a $2 3 W"qz!Fs   Ac                 @    dg}d v rd    d   k7  ryd v rK d   dv rDd v r d   dv rd   y|j                  d       d v r d   d	v rd   y|j                  d       t         fd
t         j                               t        |      z
  D              rS y )Nr   	ethertypeFr   tcpudpr   )i  r   )rX   rL   c              3   4   K   | ]  }|   |   k(    y wr9   r>   )rC   rD   rN   security_group_rules     r5   rE   zoSecurityGroupModule._build_update_security_group_rules.<locals>.is_security_group_rule_match.<locals>.<genexpr>  s)      F 'q)Yq\9 Fs   )appendallsetkeys)rN   rZ   	skip_keyss   `` r5   rQ   z\SecurityGroupModule._build_update_security_group_rules.<locals>.is_security_group_rule_matchm  s    %Iy(&{3y7NNY&Z(N: $y0 !12kA*+;<H$$$%56#y0 !12g=*+;<H$$$%56 F !12S^CF F**r7   idcreate_security_group_rulesdelete_security_group_rules)r(   _generate_security_group_rulesr    r[   r^   )r3   r'   rR   r4   keep_security_group_rulesra   rb   rN   matchrZ   rQ   s   `         @r5   r;   z6SecurityGroupModule._build_update_security_group_rulesY  s    ;;-.6 I
		8 $&!&(#&(#<<^L 	>I2>>>@E9>)%+6+229=	> $2#F#F 	H#D)/4467+223FG	H
 '4OF01&4OF01r7   c                 T    t         fddD              } j                  d   }|6 j                  j                  j	                  |d      }|j
                  |d<     j                  j                  j                  di |} j                  |      }|r j                  ||      }|S )Nc              3   ^   K   | ]$  }j                   |   |j                   |   f & y wr9   rB   )rC   rD   r3   s     r5   rE   z.SecurityGroupModule._create.<locals>.<genexpr>  s4      5Q3 $++a.) 5s   *-)r   r   r"   r   F
name_or_idignore_missing
project_idr>   )
r   r(   connidentityfind_projectr`   networkcreate_security_groupr;   _update_security_group_rules)r3   kwargsproject_name_or_idr   r'   r4   s   `     r5   r.   zSecurityGroupModule._create  s     5B5 5 "[[3)ii((55-e 6 EG#*::F< @**@@J6J88H!>>~?EGN r7   c                 b    | j                   j                  j                  |j                         y r9   )rl   ro   delete_security_groupr`   r<   s     r5   r2   zSecurityGroupModule._delete  s     		//0A0ABr7   c                    t        | j                  d         }| j                  d   }|6| j                  j                  j	                  |d      }|j
                  |d<    | j                  j                  j                  di |S )Nr   )ri   r   Frh   rk   r>   )r   r(   rl   rm   rn   r`   ro   find_security_group)r3   rr   rs   r   s       r5   r)   zSecurityGroupModule._find  s}    V!45![[3)ii((55-e 6 EG#*::F< 4tyy  44>v>>r7   c                      i j                   <   j                  <    fd} j                  d   xs g D cg c]
  } ||       c}S c c}w )Nc                     t         fddD              }j                  |d<   j                  |d<    d   }|E|	v r	|   }n,
j                  j                  j                  |d      }|	|<   |j                  |d<    d	   }|||d	<    d
   }|	|dvr||d
<    d   } d   }|dv r>|t        |      dk7  rt        |      |d<   |t        |      dk7  rt        |      |d<   |S |dv r>|t        |      dk7  rt        |      |d<   |t        |      dk7  rt        |      |d<   |S |dv r	 |S |t        |      |d<   |t        |      |d<   |S )Nc              3   6   K   | ]  }|   	||   f  y wr9   r>   )rC   rD   r(   s     r5   rE   zlSecurityGroupModule._generate_security_group_rules.<locals>._generate_security_group_rule.<locals>.<genexpr>  s+      *!9( F1I*s   )r   r   r   rk   security_group_idr   F)rj   remote_group_idr   r   )any0r   r   )icmpz	ipv6-icmprX   rU   )r   rk   r`   rl   ro   rw   r   )r(   rN   remote_group_name_or_idr   r   r   r   r   r'   security_group_cacher3   s   `       r5   _generate_security_group_rulezYSecurityGroupModule._generate_security_group_rules.<locals>._generate_security_group_rule  s    *I* *I& '5&?&?Il#-;->->I)*&,^&<#&2*.BB,-DE ! $(99#4#4#H#H/ $I $GL % ))@A 0<	+,-J%*4	,'j)H#(D(0	*%#$45N#$45N00!-#n2E2K25n2EI./!-#n2E2K25n2EI./  ^+!-#n2E2K25n2EI./!-#n2E2K25n2EI./  \)  "-25n2EI./!-25n2EI./r7   r    )r   r`   r(   )r3   r'   r   rO   r   s   ``  @r5   rc   z2SecurityGroupModule._generate_security_group_rules  sl    !4B^0012@^../C	L ++&<=CF .a0 F 	F Fs    Ac                 J    | j                  ||      }| j                  ||      S r9   )_update_security_grouprq   )r3   r'   r4   s      r5   r1   zSecurityGroupModule._update  s'    44^VL00HHr7   c                     |j                  d      }|r1 | j                  j                  j                  |j                  fi |}|S )NrF   )getrl   ro   update_security_groupr`   )r3   r'   r4   rF   s       r5   r   z*SecurityGroupModule._update_security_group  sH    ZZ-
DTYY..DD!!1%/1N r7   c                 `   |j                  d      }|r/|D ]*  }| j                  j                  j                  |d          , |j                  d      }|r%| j                  j                  j	                  |       |s|r/| j                  j                  j                  |j                        S |S )Nrb   r`   ra   )r   rl   ro   delete_security_group_rulera   get_security_groupr`   )r3   r'   r4   rb   rZ   ra   s         r5   rq   z0SecurityGroupModule._update_security_group_rules  s    &,jj1N&O#&'B J#		!!../B4/HIJ '-jj1N&O#&II++,GH&*E99$$778I8IJJ!!r7   c                 f    |dk(  r|sy|dk(  r|rt        | j                  |            S |dk(  r|ryy)Nr   Tr   F)r   r0   )r3   r!   r'   s      r5   r-   z SecurityGroupModule._will_change0  sA    IniN**>:;;h> r7   N)__name__
__module____qualname__r   argument_specmodule_kwargsr6   r0   r:   r;   r.   r2   r)   rc   r1   r   rq   r-   r>   r7   r5   r   r      s     F4 !& Fy(0)'<>8HI#/#/!V!%

 9x.CD6"'M,  M *DG
0HT(C	?KFZI"$	r7   r   c                  &    t               }  |         y r9   )r   )modules    r5   mainr   <  s     "F
Hr7   __main__N)DOCUMENTATIONRETURNEXAMPLESBansible_collections.openstack.cloud.plugins.module_utils.openstackr   r   r   r   r>   r7   r5   <module>r      sM   rhJ
X/b _}/ }@

 zF r7   