
    j                         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
ZdZdZdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)z$Resource Settings command utilities.    )absolute_import)division)unicode_literalsN)encoding)
exceptions)yaml)filesz	settings/organizationfolderprojectc                 R   t        j                  |       }	 t        j                  |      }t	        j
                  |      }	 t        j                  ||      S # t        j                  $ r |}Y .w xY w# t        $ r*}t        j                  dj                  | |            d}~ww xY w)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!!'844	 
		 H
 
 

&
&'G'N'N!(  s)   *A A3 A0/A03	B&<%B!!B&c                 ~    | j                   j                  t              r| j                   S t        | j                   z   S )a^  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   s5     
!!/2	4,,	,,    c                     | j                   j                  t              r| j                   t        t              d S | j                   S )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S1233			r&   c                 *    | j                  d      d   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                     | j                   xs | j                  xs | j                  }| j                   rt        }n| j                  rt        }nt
        }dj                  |dz   |      S )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                 v    | j                  d      d   }| j                  d      d   }dj                  ||      S )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?   }   s;     --$Q'-c"1%+	{	33r&   c                     | j                  d      rt        }|S | j                  d      rt        }|S | j                  d      rt        }|S d}|S )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      sg     () M 
 *%M 
 +&M 
 M	r&   c                 *    | j                  d      d   S )r+   r,   r>   r.   r0   s    r   GetResourceIdFromStringrD      r3   r&   c                 R    t        |       }t        |       }dj                  ||      S )a6  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                 R    t        |       }t        |       }dj                  ||      S )a?  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,('-,	"	)	)(L	AAr&   c                 t    t        |       dk(  ry| j                  d      }t        |      dk7  ry|d   dk7  ryy)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&   