-
Notifications
You must be signed in to change notification settings - Fork 2
Extend api key #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Extend api key #29
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2bbe2d9
feat: add first working draft
OliverTrautvetter c670c0f
Merge branch 'main' into extend-api-key
OliverTrautvetter bf0ff21
fix: json keys
OliverTrautvetter 88db295
fix: ApiKey struct fields with correct json key
OliverTrautvetter 8db2a33
refactor: replace direct flag requirement calls with util function
OliverTrautvetter 4b7851e
fix: update command descriptions for clarity and consistency
OliverTrautvetter 1ffd6f8
fix: update function names to export them
OliverTrautvetter d3a893f
Merge branch 'main' into extend-api-key
OliverTrautvetter 05db306
refactor: update mock function signatures and fix missing bracket
OliverTrautvetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/codesphere-cloud/oms/internal/portal" | ||
| "github.com/codesphere-cloud/oms/internal/util" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type UpdateAPIKeyCmd struct { | ||
| Opts UpdateAPIKeyOpts | ||
| } | ||
|
|
||
| type UpdateAPIKeyOpts struct { | ||
| GlobalOptions | ||
| APIKeyID string | ||
| ExpiresAtStr string | ||
| } | ||
|
|
||
| func addApiKeyUpdateCmd(parentCmd *cobra.Command) { | ||
| cmdState := &UpdateAPIKeyCmd{ | ||
| Opts: UpdateAPIKeyOpts{}, | ||
| } | ||
|
|
||
| apiKeyCmd := &cobra.Command{ | ||
| Use: "api-key", | ||
| Short: "Update an API key's expiration date", | ||
| Long: `Updates the expiration date for a given API key using the --id and --valid-to flags.`, | ||
| Args: cobra.ExactArgs(0), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| p := portal.NewPortalClient() | ||
| return cmdState.UpdateAPIKey(p) | ||
| }, | ||
| } | ||
|
|
||
| apiKeyCmd.Flags().StringVarP(&cmdState.Opts.APIKeyID, "id", "i", "", "The ID of the API key to update") | ||
| apiKeyCmd.Flags().StringVarP(&cmdState.Opts.ExpiresAtStr, "valid-to", "v", "", "The new expiration date in RFC3339 format (e.g., \"2025-12-31T23:59:59Z\")") | ||
|
|
||
| util.MarkFlagRequired(apiKeyCmd, "id") | ||
| util.MarkFlagRequired(apiKeyCmd, "valid-to") | ||
|
|
||
| parentCmd.AddCommand(apiKeyCmd) | ||
| } | ||
|
|
||
| func (c *UpdateAPIKeyCmd) UpdateAPIKey(p portal.Portal) error { | ||
| expiresAt, err := time.Parse(time.RFC3339, c.Opts.ExpiresAtStr) | ||
|
|
||
| if err != nil { | ||
| return fmt.Errorf("invalid date format for <valid-to>: %w", err) | ||
| } | ||
|
|
||
| if err := p.UpdateAPIKey(c.Opts.APIKeyID, expiresAt); err != nil { | ||
| return fmt.Errorf("failed to update API key: %w", err) | ||
| } | ||
|
|
||
| fmt.Printf("Successfully updated API key '%s' with new expiration date %s.\n", c.Opts.APIKeyID, expiresAt.Format(time.RFC1123)) | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
|
|
||
| "github.com/codesphere-cloud/oms/cli/cmd" | ||
| "github.com/codesphere-cloud/oms/internal/portal" | ||
| ) | ||
|
|
||
| var _ = Describe("UpdateAPIKey", func() { | ||
|
|
||
| var ( | ||
| mockPortal *portal.MockPortal | ||
| c cmd.UpdateAPIKeyCmd | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| mockPortal = portal.NewMockPortal(GinkgoT()) | ||
| c = cmd.UpdateAPIKeyCmd{} | ||
| }) | ||
|
|
||
| Describe("Run", func() { | ||
| It("successfully updates the API key when given valid input", func() { | ||
| apiKeyID := "aaaaaaaaaaaaaaaaaaaaaa" | ||
| expiresAtStr := "2027-12-31T23:59:59Z" | ||
| expectedExpiresAt, err := time.Parse(time.RFC3339, expiresAtStr) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| c.Opts.APIKeyID = apiKeyID | ||
| c.Opts.ExpiresAtStr = expiresAtStr | ||
|
|
||
| mockPortal.EXPECT().UpdateAPIKey(apiKeyID, expectedExpiresAt).Return(nil) | ||
|
|
||
| err = c.UpdateAPIKey(mockPortal) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| }) | ||
|
|
||
| It("returns an error for an invalid api key id format", func() { | ||
| apiKeyID := "not-a-valid-id" | ||
| expiresAtStr := "2027-12-31T23:59:59Z" | ||
| expectedExpiresAt, err := time.Parse(time.RFC3339, expiresAtStr) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| c.Opts.APIKeyID = apiKeyID | ||
| c.Opts.ExpiresAtStr = expiresAtStr | ||
|
|
||
| mockPortal.EXPECT().UpdateAPIKey(apiKeyID, expectedExpiresAt).Return(fmt.Errorf("invalid api key id format")) | ||
|
|
||
| err = c.UpdateAPIKey(mockPortal) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("invalid api key id format")) | ||
| }) | ||
|
|
||
| It("returns an error for an invalid date format", func() { | ||
| c.Opts.APIKeyID = "valid id" | ||
| c.Opts.ExpiresAtStr = "2025/123/123" | ||
|
|
||
| err := c.UpdateAPIKey(mockPortal) | ||
|
|
||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("invalid date format")) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.