
    8                         d Z ddlZddlZddlZddlmZ dZdZdZdZ	dZ
d	Zd
Zd Zd dZd Zd Zd Zd Zd Zd Zd!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-Helper utilities for fleet packages commands.    N)yamlv1alpha      a  table(info.rolloutStrategyInfo.rollingStrategyInfo.clusters.membership.basename():label=CLUSTER,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.current.version:label=CURRENT_VERSION,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.current.syncState:label=SYNC_STATE,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.desired.version:label=DESIRED_VERSION,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.startTime:label=START_TIME,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.endTime:label=END_TIME,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.state:label=STATE,
                    trim_message():label=MESSAGE)a  table(info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.membership.basename():label=CLUSTER,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.current.version:label=CURRENT_VERSION,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.current.syncState:label=SYNC_STATE,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.desired.version:label=DESIRED_VERSION,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.startTime:label=START_TIME,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.endTime:label=END_TIME,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.state:label=STATE,
                    trim_message():label=MESSAGE)a  table(info.rolloutStrategyInfo.rollingStrategyInfo.clusters.membership.basename():label=CLUSTER,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.current.version:label=CURRENT_VERSION,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.current.syncState:label=SYNC_STATE,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.desired.version:label=DESIRED_VERSION,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.startTime:label=START_TIME,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.endTime:label=END_TIME,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.state:label=STATE,
                    all_messages():label=MESSAGES)a  table(info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.membership.basename():label=CLUSTER,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.current.version:label=CURRENT_VERSION,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.current.syncState:label=SYNC_STATE,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.desired.version:label=DESIRED_VERSION,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.startTime:label=START_TIME,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.endTime:label=END_TIME,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.state:label=STATE,
                    all_messages():label=MESSAGES)c                      t         S N)_DEFAULT_API_VERSION     @lib/googlecloudsdk/command_lib/container/fleet/packages/utils.py
ApiVersionr   @   s    	r   c                 X   | y| j                   r| j                   j                  r| j                   j                  j                  r!|rt        |_        nt
        |_        dg|_        | j                   j                  j                  r"|rt        |_        nt        |_        dg|_        yyyy)zSets format for `rollouts describe` depending on rollout strategy.

  Args:
    rollout: Rollout from `rollouts describe`
    args: Command line args
    less: Whether to show truncate rollout messages

  Returns:
    None
  Nz7info.rolloutStrategyInfo.rollingStrategyInfo.clusters[]z9info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters[])
inforolloutStrategyInforollingStrategyInfo3ROLLOUTS_DESCRIBE_ROLLING_TRUNCATED_MESSAGES_FORMATformat.ROLLOUTS_DESCRIBE_ROLLING_FULL_MESSAGES_FORMATflattenallAtOnceStrategyInfo5ROLLOUTS_DESCRIBE_ALLATONCE_TRUNCATED_MESSAGES_FORMAT0ROLLOUTS_DESCRIBE_ALLATONCE_FULL_MESSAGES_FORMAT)rolloutargslesss      r   FormatForRolloutsDescriber   D   s     _
\\gll66||'';;	IDOPdl||''==	KF
Edl > 7\r   c                    | j                  d      s| j                  d      sg S t        j                  j                  |       rg S g }t	        j
                  |       }|D ]+  }|st	        j                  |      }|j                  |       - |S )Nz.yamlz.yml)endswithospathisdirr   load_all_pathdumpappend)r    	resourcesloaded_resourcesresourcedumped_resources        r   _LoadResourcesFromFiler)   b   sz    
--
 DMM&$9IWW]]4I)''-"h		(+o' # 
r   c                     | j                  d      s| dz  } t        j                  j                  |       dz   }t	        j                  |d      S )N/z/**/*T)pathname	recursive)r   r   r    dirnameglob)r    	glob_paths     r   _AllFilesUnderDirr1   p   sD    	s	CKDggood#g-)	D
 r   c                 @    t        j                  |       j                  S r   )pathlibPathstemr    s    r   _VariantNameFromPathr7   y   s    	d		 	  r   c                 Z    | j                  d      s| dz  } | j                  d      d   }|S )Nr+   )r   split)r    variant_names     r   _VariantNameFromDirr<   }   s/    	s	CKDC$,	r   c                 @    t         j                  j                  |       S r   )r   r    
expanduserr6   s    r   ExpandPathForUserr?      s    			D	!!r   c                     t         j                  j                  |       }|}d|v rt         j                  j                  |      }|S )N$)r   r    r>   
expandvars)r    user_expanded_pathvars_expanded_paths      r   _ExpandPathForUserAndVarsrE      sB    ww))$/)++,>?	r   c                     |s| S t        |       }t        |      }t        j                  j                  |      r t        j                  j	                  ||      S |S )a2  Creates glob pattern by combining source and variants_pattern.

  Args:
    source: Directory or source configuration file.
    variants_pattern: Optional variants_pattern for use with source.

  Returns:
    A glob_pattern for use with 'VariantsFromGlobPattern'. If source
    is a directory, the pattern is applied within the directory. If source is
    not a directory i.e., a file, the pattern is not applied.

    Ex: source=/cfg/, variants_pattern='*.yaml'; returns '/cfg/*.yaml'.
        source=manifest.yaml, variants_pattern=*; returns manifest.yaml.
  )rE   r   r    r!   join)sourcevariants_patternexpanded_sourceexpanded_variants_patterns       r   'GlobPatternFromSourceAndVariantsPatternrL      sN     
M-f5/78HIWW]]?#77<<)BCCr   c                     d|  dS )NzSource file or dir not found: .r
   r6   s    r   _FileNotFoundMessagerO      s    )$q	11r   c                     d|  dS )Nz)Source is not of type directory or file: rN   r
   r6   s    r   _FileWrongTypeMessagerQ      s    4TF!	<<r   c                     t        |       }t        j                  j                  |      sTt        j                  j	                  |      s4t        j                  j                  |      st        t        |            y y y r   )rE   r   r    r!   isfileexistsFileNotFoundErrorrO   )rH   rJ   s     r   ValidateSourcerV      sZ    -f5/		'0O77>>/*2?CDD + 1P	'r   c                    t         j                  j                  |       }|}d|v rt         j                  j                  |      }t	        j                  |      }|j                          i }t        |      dk(  rt         j                  j                  |d         rt        |d         }|r||d<   |S t         j                  j                  |d         r\t        |d         }g }|D ]@  }t         j                  j                  |      }	t        |	      }|s0|j                  |       B |r||d<   |S t        |      dkD  r|D ]  }
t         j                  j                  |
      rt        |
      }|s0t        |
      }|||<   At         j                  j                  |
      sat        |
      }g }|D ]@  }t         j                  j                  |      }	t        |	      }|s0|j                  |       B |st        |
      }|||<    |S )a!  Returns a dictionary of input-format variants discovered from a glob.

  Gets all files from user-supplied glob pattern and creates variant(s). There
  will be a single variant 'default' if the inputted glob pattern has no
  wildcards, and multiple variants if there are wildcards. These variant(s)
  can be used for more advanced deployment setups.

  Args:
    glob_pattern: Pattern compatible with python's glob library

  Returns:
    A dict of input-formatted variants, for example:
      {'us-a': [resources...], 'us-b': [resources...]}
  rA   r   r   default)r   r    r>   rB   r/   sortlenrS   r)   r!   r1   abspathextendr7   r<   )glob_patternuser_expanded_globexpanded_globpathsvariantsr%   
files_listall_resourcesfilefull_file_pathr    r;   s               r   VariantsFromGlobPatternrf      s    ww)),7$-MGG&&}5M
))M
"%**,(Z1_	ww~~eAh(q2i	': 
/9 
uQx	 $U1X.jm$.*>:	


y
)	 
 
+& 
/% 5zA~		*40	-d3,#,(<
 77==&t,
D77??40.,^<)  +	 
 ,T2,#0(<
 ! " 
/r   c                     d}t        |       }|syt        |      dk\  rt        |d         |kD  r|d   d| dz   S |d   S )zShows the first cluster-level message and truncates it if it's too long.

  Args:
    resource: A RolloutInfo resource

  Returns:
    Message limited to 40 characters
  (    r   r   N...$_GetClusterLevelMessagesFromResourcerZ   )r'   truncated_message_lengthmessagess      r   !TransformTrimClusterLevelMessagesro      sY      1(;(	]aC,/GGA;001E99	!r   c                     | j                  di       }|r,|j                  dd      }|rt        |      dkD  r|dd dz   S |S y)zTrims rollout-level message if it's too long.

  Args:
    resource: A Rollout resource

  Returns:
    String message limited to 40 characters
  r   messageri   rh   Nrj   )getrZ   )r'   rollout_inforollout_messages      r    TransformTrimRolloutLevelMessageru     sT     fb),"&&y"5O	_		"s#e++	r   c                     g }| sg S | j                  di       j                  di       }|j                         D ]q  }|j                  dg       }d|v r!|j                  |j                  dg              |j                  di       }d|v sQ|j                  |j                  dg              s | j                  di       j                  dg       }|r-|D ](  }|j                  dd      }|s|j                  |       * |S )	zGathers cluster-level messages from a Rollout resource.

  Args:
    resource: A Rollout resource, from `... rollouts describe ...`

  Returns:
    A list of messages from the Rollout resource.
  r   r   clustersrn   currenterrorserrorMessageri   )rr   valuesr\   r$   )	r'   rn   rollout_strategy_infors   rw   rx   info_errorserrorinfo_messages	            r   rl   rl     s     (	I",,vr266R ,224l
B/HXoohll:r23ll9b)GWoogkk*b12 5 VR(,,Xr:+YY~r2l	% 
 
/r   c                 H    t        |       }|syt        |      dk(  r|d   S |S )zReturns all cluster-level messages from a Rollout resource.

  Args:
    resource: A Rollout resource, from `... rollouts describe ...`

  Returns:
    A single string or string array of cluster-level messages.
  ri   r   r   rk   )r'   rn   s     r    TransformAllClusterLevelMessagesr   8  s/     2(;(	
8}A;	/r   c                     g }| y| j                  di       j                  dg       }|D ](  }|j                  dd      }|s|j                  |       * |syt        |      dk(  r|d   S |S )zBGathers errors from 'info.Errors' and returns their errorMessages.ri   r   ry   rz   r   r   )rr   r$   rZ   )r'   rn   ry   r~   error_messages        r   TransformListFleetPackageErrorsr   I  sy    (<<#''"5&eIInb1Moom$ 
 

8}A;	/r   c                 ,    | j                   s|| _         | S )aF  Upserts the correct fleet package name into fleet package resource.

  Args:
    fleet_package: A user-inputted FleetPackage which may or may not have a name
    fully_qualified_name: The fully qualified name of the FleetPackage resource.

  Returns:
    A FleetPackage that definitely has the correct fully qualified name.
  )name)fleet_packagefully_qualified_names     r   UpsertFleetPackageNamer   \  s     
		-M	r   c                 >   t        |       r| j                  j                  j                  }|o|j	                  d      r^|dk(  rd| j                  j                  _        | S | j                  j                  j                  dd | j                  j                  _        | S )a  Removes leading slash from fleet package path if it uses Cloud Build.

  If we don't remove the leading slash, parsing the path will fail for cloud
  build. See b/352756986#comment13

  Args:
    fleet_package: A user-inputted FleetPackage which may need its path fixed.

  Returns:
    A FleetPackage with a fixed path if it uses Cloud Build, unchanged if it
    doesn't use Cloud Build.
  Nr+   z./r   )_FleetPackageUsesCloudBuildresourceBundleSelectorcloudBuildRepositoryr    
startswith)r   r    s     r    FixFleetPackagePathForCloudBuildr   k  s     !///DDIIDDOOC0	IM,,AAF
 
 00EEJJ12N 	,,AAF 
r   c                 V    | xr& | j                   xr | j                   j                  d uS r   )r   r   )r   s    r   r   r     s3     P

.
.P

.
.
C
C4
Or   )Fr   )"__doc__r/   r   r3   googlecloudsdk.corer   r	    _RESOURCE_BUNDLE_PROJECT_SEGMENT!_RESOURCE_BUNDLE_LOCATION_SEGMENTr   r   r   r   r   r   r)   r1   r7   r<   r?   rE   rL   rO   rQ   rV   rf   ro   ru   rl   r   r   r   r   r   r
   r   r   <module>r      s    4  	  $  #$  $% !75 395 526 .46 0<!"22=E7t$&B"&2r   