
    B                     ~   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	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Jr  SSKJr  SSKJr  SSKr\
R:                  " S5      r\
R:                  " S5      rS r  SS jr!S r"S r#S r$S r%S r&S r'S r(S r)S r*S r+ SS jr,S r-S r.g) z%Bigtable authorized views API helper.    )absolute_import)division)unicode_literalsN)encoding)
exceptions)util)yaml)
console_io)resource_diff)edita      To create an authorized view, specify a JSON or YAML formatted
    representation of a valid authorized view protobuf.
    Lines beginning with "#" are ignored.

    Example:
    {
      "subsetView":
      {
        "rowPrefixes": ["store1#"],
        "familySubsets":
        {
          "column_family_name":
          {
            "qualifiers":["address"],
            "qualifierPrefixes":["tel"]
          }
        }
      },
      "deletionProtection": true
    }
  a      Please pecify a JSON or YAML formatted representation of the new authorized
    view. Lines beginning with "#" are ignored.

    Example:
    {
      "subsetView":
      {
        "rowPrefixes": ["store1#"],
        "familySubsets":
        {
          "column_family_name":
          {
            "qualifiers":["address"],
            "qualifierPrefixes":["tel"]
          }
        }
      },
      "deletionProtection": true
    }

    Current authorized view:
  c                     UR                   (       a&  [        UR                   UR                  5      Ul        O[	        SUR                  S9Ul        SUR                  l        U$ )zParse argument and construct create authorized view request.

Args:
  unused_ref: the gcloud resource (unused).
  args: input arguments.
  req: the real request to be sent to backend service.

Returns:
  The real request to be sent to backend service.
T)	is_createpre_encodedN)definition_file/ParseAuthorizedViewFromYamlOrJsonDefinitionFiler   authorizedView!PromptForAuthorizedViewDefinitionname)
unused_refargsreqs      7lib/googlecloudsdk/api_lib/bigtable/authorized_views.py!ModifyCreateAuthorizedViewRequestr   X   s\     
Hd..C ;D$4$4C !# 
*    c                    [         R                  " 5       R                  nU (       a  [        5       nO[	        X!5      n [
        R                  " U5      n [        R                  " U5      nU(       d  [        U5        [        R                  " X75      nU$ ! [
        R                   a    [        S5      e[
        R                   a  n[        SR                  U5      5      eSnAff = f! [        R                    a  n[#        SR                  U5      5      eSnAf[$         a  n[#        SR                  U5      5      eSnAff = f)a  Prompt user to fill out a JSON/YAML format representation of an authorized view.

Returns the parsed authorized view proto message from user's response.

Args:
  is_create: True if the prompt is for creating an authorized view. False if
    the prompt is for updating an authorized view.
  pre_encoded: True if all binary fields in the authorized view definition are
    already Base64-encoded. We skip the step of applying Base64 encoding in
    this case.
  current_authorized_view: The current authorized view definition. Only used
    in the update case to be included as part of the initial commented prompt.

Returns:
  an authorized view proto message with fields filled accordingly.

Raises:
  ChildProcessError if the user did not save the temporary file.
  ChildProcessError if there is a problem running the editor.
  ValueError if the user's response does not follow YAML or JSON format.
  ValueError if the YAML/JSON object cannot be parsed as a valid authorized
    View.
zEdit aborted by user.z1There was a problem applying your changes. [{0}].NzGProvided response is not a properly formatted YAML or JSON file. [{0}].zEProvided response cannot be parsed as a valid authorized view. [{0}].)r   GetAdminMessagesAuthorizedView%BuildCreateAuthorizedViewFileContents%BuildUpdateAuthorizedViewFileContentsr   
OnlineEditNoSaveExceptionChildProcessErrorEditorExceptionformatr	   load*Base64EncodingYamlAuthorizedViewDefinitionr   PyValueToMessageYAMLParseError
ValueErrorAttributeError)	r   r   current_authorized_viewauthorized_view_message_type	help_textcontenteauthorized_view_to_parseauthorized_views	            r   r   r   y   s"   4 "&!6!6!8!G!G57I5Iooi(G#yy101IJ//$O 
/ 
		 5
3
44			 
;BB1E  
		 
	&)  
 
O	 s;   B >C) 2C&C!!C&)E=DE$D>>Ec                    [         R                  " 5       R                  n [        R                  " U 5      nU(       d  [        U5        [        R                  " X#5      nU$ ! [        R                  [        R                  4 a  n[        R                  " SU5      eSnAf[         a  n[        SR                  X5      5      eSnAff = f)af  Create an authorized view proto message from a YAML or JSON formatted definition file.

Args:
  file_path: Path to the YAML or JSON definition file.
  pre_encoded: True if all binary fields in the authorized view definition are
    already Base64-encoded. We skip the step of applying Base64 encoding in
    this case.

Returns:
  an authorized view proto message with fields filled accordingly.

Raises:
  BadArgumentException if the file cannot be read.
  BadArgumentException if the file does not follow YAML or JSON format.
  ValueError if the YAML/JSON object cannot be parsed as a valid authorized
    view.
z--definition-fileNz>File [{0}] cannot be parsed as a valid authorized view. [{1}].)r   r   r   r	   	load_pathr&   r   r'   FileLoadErrorr(   calliope_exceptionsBadArgumentExceptionr*   r)   r$   )	file_pathr   r,   r0   r1   r/   s         r   r   r      s    $ "&!6!6!8!G!G#~~i801IJ//$O 
 

d11	2 K

2
23F
JJ	 
HOO	
 s#   >A! !$CBC)CCc                    U (       a  SU ;  a  U $ U S   nSU;   a/  UR                  S/ 5       Vs/ s H  n[        U5      PM     snUS'   SU;   a  US   R                  5        Hv  u  p4SU;   a/  UR                  S/ 5       Vs/ s H  n[        U5      PM     snUS'   SU;   a/  UR                  S/ 5       Vs/ s H  n[        U5      PM     snUS'   XAS   U'   Mx     U $ s  snf s  snf s  snf )z\Apply base64 encoding to all binary fields in the authorized view definition in YAML format.
subsetViewrowPrefixesfamilySubsets
qualifiersqualifierPrefixes)getUtf8ToBase64itemsyaml_authorized_viewyaml_subset_viewsfamily_namefamily_subsets        r   r&   r&         	5I!I),7&&!1!5!5mR!H'!HAQ!H']# ((&6&G&M&M&O"		&%2%6%6|R%H'
%HLO%H'
l# 
	-%2%6%67JB%O.
%OLO%O.
)* 8E'4 'P 
''
.
   CC$7C)c                    U (       a  SU ;  a  U $ U S   nSU;   a/  UR                  S/ 5       Vs/ s H  n[        U5      PM     snUS'   SU;   a  US   R                  5        Hv  u  p4SU;   a/  UR                  S/ 5       Vs/ s H  n[        U5      PM     snUS'   SU;   a/  UR                  S/ 5       Vs/ s H  n[        U5      PM     snUS'   XAS   U'   Mx     U $ s  snf s  snf s  snf )z\Apply base64 decoding to all binary fields in the authorized view definition in YAML format.r9   r:   r;   r<   r=   )r>   Base64ToUtf8r@   rA   s        r   *Base64DecodingYamlAuthorizedViewDefinitionrK      rG   rH   c                 ~    [         R                  " [        R                  " [         R                  " U 5      5      5      $ )z)Encode a utf-8 string as a base64 string.)sixensure_textbase64	b64encodeensure_binary)rD   s    r   r?   r?     s'    	))#*;*;A*>?	@@r   c                      [         R                  " [        R                  " U 5      5      $ ! [        [
        R                  4 a  n[        SR                  X5      5      eSnAff = f)z)Decode a base64 string as a utf-8 string.z_Error decoding base64 string [{0}] in the current authorized view definition into utf-8. [{1}].N)	rM   rN   rO   	b64decode	TypeErrorbinasciiErrorr)   r$   rD   errors     r   rJ   rJ     sV    ??6++A.//
X^^	$ 
	))/)9 s   ), A%A  A%c                 z   U b  U R                   c  gU R                   nUR                  b  UR                   H  n[        U5        M     UR                  bf  UR                  R                   HK  nUR
                  nUR                   H  n[        U5        M     UR                   H  n[        U5        M     MM     gg)z>Raises a ValueError if the view contains non-ascii characters.N)r9   r:   
CheckAsciir;   additionalPropertiesvaluer<   r=   )r1   subset_view
row_prefixadditional_propertyrF   	qualifierqualifier_prefixs          r   (CheckOnlyAsciiCharactersInAuthorizedViewrb     s     : : B
**+(!--
 . **88MM)//m$//)9 0+==
#$ >	  N +r   c                      U R                  S5        g! [         a  n[        SR                  X5      5      eSnAff = f)zCheck if a string is ascii.asciiztNon-ascii characters [{0}] found in the current authorized view definition, please use --pre-encoded instead. [{1}].N)decodeUnicodeErrorr)   r$   rW   s     r   rZ   rZ   .  s@    HHW	 
	@@Fq@P s    
=8=c                  :   [         R                  " 5       n [        R                  5        HN  nU R	                  S5        U(       a  U R	                  S5        U R	                  U5        U R	                  S5        MP     U R	                  S5        U R                  5       $ )zQBuilds the help text for creating an authorized view as the initial file content.# 
)ioStringIOCREATE_HELP
splitlineswritegetvalue)buflines     r   r   r   9  sh    
#$$&dIIcN	iinIIdOIIdO ' ))D/	r   c                 >   SnUR                   (       a&  [        UR                   UR                  5      Ul        OC[	        U R                  5       UR                  (       + 5      n[        SUR                  US9Ul        UR                  R                  b  [        SU5      nUR                  R                  b  [        SU5      nUR                  (       Ga  Uc  [	        U R                  5       SS9n[        R                  " U5      nUR                  R                  b  UR                  R                  Ul        UR                  R                  b  UR                  R                  Ul	        [        R                  " 5       n[        R                   " X4S9nUR#                  SUS	9  UR%                  5       (       a'  [&        R(                  " S
UR%                  5       -   SS9  O[&        R(                  " SSS9  SUR                  l        UR,                  (       a  SUl        U$ )zParse argument and construct update authorized view request.

Args:
  original_ref: the gcloud resource.
  args: input arguments.
  req: the real request to be sent to backend service.

Returns:
  The real request to be sent to backend service.
NF)r   r   r+   r]   deletion_protection)check_ascii)originalchangeddefault)outzLDifference between the current authorized view and the new authorized view:
T)messagecancel_on_noz5The authorized view will NOT change with this update.)r   r   r   r   GetCurrentAuthorizedViewRelativeNamer   r9   AddFieldToUpdateMaskdeletionProtectioninteractivecopydeepcopyrk   rl   r   ResourceDiffPrintrp   r
   PromptContinuer   ignore_warningsignoreWarnings)original_refr   r   r+   new_authorized_viewrq   differs          r   !ModifyUpdateAuthorizedViewRequestr   F  s    !	Hd..C 7!!#)9)9%9 ;$$ 7C 	"".
}c
2C**6
4c
:C	& 8

#
#
%5! --(?@
$$0'*'9'9'D'D$
,,8



/
/ , ++-C''(F LLL$
||~~$ LLN	
  I !#	C	*r   c                 4   [         R                  " 5       n[         R                  " 5       R                  U S9n UR                  R                  U5      nU(       a  [        U5        U$ ! [        R                   a  n[        R                  " U5      eSnAff = f)a  Get the current authorized view resource object given the authorized view name.

Args:
  authorized_view_name: The name of the authorized view.
  check_ascii: True if we should check to make sure that the returned
    authorized view contains only ascii characters.

Returns:
  The view resource object.

Raises:
  ValueError if check_ascii is true and the current authorized view definition
  contains invalid non-ascii characters.
)r   N)r   GetAdminClientr   =BigtableadminProjectsInstancesTablesAuthorizedViewsGetRequest)projects_instances_tables_authorizedViewsGetrb   api_exceptions	HttpErrorr   HttpException)authorized_view_nameru   clientrequestr1   rX   s         r   r|   r|     s      &!!#aa b '*FFJJO .?		!	! *

"
"5
))*s   .A( (B<BBc                    [         R                  " U 5      nU(       d  [        U5      nUS:X  a)  [        R                  " [
        R                  " USS95      $ US:X  a*  [        R                  " [        R                  " U5      5      $ g)z=Serializes a authorized view protobuf to either JSON or YAML.json   )indentr	   N)	r   MessageToDictrK   rM   	text_typer   dumpsr	   dump)r1   r   serialized_formatauthorized_view_dicts       r   SerializeToJsonOrYamlr     sq     "//@	E & ==$8CDD& ==#7899 !r   c                    [         R                  " 5       n[        R                  5        HN  nUR	                  S5        U(       a  UR	                  S5        UR	                  U5        UR	                  S5        MP     [        X5      nUR                  5        HN  nUR	                  S5        U(       a  UR	                  S5        UR	                  U5        UR	                  S5        MP     UR	                  S5        UR                  5       $ )a  Builds the help text for updating an existing authorized view.

Args:
  current_authorized_view: The current authorized view resource object.
  pre_encoded: When pre_encoded is False, user is passing utf-8 values for
    binary fields in the authorized view definition and expecting us to apply
    base64 encoding. Thus, we should also display utf-8 values in the help
    text, which requires base64 decoding the binary fields in the
    `current_authorized_view`.

Returns:
  A string containing the help text for update authorized view.
rh   ri   rj   )rk   rl   UPDATE_HELPrn   ro   r   rp   )r+   r   rq   rr   #serialized_original_authorized_views        r   r   r     s     	#$$&dIIcN	iinIIdOIIdO ' )>)% 2<<>dIIcN	iinIIdOIIdO ? ))D/	r   c                     UR                   nU(       a$  UR                  U 5      S:X  a  US-   U -   Ul         U$ Xl         U$ )zAdding a new field to the update mask of the UpdateAuthorizedViewRequest.

Args:
  field: the field to be updated.
  req: the original UpdateAuthorizedViewRequest.

Returns:
  req: the UpdateAuthorizedViewRequest with update mask refreshed.
r   ,)
updateMaskcount)fieldr   update_masks      r   r~   r~     sG     +1$"S(50cn 
* N	*r   )N)r   )/__doc__
__future__r   r   r   rO   rU   r   rk   r   textwrapapitools.base.pyr   r   r   googlecloudsdk.api_lib.bigtabler   googlecloudsdk.api_lib.utilgooglecloudsdk.callioper5   googlecloudsdk.corer	   googlecloudsdk.core.consoler
   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   rM   dedentrm   r   r   r   r   r&   rK   r?   rJ   rb   rZ   r   r   r|   r   r   r~    r   r   <module>r      s     , &  '    	   % 9 0 2 E $ 2 6 ) 
oo  . oo  2D 59:z"J00A
%&
M`*> 5;: Fr   