Skip to content

Commit

Permalink
Fetch Tanzu Hub endpoint from CSP when creating tanzu context
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Apr 23, 2024
1 parent 397ea59 commit ec5baff
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pkg/auth/csp/tanzu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package csp

import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -427,3 +428,61 @@ func GetOrgNameFromOrgID(orgID, accessToken, issuer string) (string, error) {

return org.Name, nil
}

// GetTanzuHubEndpointForTAP retrieves Tanzu Hub Endpoint For TAP SaaS through the CSP API
func GetTanzuHubEndpointForTAP(orgID, accessToken string, useStagingIssuer bool) (string, error) {
// CSPServiceURLs stores the CSP service URL information
type CSPServiceURLs struct {
ServiceHome string `json:"serviceHome"`
}

// CSPService stores the CSP service details
type CSPService struct {
DisplayName string `json:"displayName"`
ProductIdentifier string `json:"productIdentifier"`
ServiceDefinitionID string `json:"serviceDefinitionId"`
ServiceUrls CSPServiceURLs `json:"serviceUrls"`
}

// CSPServices stores the CSP services list
type CSPServices struct {
ServicesList []CSPService `json:"servicesList"`
}

endpoint := "https://console.cloud.vmware.com"
if useStagingIssuer {
endpoint = "https://console-stg.cloud.vmware.com"
}
api := fmt.Sprintf("%s/csp/gateway/slc/api/v2/ui/definitions/?orgId=%s", endpoint, orgID)

data := url.Values{}
req, _ := http.NewRequestWithContext(context.Background(), "GET", api, bytes.NewBufferString(data.Encode()))
req.Header.Set("authorization", "Bearer "+accessToken)

resp, err := httpRestClient.Do(req)
if err != nil {
return "", errors.WithMessage(err, "Failed to obtain available services for the specified organization")
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return "", errors.Errorf("Failed to obtain available services for the specified organization. %s", string(body))
}

defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
services := CSPServices{}

if err = json.Unmarshal(body, &services); err != nil {
return "", errors.Wrap(err, "could not unmarshal the services response")
}

for _, s := range services.ServicesList {
if s.ProductIdentifier == "TANZU-SAAS" && strings.Contains(s.DisplayName, "Tanzu Application Platform") { // TODO: Can this be improved to use some unique id?
// Remove `www.` if present from the endpoint. Because when invoking directly through API it does not work
tanzuHubEndpoint := strings.Replace(s.ServiceUrls.ServiceHome, "www.", "", 1)
return tanzuHubEndpoint, nil
}
}

return "", errors.New("could not find 'Tanzu Application Platform' service associated with the specified organization")
}
9 changes: 9 additions & 0 deletions pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,15 @@ func globalTanzuLogin(c *configtypes.Context, generateContextNameFunc func(orgNa
if err != nil {
return err
}

// Fetch the Tanzu Hub endpoint for the Tanzu context as a best case effort
tanzuHubEndpoint, err := csp.GetTanzuHubEndpointForTAP(claims.OrgID, c.GlobalOpts.Auth.AccessToken, staging)
if err != nil {
log.V(7).Infof("unable to get Tanzu Hub endpoint. Error: %v", err.Error())
} else {
c.AdditionalMetadata[config.TanzuHubEndpointKey] = tanzuHubEndpoint

Check failure on line 655 in pkg/command/context.go

View workflow job for this annotation

GitHub Actions / Build

undefined: config.TanzuHubEndpointKey) (typecheck)

Check failure on line 655 in pkg/command/context.go

View workflow job for this annotation

GitHub Actions / Build

undefined: config.TanzuHubEndpointKey (typecheck)

Check failure on line 655 in pkg/command/context.go

View workflow job for this annotation

GitHub Actions / Tanzu CLI Unit Tests (ubuntu-latest)

undefined: config.TanzuHubEndpointKey

Check failure on line 655 in pkg/command/context.go

View workflow job for this annotation

GitHub Actions / Tanzu CLI Test Framework E2E Tests (ubuntu-latest)

undefined: config.TanzuHubEndpointKey

Check failure on line 655 in pkg/command/context.go

View workflow job for this annotation

GitHub Actions / Tanzu CLI Coexistence Tests

undefined: config.TanzuHubEndpointKey

Check failure on line 655 in pkg/command/context.go

View workflow job for this annotation

GitHub Actions / Tanzu CLI Core E2E Tests (ubuntu-latest)

undefined: config.TanzuHubEndpointKey

Check failure on line 655 in pkg/command/context.go

View workflow job for this annotation

GitHub Actions / Tanzu CLI Unit Tests (macos-latest)

undefined: config.TanzuHubEndpointKey

Check failure on line 655 in pkg/command/context.go

View workflow job for this annotation

GitHub Actions / Tanzu CLI Core E2E Tests (macos-latest)

undefined: config.TanzuHubEndpointKey
}

// update the context name using the context name generator
if generateContextNameFunc != nil {
c.Name = generateContextNameFunc(orgName, endpoint, staging)
Expand Down

0 comments on commit ec5baff

Please sign in to comment.