-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_api_key.go
More file actions
64 lines (50 loc) · 1.7 KB
/
update_api_key.go
File metadata and controls
64 lines (50 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"fmt"
"log"
"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().StringVar(&cmdState.Opts.ExpiresAtStr, "valid-to", "", "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)
}
log.Printf("Successfully updated API key '%s' with new expiration date %s.\n", c.Opts.APIKeyID, expiresAt.Format(time.RFC1123))
return nil
}