
                             d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ d Zd Zd Zd Zd Zd Zd Zd Zd Zy)z,Utility functions for `gcloud pam` commands.    )absolute_import)division)unicode_literalsN)apis)arg_parsers)base)log)yamlc                     d|_         |S )zRModify request hook to set the force field in delete entitlement requests to true.T)force
unused_refunused_argsreqs      *lib/googlecloudsdk/command_lib/pam/util.py'SetForceFieldInDeleteEntitlementRequestr      s    #)	*    c                     |j                   j                  j                         }|j                  j	                         j                         |_        |j                  j                         |_        |S )zRModify request hook to parse the entitlement name into a CreateEntitlementRequest.)	CONCEPTSentitlementParseresultParentRelativeNameparentNameentitlementId)r   argsr   r   s       r   0ParseEntitlementNameIntoCreateEntitlementRequestr   %   sU    ))//1+!!((*779#*!((--/#	*r   c                     d|_         |S )zWModify request hook to set the update mask field in update entitlement requests to '*'.*
updateMaskr   s      r   'SetUpdateMaskInUpdateEntitlementRequestr$   -       #.	*r   c                 z   i }| j                   r| j                   |d<   | j                  s|S i |d<   | j                  j                  }|D ]7  }|j                  dv s|j                  j
                  |d   |j                  <   9 t        j                  j                  dj                  | j                                |S )z-Formats the response of the withdraw command.namemetadata)
apiVersion
createTimetargetzGrant withdrawal initiated. The operation will complete in some time. To track its status, run:
`gcloud pam operations wait {}`
Note that the wait command requires you to have the `privilegedaccessmanager.operations.get` permission on the resource.)
r'   r(   additionalPropertieskeyvaluestring_valuer	   statusPrintformat)responser   modified_response
propertiesprops        r   FormatWithdrawResponser7   3   s    ]] (f			"$J  55*dxx9904

0G0G
#DHH-  **N vhmm 
r   c                     | j                   j                         }|t        j                  j                  k(  ry|t        j                  j                  k(  ry|t        j                  j
                  k(  ryy)zReturn API version based on args.

  Args:
    args: The argparse namespace.

  Returns:
    API version (e.g. v1alpha or v1beta).
  v1alphav1betav1N)calliope_commandReleaseTrackr   ALPHABETAGA)r   release_tracks     r   GetApiVersionFromArgsrB   J   sb     ''446-d''---d'',,,d''*** +r   c                    |j                   s|S t        j                  dt        |            }t	        |j                         dkD  rt        j                  d      |j                   D ]  }|j                         }d}|j                  d      }t        j                  ||      rt	        |      dk7  r$t        j                  dj                  |            d|d	   j                         d
d z   }d|z   }|j                         }	|j                         |	_        ||	j                  _        ||	j                  _        |j$                  j&                  j)                  |	        |S )z]Modify request hook to populate the requestedPrivilegedAccess field in create grant requests.privilegedaccessmanager   z/Only one resource is supported for grant scope.z%^(projects|organizations|folders)\/.+/   zcInvalid resource name: {}. Resource name must be of the form (projects|organizations|folders)/<id>.z$cloudresourcemanager.googleapis.com/r   Nz&//cloudresourcemanager.googleapis.com/)requested_resourcesr   GetMessagesModulerB   lenr   ArgumentTypeErrorstripsplitrematchr2   
capitalizeRequestedPrivilegedAccess%RequestedPrivilegedAccessGcpIamAccessgcpIamAccessresourceTyperesourcegrantrequestedPrivilegedAccessappend)
r   r   r   messagesrV   pattern
componentsresource_type	full_namerequested_privileged_accesss
             r   0SetRequestedPrivilegedAccessInCreateGrantRequestr`   \   sU   		!	!J##!6t!<( 		!	!"Q&

'
'9  **h~~H6G$J88GX&#j/Q*>))44:F84D 
 	/A1I1I1KCR1PP  98CI"*"D"D"F668  , =J,,98A,,5II''../JK' +* 
*r   c                 D    | r| d   sg S t        j                  | d         S )aJ  Loads a YAML document from a stream.

  This function takes a stream (expected to be a list with a single string
  element) and parses it as a YAML document. It returns the loaded YAML data as
  a Python object (typically a list or dictionary).

  Args:
    stream: The stream to load from.

  Returns:
   The loaded YAML data.
  r   )r
   load)streams    r   LoadGrantScopeFromYamlrd      s%     
vayI	6!9	r   c                 .    |j                   dz   |_         |S )zFModify request hook to update the resource field in settings requests.z	/settings)r'   r   s      r   UpdateSettingsResourcerf      s    XX##(	*r   c                     d|_         |S )zTModify request hook to set the update mask field in update settings requests to '*'.r!   r"   r   s      r   $SetUpdateMaskInUpdateSettingsRequestrh      r%   r   )__doc__
__future__r   r   r   rO   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r   googlecloudsdk.corer	   r
   r   r   r$   r7   rB   r`   rd   rf   rh    r   r   <module>ro      sS     3 &  ' 	 , / ( # $.$ F*r   