
    -                         S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSK
Jr  SS	KJr  S
rSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrS rS r S r!S r"S r#S r$S r%S  r&S! r'S" r(S# r)S$ r*g)%zUtils for Fleet commands.    )absolute_import)division)unicode_literalsN)projects_api)
exceptions)
properties)filesunknownautopushstagingprodz[
    table(
      displayName:sort=1,
      name.segment(1):label=PROJECT,
      uid
    )
z
    table(
      name.basename():label=NAME,
      unique_id:label=UNIQUE_ID,
      name.scope().segment(-3):label=LOCATION
    )
z_
    table(
      name.segment(5):sort=1:label=NAME,
      name.segment(1):label=PROJECT
    )
z_
    table(
      name.segment(7):sort=1:label=NAME,
      name.segment(1):label=PROJECT
    )
z
    table(
      name.segment(7):sort=1:label=NAME,
      user:sort=1:label=USER,
      group:sort=1:label=GROUP,
      role.predefinedRole:label=ROLE,
      role.customRole:label=CUSTOM_ROLE
    )
zz
    table(
      name.segment(7):sort=1:label=NAME,
      scope:sort=2:label=SCOPE,
      fleet:sort=2:label=FLEET
    )
zj
table(
    name.basename():label=NAME,
    name.segment(3):label=LOCATION,
    state.code:label=STATUS
)
a?  
table(
    name.basename():label=NAME,
    metadata.verb:label=ACTION,
    metadata.target.segment(-2):label=TYPE,
    metadata.target.basename():label=TARGET,
    name.segment(3):label=LOCATION,
    done:label=DONE,
    metadata.createTime.date():label=START_TIME:sort=1,
    metadata.endTime.date():label=END_TIME
)
zy
table(
    name.basename():label=NAME,
    state:label=STATE,
    create_time.date():sort=1:reverse:label=CREATE_TIME
)
zM
table(
    name.basename():label=NAME,
    name.segment(3):label=LOCATION
)
a  
    table(
      principal:sort=1:label=PRINCIPAL,
      overall_role:label=OVERALL_ROLE,
      scope_rrb_role:label=SCOPE_RBAC,
      scope_iam_role:label=SCOPE_IAM,
      project_iam_role:label=PROJECT_IAM,
      log_view_access:label=LOG_VIEW_ACCESS
    )
c                      g)zEReturns 'default' to be used as a fallthrough hook in resources.yaml.default r       6lib/googlecloudsdk/command_lib/container/fleet/util.pyDefaultFleetIDr      s    	r   c                    U R                  SSSS9nUR                  S[        [        R                  " S5      S9  UR                  S[        S[        R                  " S	5      S
9  UR                  SS9nUR                  S[        S[        R                  " S5      S9  UR                  S[        [        R                  " S5      S9  g)zAdds the flags necessary to create a KubeClient.

Args:
  parser: an argparse.ArgumentParser, to which the common flags will be added
TzCluster identifier.)mutexrequiredhelpz	--gke-uria            The URI of a GKE cluster that you want to register to Hub; for example,
          'https://container.googleapis.com/v1/projects/my-project/locations/us-central1-a/clusters/my-cluster'.
          To obtain the URI, you can run 'gcloud container clusters list --uri'.
          Note that this should only be provided if the cluster being registered
          is a GKE cluster. The service will validate the provided URI to
          confirm that it maps to a valid GKE cluster."
        typer   z--gke-clusterzLOCATION/CLUSTER_NAMEz          The location/name of the GKE cluster. The location can be a zone or
          a region for e.g `us-central1-a/my-cluster`.
        )r   metavarr   zNon-GKE cluster identifier.)r   	--contextz        The cluster context as it appears in the kubeconfig file. You can get
        this value from the command line by running command:
        `kubectl config current-context`.
      )r   r   r   --kubeconfigz            The kubeconfig file containing an entry for the cluster. Defaults to
            $KUBECONFIG if it is set in the environment, otherwise defaults to
            $HOME/.kube/config.
          N)	add_groupadd_argumentstrtextwrapdedent)parsergroupcontext_groups      r   AddClusterConnectionCommonArgsr%      s     

4&;  % ??     %??  	   //'D/E-?? 
 	  	 ??    r   c                     U R                  S[        [        R                  " S5      S9  U R                  S[        [        R                  " S5      S9  g)zAdds the flags shared between 'hub' subcommands to parser.

Args:
  parser: an argparse.ArgumentParser, to which the common flags will be added
r   z          The kubeconfig file containing an entry for the cluster. Defaults to
          $KUBECONFIG if it is set in the environment, otherwise defaults to
          to $HOME/.kube/config.
        r   r   zM        The context in the kubeconfig file that specifies the cluster.
      N)r   r   r    r!   )r"   s    r   AddCommonArgsr'      sZ     	??     	?? 
   r   c                  L    [        S [        R                  " 5        5       5      $ )zZRetrieve the project IDs of projects the user can access.

Returns:
  set of project IDs.
c              3   8   #    U  H  oR                   v   M     g 7f)N)	projectId).0ps     r   	<genexpr>-UserAccessibleProjectIDSet.<locals>.<genexpr>   s     6"5Q[["5s   )setr   Listr   r   r   UserAccessibleProjectIDSetr1      s     
6,"3"3"56	66r   c                 ~    [         R                  " [        R                  " [        R                  " U 5      5      5      $ )a  Reads the provided file, and returns its contents, base64-encoded.

Args:
  filename: The path to the file, absolute or relative to the current working
    directory.

Returns:
  A string, the contents of filename, base64-encoded.

Raises:
 files.Error: if the file cannot be read.
)base64	b64encoder	   ReadBinaryFileContentsExpandHomeDir)filenames    r   Base64EncodedFileContentsr8      s0     
		""5#6#6x#@A
 r   c                     U R                   (       a  U(       d  SR                  X#5      $ U R                   (       d  U(       a  SR                  X#5      $ g)am  Generates user message with information about enabling/disabling Workload Identity.

We do not allow updating issuer url from one non-empty value to another.
Args:
  membership: membership resource.
  issuer_url: The discovery URL for the cluster's service account token
    issuer.
  resource_name: The full membership resource name.
  cluster_name: User supplied cluster_name.

Returns:
  A string, the message string for user to display information about
  enabling/disabling WI on a membership, if the issuer url is changed
  from empty to non-empty value or vice versa. An empty string is returned
  for other cases
zA membership [{}] for the cluster [{}] already exists. The cluster was previously registered with Workload Identity enabled. Continuing will disable Workload Identity on your membership, and will reinstall the Connect agent deployment.zA membership [{}] for the cluster [{}] already exists. The cluster was previously registered without Workload Identity. Continuing will enable Workload Identity on your membership, and will reinstall the Connect agent deployment. )	authorityformat)
membership
issuer_urlresource_namecluster_names       r   GenerateWIUpdateMsgStringrA      s[    & *	H IOI
	 
		*	H IOI
	 
r   c                 6    U R                   nU(       a  US-   $ S$ )zReturns a prefix to add to a gcloud command.

This is meant for formatting an example string, such as:
  gcloud {}container fleet register-cluster

Args:
  release_track: A ReleaseTrack

Returns:
 a prefix to add to a gcloud based on the release track
 r:   )prefix)release_trackrD   s     r   ReleaseTrackCommandPrefixrF   (  s      &#'R'r   c                      g)z?Returns '-' to be used as a fallthrough hook in resources.yaml.-r   r   r   r   DefaultToAllLocationsrI   9  s    	r   c                      g)zDReturns 'global' to be used as a fallthrough hook in resources.yaml.globalr   r   r   r   DefaultToGlobalrL   >  s    	r   c                      [         R                  R                  R                  S5      R	                  5       n U (       a  SU ;   a  [        $ SU ;   a  [        $ SU ;   a  [        $ [        $ ! [         R
                   a    Sn  NIf = f)zReturns the current GKEHub API environment.

Assumes prod endpoint if override is unset, unknown endpoint if overrides has
unrecognized value.

Returns:
  One of prod, staging, autopush, or unknown.
gkehubNzgkehub.googleapis.comzstaging-gkehubzautopush-gkehub)
r   VALUESapi_endpoint_overridesPropertyGetNoSuchPropertyErrorPROD_APISTAGING_APIAUTOPUSH_APIUNKNOWN_API)hub_endpoint_overrides    r   APIEndpointrY   C  s    !&--DDMM	ce   	 $9	9O0011 
	'	' ! !s   7A* *BBc                    SnU R                   (       aQ  [        R                  " SU R                   5      nU(       a  UR                  S5      nU$ [        R
                  " S5      eU R                  (       aQ  [        R                  " SU R                  5      nU(       a  UR                  S5      nU$ [        R
                  " S5      eU$ )a  Returns the location for a membership based on GKE cluster flags.

For GKE clusters, use cluster location as membership location, unless
they are registered with kubeconfig in which case they are not
considered "GKE clusters."

Args:
  args: The command line args

Returns:
  a location, e.g. "global" or "us-central1".

Raises:
  a core.Error, if the location could not be found in the flag
r:   z+([a-z0-9]+\-[a-z0-9]+)(\-[a-z])?/(\-[a-z])?   zUnable to parse location from `gke-cluster` parameter. Expecting `$CLUSTER_LOCATION/$CLUSTER_NAME` e.g. `us-central1/my-cluster`zC(regions|locations|zones)/([a-z0-9]+\-[a-z0-9]+)(\-[a-z])?/clusters   zUnable to parse location from `gke-uri` parameter. Expecting a string like projects/123/locations/us-central1-a/clusters/my-cluster)gke_clusterresearchr#   r   Errorgke_uri)argslocationlocation_res      r   LocationFromGKEArgsre   _  s      (	))68H8HK ""1%h& 
/# M  ||))NK ""1%h 
/	 Q  
/r   c                 2    U R                  S5      nXS-   S $ )zReturns resource ID from resource path.

Args:
  path: resource path, e.g., "projects/p/locations/global/scopes/my-scope"

Returns:
  resource ID, e.g., "my-scope"

Raises ValueError if resource ID could not be found
/r[   N)rindex)pathidxs     r   ResourceIdFromPathrk     s      	C#	Aghr   )+__doc__
__future__r   r   r   r3   r^   r    +googlecloudsdk.api_lib.cloudresourcemanagerr   googlecloudsdk.corer   r   googlecloudsdk.core.utilr	   rW   rV   rU   rT   LIST_FORMATMEM_LIST_FORMATSC_LIST_FORMATNS_LIST_FORMATSC_NS_LIST_FORMATRB_LIST_FORMATB_LIST_FORMATFLEET_FORMATOPERATION_FORMATROLLOUT_LIST_FORMATROLLOUTSEQUENCE_LIST_FORMATAPP_OPERATOR_LIST_FORMATr   r%   r'   r1   r8   rA   rF   rI   rL   rY   re   rk   r   r   r   <module>r}      s      &  '  	  D * * *     	 
3l27$+\("

8*Zr   