
                         v    S 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S	KJr  S
 rS rS rg)z%Utility functions for create command.    )absolute_import)division)unicode_literals)utils)
exceptions)
propertiesN)rangec                    / n0 nSnU R                  USSU0S9nUR                  n	U R                  USU	US.S9n
U
R                  U	:w  a  U
R                  n	U
R                  nX:X  a.  [        R                  R
                  R                  R                  n	U H#  n[        XXyXdUU5      nUR                  U5        M%     U$ )a>  Parse disks arguments taking into account project, region and zones.

Try to deduce --region from --replica-zones and parse disk references:
0. parse --project
1. parse --region falling back to 0 for project
2. for each disk:
 2.1. parse disk falling back to 0 and 1 falling back to property if necessary
 2.2. extract disk project from 2.1
 2.3. parse --replica-zones falling back to 2.2
 2.4. check zones are in disk project
 2.5. check zones are from the same region
 2.6. if --region is present, check if equal to 2.5
 2.7. parse disk falling back to 2.2 and 2.5
 2.8. check if disk is in 2.5 region
 2.9. yield 2.7

Function is greedy - checks/deduces/parses all data before returning. If any
error occurs, exception is raised.

Args:
  resources: resources.Registry: resource parser
  disks: str, parsed disks argument (args.DISK_NAME)
  replica_zones: str, parsed --replica-zones flag (args.replica_zones)
  project: str, parsed --project flag or None (args.project)
  region: str, parsed --region flag or None (args.region)

Returns:
  List disk resources [compute.regionDisks]
z$SAMPLE$zcompute.projectsproject
collectionparamszcompute.regions)r   region)	Parser   r   r   VALUEScore	GetOrFail
_ParseDiskappend)	resourcesdisksreplica_zonesr   r   result_disksproject_to_regionsampleproject_resproject_name
region_resregion_namediskresult_disks                 6lib/googlecloudsdk/command_lib/compute/disks/create.pyParseRegionDisksResourcesr#      s    > ,& ,i5H   J+$$, "%   !*
 <'%%L!!+$$))11;;LdYf.*,K $	  
    c           	      .   U R                  UUUS.SS9nUR                  n	X;  a  [        X	XUXg5      XI'   U R                  USXI   U	S.S9n
U
R                  XI   :w  a7  [        R
                  " SSR                  U
R                  5       XI   5      5      eU
$ )zParse single disk reference.)r   r   zcompute.regionDisks)r   r   r   --replica-zoneszFRegion from [DISK_NAME] ({}) is different from [--replica-zones] ({}).)r   r   _DeduceRegionInProjectr   r   InvalidArgumentExceptionformatSelfLink)r   r    r   r   r   r   r   r   disk_resourcecurrent_projectr!   s              r"   r   r   Z   s     //
! ' " (- "))/-)?M6*$&
 
&%6$   	+ ,==

-
-.?	,,.):<= = 
r$   c                    U Vs/ s H  nU R                  USSU0S9PM     nnU H~  nUR                  U:w  d  M  [        R                  " SSR	                  [
        R                  " UR                  5       5      [
        R                  " UR                  5       5      5      5      e   [        [        U5      S-
  5       H  n	[        R                  " X   R                  5      [        R                  " XS-      R                  5      :w  d  MN  [        R                  " SSR	                  X   R                  XS-      R                  [        R                  " X   R                  5      [        R                  " XS-      R                  5      5      5      e   [        R                  " US	   R                  5      n
Ub0  XS:w  a+  XZ:w  a&  [        R                  " SS
R	                  X5      5      eU
$ s  snf )z*Deduce region from zones in given project.zcompute.zonesr   r   z--zonez4Zone [{}] lives in different project than disk [{}].   r&   zOZones [{}, {}] live in different regions [{}, {}], but should live in the same.r   zERegion from [--replica-zones] ({}) is different from [--region] ({}).)r   r   r   r(   r)   six	text_typer*   r	   lenr   ZoneNameToRegionNamezone)r   r,   r+   r   r   r   r   r3   current_zonesiresults              r"   r'   r'   |   s       $ oo
?I3O  Q   d||&//

@
G
GmmDMMO,mmM224578 8  ]#a'(a""=#3#8#89""=Q#7#<#<=	>//0A%v##]q5%9%>%>(()9)>)>?((1u)=)B)BC EF F ) %%mA&6&;&;<&K1k6K

-
-.?	,. . 
-;s   G.)__doc__
__future__r   r   r   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   googlecloudsdk.corer   r/   	six.movesr	   r#   r   r'    r$   r"   <module>r>      s3    , &  ' 0 . * 
 ;|D!r$   