
    R                         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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 d      Z G d d      Zy)z4Functions to add flags in rollout sequence commands.    )absolute_import)division)unicode_literalsN)List)messages)util)arg_parsers)base)parser_arguments)parser_extensions)	resources)yaml)gkehub_v1alpha_messagesc                       e Zd ZdZej
                  j                  fdej                  dej
                  fdZ	e
d        Ze
d        Zd Zd Zd	 ZddZd Zy
)RolloutSequenceFlagsz7Add flags to the fleet rolloutsequence command surface.parserrelease_trackc                      || _         || _        y N)_parser_release_track)selfr   r   s      Hlib/googlecloudsdk/command_lib/container/fleet/rolloutsequences/flags.py__init__zRolloutSequenceFlags.__init__&   s    
 DL'D    c                     | j                   S r   )r   r   s    r   r   zRolloutSequenceFlags.parser.   s    <<r   c                     | j                   S r   )r   r   s    r   r   z"RolloutSequenceFlags.release_track2   s    r   c                 V    t         j                  j                  | j                         y r   )r
   
ASYNC_FLAGAddToParserr   r   s    r   AddAsynczRolloutSequenceFlags.AddAsync6   s    OO,r   c                 n    | j                   j                  dt        t        j                  d             y )Nz--display-namez            Display name of the rollout sequence to be created (optional). 4-30
            characters, alphanumeric and [ '"!-] only.
        )typehelp)r   add_argumentstrtextwrapdedentr   s    r   AddDisplayNamez#RolloutSequenceFlags.AddDisplayName9   s0    KK__    r   c                 f    | j                   j                  dddt        j                                y )N--labelsz Labels for the rollout sequence.z	KEY=VALUE)r%   metavarr$   )r   r&   r	   ArgDictr   s    r   	AddLabelszRolloutSequenceFlags.AddLabelsC   s.    KK/  "	  r   Nc                 f    | j                   j                  dt        j                         dd       y )N--stage-configTav              Path to the YAML file containing the stage configurations. The YAML
            file should contain a list of stages. Fleet projects and soak_duration are required.
            If label_selector is not specified, there is no filtering. Example:

            ```yaml
            - stage:
              fleet-projects:
              # Expected format: projects/{project}
              - projects/12345678
              - projects/87654321
              soak-duration: 1h
              label-selector: key=value
            - stage:
              fleet-projects:
              - projects/11111111
            ```
        )r$   requiredr%   )r   r&   r	   FileContentsr   s    r   AddStageConfigz#RolloutSequenceFlags.AddStageConfigK   s0    KK%%'	  r   c                 |    t        j                  | j                  t        j                  | j
                            y )N)r   api_version)fleet_resourcesAddRolloutSequenceResourceArgr   r   VERSION_MAPr   r   s    r   r8   z2RolloutSequenceFlags.AddRolloutSequenceResourceArgd   s+    11{{$$T%7%78r   )returnN)__name__
__module____qualname____doc__r
   ReleaseTrackALPHAr   ArgumentInterceptorr   propertyr   r   r"   r*   r/   r4   r8    r   r   r   r   #   sz    ?
 *.):):)@)@(22( &&(    -2r   r   c                   6   e Zd ZdZdej
                  dej                  fdZde	j                  defdZde	j                  fdZdej                  fd	Zdefd
Zdej                  j$                  fdZdeej*                     fdZdefdZdefdZdefdZy)RolloutSequenceFlagParserz9Parse flags during fleet rolloutsequence command runtime.argsr   c                 T    || _         || _        t        j                  |      | _        y r   )rF   r   r   GetMessagesModuler   )r   rF   r   s      r   r   z"RolloutSequenceFlagParser.__init__n   s%     DI&D**=9DMr   messager:   c                 (    | t        |             k(  S )zDetermines if a message is empty.

    Args:
      message: A message to check the emptiness.

    Returns:
      A bool indictating if the message is equivalent to a newly initialized
      empty message instance.
    )r$   r   rI   s     r   IsEmptyz!RolloutSequenceFlagParser.IsEmptyu   s     md7mo%%r   c                 *    | j                  |      s|S y)z/Trim empty messages to avoid cluttered request.N)rL   rK   s     r   	TrimEmptyz#RolloutSequenceFlagParser.TrimEmpty   s    << nr   c                     t        j                         }t        j                  | j                        |_        | j                         |_        | j                         |_	        | j                         |_        |S r   )fleet_messagesRolloutSequencer   RolloutSequenceNamerF   name_DisplayNamedisplayName_Labelslabels_Stagesstages)r   rollout_sequences     r   rQ   z)RolloutSequenceFlagParser.RolloutSequence   s_    %557 44TYY?#'#4#4#6 "lln"llnr   c                 .    | j                   j                  S r   )rF   display_namer   s    r   rT   z&RolloutSequenceFlagParser._DisplayName   s    99!!!r   c                 `   d| j                   j                         vry| j                   j                  }t        j                  j                         }|j                         D ]I  \  }}|j                  j                  t        j                  j
                  j                  ||             K |S )zParses --labels.r,   N)keyvalue)
rF   GetSpecifiedArgsrW   rP   rQ   LabelsValueitemsadditionalPropertiesappendAdditionalProperty)r   rW   labels_valuer^   r_   s        r   rV   z!RolloutSequenceFlagParser._Labels   s    3355YYF!11==?Llln
U''..

(
(
4
4
G
GU H  % r   c                    d| j                   j                         vrg S 	 t        j                  | j                   j                  d      }t        |t              st        d      g }|D ]  }t        j                  |j                  d      	      }|j                  d
      }|st        d      |j                  d      }|st        d      t        |t              st        d      | j                  |      }t        j                  |||      }|j                  |        |S # t        j
                  $ r}t        d|       d}~wt        $ r}t        d|       d}~ww xY w)zParses --stage-config.r1   T)location_valuezError parsing YAML file: NzError reading config file: z0The config file should contain a list of stages.zlabel-selector)labelSelectorzsoak-durationz*soak-duration is required in the yaml filezfleet-projectsz+fleet-projects is required in the yaml filez0fleet-projects should be a list in the yaml file)clusterSelectorsoakDurationfleetProjects)rF   r`   r   loadstage_configYAMLParseError
ValueError	Exception
isinstancelistrP   ClusterSelectorgetrN   Stagerd   )	r   stage_data_listerY   
stage_datacluster_selectorsoak_durationfleet_projectsstages	            r   rX   z!RolloutSequenceFlagParser._Stages   s\   tyy99;;i:		
))
 
 o ot,IJJF%
'77"'78 !nn_5mEFF!~~&67nFGG-KLL(89""*$&e
 mmE- &0 MC  821#677 :4QC899:s#   +D' 'E':EE'E""E'c                 .    | j                   j                  S r   )rF   projectr   s    r   Projectz!RolloutSequenceFlagParser.Project   s    99r   c                 .    | j                   j                  S r   )rF   locationr   s    r   Locationz"RolloutSequenceFlagParser.Location   s    99r   c                 .    | j                   j                  S )zParses --async flag.

    The internal representation of --async is set to args.async_, defined in
    calliope/base.py file.

    Returns:
      bool, True if specified, False if unspecified.
    )rF   async_r   s    r   AsynczRolloutSequenceFlagParser.Async   s     99r   N)r;   r<   r=   r>   r   	Namespacer
   r?   r   r   MessageboolrL   rN   rP   rQ   r'   rT   ra   rV   r   rv   rX   r   r   r   rC   r   r   rE   rE   k   s    A:#--:>B>O>O:
&X-- 
&$ 
&x// ~== "C "~55AA *tN001 *Xs  	T 	r   rE   )r>   
__future__r   r   r   r(   typingr   apitools.base.protorpcliter   &googlecloudsdk.api_lib.container.fleetr   googlecloudsdk.callioper	   r
   r   r   *googlecloudsdk.command_lib.container.fleetr   r7   googlecloudsdk.corer   4googlecloudsdk.generated_clients.apis.gkehub.v1alphar   rP   r   rE   rC   r   r   <module>r      sM    ; &  '   / 7 / ( 4 5 S $ jE EPq qr   