
                         p    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	 Zd
 Zd Zd Zy)z"Utils for GKE cluster memberships.    )absolute_import)division)unicode_literalsN)apis)
exceptions)
propertiesc                    d}d}d}t        j                  ||       }|2|j                  d      |j                  d      |j                  d      fS t        j                  ||       }|2|j                  d      |j                  d      |j                  d      fS t        j                  ||       }|2|j                  d      |j                  d      |j                  d      fS t        j                  dj                  |             )a>  The GKE resource URI can be of following types: zonal, regional or generic.

  zonal - */projects/{project_id}/zones/{zone}/clusters/{cluster_name}
  regional - */projects/{project_id}/regions/{zone}/clusters/{cluster_name}
  generic - */projects/{project_id}/locations/{zone}/clusters/{cluster_name}

  The expected patterns are matched to extract the cluster location and name.
  Args:
   gke_uri: GKE resource URI, e.g., https://container.googleapis.com/v1/
     projects/my-project/zones/us-west2-c/clusters/test1

  Returns:
    cluster's project, location, and name
  z/.*\/projects\/(.*)\/zones\/(.*)\/clusters\/(.*)z1.*\/projects\/(.*)\/regions\/(.*)\/clusters\/(.*)z3.*\/projects\/(.*)\/locations\/(.*)\/clusters\/(.*)         a  argument --gke-uri: {} is invalid. --gke-uri must be of format: `https://container.googleapis.com/v1/projects/my-project/locations/us-central1-a/clusters/my-cluster`. You can use command: `gcloud container clusters list --uri` to view the current GKE clusters in your project.researchgroupr   Errorformat)gke_urizonal_uri_patternregional_uri_patternlocation_uri_patternzone_matcherregion_matcherlocation_matchers          Flib/googlecloudsdk/command_lib/container/fleet/memberships/gke_util.pyParseGKEURIr      s    IMO,g6,a ,"4"4Q"79K9KA9NNN9917;.QQQ  YY3W=!q!q!q!  	. /5fWo	     c                     d}t        j                  ||       }|"|j                  d      |j                  d      fS t        j                  dj                  |             )zParse GKE cluster's location and name.

  Args:
   gke_cluster: GKE cluster sepecified in format {location}/{cluster_name}

  Returns:
    cluster's location, and name
  z
(.*)\/(.*)r
   r   znargument --gke-cluster: {} is invalid. --gke-cluster must be of format: `{{REGION OR ZONE}}/{{CLUSTER_NAME`}}`r   )gke_clusterrgxcluster_matchers      r   ParseGKEClusterr!   K   sb     	#IIc;//   #_%:%:1%===//5vk/B	 r   c                     t        j                  dd      }|j                  d      r|dd }dj                  |j	                  ddd	      j	                  d
dd	      | ||      }dj                  || ||      }||fS )a7  Constructs GKE URI and resource name from args and container endpoint.

  Args:
    project_id: the project identifier to which the cluster to be registered
      belongs.
    cluster_location: zone or region of the cluster.
    cluster_name: name of the cluster to be registered.

  Returns:
    GKE resource link: full resource name as per go/resource-names
      (including preceding slashes).
    GKE cluster URI: URI string looks in the format of
    https://container.googleapis.com/v1/
      projects/{projectID}/locations/{location}/clusters/{clusterName}.
  	containerv1/Nz)//{}/projects/{}/locations/{}/clusters/{}zhttps:// r
   zhttp://z*{}/v1/projects/{}/locations/{}/clusters/{})	core_apisGetEffectiveApiEndpointendswithr   replace)
project_idcluster_locationcluster_namecontainer_endpointgke_resource_linkgke_cluster_uris         r   %ConstructGKEClusterResourceLinkAndURIr2   ^   s    $ !88dK  %+CR0AHH  R3;;Ir1M	 AGG*&6/ 
O	++r   c                     | |yd}| rt        |       \  }}}n@t        j                  j                  j                  j                         }t        |      \  }}t        |||      S )z5Get GKE cluster's full resource name and cluster URI.N)NN)r   r   VALUEScoreproject	GetOrFailr!   r2   )r   r   cluster_projectlocationnames        r   GetGKEClusterResoureLinkAndURIr;      se    _,/&1'&:#OXt '',,44>>@O$[1NHd	.$	OOr   )__doc__
__future__r   r   r   r   googlecloudsdk.api_lib.utilr   r(   googlecloudsdk.corer   r   r   r!   r2   r;    r   r   <module>rA      s6    ) &  ' 	 9 * *-`&,BPr   