
    Vh@                         d dl mZmZmZ eZd dlZd dlZd dlm	Z	 dZ
	 d dlmZ dZd Zd Zd	 Zd
 Z G d de      Z G d de      Z G d de      Zy# e$ r  ej                         Z
dZY Hw xY w)    )absolute_importdivisionprint_functionN)missing_required_lib)ManageIQClientTFc                     t        t        t        j                  j                  dd             t        t        j                  j                  dd             t        t        j                  j                  dd       d      t        t        j                  j                  dd       d      t        ddd	g
      t        dd dg            } t        t        dd|             S )NMIQ_URL)defaultMIQ_USERNAMEMIQ_PASSWORDT)r
   no_log	MIQ_TOKENbool
verify_ssl)r
   typealiasesFca_bundle_path)requiredr
   r   )urlusernamepasswordtokenvalidate_certsca_certdict)r   apply_defaultsoptions)manageiq_connection)r   osenvironget)r   s    s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/module_utils/manageiq.pymanageiq_argument_specr#       s    	489bjjnn^TBCbjjnn^TB4P2::>>+t<TJDv~NeT<L;MNG  f04)02     c                 R    t         s!| j                  t        d      t               y y )Nzmanageiq-client)msg	exception)
HAS_CLIENT	fail_jsonr   CLIENT_IMP_ERR)modules    r"   check_clientr,   1   s%    12CDP^_ r$   c                     | j                   d   }d}|d   }|d   }|d   }|d   }|r|r|s|r|r|S dD ]+  }||   dv s| j                  |j                  |      	       - y )
Nr   z2missing required argument: manageiq_connection[{}]r   r   r   r   )r   r   r   )N r&   )paramsr)   format)r+   r0   	error_strr   r   r   r   args           r"   validate_connection_paramsr4   6   s    ]]01FDI
-C7OEj!Hj!HX35. 8#;*$!1!1#!678r$   c                  $    ddddddddd	d
dddddS )N	providershostsvms
categoriesclustersdata_storesgroupsresource_poolsservicesservice_templates	templatestenantsusers
blueprints)providerhostvmcategoryclusterz
data storegroupzresource poolservicezservice templatetemplatetenantuser	blueprint rO   r$   r"   manageiq_entitiesrP   E   s.     Z},</[W< r$   c                   ^    e Zd ZdZd Zed        Zed        Zed        Zd Z	d Z
d Zd	 Zy
)ManageIQz6
        class encapsulating ManageIQ API client.
    c           	         t        |       t        |      }|d   }|d   }|d   }|d   }|d   }|d   }|| _        |dz   | _        t	        |||      | _        	 t        | j                  | j
                  ||	      | _        y # t        $ r5}	| j                  j                  d
|dt        |	             Y d }	~	y d }	~	ww xY w)Nr   r   r   r   r   r   z/api)rM   r   r   )r   r   zfailed to open connection (z): r/   )r,   r4   _module_api_urlr   _authr   _client	Exceptionr+   r)   str)
selfr+   r0   r   r   r   r   r   r   es
             r"   __init__zManageIQ.__init__T   s    V+F3Um*%*%w,-
	*fx(%H
	\)$--PZkyzDL 	\KK!!cSVWXSY&Z![[	\s   (B 	C+B<<Cc                     | j                   S )zP Ansible module module

        Returns:
            the ansible module
        )rT   rZ   s    r"   r+   zManageIQ.modulei        ||r$   c                     | j                   S )zO Base ManageIQ API

        Returns:
            the base ManageIQ API
        )rU   r^   s    r"   api_urlzManageIQ.api_urlr   s     }}r$   c                     | j                   S )zK ManageIQ client

        Returns:
            the ManageIQ client
        )rW   r^   s    r"   clientzManageIQ.client{   r_   r$   c                 0   	  | j                   j                  j                  |      j                  di |}t        |      S # t        $ r Y yt
        $ r@}| j                  j                  dj                  |             Y d}~t              S d}~ww xY w)z Searches the collection resource by the collection name and the param passed.

        Returns:
            the resource as an object if it exists in manageiq, None otherwise.
        Nzfailed to find resource {error})errorr/   rO   )
rc   collections__getattribute__r!   
ValueErrorrX   r+   r)   r1   vars)rZ   collection_namer0   entityr[   s        r"   find_collection_resource_byz$ManageIQ.find_collection_resource_by   s    	YRT[[,,==oNRR\U[\F
 F|	  	 	YKK!!&G&N&NUV&N&W!XXF|	Ys   5A 	BB,BBc                      | j                   |fi |}|r|S dj                  |t        |            }| j                  j	                  |       y)z Searches the collection resource by the collection name and the param passed.

        Returns:
            the resource as an object if it exists in manageiq, Fail otherwise.
        z;{collection_name} where {params} does not exist in manageiq)rj   r0   r/   N)rl   r1   rY   r+   r)   )rZ   rj   r0   resourcer&   s        r"    find_collection_resource_or_failz)ManageIQ.find_collection_resource_or_fail   sX     4433ONvNOOVV /F W ECKK!!c!*r$   c                 v    t        | j                        }||j                  ||      d   }t        |||      S )Nnameid)rR   r+   ro   ManageIQPolicies)rZ   resource_idresource_typeresource_namemanageiqs        r"   policieszManageIQ.policies   sC    DKK( "CCMXeCfgklK-EEr$   c                     | j                  ||      }|r|d   S dj                  ||      }| j                  j                  |       y)z Query the resource name in ManageIQ.

        Returns:
            the resource ID if it exists in ManageIQ, Fail otherwise.
        rq   rs   z:{resource_name} {resource_type} does not exist in manageiq)rw   rv   r/   N)rl   r1   r+   r)   )rZ   rv   rw   rn   r&   s        r"   query_resource_idzManageIQ.query_resource_id   sW     33M3VD>!NUU+= V JCKK!!c!*r$   N)__name__
__module____qualname____doc__r\   propertyr+   ra   rc   rl   ro   ry   r{   rO   r$   r"   rR   rR   O   s^    \*      +F+r$   rR   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)rt   zU
        Object to execute policies management operations of manageiq resources.
    c                    || _         | j                   j                  | _        | j                   j                  | _        | j                   j                  | _        || _        || _        dj                  | j                  ||      | _        y Nz'{api_url}/{resource_type}/{resource_id})ra   rv   ru   rx   r+   ra   rc   rv   ru   r1   resource_urlrZ   rx   rv   ru   s       r"   r\   zManageIQPolicies.__init__   r     mm**}},,mm***&ELLLL'# M %r$   c                 b     | j                   j                  	 di |}t        |d   |d         S )z~ Add or Update the policy_profile href field

        Example:
            {name: STR, ...} => {name: STR, href: STR}
        rr   href)rr   r   )policy_profiles)rx   ro   r   )rZ   profilern   s      r"   query_profile_hrefz#ManageIQPolicies.query_profile_href   s<     B4==AA*!(*x/?@@r$   c                 ~   d}	 | j                   j                  |j                  | j                              }j                  dg       }|D cg c]  }| j                  |       }}|S # t        $ rC}dj                  | j
                  |      }| j                  j                  |       Y d}~yd}~ww xY wc c}w )zO Returns a set of the profile objects objects assigned to the resource
        z/{resource_url}/policy_profiles?expand=resourcesr   1Failed to query {resource_type} policies: {error}rv   re   r/   N	resources)	rc   r!   r1   r   rX   rv   r+   r)   clean_profile_object)rZ   r   responser[   r&   r   r   profiless           r"   query_resource_profilesz(ManageIQPolicies.query_resource_profiles   s     @	+{{szzt?P?Pz'QRH LLb1	 GPP7D--g6PP  	+ELL"00 M C KK!!c!**		+ Q   5A+ B:+	B749B22B7c                    d}	 | j                   j                  |j                  | j                  |            }j                  dg       }|D cg c]  }| j                  |       }}|S # t        $ rC}dj                  | j
                  |      }| j                  j                  |       Y d}~yd}~ww xY wc c}w )zF Returns a set of the policy objects assigned to the resource
        z6{api_url}/policy_profiles/{profile_id}?expand=policies)ra   
profile_idr   r   r/   Nry   )	rc   r!   r1   ra   rX   rv   r+   r)   clean_policy_object)	rZ   r   r   r   r[   r&   r   policyry   s	            r"   query_profile_policiesz'ManageIQPolicies.query_profile_policies   s     G	+{{szz$,,S]z'^_H LLR0	 DMMD,,V4MM  	+ELL"00 M C KK!!c!**		+ Ns   6A, B;,	B859B33B8c                     |j                  d      }|j                  d      }|j                  d      }t        |||      S )z Clean a policy object to have human readable form of:
        {
            name: STR,
            description: STR,
            active: BOOL
        }
        rr   descriptionactive)rr   r   r   r!   r   )rZ   r   rr   r   r   s        r"   r   z$ManageIQPolicies.clean_policy_object   sD     zz&!jj/H%# 	r$   c                     |d   }|j                  d      }|j                  d      }| j                  |      }t        |||      S )z Clean a profile object to have human readable form of:
        {
            profile_name: STR,
            profile_description: STR,
            policies: ARR<POLICIES>
        }
        rs   rr   r   )profile_nameprofile_descriptionry   )r!   r   r   )rZ   r   r   rr   r   ry   s         r"   r   z%ManageIQPolicies.clean_profile_object  sO     T]
{{6"kk-0..z: + 	r$   c                    g }| j                         }t        |D cg c]  }|d   	 c}      }|D ]G  }|j                  d      |v }|dk(  r|s	|dk(  s#|r&| j                  |      }|j	                  |       I |S c c}w )z Create a list of policies we need to update in ManageIQ.

        Returns:
            Whether or not a change took place and a message describing the
            operation executed.
        r   rr   unassignassign)r   setr!   r   append)rZ   r   actionprofiles_to_postassigned_profilesr   assigned_profiles_setassigneds           r"   profiles_to_updatez#ManageIQPolicies.profiles_to_update   s      88: !$L]$^W^%<$^ _ 	1G{{6*.CCH*$v7IRZ 11': ''0	1   %_s   A=c                 &   | j                  ||      }|st        ddj                  ||            S dj                  | j                        }	 | j                  j                  |||      }d   D ]:  }|d   r	dj                  ||d         }| j                  j                  |
       < t        ddj                  ||            S # t        $ r9}dj                  ||	      }| j                  j                  |
       Y d}~d}~ww xY w)( Perform assign/unassign action
        Fz5Profiles {profiles} already {action}ed, nothing to do)r   r   changedr&   z{resource_url}/policy_profilesr   r   r   z#Failed to {action} profile: {error}r   re   r/   NresultssuccessFailed to {action}: {message}messager   r   Tz,Successfully {action}ed profiles: {profiles})	r   r   r1   r   rc   postrX   r+   r)   )	rZ   r   r   r   r   r   r[   r&   results	            r"   assign_or_unassign_profilesz,ManageIQPolicies.assign_or_unassign_profiles9  sH     228VDKRR!% S '( ( /554CTCT5U	+{{''FFV'WH y) 	/F)$5<<!"9- = / %%#%.	/ >EE! F #$ 	$  	+7>> ? C KK!!c!**		+s   C 	D/DDN)r|   r}   r~   r   r\   r   r   r   r   r   r   r   rO   r$   r"   rt   rt      s0    %A(("$ 2#$r$   rt   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	ManageIQTagszQ
        Object to execute tags management operations of manageiq resources.
    c                    || _         | j                   j                  | _        | j                   j                  | _        | j                   j                  | _        || _        || _        dj                  | j                  ||      | _        y r   r   r   s       r"   r\   zManageIQTags.__init__d  r   r$   c                 4    dj                  |d   |d         S )z/ Returns the full tag name in manageiq
        z"/managed/{tag_category}/{tag_name}rG   rr   )tag_categorytag_name)r1   )rZ   tags     r"   full_tag_namezManageIQTags.full_tag_namer  s*     4::Z[ ; " 	"r$   c           	          |j                  d      }|j                  di       }t        ||j                  d      |j                  d      |j                  di       j                  d            S )z Clean a tag object to have human readable form of:
        {
            full_name: STR,
            name: STR,
            display_name: STR,
            category: STR
        }
        rr   categorizationdisplay_namerG   )	full_namerr   r   rG   r   )rZ   r   r   r   s       r"   clean_tag_objectzManageIQTags.clean_tag_objecty  sk     GGFO	!126##F+'++N;#''
B7;;FC	E 	Er$   c                 ~   d}	 | j                   j                  |j                  | j                              }j                  dg       }|D cg c]  }| j                  |       }}|S # t        $ rC}dj                  | j
                  |      }| j                  j                  |       Y d}~yd}~ww xY wc c}w )zC Returns a set of the tag objects assigned to the resource
        z>{resource_url}/tags?expand=resources&attributes=categorizationr   z-Failed to query {resource_type} tags: {error}r   r/   Nr   )	rc   r!   r1   r   rX   rv   r+   r)   r   )rZ   r   r   r[   r&   r   r   tagss           r"   query_resource_tagsz ManageIQTags.query_resource_tags  s     O	+{{szzt?P?Pz'QRH LLb1	 7@@s%%c*@@  	+AHH"00 I C KK!!c!**		+ Ar   c                    g }| j                         }t        |D cg c]  }|d   	 c}      }|D ]H  }| j                  |      |v }|r|dk(  r|j                  |       /|r2|dk(  s8|j                  |       J |S c c}w )z Create a list of tags we need to update in ManageIQ.

        Returns:
            Whether or not a change took place and a message describing the
            operation executed.
        r   r   r   )r   r   r   r   )rZ   r   r   tags_to_postassigned_tagsr   assigned_tags_setr   s           r"   tags_to_updatezManageIQTags.tags_to_update  s     002  ] Kc[!1 KL 	)C))#.2CCHFj0##C(Fh$6##C(	)  !Ls   A>c                 "   | j                  ||      }|st        ddj                  |            S dj                  | j                        }	 | j                  j                  |||      }d   D ]:  }|d   r	dj                  ||d         }| j                  j                  |
       < t        ddj                  |            S # t        $ r9}dj                  ||	      }| j                  j                  |
       Y d}~d}~ww xY w)r   Fz&Tags already {action}ed, nothing to do)r   r   z{resource_url}/tagsr   r   zFailed to {action} tag: {error}r   r/   Nr   r   r   r   r   TzSuccessfully {action}ed tags)	r   r   r1   r   rc   r   rX   r+   r)   )	rZ   r   r   r   r   r   r[   r&   r   s	            r"   assign_or_unassign_tagsz$ManageIQTags.assign_or_unassign_tags  s8    **48<CC6CRT T
 $**8I8I*J	+{{''Fd'KH y) 	/F)$5<<!"9- = / %%#%.	/ .55V5DF 	F  	+3:: ; C KK!!c!**		+s   C 	D/D		DN)
r|   r}   r~   r   r\   r   r   r   r   r   rO   r$   r"   r   r   _  s'    %"E$(0Fr$   r   )
__future__r   r   r   r   __metaclass__r   	tracebackansible.module_utils.basicr   r*   manageiq_client.apir   r(   ImportError
format_excr#   r,   r4   rP   objectrR   rt   r   rO   r$   r"   <module>r      s    C B 
  ;2J"`
8f+v f+Rd$v d$NwF6 wFI
  )Y))+NJs   A A/.A/