From ca3cd1682046c46719f89935c9b74110f088a01d Mon Sep 17 00:00:00 2001 From: Emma Upton Date: Mon, 26 Oct 2026 16:40:39 +0000 Subject: [PATCH] Add create and list commands for Application tokens Co-Authored-By: Claude Fable 5 --- cmd/token.go | 39 ++++++++++ pkg/client/console_client.go | 30 ++++++++ pkg/client/console_client_test.go | 116 ++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) diff --git a/cmd/token.go b/cmd/token.go index c34ceab..70fed2f 100644 --- a/cmd/token.go +++ b/cmd/token.go @@ -9,6 +9,8 @@ import ( var applicationInstanceNameForList *string var applicationInstanceNameForCreate *string +var applicationNameForList *string +var applicationNameForCreate *string var tokenCmd = &cobra.Command{ Use: "token", @@ -63,6 +65,24 @@ var listApplicationInstanceTokenCmd = &cobra.Command{ }, } +var listApplicationTokenCmd = &cobra.Command{ + Use: "application", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + result, err := consoleAPIClient().ListApplicationToken(*applicationNameForList) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not list application token: %s\n", err) + os.Exit(1) + } + if len(result) == 0 { + fmt.Println("No tokens found") + } + for _, token := range result { + fmt.Printf("%s:\t%s\n", token.Name, token.Id) + } + }, +} + var createTokenCmd = &cobra.Command{ Use: "create", Run: func(cmd *cobra.Command, args []string) { @@ -120,6 +140,19 @@ var createApplicationInstanceTokenCmd = &cobra.Command{ }, } +var createApplicationTokenCmd = &cobra.Command{ + Use: "application --application=myapp ", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + result, err := consoleAPIClient().CreateApplicationToken(*applicationNameForCreate, args[0]) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not create application token: %s\n", err) + os.Exit(1) + } + fmt.Printf("%s\n", result.Token) + }, +} + var deleteTokenCmd = &cobra.Command{ Use: "delete ", Args: cobra.ExactArgs(1), @@ -138,12 +171,18 @@ func init() { applicationInstanceNameForCreate = createApplicationInstanceTokenCmd.PersistentFlags().StringP("application-instance", "i", "", "Application instance name") _ = listApplicationInstanceTokenCmd.MarkPersistentFlagRequired("application-instance") _ = createApplicationInstanceTokenCmd.MarkPersistentFlagRequired("application-instance") + applicationNameForList = listApplicationTokenCmd.PersistentFlags().StringP("application", "a", "", "Application name") + applicationNameForCreate = createApplicationTokenCmd.PersistentFlags().StringP("application", "a", "", "Application name") + _ = listApplicationTokenCmd.MarkPersistentFlagRequired("application") + _ = createApplicationTokenCmd.MarkPersistentFlagRequired("application") listTokenCmd.AddCommand(listApplicationInstanceTokenCmd) + listTokenCmd.AddCommand(listApplicationTokenCmd) listTokenCmd.AddCommand(listAdminCmd) tokenCmd.AddCommand(listTokenCmd) tokenCmd.AddCommand(deleteTokenCmd) tokenCmd.AddCommand(createTokenCmd) createTokenCmd.AddCommand(createAdminTokenCmd) createTokenCmd.AddCommand(createApplicationInstanceTokenCmd) + createTokenCmd.AddCommand(createApplicationTokenCmd) rootCmd.AddCommand(tokenCmd) } diff --git a/pkg/client/console_client.go b/pkg/client/console_client.go index da8d2aa..3f37b3a 100644 --- a/pkg/client/console_client.go +++ b/pkg/client/console_client.go @@ -600,6 +600,21 @@ func (client *Client) CreateAdminToken(name string) (CreatedToken, error) { } } +func (client *Client) ListApplicationToken(applicationName string) ([]Token, error) { + result := make([]Token, 0) + client.setAuthMethodFromEnvIfNeeded() + url := client.baseURL + "/token/v1/application_tokens/" + applicationName + resp, err := client.client.R().Get(url) + if err != nil { + return result, err + } else if resp.IsError() { + return result, fmt.Errorf("%s", extractAPIError(resp)) + } else { + err = json.Unmarshal(resp.Body(), &result) + return result, err + } +} + func (client *Client) CreateApplicationInstanceToken(applicationInstanceName, name string) (CreatedToken, error) { var result CreatedToken client.setAuthMethodFromEnvIfNeeded() @@ -615,6 +630,21 @@ func (client *Client) CreateApplicationInstanceToken(applicationInstanceName, na } } +func (client *Client) CreateApplicationToken(applicationName, name string) (CreatedToken, error) { + var result CreatedToken + client.setAuthMethodFromEnvIfNeeded() + url := client.baseURL + "/token/v1/application_tokens/" + applicationName + resp, err := client.client.R().SetBody(map[string]string{"name": name}).Post(url) + if err != nil { + return result, err + } else if resp.IsError() { + return result, fmt.Errorf("%s", extractAPIError(resp)) + } else { + err = json.Unmarshal(resp.Body(), &result) + return result, err + } +} + type SQLResult struct { Header []string `json:"header"` Row [][]interface{} `json:"row"` diff --git a/pkg/client/console_client_test.go b/pkg/client/console_client_test.go index 0adafc4..032e863 100644 --- a/pkg/client/console_client_test.go +++ b/pkg/client/console_client_test.go @@ -798,3 +798,119 @@ func TestApplyWithDiffForAResourceNotThereYet(t *testing.T) { t.Errorf("expected a diff against nothing, got %q", result.Diff) } } + +func TestCreateApplicationTokenShouldWork(t *testing.T) { + defer httpmock.Reset() + baseURL := "http://baseUrl" + apiKey := "aToken" + client, err := Make(APIParameter{ + APIKey: apiKey, + BaseURL: baseURL, + }) + if err != nil { + panic(err) + } + client.setAuthMethodFromEnvIfNeeded() + httpmock.ActivateNonDefault( + client.client.GetClient(), + ) + responder := httpmock.NewStringResponder(201, `{"id": "token-id", "name": "ci-token", "token": "secret-token-value", "createdAt": "2026-10-26T10:00:00Z"}`) + + httpmock.RegisterMatcherResponderWithQuery( + "POST", + "http://baseUrl/api/token/v1/application_tokens/myapp", + nil, + httpmock.HeaderIs("Authorization", "Bearer "+apiKey). + And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")). + And(httpmock.BodyContainsString(`"name":"ci-token"`)), + responder, + ) + + result, err := client.CreateApplicationToken("myapp", "ci-token") + if err != nil { + t.Error(err) + } + if result.Token != "secret-token-value" { + t.Errorf("Bad result expected secret-token-value got: %s", result.Token) + } + if result.Id != "token-id" { + t.Errorf("Bad result expected token-id got: %s", result.Id) + } +} + +func TestCreateApplicationTokenShouldFailOnNot2XX(t *testing.T) { + defer httpmock.Reset() + client, err := Make(APIParameter{APIKey: "aToken", BaseURL: "http://baseUrl"}) + if err != nil { + panic(err) + } + client.setAuthMethodFromEnvIfNeeded() + httpmock.ActivateNonDefault(client.client.GetClient()) + + httpmock.RegisterResponder("POST", "http://baseUrl/api/token/v1/application_tokens/myapp", + httpmock.NewStringResponder(404, `{"title": "Application not found"}`)) + + _, err = client.CreateApplicationToken("myapp", "ci-token") + if err == nil { + t.Fail() + } +} + +func TestListApplicationTokenShouldWork(t *testing.T) { + defer httpmock.Reset() + baseURL := "http://baseUrl" + apiKey := "aToken" + client, err := Make(APIParameter{ + APIKey: apiKey, + BaseURL: baseURL, + }) + if err != nil { + panic(err) + } + client.setAuthMethodFromEnvIfNeeded() + httpmock.ActivateNonDefault( + client.client.GetClient(), + ) + responder := httpmock.NewStringResponder(200, `[{"id": "token-id", "name": "ci-token", "createdAt": "2026-10-26T10:00:00Z", "lastTimeUsed": "2026-10-26T11:00:00Z"}]`) + + httpmock.RegisterMatcherResponderWithQuery( + "GET", + "http://baseUrl/api/token/v1/application_tokens/myapp", + nil, + httpmock.HeaderIs("Authorization", "Bearer "+apiKey). + And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")), + responder, + ) + + result, err := client.ListApplicationToken("myapp") + if err != nil { + t.Error(err) + } + expected := []Token{{ + Id: "token-id", + Name: "ci-token", + CreatedAt: "2026-10-26T10:00:00Z", + LastTimeUsed: "2026-10-26T11:00:00Z", + }} + if !reflect.DeepEqual(result, expected) { + t.Errorf("Bad result expected %v got: %v", expected, result) + } +} + +func TestListApplicationTokenShouldFailOnNot2XX(t *testing.T) { + defer httpmock.Reset() + client, err := Make(APIParameter{APIKey: "aToken", BaseURL: "http://baseUrl"}) + if err != nil { + panic(err) + } + client.setAuthMethodFromEnvIfNeeded() + httpmock.ActivateNonDefault(client.client.GetClient()) + + httpmock.RegisterResponder("GET", "http://baseUrl/api/token/v1/application_tokens/myapp", + httpmock.NewStringResponder(403, `{"title": "Forbidden"}`)) + + _, err = client.ListApplicationToken("myapp") + if err == nil { + t.Fail() + } +}