
    VhG                     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_functiona  
module: manageiq_tenant

short_description: Management of tenants in ManageIQ
extends_documentation_fragment:
  - community.general.manageiq
  - community.general.attributes

author: Evert Mulder (@evertmulder)
description:
  - The manageiq_tenant module supports adding, updating and deleting tenants in ManageIQ.
requirements:
  - manageiq-client
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  state:
    type: str
    description:
      - V(absent) - tenant should not exist,
      - V(present) - tenant should be.
    choices: ['absent', 'present']
    default: 'present'
  name:
    type: str
    description:
      - The tenant name.
    required: true
    default:
  description:
    type: str
    description:
      - The tenant description.
    required: true
    default:
  parent_id:
    type: int
    description:
      - The ID of the parent tenant. If not supplied the root tenant is used.
      - The O(parent_id) takes president over O(parent) when supplied.
    required: false
    default:
  parent:
    type: str
    description:
      - The name of the parent tenant. If not supplied and no O(parent_id) is supplied the root tenant is used.
    required: false
    default:
  quotas:
    type: dict
    description:
      - The tenant quotas.
      - All parameters case sensitive.
      - 'Valid attributes are:'
      - '- V(cpu_allocated) (int): use null to remove the quota.'
      - '- V(mem_allocated) (GB): use null to remove the quota.'
      - '- V(storage_allocated) (GB): use null to remove the quota.'
      - '- V(vms_allocated) (int): use null to remove the quota.'
      - '- V(templates_allocated) (int): use null to remove the quota.'
    required: false
    default: {}
a  
- name: Update the root tenant in ManageIQ
  community.general.manageiq_tenant:
    name: 'My Company'
    description: 'My company name'
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!

- name: Create a tenant in ManageIQ
  community.general.manageiq_tenant:
    name: 'Dep1'
    description: 'Manufacturing department'
    parent_id: 1
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!

- name: Delete a tenant in ManageIQ
  community.general.manageiq_tenant:
    state: 'absent'
    name: 'Dep1'
    parent_id: 1
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!

- name: Set tenant quota for cpu_allocated, mem_allocated, remove quota for vms_allocated
  community.general.manageiq_tenant:
    name: 'Dep1'
    parent_id: 1
    quotas:
      - cpu_allocated: 100
      - mem_allocated: 50
      - vms_allocated:
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      username: 'admin'
      password: 'smartvm'
      validate_certs: false # only do this when you trust the network!


- name: Delete a tenant in ManageIQ using a token
  community.general.manageiq_tenant:
    state: 'absent'
    name: 'Dep1'
    parent_id: 1
    manageiq_connection:
      url: 'http://127.0.0.1:3000'
      token: 'sometoken'
      validate_certs: false # only do this when you trust the network!
ai  
tenant:
  description: The tenant.
  returned: success
  type: complex
  contains:
    id:
      description: The tenant ID.
      returned: success
      type: int
    name:
      description: The tenant name.
      returned: success
      type: str
    description:
      description: The tenant description.
      returned: success
      type: str
    parent_id:
      description: The ID of the parent tenant.
      returned: success
      type: int
    quotas:
      description: List of tenant quotas.
      returned: success
      type: list
      sample:
        cpu_allocated: 100
        mem_allocated: 50
)AnsibleModule)ManageIQmanageiq_argument_specc                   n    e 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d Zd Zd Zed        Zy)ManageIQTenantzI
        Object to execute tenant management operations in manageiq.
    c                     || _         | j                   j                  | _        | j                   j                  | _        | j                   j                  | _        y )N)manageiqmoduleapi_urlclient)selfr   s     u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/manageiq_tenant.py__init__zManageIQTenant.__init__   s<     mm**}},,mm**    c                 \   |r| j                   j                  j                  j                  |      }|s(| j                  j                  dt        |      z         |d   }| j                   j                  j                  j                  |      }|D ]<  }	 |d   }|st        |j                  d      d	         }	t        |	      |k(  s8||fc S  |dfS |r| j                   j                  j                  j                  |      }|s| j                  j                  d
|z         t        |      dkD  r| j                  j                  d|z         |d   }t        |d         }| j                   j                  j                  j                  |      }|D ]3  }	 |d   }|st        |j                  d      d	         }	|	|k(  s/||fc S  |dfS d| j                   j                  j                  j                  d      d   fS # t        $ r d}Y |w xY w# t        $ r d}Y w xY w)a	   Search for tenant object by name and parent_id or parent
            or the root tenant if no parent or parent_id is supplied.
        Returns:
            the parent tenant, None for the root tenant
            the tenant or None if tenant was not found.
        )idz0Parent tenant with id '%s' not found in manageiqmsgr   )nameancestryN/z(Parent tenant '%s' not found in manageiq   z;Multiple parent tenants not found in manageiq with name '%sr   )r   )r   collectionstenantsfind_byr   	fail_jsonstrAttributeErrorintsplitlen)
r   r   	parent_idparentparent_tenant_resparent_tenantr   tenantr   tenant_parent_ids
             r   r*   zManageIQTenant.tenant   sE     $ 7 7 ? ? G G9 G U$%%*\_bcl_m*m%n-a0Mkk--55==4=HG! 	5$%j1H '*8>>#+>r+B'C$+,	9,f44	5 !$&&$(KK$;$;$C$C$K$KQW$K$X!(KK)).X[a.a)b()A-KK)).knt.t)u 1! 4d 34	++1199AAtAL% 	9F(#)*#5  +.x~~c/B2/F+G(+y8#0&#88	9 %d** T[[44<<DDdDSTUVVVI & $#H$2 * (#'(s$   H HHHH+*H+c                 <    |xr |d   |k7  xs |xr |d   |k7  }| S )z Compare tenant fields with new field values.

        Returns:
            false if tenant fields have some difference from new fields, true o/w.
        r   description )r   r*   r   r-   found_differences        r   compare_tenantzManageIQTenant.compare_tenant   s=     ,fVn, CAVM2kA 	
 $##r   c           	      Z   	 | j                   d|d   }| j                  j                  |d      }d
   du r| j                  j                  |d          t        d|d         S # t        $ r8}| j                  j                  d|d   dt        |             Y d	}~rd	}~ww xY w)ze Deletes a tenant from manageiq.

        Returns:
            dict with `msg` and `changed`
        z	/tenants/r   deleteactionzfailed to delete tenant r   : r   NsuccessFmessageTchangedr   )r   r   post	Exceptionr   r    r!   dict)r   r*   urlresultes        r   delete_tenantzManageIQTenant.delete_tenant   s    	c%)\\6$<@C[[%%c(%;F )%KK!!fY&7!8DfY&788  	cKK!!&QW.Z]^_Z`&a!bb	cs   1A) )	B*2.B%%B*c           	      d   t        ||d      }| j                  |||      rt        dd|d   z  |d         S 	 | j                  j                  |d   d|	      }t        dd|d   z        S # t        $ r8}| j
                  j                  d
|d   dt        |             Y d}~Od}~ww xY w)z] Edit a manageiq tenant.

        Returns:
            dict with `msg` and `changed`
        F)r   r-   use_config_for_attributesztenant %s is not changed.r   _data)r9   r   r*   hrefeditr4   resourcezfailed to update tenant r5   r   NTz*successfully updated the tenant with id %sr   r8   )r<   r0   r   r:   r;   r   r    r!   )r   r*   r   r-   rG   r>   r?   s          r   edit_tenantzManageIQTenant.edit_tenant  s     T{V[\ vt[9/&.@g( (	c[[%%fVnVh%WF <tMO 	O  	cKK!!&QW.Z]^_Z`&a!bb	cs   !A. .	B/7.B**B/c           	         |d   }t        |||      j                         D ])  \  }}|dv s| j                  j                  d|z         + d| j                  z  }||d|id}	 | j
                  j                  |d|	      }	|	d
   d   d   }
t        dd|d
d|
      S # t        $ r5}| j                  j                  d|dt        |             Y d}~Nd}~ww xY w)zr Creates the tenant in manageiq.

        Returns:
            dict with `msg`, `changed` and `tenant_id`
        r   )r   r-   r&   )N zmissing required argument: %sr   z
%s/tenants)r   r-   r'   createrF   resultsr   zfailed to create tenant r5   NTzsuccessfully created tenant 'z' with id '')r9   r   	tenant_id)	r<   itemsr   r    r   r   r:   r;   r!   )r   r   r-   r)   r&   keyvaluer=   rG   r>   rN   r?   s               r   create_tenantzManageIQTenant.create_tenant&  s    "$'	DkYW]]_ 	QJC
"%%*IC*O%P	Q T\\) yHYZ	Y[[%%c(X%NFy)!,T2I CGS! 	!  	YKK!!$PSTUPV&W!XX	Ys   &)B$ $	C"-+CC"c                 R    | j                   j                  |d   d|      }|d   S )z Search for tenant quota object by tenant and quota_key.
        Returns:
            the quota for the tenant, or None if the tenant quota was not found.
        rD   z'/quotas?expand=resources&filter[]=name=	resourcesr   get)r   r*   	quota_keytenant_quotass       r   tenant_quotazManageIQTenant.tenant_quotaA  s.     Y_`fYgir(st[))r   c                 N    | j                   j                  d|d   z        }|d   S )z Search for tenant quotas object by tenant.
        Returns:
            the quotas for the tenant, or None if no tenant quotas were not found.
        z%s/quotas?expand=resourcesrD   rT   rU   )r   r*   rX   s      r   rX   zManageIQTenant.tenant_quotasK  s+     (Dv(WX[))r   c                    d}g }|j                         D ]  \  }}| j                  ||      }|r|d   }nd}|rO|dv rt        |      dz  dz  dz  }	nt        |      }	|r| j                  ||||	      }
n9| j	                  |||	      }
n%|r| j                  ||      }
nt        dd|z        }
|
d   rd	}|j                  |
d
           t        |dj                  |            S )zl Creates the tenant quotas in manageiq.

        Returns:
            dict with `msg` and `changed`
        Fr   N)storage_allocatedmem_allocatedi   z tenant quota '%s' does not existr8   r9   Tr   z, )	rO   rY   r#   edit_tenant_quotacreate_tenant_quotadelete_tenant_quotar<   appendjoin)r   r*   quotasr9   messagesrW   quota_valuecurrent_quota_filteredcurrent_quotaquota_value_intress              r   update_tenant_quotasz#ManageIQTenant.update_tenant_quotasU  s    &,lln 	("I{%)%6%6vy%I"% 6q 9 $ FF&)+&6&=&Dt&KO&)+&6O 00	SbcC2269oVC 226=ICu2TW`2`aC9~OOCJ'5	(8 		(#% 	%r   c           	      :   |d   |k(  rt        dd|d|      S |d   d|d   }d|i}	 | j                  j                  |d	|
       t        dd|z        S # t        $ r5}| j                  j                  d|dt        |             Y d}~Id}~ww xY w)zT Update the tenant quotas in manageiq.

        Returns:
            result
        rQ   Fztenant quota z already has value r8   rD   z/quotas/r   rE   rF   zfailed to update tenant quota r5   r   NTz$successfully updated tenant quota %s)r<   r   r:   r;   r   r    r!   )r   r*   rg   rW   re   r=   rG   r?   s           r   r^   z ManageIQTenant.edit_tenant_quota~  s     ![0>GUW W
 %+6NM$4GHC-Hh  Vh G :YFH H  h%%T]_bcd_e*f%gghs   A 	B%+BBc           	          d|d   z  }||d}	 | j                   j                  |d|       t        d
d|z        S # t        $ r5}| j                  j	                  d|dt        |             Y d	}~Id	}~ww xY w)zU Creates the tenant quotas in manageiq.

        Returns:
            result
        z	%s/quotasrD   )r   rQ   rK   rF   zfailed to create tenant quota r5   r   NTz$successfully created tenant quota %sr8   r   r:   r;   r   r    r!   r<   )r   r*   rW   re   r=   rG   r?   s          r   r_   z"ManageIQTenant.create_tenant_quota  s     VF^,%<	dKKSHE 6BD 	D  	dKK!!PY[^_`[a&b!cc	ds   = 	A;+A66A;c           	          	 | j                   j                  |d   d      }t        d	d
         S # t        $ r8}| j                  j	                  d|d   dt        |             Y d}~Ld}~ww xY w)zU deletes the tenant quotas in manageiq.

        Returns:
            result
        rD   r2   r3   zfailed to delete tenant quota 'r   z': r   NTr7   r8   rm   )r   r*   quotar>   r?   s        r   r`   z"ManageIQTenant.delete_tenant_quota  sx    	j[[%%eFmH%EF DfY&788  	jKK!!RWX^R_adefag&h!ii	js    2 	A3.A..A3c                     | j                  |d         }	 |d   }|j                  d      d   }t        |d   |d   |d   ||	      S # t        $ r d}Y &w xY w)
z Creates the ansible result object from a manageiq tenant entity

        Returns:
            a dict with the tenant id, name, description, parent id,
            quota's
        rX   r   r   r   Nr   r   r-   )r   r   r-   r&   rc   )create_tenant_quotas_responser$   r"   r<   )r   r*   r)   rX   r   r+   s         r   create_tenant_responsez%ManageIQTenant.create_tenant_response  s~     ::6/;RS	$j)H'~~c226
 d|}-& 
 	
	  	$#	$s   A AAc                 n    | si S i }| D ])  }|d   dk(  rt        |d         dz  }n|d   }|||d   <   + |S )z Creates the ansible result object from a manageiq tenant_quotas entity

        Returns:
            a dict with the applied quotas, name and value
        unitbytesrQ   i   @r   )float)rX   r>   ro   rQ   s       r   rq   z,ManageIQTenant.create_tenant_quotas_response  s^     I" 	*EV}'eGn-1CDg$)F5=!	* r   N)__name__
__module____qualname____doc__r   r*   r0   r@   rH   rR   rY   rX   rj   r^   r_   r`   rr   staticmethodrq   r.   r   r   r
   r
      sb    +6Wp$9"O2!6**'%RH0D"9
0  r   r
   c                     t        t        dd      t        dd      t        dd      t        dd      t        ddgd      t        d	i 
            } | j                  t                      t        |       }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t        |      }t        |      }	|	j                  |||      \  }
}|dk(  r4|r|	j                  |      }n |rd||fz  }n	d|d|d}t        d|      }|dk(  r|r|	j                  |||      }n2|	j                  |||
      }|j                  j                  d|d         }|r$|	j                  ||      }|d   rd|d<   |d   |d<   |j                  ddg       |	j                  ||
      |d <    |j                   d!i  y )"NTr!   )requiredtypeFr#   absentpresent)choicesdefaultr<   )r~   r   )r   r-   r&   r'   staterc   )argument_specr   r-   r&   r'   r   rc   z8tenant '%s' with parent_id %i does not exist in manageiqztenant 'z' with parent 'z' does not exist in manageiqr8   r   rN   r9   r   tenant_quotas_msgrT   rX   )expand
attributesr*   r.   )r<   updater   r   paramsr   r
   r*   r@   rH   rR   r   
get_entityrj   reloadrr   	exit_json)r   r   r   r-   r&   r'   r   rc   r   manageiq_tenantr)   r*   res_argsr   tenant_quotas_ress                  r   mainr     s   4e,$U3E2U/Hi0)D,M /12#F == D--.Kk*I]]8$FMM'"E]]8$FH$X.O+224FKM6 &44V<H PTXZcSdd  TXY_`H
 	&2264MH '44T;VH__//	8K;PQF  / D DVV T +&*#0A%0H,-[o5FG,CCFMZF x r   __main__N)
__future__r   r   r   r~   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Cansible_collections.community.general.plugins.module_utils.manageiqr   r   objectr
   r   rw   r.   r   r   <module>r      s]    C B@D9v
> 5 psV sl	A!H zF r   