
    *                        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 g dZdZddgZddddZd Z ej6                  ej8                  j:                  ej8                  j<                  ej8                  j>                        ej@                   G d dejB                                      Z"ee"_#        y)z;Command for reporting a virtual machine instance as faulty.    )absolute_import)division)unicode_literals)base_classes)poller)waiter)arg_parsers)base)
exceptions)flags)	arg_utils)log)BEHAVIOR_UNSPECIFIEDPERFORMANCESILENT_DATA_CORRUPTIONUNRECOVERABLE_GPU_ERROR	IMMEDIATEFUTURE4Report a host as faulty to start the repair process.zh          *{command}* is used to report a host as faulty to start the repair
          process.
        z        To report a host as faulty for an instance named ``test-instance'', run:

          $ {command} test-instance --fault-reasons=behavior=SILENT_DATA_CORRUPTION,description="affecting our ML job"           --disruption-schedule=IMMEDIATE         )briefDESCRIPTIONEXAMPLESc                     | j                         }|t        vr%t        j                  ddj	                  |             |S )z6Validates behavior field, throws exception if invalid.behaviorzInvalid value {} for behavior)upperFAULT_REASONS_CHOICESr   InvalidArgumentExceptionformat)behavior_inputr   s     6lib/surface/compute/instances/report_host_as_faulty.py_ValidateFaultReasonsBehaviorr!   :   sD    !!#(**

-
-3::>J  
/    c                   T    e Zd ZdZed        Zed        Zed        Zd Zd	dZ	d Z
y)
ReportHostAsFaultyr   c                     t         j                  j                  |        t        j	                  |        t        j                  |        t        j                  j                  |        y N)	r   INSTANCE_ARGAddArgumentr$   _AddDisruptionSchedule_AddFaultReasonsr
   
ASYNC_FLAGAddToParserparsers    r    ArgszReportHostAsFaulty.ArgsK   sD    	""6*--f5''/OO'r"   c           	      |    | j                  ddt        t        j                  dj	                  t
                     y )Nz--disruption-scheduleTz        Specifies the timing for initiating the fault reporting process.
        The default value is {default_choice} which initiates the process right away.
        )default_choice)requiredchoicestypehelp)add_argumentDISRUPTION_SCHEDULE_CHOICESr   ChoiceToEnumNamer   "DISRUPTION_SCHEDULE_DEFAULT_CHOICEr-   s    r    r)   z)ReportHostAsFaulty._AddDisruptionScheduleR   s;    
+'' F"DFE  	r"   c           	          | j                  dt        j                  dt        t        ddg      dddj                  t        	      
       y )Nz--fault-reasons   )descriptionr   r   )
min_lengthspecrequired_keysTappendz        Specified and can include one or more of the following types:
        {choices}.
        This helps categorize the nature of the fault being reported.
        )r3   )r4   r2   actionr5   )r6   r	   ArgDictstrr!   r   r   r-   s    r    r*   z#ReportHostAsFaulty._AddFaultReasons_   sW    
  "9 &,
  F0F1!  r"   c           
      L   |j                   D cg c]o  }|j                  j                  t        j                  |j                  d      |j                  j                  j                        |j                  d            q }}t        j                  |j                  |j                  j                  j                        }|j                  j                  |j                         |j                  |j                  |j                  j                  ||            }|S c c}w )Nr   r<   )r   r<   )disruptionSchedulefaultReasons)instanceprojectzone"instancesReportHostAsFaultyRequest)fault_reasonsmessages-InstancesReportHostAsFaultyRequestFaultReasonr   ChoiceToEnumgetBehaviorValueValuesEnumdisruption_schedule"InstancesReportHostAsFaultyRequest!DisruptionScheduleValueValuesEnum)ComputeInstancesReportHostAsFaultyRequestNamerH   rI   )selfargsinstance_refclientreasonrK   disruption_schedule_enumrequests           r    _BuildRequestz ReportHostAsFaulty._BuildRequestt   s    ((	 )F 	EE++

:&MMee 

=1 	F 	
 )  	  )55  ::\\ 
 ooGG""$$$+1??+]+]7& ,^ ,
	 H G N3	s   A4D!c           
         t        |t              s|g}|D cg c],  }|j                  j                  d| j	                  |||      f. }}g }|j                  ||      }	|rt        j                  |      |	D 
cg c]'  }
|j                  j                  |
j                        ) }}
|rd|D ]>  }t        j                  j                  dj                  |j                                      @ t        j                  j                  d       |	S t!        j"                  ||j                  j                  |      }t%        j&                  |t!        j(                  |      dj                  dj+                  d |D                    d       }|D ]>  }t        j                  j                  dj                  |j-                                      @ |S c c}w c c}
w )	Nr$   zRThe report host as faulty operation is currently in progress. Operation URI: [{}].z]Use [gcloud compute operations describe URI] command to check the status of the operation(s).z9Reporting host as faulty for instance(s) {0} in progress.z, c              3   <   K   | ]  }|j                           y wr&   )rU   ).0is     r    	<genexpr>z*ReportHostAsFaulty._Run.<locals>.<genexpr>   s     8-Q-s   )max_wait_msz[Successfully reported host as faulty for instance: [{0}]. The operation has been completed.)
isinstancelistapitools_client	instancesr]   AsyncRequestscore_exceptions
MultiError	resourcesParseselfLinkr   statusPrintr   SelfLinkr   BatchPollerr   WaitForOperationBatchjoinrU   )rV   rW   instance_refsrY   holderis_asyncrX   requestserrors_to_collect	responsesroperation_refsoperation_refoperation_pollerresults                  r    _RunzReportHostAsFaulty._Run   s   mT*$om * *L	 "",, t\6:	

 *   $$I &&'899BKL)Qf&&,,QZZ8)NL)-

$$*F&&(%	
 * 
jj( ++
&((22M ~~




/
E
L
Lii8-88 f (,

1178I8I8K1L	
 ( mi" Ms   1G:,Gc                    t        j                  | j                               }|j                  }t        j
                  j                  ||j                  t	        j                  |            }| j                  |||||j                        S )N)scope_lister)rw   )r   ComputeApiHolderReleaseTrackrY   r   r'   ResolveAsResourcerk   GetInstanceZoneScopeListerr   async_)rV   rW   rv   rY   ru   s        r    RunzReportHostAsFaulty.Run   sx    **4+<+<+>?F]]F&&8855f= 9 M 99T=&&4;;9OOr"   N)F)__name__
__module____qualname____doc__staticmethodr/   r)   r*   r]   r   r    r"   r    r$   r$   D   sR    
 =( ( 
 
  (88t
Pr"   r$   N)$r   
__future__r   r   r   googlecloudsdk.api_lib.computer   )googlecloudsdk.api_lib.compute.operationsr   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper	   r
   r   ,googlecloudsdk.command_lib.compute.instancesr   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.coreri   r   r   r9   r7   DETAILED_HELPr!   ReleaseTracksr   ALPHABETAGAUniverseCompatibleSilentCommandr$   detailed_helpr   r"   r    <module>r      s    B &  ' 7 < . / ( . > : = #  &1 "*H5  D T..33T5F5F5I5I LP++ LP LP^ $1   r"   