
    !                         S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSK	J
r  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKrS rS rS rS rS rS rS rS rS rS rS rS r S r!g)z?Client for interaction with Governance Rules API CRUD DATAPLEX.    )absolute_import)division)unicode_literalsN)util)messages)
exceptions)iam_util)yaml)filesc           
         [         R                  " 5       nUR                  U R                  U R                  [         R
                  " UR                  U 5      [        U 5      [        U 5      [        U 5      S9nU R                  S5      (       a`  [        U 5      nUc  [        S5      eUR                  S5      c  [        S5      e[        U 5       c  [        U 5      (       d  [        S5      eU$ [        S5      e)z Create Governance Rule Requests.)descriptiondisplayNamelabelsqueryspecsfieldsrule_metadata_filez@Rule metadata file is empty for Governance Rules create request.r   z=Query should be provided for Governance Rules create request.zMEither specs or field should be provided for Governance Rules create request.zHRule metadata file is not specified for Governance Rules create request.)dataplex_apiGetMessageModule#GoogleCloudDataplexV1GovernanceRuler   display_nameCreateLabelsGenerateGovernanceRuleQueryGenerateGovernanceRuleSpecsGenerateGovernanceRuleFieldsIsSpecifiedReadRuleMetadataFile
ValueErrorget)argsmodulerequestr   s       6lib/googlecloudsdk/api_lib/dataplex/governance_rule.py&GenerateGovernanceRuleForCreateRequestr$       s   ((*&66""##&&

4
4d (-'-)$/ 7 	' 
*++-d3!
L  g&.
I  #	2488  
. 	     c           	         [         R                  " 5       nUR                  U R                  U R                  U R
                  [         R                  " UR                  U 5      S9nU R                  S5      (       aM  U R                  b@  [        U 5      n[        U 5      n[        U 5      nUb  X2l        Ub  XBl        U(       a  XRl        U$ )z Update Governance Rule Requests.)r   r   etagr   r   )r   r   r   r   r   r'   r   r   r   r   r   r   r   r   r   )r    r!   r"   r   r   r   s         r#   &GenerateGovernanceRuleForUpdateRequestr(   H   s    ((*&66""##99&&

4
4d	 7 ' +,,

!
!
-'-E'-E)$/Fmmn	.r%   c                    [         R                  R                  U R                  5      (       d/  [        R
                  " SR                  U R                  5      5      e[         R                  R                  U R                  5      (       a/  [        R
                  " SR                  U R                  5      5      e [        R                  " U R                  5       n[        R                  " U5      sSSS5        $ ! , (       d  f       g= f! [         aX  n[        US[        R                  " U5      5      nSR                  U R                  U5      n[        R
                  " U5      eSnAff = f)zRead Rule Metadata File.zNo such file [{0}]z[{0}] is a directoryNmessagezKUnable to read Rule Metadata config from specified file [{0}] because [{1}])ospathexistsr   r   BadFileExceptionformatisdirr   
FileReaderr
   load	Exceptiongetattrsix	text_type)r    import_fileexpexp_msgmsgs        r#   r   r   c   s   	//	0	0

%
%##D$;$;<  WW]]4**++

%
%%%d&=&=> 	+			$11	2kYY{# 
3	2	2	 +c9cmmC&89G	$fT%<%<gF  
%
%c
**+s7   < D C<2	D <
D
D 
D 
E/AE**E/c                    [         R                  " 5       nUR                  5       n[        U 5      nUbW  UR	                  S5      bE  [
        R                  " [         R                  " UR	                  S5      5      UR                  S5      nU$ )z7Generate Governance Rule Query From Rule Metadata File.r   T)r   r   (GoogleCloudDataplexV1GovernanceRuleQueryr   r   messages_utilDictToMessageWithErrorCheckSnakeToCamelDict)r    r!   governance_rule_queryr   s       r#   r   r   y   s    ((*& IIK+D1$

 
 
)
5)EE%%&8&<&<W&EF77
 
r%   c                     [         R                  " 5       nSn[        U 5      nUbV  UR                  S5      bD  [        R
                  " [         R                  " UR                  S5      5      UR                  5      nU$ )z7Generate Governance Rule Specs From Rule Metadata File.Nr   )r   r   r   r   r=   r>   r?   (GoogleCloudDataplexV1GovernanceRuleSpecs)r    r!   governance_rule_specsr   s       r#   r   r      st    ((*&+D1$

 
 
)
5)EE%%&8&<&<W&EF77 
r%   c                 0   [         R                  " 5       n/ n[        U 5      nUbp  UR                  S5      b^  UR                  S5      nU HG  nUR	                  [
        R                  " [         R                  " U5      UR                  5      5        MI     U$ )z8Generate Governance Rule Fields From Rule Metadata File.r   )	r   r   r   r   appendr=   r>   r?   (GoogleCloudDataplexV1GovernanceRuleField)r    r!   governance_rule_fieldsr   r   fields         r#   r   r      s    ((*&+D1$

 
 
*
6##H-F##

3
3++E2==  
 r%   c                    / nU R                  S5      (       a  UR                  S5        U R                  S5      (       a  UR                  S5        U R                  S5      (       a  UR                  S5        U R                  S5      (       ah  U R                  b[  [        U 5      b  UR                  S5        [	        U 5      b  UR                  S5        [        U 5      (       a  UR                  S5        U$ )	z'Create Update Mask for Governance Rule.r   r   r   r   r   r   r   r   )r   rE   r   r   r   r   )r    update_masks     r#   GenerateUpdateMaskrK      s    +	m$$}%	n%%}%	hx 	*++*	$T	*	67#	$T	*	67#	%d	+	+8$	r%   c                 j    [         R                  " U [         R                  " 5       R                  5      $ )z=Waits for the given google.longrunning.Operation to complete.)r   WaitForOperationGetClientInstance"projects_locations_governanceRules)	operations    r#   rM   rM      s*    		&	&$$&II
 r%   c                    [         R                  " 5       R                  US9n[         R                  " 5       R                  U R	                  5       US9n[         R
                  " 5       R                  R                  U5      $ )zSet Iam Policy request.)policy)resourcegoogleIamV1SetIamPolicyRequest)r   r   GoogleIamV1SetIamPolicyRequest;DataplexProjectsLocationsGovernanceRulesSetIamPolicyRequestRelativeNamerN   rO   SetIamPolicy)governance_rule_refrR   $google_iam_v1_set_iam_policy_requestset_iam_policy_reqs       r#   rX   rX      s     ##%DD E  '
 $446rr"//1%I s 
 
	'	'	)	L	L	Y	Y
 r%   c                     [         R                  " 5       R                  U R                  5       S9n[         R                  " 5       R
                  R                  U5      $ )zGet Iam Policy request.)rS   )r   r   ;DataplexProjectsLocationsGovernanceRulesGetIamPolicyRequestrW   rN   rO   GetIamPolicy)rY   get_iam_policy_reqs     r#   r^   r^      sU    #446rr"//1 s  
	'	'	)	L	L	Y	Y
 r%   c                     [        U 5      n[        R                  " [        R                  " 5       R
                  X1U5        [        X5      $ )zAdd IAM policy binding request.)r^   r	   AddBindingToIamPolicyr   r   GoogleIamV1BindingrX   rY   memberrolerR   s       r#   AddIamPolicyBindingrf      s?    +,&
  ##%88&$ 
)	22r%   c                 \    [        U 5      n[        R                  " X1U5        [        X5      $ )z"Remove IAM policy binding request.)r^   r	   RemoveBindingFromIamPolicyrX   rc   s       r#   RemoveIamPolicyBindingri      s)    +,&
%%fd;	)	22r%   c                     [         R                  " U[        R                  " 5       R                  5      n[        X5      $ )z)Set IAM policy binding request from file.)r	   ParsePolicyFiler   r   GoogleIamV1PolicyrX   )rY   policy_filerR   s      r#   SetIamPolicyFromFilern      s4    ##<002DD& 
)	22r%   )"__doc__
__future__r   r   r   r+   googlecloudsdk.api_lib.dataplexr   r   googlecloudsdk.api_lib.utilr   r=   googlecloudsdk.callioper   googlecloudsdk.command_lib.iamr	   googlecloudsdk.corer
   googlecloudsdk.core.utilr   r5   r$   r(   r   r   r   r   rK   rM   rX   r^   rf   ri   rn    r%   r#   <module>rx      sn    F &  ' 	 @ A . 3 $ * 
%P6+,"  (*"333r%   