Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Oct 7, 2024
1 parent 393f9f9 commit f008087
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
24 changes: 18 additions & 6 deletions pkg/command/apitoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package command

import (
"fmt"
"net/url"

"github.com/fatih/color"
"github.com/pkg/errors"
Expand Down Expand Up @@ -50,7 +51,8 @@ func newAPITokenCreateCmd() *cobra.Command {
# Note: The retrieved token can be used as the value of TANZU_API_TOKEN
# when running 'tanzu login'. For example:
TANZU_API_TOKEN=<token> tanzu login`,
RunE: createAPIToken,
RunE: createAPIToken,
ValidArgsFunction: noMoreCompletions,
}

return createCmd
Expand All @@ -59,14 +61,14 @@ func newAPITokenCreateCmd() *cobra.Command {
func createAPIToken(cmd *cobra.Command, _ []string) (err error) {
c, err := config.GetActiveContext(types.ContextTypeTanzu)
if err != nil {
return errors.New("No active context of type `tanzu`. Please login to Tanzu Platform first to generate an API token")
return errors.New("no active context of type `tanzu`. Please login to Tanzu Platform first to generate an API token")
}
if c == nil || c.GlobalOpts == nil || c.GlobalOpts.Auth.Issuer == "" {
return errors.New("Invalid active context of type `tanzu`. Please login to Tanzu Platform first to generate an API token")
return errors.New("invalid active context of type `tanzu`. Please login to Tanzu Platform first to generate an API token")
}
// Make sure it is of type tanzu with tanzuIdpType as `uaa` else return error
if idpType, exist := c.AdditionalMetadata[config.TanzuIdpTypeKey]; !exist || idpType != "uaa" {
return errors.New("Only UAA IDP type is supported for generating an API token. The generation of the API token for public endpoint should be done via https://console.tanzu.broadcom.com")
if idpType, exist := c.AdditionalMetadata[config.TanzuIdpTypeKey]; !exist || idpType != string(config.UAAIdpType) {
return errors.New("only UAA IDP type is supported for generating an API token. The generation of the API token for public endpoint should be done via https://console.tanzu.broadcom.com")
}

var token *commonauth.Token
Expand All @@ -81,12 +83,22 @@ func createAPIToken(cmd *cobra.Command, _ []string) (err error) {
if err != nil {
return errors.Wrap(err, "unable to login")
}

// Get tanzu platform endpoint as best effort from the existing context
tpEndpoint := "<tanzu-platform-endpoint>"
if hubEndpoint, exist := c.AdditionalMetadata[config.TanzuHubEndpointKey]; exist && hubEndpoint != nil {
u, err := url.Parse(hubEndpoint.(string))
if err == nil {
tpEndpoint = fmt.Sprintf("%s://%s", u.Scheme, u.Host)
}
}

cyanBold := color.New(color.FgCyan).Add(color.Bold)
bold := color.New(color.Bold)

fmt.Fprint(cmd.OutOrStdout(), bold.Sprint("==\n\n"))
fmt.Fprintf(cmd.OutOrStdout(), "%s Your generated API token is: %s\n\n", bold.Sprint("API Token Generation Successful!"), cyanBold.Sprint(token.RefreshToken))
fmt.Fprintf(cmd.OutOrStdout(), "For non-interactive login use the API token as follow: %s\n\n", cyanBold.Sprint("TANZU_API_TOKEN=<token> tanzu login --endpoint <tanzu-platform-endpoint>"))
fmt.Fprintf(cmd.OutOrStdout(), "For non-interactive login use the API token as follows: %s\n\n", cyanBold.Sprintf("TANZU_API_TOKEN=%s tanzu login --endpoint %s", token.RefreshToken, tpEndpoint))
fmt.Fprint(cmd.OutOrStdout(), "Please copy and save your token securely. Note that you will need to regenerate a new token before expiration time and login again to continue using the CLI.\n")

return nil
Expand Down
34 changes: 30 additions & 4 deletions pkg/command/apitoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,33 @@ func TestCreateAPIToken(t *testing.T) {
API Token Generation Successful! Your generated API token is: refresh-token
For non-interactive login use the API token as follow: TANZU_API_TOKEN=<token> tanzu login --endpoint <tanzu-platform-endpoint>
For non-interactive login use the API token as follows: TANZU_API_TOKEN=refresh-token tanzu login --endpoint <tanzu-platform-endpoint>
Please copy and save your token securely. Note that you will need to regenerate a new token before expiration time and login again to continue using the CLI.
`,
},
{
name: "success with specific endpoint in log message",
context: &configtypes.Context{
Name: "fakecontext",
ContextType: configtypes.ContextTypeTanzu,
GlobalOpts: &configtypes.GlobalServer{
Auth: configtypes.GlobalServerAuth{
Issuer: "https://example.com",
},
},
AdditionalMetadata: map[string]interface{}{
config.TanzuIdpTypeKey: config.UAAIdpType,
config.TanzuHubEndpointKey: "https://platform.tanzu.broadcom.com/hub",
},
},
tanzuLoginErr: nil,
wantErr: false,
output: `==
API Token Generation Successful! Your generated API token is: refresh-token
For non-interactive login use the API token as follows: TANZU_API_TOKEN=refresh-token tanzu login --endpoint https://platform.tanzu.broadcom.com
Please copy and save your token securely. Note that you will need to regenerate a new token before expiration time and login again to continue using the CLI.
`,
Expand All @@ -99,7 +125,7 @@ Please copy and save your token securely. Note that you will need to regenerate
context: nil,
tanzuLoginErr: nil,
wantErr: true,
errMsg: "No active context of type `tanzu`. Please login to Tanzu Platform first to generate an API token",
errMsg: "no active context of type `tanzu`. Please login to Tanzu Platform first to generate an API token",
},
{
name: "invalid active context",
Expand All @@ -112,7 +138,7 @@ Please copy and save your token securely. Note that you will need to regenerate
},
tanzuLoginErr: nil,
wantErr: true,
errMsg: "Invalid active context of type `tanzu`. Please login to Tanzu Platform first to generate an API token",
errMsg: "invalid active context of type `tanzu`. Please login to Tanzu Platform first to generate an API token",
},
{
name: "invalid IDP type",
Expand All @@ -130,7 +156,7 @@ Please copy and save your token securely. Note that you will need to regenerate
},
tanzuLoginErr: nil,
wantErr: true,
errMsg: "Only UAA IDP type is supported for generating an API token. The generation of the API token for public endpoint should be done via https://console.tanzu.broadcom.com",
errMsg: "only UAA IDP type is supported for generating an API token. The generation of the API token for public endpoint should be done via https://console.tanzu.broadcom.com",
},
{
name: "TanzuLogin error",
Expand Down

0 comments on commit f008087

Please sign in to comment.