
    VhL                     ^    d Z dZdZddlmZ ddlmZ  G d de      Zd Ze	d	k(  r e        y
y
)a  
---
module: quota
short_description: Manage OpenStack Quotas
author: OpenStack Ansible SIG
description:
    - Manage OpenStack Quotas. Quotas can be created,
      updated or deleted using this module. A quota will be updated
      if matches an existing project and is present.
options:
    backup_gigabytes:
        description: Maximum size of backups in GB's.
        type: int
    backups:
        description: Maximum number of backups allowed.
        type: int
    cores:
        description: Maximum number of CPU's per project.
        type: int
    fixed_ips:
        description:
          - Number of fixed IP's to allow.
          - Available until Nova API version 2.35.
        type: int
    floating_ips:
        description: Number of floating IP's to allow.
        aliases: [compute_floating_ips, floatingip, network_floating_ips]
        type: int
    gigabytes:
        description: Maximum volume storage allowed for project.
        type: int
    groups:
        description: Number of groups that are allowed for the project
        type: int
    health_monitors:
        description: Maximum number of health monitors that can be created.
        type: int
    injected_file_content_bytes:
        description:
          - Maximum file size in bytes.
          - Available until Nova API version 2.56.
        type: int
        aliases: [injected_file_size]
    injected_files:
        description:
          - Number of injected files to allow.
          - Available until Nova API version 2.56.
        type: int
    injected_file_path_bytes:
        description:
          - Maximum path size.
          - Available until Nova API version 2.56.
        type: int
        aliases: [injected_path_size]
    instances:
        description: Maximum number of instances allowed.
        type: int
    key_pairs:
        description: Number of key pairs to allow.
        type: int
    l7_policies:
        description: The maximum amount of L7 policies you can create.
        type: int
    listeners:
        description: The maximum number of listeners you can create.
        type: int
    load_balancers:
        description: The maximum amount of load balancers you can create
        type: int
        aliases: [loadbalancer]
    metadata_items:
       description: Number of metadata items allowed per instance.
       type: int
    members:
       description: Number of members allowed for loadbalancer.
       type: int
    name:
        description: Name of the OpenStack Project to manage.
        required: true
        type: str
    networks:
        description: Number of networks to allow.
        type: int
        aliases: [network]
    per_volume_gigabytes:
        description: Maximum size in GB's of individual volumes.
        type: int
    pools:
        description: The maximum number of pools you can create
        type: int
        aliases: [pool]
    ports:
        description: Number of Network ports to allow, this needs to be greater
                     than the instances limit.
        type: int
        aliases: [port]
    ram:
        description: Maximum amount of ram in MB to allow.
        type: int
    rbac_policies:
        description: Number of policies to allow.
        type: int
        aliases: [rbac_policy]
    routers:
        description: Number of routers to allow.
        type: int
        aliases: [router]
    security_group_rules:
        description: Number of rules per security group to allow.
        type: int
        aliases: [security_group_rule]
    security_groups:
        description: Number of security groups to allow.
        type: int
        aliases: [security_group]
    server_group_members:
        description: Number of server group members to allow.
        type: int
    server_groups:
        description: Number of server groups to allow.
        type: int
    snapshots:
        description: Number of snapshots to allow.
        type: int
    state:
        description: A value of C(present) sets the quota and a value of
                     C(absent) resets the quota to defaults.
        default: present
        type: str
        choices: [absent, present]
    subnets:
        description: Number of subnets to allow.
        type: int
        aliases: [subnet]
    subnet_pools:
        description: Number of subnet pools to allow.
        type: int
        aliases: [subnetpool]
    volumes:
        description: Number of volumes to allow.
        type: int
extends_documentation_fragment:
- openstack.cloud.openstack
a  
- name: Fetch current project quota
  openstack.cloud.quota:
    cloud: mycloud
    name: demoproject

- name: Reset project quota back to defaults
  openstack.cloud.quota:
    cloud: mycloud
    name: demoproject
    state: absent

- name: Change number of cores and volumes
  openstack.cloud.quota:
    cloud: mycloud
    name: demoproject
    cores: 100
    volumes: 20

- name: Update quota again
  openstack.cloud.quota:
    cloud: mycloud
    name: demo_project
    floating_ips: 5
    networks: 50
    ports: 300
    rbac_policies: 5
    routers: 5
    subnets: 5
    subnet_pools: 5
    security_group_rules: 5
    security_groups: 5
    backup_gigabytes: 500
    backups: 5
    gigabytes: 500
    groups: 1
    pools: 5
    per_volume_gigabytes: 10
    snapshots: 5
    volumes: 5
    cores: 5
    instances: 5
    key_pairs: 5
    metadata_items: 5
    ram: 5
    server_groups: 5
    server_group_members: 5

a  
quotas:
    description: Dictionary describing the project quota.
    returned: Regardless if changes where made or not
    type: dict
    contains:
        compute:
            description: Compute service quotas
            type: dict
            contains:
                cores:
                    description: Maximum number of CPU's per project.
                    type: int
                injected_file_content_bytes:
                    description: Maximum file size in bytes.
                    type: int
                injected_files:
                    description: Number of injected files to allow.
                    type: int
                injected_file_path_bytes:
                    description: Maximum path size.
                    type: int
                instances:
                    description: Maximum number of instances allowed.
                    type: int
                key_pairs:
                    description: Number of key pairs to allow.
                    type: int
                metadata_items:
                   description: Number of metadata items allowed per instance.
                   type: int
                ram:
                    description: Maximum amount of ram in MB to allow.
                    type: int
                server_group_members:
                    description: Number of server group members to allow.
                    type: int
                server_groups:
                    description: Number of server groups to allow.
                    type: int
        load_balancer:
            description: Load_balancer service quotas
            type: dict
            contains:
                health_monitors:
                    description: Maximum number of health monitors that can be
                      created.
                    type: int
                l7_policies:
                    description: The maximum amount of L7 policies you can
                       create.
                    type: int
                listeners:
                    description: The maximum number of listeners you can create
                    type: int
                load_balancers:
                    description: The maximum amount of load balancers one can
                                 create
                    type: int
                members:
                    description: The maximum amount of members for
                      loadbalancer.
                    type: int
                pools:
                    description: The maximum amount of pools one can create.
                    type: int

        network:
            description: Network service quotas
            type: dict
            contains:
                floating_ips:
                    description: Number of floating IP's to allow.
                    type: int
                networks:
                    description: Number of networks to allow.
                    type: int
                ports:
                    description: Number of Network ports to allow, this needs
                        to be greater than the instances limit.
                    type: int
                rbac_policies:
                    description: Number of policies to allow.
                    type: int
                routers:
                    description: Number of routers to allow.
                    type: int
                security_group_rules:
                    description: Number of rules per security group to allow.
                    type: int
                security_groups:
                    description: Number of security groups to allow.
                    type: int
                subnet_pools:
                    description: Number of subnet pools to allow.
                    type: int
                subnets:
                    description: Number of subnets to allow.
                    type: int
        volume:
            description: Block storage service quotas
            type: dict
            contains:
                backup_gigabytes:
                    description: Maximum size of backups in GB's.
                    type: int
                backups:
                    description: Maximum number of backups allowed.
                    type: int
                gigabytes:
                    description: Maximum volume storage allowed for project.
                    type: int
                groups:
                    description: Number of groups that are allowed for the
                                 project
                    type: int
                per_volume_gigabytes:
                    description: Maximum size in GB's of individual volumes.
                    type: int
                snapshots:
                    description: Number of snapshots to allow.
                    type: int
                volumes:
                    description: Number of volumes to allow.
                    type: int
    sample:
        quotas:
            compute:
                cores: 150,
                fixed_ips: -1,
                floating_ips: 10,
                injected_file_content_bytes: 10240,
                injected_file_path_bytes: 255,
                injected_files: 5,
                instances: 100,
                key_pairs: 100,
                metadata_items: 128,
                networks: -1,
                ram: 153600,
                security_group_rules: -1,
                security_groups: -1,
                server_group_members: 10,
                server_groups: 10,
            network:
                floating_ips: 50,
                networks: 10,
                ports: 160,
                rbac_policies: 10,
                routers: 10,
                security_group_rules: 100,
                security_groups: 10,
                subnet_pools: -1,
                subnets: 10,
            volume:
                backup_gigabytes: 1000,
                backups: 10,
                gigabytes: 1000,
                groups: 10,
                per_volume_gigabytes: -1,
                snapshots: 10,
                volumes: 10,
            load_balancer:
                health_monitors: 10,
                load_balancers: 10,
                l7_policies: 10,
                listeners: 10,
                pools: 5,
                members: 5,
    )OpenStackModule)defaultdictc            
       V   e Zd Z edDi d ed      d ed      d ed      d ed      d edg d	      d
 ed      d ed      d ed      d eddg	      d eddg	      d ed      d ed      d edd      d ed      d ed      d eddg	      d ed      d ed      d ed      d edd g	      d! ed      d" edd#g	      d$ edd%g	      d& ed      d' edd(g	      d) edd*g	      d+ edd,g	      d- edd.g	      d/ ed      d0 ed      d1 ed      d2 ed3d4d3g5      d6 edd7g	      d8 edd9g	      d: ed      Z ed;      Zh d<dhh d=dhd>Zd? Zd@ ZdA Z	dB Z
yC)EQuotaModulebackup_gigabytesint)typebackupscores	fixed_ipsfloating_ips)
floatingipcompute_floating_ipsnetwork_floating_ips)r	   aliases	gigabytesgroupshealth_monitorsinjected_file_content_bytesinjected_file_sizeinjected_file_path_bytesinjected_path_sizeinjected_files	instances	key_pairsF)r	   no_logl7_policies	listenersload_balancersloadbalancermetadata_itemsmembersnameT)requirednetworksnetworkper_volume_gigabytespoolspoolportsportramrbac_policiesrbac_policyroutersroutersecurity_group_rulessecurity_group_rulesecurity_groupssecurity_groupserver_group_membersserver_groups	snapshotsstatepresentabsent)defaultchoicessubnet_pools
subnetpoolsubnetssubnetvolumes)supports_check_mode>   r#   r%   r   r3   r1   >   r#   r(   r   r   r    r   r   )computeload_balancerr&   volumec                    i }| j                   j                  d      r3| j                   j                  j                  |j                        |d<   n| j                  d       | j                   j                  d      r3| j                   j                  j                  |j                        |d<   n| j                  d       | j                   j                  d      r3| j                   j                  j                  |j                        |d<   n| j                  d       | j                   j                  j                  |j                        |d	<   |S )
Nzblock-storagerE   z`Block storage service aka volume service is not supported by your cloud. Ignoring volume quotas.zload-balancerrD   zRLoadbalancer service is not supported by your cloud. Ignoring loadbalancer quotas.r&   zHNetwork service is not supported by your cloud. Ignoring network quotas.rC   )
connhas_serviceblock_storageget_quota_setidwarnrD   	get_quotar&   rC   )selfprojectquotas      i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/openstack/cloud/plugins/modules/quota.py_get_quotaszQuotaModule._get_quotas  s   99  1"ii55CCGJJOE(OII J K 99  1%)YY%<%<%F%F

&E/" II > ? 99  +#yy00::7::FE)II 2 399,,::7::Fi    c                 @   t        t              }|j                         D ]{  }| j                  |   }||   j                         D ]T  }||v r|| j                  v s| j                  |   '||   |   | j                  |   k7  s@| j                  |   ||   |<   V } |S N)r   dictkeysexclusion_mapparams)rN   quotaschanges
quota_type
exclusionsattrs         rQ   _build_updatezQuotaModule._build_update  s    d# ++- 	BJ++J7Jz*//1 B:%DKK'DKK,=,I":.t4D8II04D0AGJ'-B	B rS   c                 \    | j                   d   dk(  ryt        | j                  |            S )z
        Determine if changes are required to the current project quota.

        This is done by comparing the current project_quota_output against
        the desired quota settings set on the module params.
        r8   r:   T)rY   boolr_   )rN   project_quota_outputs     rQ   _system_state_changez QuotaModule._system_state_change  s/     ;;w8+D&&';<==rS   c                 v   | j                   j                  j                  | j                  d   d      }| j	                  |      }d}| j
                  j                  r!| j                  | j                  |             | j                  d   dk(  rd}| j                   j                  j                  |       d|v r/| j                   j                  j                  |j                         d	|v r%| j                   j                  j                  |       d
|v r/| j                   j                  j                  |j                         | j	                  |      }n| j                  d   dk(  r| j!                  |      }|rd	|v r7 | j                   j                  j"                  |j                  fi |d	   |d	<   d|v r7 | j                   j                  j"                  |j                  fi |d   |d<   d|v r7 | j                   j                  j$                  |j                  fi |d   |d<   d
|v r7 | j                   j                  j$                  |j                  fi |d
   |d
<   d}|j'                         D ci c]  \  }}||j)                  d       }}}| j                  ||       y c c}}w )Nr#   F)ignore_missing)changedr8   r:   Tr&   rE   rD   r9   rC   )computed)rf   rZ   )rG   identityfind_projectrY   rR   ansible
check_mode	exit_jsonrc   rC   revert_quota_setr&   delete_quotarK   rI   rD   r_   update_quota_setupdate_quotaitemsto_dict)rN   rO   rZ   rf   r[   kvs          rQ   runzQuotaModule.run  s}   ))$$11KK 2 7 !!'*<<""NN4#<#<V#DNE;;w8+ GII..w7F"		!!..wzz:6!		''88A&(		''44WZZ@ %%g.F[[!Y.((0Gw&'Otyy'>'>'O'O

(9&-h&7(9F8$'(J		(9(9(J(J

):&-i&8):F9%'(F		(9(9(F(F

):&-i&8):F9%"g-<		//<<

@&-o&>@ ?+ ;A<<>J41a!QYYY..JJwv6 Ks   J5N )__name__
__module____qualname__rV   argument_specmodule_kwargsrX   rR   r_   rc   ru   rv   rS   rQ   r   r   x  s     (5)(% ( ( E"	(
  !9:( E"( ( %(( %)e2F1G%I( "&5/C.D"F( '(  E"!(" E%0#($ e$%(& E"'(( 0@A)(* '+(, % -(. 4 /(0 59+61(2 "u-3(4 x05(6 x07(8 e9(: ?;(< %(4=(> "u7L6MN?(@ %2B1CDA(B "u-C(D &E(F E"G(H 9x.CDI(J u|n=K(L %(4M(N % O(MT  M

 !
 (-M20>/7rS   r   c                  &    t               }  |         y rU   )r   )modules    rQ   mainr~   *  s    ]F
HrS   __main__N)
DOCUMENTATIONEXAMPLESRETURNBansible_collections.openstack.cloud.plugins.module_utils.openstackr   collectionsr   r   r~   rw   rv   rS   rQ   <module>r      sP   Ob0dh
T _ #o7/ o7d
 zF rS   