
    j                         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
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S rg)z$Resource Settings command utilities.    )absolute_import)division)unicode_literalsN)encoding)
exceptions)yaml)filesz	settings/organizationfolderprojectc                 \   [         R                  " U 5      n [        R                  " U5      n[        R
                  " U5      n [        R                  " X5      $ ! [        R                   a    Un N/f = f! [         a*  n[        R                  " SR                  X5      5      eSnAff = f)zReturns a message populated from the JSON or YAML file.

Args:
  filepath: str, A local path to an object specification in JSON or YAML
    format.
  message: messages.Message, The message class to populate from the file.
zUnable to parse file [{}]: {}.N)r	   ReadFileContentsr   loadjsondumpsYAMLParseErrorr   JsonToMessage	Exceptionr   InvalidInputErrorformat)filepathmessagefile_contentsyaml_objjson_stres         Alib/googlecloudsdk/command_lib/resource_manager/settings/utils.pyGetMessageFromFiler   "   s     ((2-yy'Hzz(#H!!'44	 
		 H
 
 

&
&'G'N'N(  s)   ,A A7 A43A47
B+%B&&B+c                     U R                   R                  [        5      (       a  U R                   $ [        U R                   -   $ )aP  Returns the setting from the user-specified arguments.

A setting has the following syntax: settings/{setting_name}.

This handles both cases in which the user specifies and does not specify the
constraint prefix.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
)setting_name
startswithSETTINGS_PREFIXargss    r   GetSettingFromArgsr%   9   s8     
!!/22	4,,	,,    c                     U R                   R                  [        5      (       a  U R                   [        [        5      S $ U R                   $ )a  Returns the setting name from the user-specified arguments.

This handles both cases in which the user specifies and does not specify the
setting prefix.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
N)r    r!   r"   lenr#   s    r   GetSettingNameFromArgsr)   K   s@     
!!/22S1233			r&   c                 *    U R                  S5      S   $ )Returns the resource id from the setting path.

A setting path should start with following syntax:
[organizations|folders|projects]/{resource_id}/settings/{setting_name}

Args:
  setting: A String that contains the setting path
/   splitsettings    r   GetSettingNameFromStringr2   [        
s	A	r&   c                     U R                   =(       d    U R                  =(       d    U R                  nU R                   (       a  [        nOU R                  (       a  [        nO[
        nSR                  US-   U5      $ )a  Returns the resource from the user-specified arguments.

A resource has the following syntax:
[organizations|folders|projects]/{resource_id}.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
{}/{}s)r
   r   r   ORGANIZATIONFOLDERPROJECTr   )r$   resource_idresource_types      r   GetParentResourceFromArgsr<   g   sV     !!@T[[@DLL+	 M{{MM	+[	99r&   c                 t    U R                  S5      S   nU R                  S5      S   nSR                  X5      $ )zReturns the resource from the user-specified arguments.

A setting path should start with following syntax:
[organizations|folders|projects]/{resource_id}/settings/{setting_name}/value

Args:
  setting: A String that contains the setting path
r,   r      r5   )r/   r   )r1   r;   r:   s      r   GetParentResourceFromStringr?   }   s9     --$Q'-c"1%+		33r&   c                     U R                  S5      (       a  [        nU$ U R                  S5      (       a  [        nU$ U R                  S5      (       a  [        nU$ SnU$ )zReturns the resource type from the setting path.

A setting path should start with following syntax:
[organizations|folders|projects]/{resource_id}/settings/{setting_name}

Args:
  setting: A String that contains the setting path
zorganizations/zfolders/z	projects/invalid)r!   r7   r8   r9   )r1   r;   s     r   GetResourceTypeFromStringrB      sp     ()) M 
 *%%M 
 +&&M 
 M	r&   c                 *    U R                  S5      S   $ )r+   r,   r>   r.   r0   s    r   GetResourceIdFromStringrD      r3   r&   c                 P    [        U 5      n[        U 5      nSR                  X5      $ )a*  Returns the settings path from the user-specified arguments.

A settings path has the following syntax:
[organizations|folders|projects]/{resource_id}/settings/{setting_name}.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
z{}/settings/{}r<   r)   r   r$   resourcer    s      r   GetSettingsPathFromArgsrI      s)     't,('-,		 	 	88r&   c                 P    [        U 5      n[        U 5      nSR                  X5      $ )a3  Returns the settings path from the user-specified arguments.

A settings path has the following syntax:
[organizations|folders|projects]/{resource_id}/effectiveSettings/{setting_name}.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
z{}/effectiveSettings/{}rF   rG   s      r    GetEffectiveSettingsPathFromArgsrK      s)     't,('-,	"	)	)(	AAr&   c                 z    [        U 5      S:X  a  gU R                  S5      n[        U5      S:w  a  gUS   S:w  a  gg)r+   rA   Fr,         settingsT)rB   r/   r(   )r1   setting_lists     r   ValidateSettingPathrQ      sD     w'94s#,!A*$	r&   )__doc__
__future__r   r   r   r   apitools.base.pyr   4googlecloudsdk.command_lib.resource_manager.settingsr   googlecloudsdk.corer   googlecloudsdk.core.utilr	   r"   r7   r8   r9   r   r%   r)   r2   r<   r?   rB   rD   rI   rK   rQ    r&   r   <module>rY      sn    + &  '  % K $ *	
.-$ 	:,4 ,	9 B r&   