@@ -28,6 +28,9 @@ import (
2828 "golang.org/x/oauth2"
2929 "golang.org/x/term"
3030
31+ "github.com/vmware-tanzu/tanzu-cli/pkg/centralconfig"
32+ cliconfig "github.com/vmware-tanzu/tanzu-cli/pkg/config"
33+ "github.com/vmware-tanzu/tanzu-plugin-runtime/config"
3134 "github.com/vmware-tanzu/tanzu-plugin-runtime/log"
3235)
3336
@@ -37,6 +40,10 @@ const (
3740 tanzuCLIClientID = "tanzu-cli-client-id"
3841 defaultListenAddress = "127.0.0.1:0"
3942 defaultCallbackPath = "/callback"
43+
44+ centralConfigTanzuHubMetadata = "cli.core.tanzu_hub_metadata"
45+ defaultCSPDisplayName = "Tanzu Platform"
46+ defaultCSPProductIdentifier = "TANZU-SAAS"
4047)
4148
4249// stdin returns the file descriptor for stdin as an int.
@@ -47,6 +54,15 @@ type orgInfo struct {
4754 Name string `json:"displayName"`
4855}
4956
57+ // tanzuHubMetadata to parse the hub metadata from central config
58+ type tanzuHubMetadata struct {
59+ CSPProductIdentifier string `json:"cspProductIdentifier" yaml:"cspProductIdentifier"`
60+ CSPDisplayName string `json:"cspDisplayName" yaml:"cspDisplayName"`
61+ EndpointProduction string `json:"endpointProduction" yaml:"endpointProduction"`
62+ EndpointStaging string `json:"endpointStaging" yaml:"endpointStaging"`
63+ UseCentralConfig bool `json:"useCentralConfig" yaml:"useCentralConfig"`
64+ }
65+
5066type cspLoginHandler struct {
5167 tokenExchange context.Context
5268 tokenExchangeComplete context.CancelFunc
@@ -429,8 +445,44 @@ func GetOrgNameFromOrgID(orgID, accessToken, issuer string) (string, error) {
429445 return org .Name , nil
430446}
431447
432- // GetTanzuHubEndpointForTAP retrieves Tanzu Hub Endpoint For TAP SaaS through the CSP API
433- func GetTanzuHubEndpointForTAP (orgID , accessToken string , useStagingIssuer bool ) (string , error ) {
448+ // GetTanzuHubEndpoint retrieves Tanzu Hub Endpoint through the CSP API or through Central Config as fallback
449+ func GetTanzuHubEndpoint (orgID , accessToken string , useStagingIssuer bool ) (string , error ) {
450+ var errCSP error
451+ var endpoint string
452+
453+ hubMetadata := getTanzuHubMetadataFromCentralConfig ()
454+ if hubMetadata .CSPDisplayName == "" {
455+ hubMetadata .CSPDisplayName = defaultCSPDisplayName
456+ }
457+ if hubMetadata .CSPProductIdentifier == "" {
458+ hubMetadata .CSPProductIdentifier = defaultCSPProductIdentifier
459+ }
460+
461+ // If feature-flag to get endpoint from central config is not configured
462+ // try to use CSP api to get the endpoint
463+ if ! hubMetadata .UseCentralConfig {
464+ endpoint , errCSP = getTanzuHubEndpointFromCSP (hubMetadata , orgID , accessToken , useStagingIssuer )
465+ }
466+
467+ // If the endpoint is empty or we got error while getting endpoint from CSP
468+ // try to get the endpoint from central configuration with hubMetadata
469+ if endpoint == "" || errCSP != nil {
470+ endpoint = hubMetadata .EndpointProduction
471+ if useStagingIssuer {
472+ endpoint = hubMetadata .EndpointStaging
473+ }
474+ }
475+
476+ // If endpoint is still empty return error
477+ if endpoint == "" {
478+ return "" , errCSP
479+ }
480+
481+ return endpoint , nil
482+ }
483+
484+ // getTanzuHubEndpointFromCSP retrieves Tanzu Hub Endpoint through the CSP API
485+ func getTanzuHubEndpointFromCSP (metadata tanzuHubMetadata , orgID , accessToken string , useStagingIssuer bool ) (string , error ) {
434486 // CSPServiceURLs stores the CSP service URL information
435487 type CSPServiceURLs struct {
436488 ServiceHome string `json:"serviceHome"`
@@ -477,12 +529,29 @@ func GetTanzuHubEndpointForTAP(orgID, accessToken string, useStagingIssuer bool)
477529 }
478530
479531 for _ , s := range services .ServicesList {
480- if s .ProductIdentifier == "TANZU-SAAS" && strings .Contains (s .DisplayName , "Tanzu Application Platform" ) { // TODO: Can this be improved to use some unique id?
532+ if s .ProductIdentifier == metadata . CSPProductIdentifier && strings .Contains (s .DisplayName , metadata . CSPDisplayName ) {
481533 // Remove `www.` if present from the endpoint. Because when invoking directly through API it does not work
482534 tanzuHubEndpoint := strings .Replace (s .ServiceUrls .ServiceHome , "www." , "" , 1 )
483535 return tanzuHubEndpoint , nil
484536 }
485537 }
486538
487- return "" , errors .New ("could not find 'Tanzu Application Platform' service associated with the specified organization" )
539+ return "" , errors .Errorf ("could not find '%s' service associated with the specified organization" , metadata .CSPDisplayName )
540+ }
541+
542+ // getTanzuHubMetadataFromCentralConfig gets the tanzu hub metadata from central config as best effort
543+ // If cannot get the data from central config it will set few default CSP config before returning the object
544+ func getTanzuHubMetadataFromCentralConfig () tanzuHubMetadata {
545+ hubMetadata := tanzuHubMetadata {}
546+
547+ // We will get the central configuration from the default discovery source
548+ discoverySource , err := config .GetCLIDiscoverySource (cliconfig .DefaultStandaloneDiscoveryName )
549+ if err != nil {
550+ return hubMetadata
551+ }
552+ centralConfigReader := centralconfig .NewCentralConfigReader (discoverySource )
553+
554+ // Get the tanzu hub metadata
555+ _ = centralConfigReader .GetCentralConfigEntry (centralConfigTanzuHubMetadata , & hubMetadata )
556+ return hubMetadata
488557}
0 commit comments