
    <                     L   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	mZ
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddddZd Zd Zej<                   ej>                  ej@                  jB                         G d dejD                                      Z# ej>                  ej@                  jH                         G d de#             Z% ej>                  ej@                  jL                         G d de#             Z'ee#_(        y)zCommand for creating instances.    )absolute_import)division)unicode_literals)base_classes)filter_rewrite)utils)base)
exceptions)scope)flags)util)log)
propertieszD
          Create multiple Compute Engine virtual machines.
        a  
        *{command}* facilitates the creation of multiple Compute Engine
        virtual machines with a single command. They offer a number of advantages
        compared to the single instance creation command. This includes the
        ability to automatically pick a zone in which to create instances based
        on resource availability, the ability to specify that the request be
        atomic or best-effort, and a faster rate of instance creation.
        a  
        To create instances called 'example-instance-1', 'example-instance-2',
        and 'example-instance-3' in the 'us-central1-a' zone, run:

          $ {command} --predefined-names=example-instance-1,example-instance-2,example-instance-3 --zone=us-central1-a
        )briefDESCRIPTIONEXAMPLESc                 *   g }t        j                         j                  d|z         \  }}|j                  }	g }
|t        j
                  j                  k(  rt        j                  |j                        }|j                  |	j                  d||      g      }t        |      dk7  r7|j                  t        j                  dj!                  |                   d|fS |
|d   j"                  D cg c]  }|	j%                  |      j&                   c}z  }
n|
|gz  }
| j)                  |
D cg c]G  }| j*                  j,                  d	 | j*                  j,                  j/                  d	      |||
      fI c}|ddd      }||fS c c}w c c}w )z9Requests operations with group id matching the given one.zoperationGroupId=)
expressionzcompute.regions)
collectionprojectregion   zRegion count is not 1: {}Nr   List)filterr   zoneFT)errors_to_collect
log_resultalways_return_operationno_followup)r   RewriterRewrite	resourcescompute_scopes	ScopeEnumREGIONregion_utilsRegionResourceFetcherclient
GetRegionsCreatelenappendr
   ToolExceptionformatzonesParser   MakeRequestsapitools_clientzoneOperationsGetRequestType)compute_clientr   operation_group_idholderlocationr   r   _operation_filterresource_parserr/   region_fetcherregionsr   operations_responses                  ,lib/surface/compute/instances/bulk/create.py_GetOperationsr@   7   s   
 &//199$'99 : ! $$/
%
n&&---!77FN''('( 	 	
) G
 7|q

"
"#>#E#Eh#O
P $$$	71:;K;KL;K4o##D)..;KLLE	hZE&33 	 d ,,;;n,,;;JJ'tE 	 *" 4 " 
/	//+ M
	s   2"F/AFc                    t        | |j                  ||||      \  }}|rt        j                  |d       |g d}|D 	cg c]7  }	|	j                  dk(  r&t        |	j                        dk(  r|	j                  |	9 }
}	t        |
      }|j                  j                  |z
  }d }|
D 	cg c]
  }	 ||	       }}	||d<   ||d<   ||d	<   |S c c}	w c c}	w )
z?Requests operations with group id and parses them as an output.zCould not fetch resource:)error_message)operationGroupId	instancesinsertDONEc                     | j                   | j                  j                  d      d   | j                  | j                  dS )N/)idnamer   selfLink)targetId
targetLinksplitr   )ops    r?   GetInstanceStatusz%_GetResult.<locals>.GetInstanceStatus   s8    kk##C(,MM	     createdInstanceCountfailedInstanceCountrD   )r@   r   r   RaiseToolExceptionoperationTypestrstatuserrorr+   bulkInsertInstanceResourcecount)r5   requestr6   r7   r8   r   r>   errorsresultrP   
successfulnum_successfulnum_unsuccessfulrQ   instances_statuss                  r?   
_GetResultrc   h   s	   
 !/oo!v 	V3NO 2D& $#"			X	%
bii.F
"
((
	 	#   z?.77==N 7AAj'+jA#1&	 "2&	(&	-1$ Bs    <C)Cc                       e 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dZed        Zed        Zd Zd Zd Zd	 Zy
)r*   0Create Compute Engine virtual machine instances.FTc                    t        j                  |t        j                  j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                         | j!                  |       t#        j$                  |       t#        j&                  |       y )N)support_display_devicesupport_numa_node_countsupport_snp_svsmsupport_max_count_per_zonesupport_custom_hostnames support_specific_then_x_affinitysupport_watchdog_timersupport_flex_startsupport_source_snapshot_regionsupport_skip_guest_os_shutdown"support_preemption_notice_duration)
bulk_flagsAddCommonBulkInsertArgsr	   ReleaseTrackGA_support_display_device_support_numa_node_count_support_snp_svsm_support_max_count_per_zone_support_custom_hostnames!_support_specific_then_x_affinity_support_watchdog_timer_support_flex_start_support_source_snapshot_region_support_skip_guest_os_shutdown#_support_preemption_notice_durationAddSourceInstanceTemplateinstances_flagsAddLocalSsdRecoveryTimeoutArgsAddHostErrorTimeoutSecondsArgsclsparsers     r?   ArgszCreate.Args   s    &&":: # < <..#&#B#B!$!>!>),)N)N"::22'*'J'J'*'J'J+.+R+R !!&) 226:226:rR   c                 l    t        j                         | _        | j                  j                  |       y )N)rr   !MakeBulkSourceInstanceTemplateArgSOURCE_INSTANCE_TEMPLATEAddArgumentr   s     r?   r   z Create.AddSourceInstanceTemplate   s.     	446     ,,V4rR   c                      y)Nzcompute.instances )selfs    r?   
CollectionzCreate.Collection   s    rR   c                    t        j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        }t        j                  |||||||| j                  |	      }	|t         j"                  j$                  k(  r8|j&                  j(                  }
|j*                  j-                  |	||      }|
|fS |t         j"                  j.                  k(  r4|j&                  j0                  }
|j*                  j3                  |	||      }
fS )N)rZ   r   r   )rZ   r   r   )	bulk_utilSupportedFeaturesrv   _support_secure_tagsrw   rx   ry   rz   r{   r|   _support_graceful_shutdownr~   r   r    CreateBulkInsertInstanceResourcer   r#   r$   ZONEr2   rD   messages!ComputeInstancesBulkInsertRequestr%   regionInstances'ComputeRegionInstancesBulkInsertRequest)r   argsr7   r5   r;   r   r8   r   supported_featuresbulk_instance_resourceinstance_servicerequest_messages               r?   _CreateRequestszCreate._CreateRequests   so    #44$$!!%%((&&..$$'',,,,00 'GG%%
 ((---'77AA

!
!
C
C)? D  " _,, 
.**11	1'77GG

!
!
I
I)? J   _,,rR   c           	      @   t        j                  || j                  | j                         t	        j
                  | j                               }|j                  }|j                  }t        j                  j                  j                  j                         }d}d}|j                  d      r'|j                  }t         j"                  j$                  }n7|j                  d      r&|j&                  }t         j"                  j(                  }| j+                  |||||||      \  }}	g | _        d| _        d| _        |j2                  r8d| _        	 |j5                  |	      }
|
j6                  | _        d|
j:                  iS |jA                  |d|	fgddd	      }
g | _        |
r`|
d
   j:                  }tC        ||	||||      }|jE                  d      .|jE                  d      djG                  |d   |d         | _        |S y# t<        j>                  $ r}|d}~ww xY w)zRuns bulk create command.

    Args:
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the .Args() method.

    Returns:
      A resource object dispatched by display.Displayer().
    )rj   rk   Nr   r   FTrC   
BulkInsert)r   r   r   r   rS   rT   z%VM instances created: {}, failed: {}.)$rr   ValidateBulkInsertArgsry   rz   r   ComputeApiHolderrt   r(   r"   r   VALUEScorer   	GetOrFailIsSpecifiedr   r#   r$   r   r   r%   r   _errors
_log_async_status_messageasync_r   rL   _operation_selflinkrC   r
   HttpExceptionr1   rc   getr.   )r   r   r7   r5   r;   r   r8   r   instances_servicer\   responserY   r6   r^   s                 r?   Runz
Create.Run  s    %%#'#C#C!%!?!? **4+<+<+>?F]]N&&O$$,,668GHEh&&++e			(	#h&&--e!%!5!5fnow%"w DLDOD{{do$//8#+#4#4 "H$=$=>> **
\7	34 $	 + H DL#A;77
'#5vxf **+
,
8jj./;FMM)*F3H,I 
 m
1 %% s   /H HHHc                    ~| j                   r*| j                   D ]  }t        j                  |d           y | j                  r9t        j                  j                  dj                  | j                               y | j                   r)| j                   D ]  }t        j                  |d           t        j                  j                  dj                  | j                               y )Nr   z&Bulk instance creation in progress: {}z6Bulk create request finished with status message: [{}])
r   r   rY   r   rX   Printr.   r   warningr   )r   resources_were_displayedrY   s      r?   EpilogzCreate.EpilogL  s     ||<<%		%(  		jj
2
9
9&& 
\\E
++eAh
 "	jj
B
I
I""rR   N)__name__
__module____qualname____doc__rv   r   rw   rx   ry   rz   r{   r|   r   r}   r~   r   r   r   classmethodr   r   r   r   r   r   r   rR   r?   r*   r*      s     9!" $#&+#!#$)!$(!(-%*; ;. 5 57-rENrR   r*   c                   X    e 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dZed        Zy)
CreateBetare   TFc                    t        j                  |t        j                  j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                   | j"                         | j%                  |       t'        j(                  |       t'        j*                  |       y )N)rg   rh   ri   rj   rk   rl   rm   support_graceful_shutdownrn   support_igmp_queryro   rp   rq   )rr   rs   r	   rt   BETArv   rw   rx   ry   rz   r{   r|   r   r}   _support_igmp_queryr~   r   r   r   r   r   r   r   s     r?   r   zCreateBeta.Argsu  s    &&":: # < <..#&#B#B!$!>!>),)N)N"::"%"@"@2222'*'J'J'*'J'J+.+R+R" !!&) 226:226:rR   N)r   r   r   r   rv   r   rw   rx   ry   rz   r{   r|   r   r}   r   r~   r   r   r   r   r   rR   r?   r   r   b  sk    8 " $"&*#!#$)!$(!(-%; ;rR   r   c                   X    e 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dZed        Zy)CreateAlphare   TFc                 f   t        j                  |t        j                  j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                   | j"                         | j%                  |       t'        j(                  |       t'        j*                  |       t'        j,                         j/                  |       t'        j0                  |       y )N)rg   rh   ri   rj   rk   rl   rm   r   r   rn   ro   rp   rq   )rr   rs   r	   rt   ALPHArv   rw   rx   ry   rz   r{   r|   r   r   r}   r~   r   r   r   r   AddSecureTagsArgsr   AddMaintenanceIntervalAddToParserr   r   s     r?   r   zCreateAlpha.Args  s    &&":: # < <..#&#B#B!$!>!>),)N)N"::22"%"@"@22'*'J'J'*'J'J+.+R+R$ !!&) %%f-226:**,88@226:rR   N)r   r   r   r   rv   r   rw   rx   ry   rz   r{   r|   r   r   r}   r~   r   r   r   r   r   rR   r?   r   r     sm    8 !! $"&*# #$(!$(!(,%; ;rR   r   N))r   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   r   &googlecloudsdk.api_lib.compute.regionsr&   googlecloudsdk.callioper	   r
   "googlecloudsdk.command_lib.computer   r#   ,googlecloudsdk.command_lib.compute.instancesr   r   1googlecloudsdk.command_lib.compute.instances.bulkrr   r   r   googlecloudsdk.corer   r   DETAILED_HELPr@   rc   UniverseCompatibleReleaseTracksrt   ru   Commandr*   r   r   r   r   detailed_helpr   rR   r?   <module>r      s   & &  ' 7 9 0 H ( . F Q Q O # **.0b(V D%%(()JT\\ J * JZ D%%**+); ); ,);X D%%++,-;& -; --;d % rR   