-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstance_account.go
More file actions
81 lines (68 loc) · 2.16 KB
/
instance_account.go
File metadata and controls
81 lines (68 loc) · 2.16 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package cmd
import (
"fmt"
"cloudamqp-cli/client"
"github.com/spf13/cobra"
)
var instanceAccountCmd = &cobra.Command{
Use: "account",
Short: "Manage instance account operations",
Long: `Rotate password and API key for the instance.`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Help()
cmd.SilenceUsage = true
return fmt.Errorf("subcommand required")
},
}
var rotatePasswordCmd = &cobra.Command{
Use: "rotate-password <instance_id>",
Short: "Rotate password",
Long: `Initiate rotation of the user password on your instance.`,
Example: ` cloudamqp instance account rotate-password 1234`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
apiKey, err := getAPIKey()
if err != nil {
return fmt.Errorf("failed to get API key: %w", err)
}
c := client.New(apiKey, Version)
err = c.RotatePassword(args[0])
if err != nil {
fmt.Printf("Error rotating password: %v\n", err)
return err
}
fmt.Println("Password rotation initiated successfully.")
return nil
},
}
var rotateInstanceAPIKeyCmd = &cobra.Command{
Use: "rotate-apikey <instance_id>",
Short: "Rotate Instance API key",
Long: `Rotate the Instance API key.`,
Example: ` cloudamqp instance account rotate-apikey 1234`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
apiKey, err := getAPIKey()
if err != nil {
return fmt.Errorf("failed to get API key: %w", err)
}
c := client.New(apiKey, Version)
err = c.RotateInstanceAPIKey(args[0])
if err != nil {
fmt.Printf("Error rotating instance API key: %v\n", err)
return err
}
fmt.Println("Instance API key rotation initiated successfully.")
fmt.Printf("Warning: The local config for instance %s will need to be updated.\n", args[0])
fmt.Printf("Run 'cloudamqp instance get %s' to retrieve and save the new API key.\n", args[0])
return nil
},
}
func init() {
rotatePasswordCmd.ValidArgsFunction = completeInstances
rotateInstanceAPIKeyCmd.ValidArgsFunction = completeInstances
instanceAccountCmd.AddCommand(rotatePasswordCmd)
instanceAccountCmd.AddCommand(rotateInstanceAPIKeyCmd)
}