mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-02 01:45:23 +08:00
add access key subcommand
This commit is contained in:
parent
dddfab263c
commit
aab92ce88d
9 changed files with 171 additions and 1 deletions
38
cli/cmd/keys/create.go
Normal file
38
cli/cmd/keys/create.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package keys
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/gravitl/netmaker/cli/functions"
|
||||
"github.com/gravitl/netmaker/models"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const FlagKeyName = "name"
|
||||
|
||||
var keyName string
|
||||
|
||||
var keysCreateCmd = &cobra.Command{
|
||||
Use: fmt.Sprintf("create [NETWORK NAME] [NUM USES] [--%s=test_key]", FlagKeyName),
|
||||
Args: cobra.ExactArgs(2),
|
||||
Short: "Create an access key",
|
||||
Long: `Create an access key`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
keyUses, err := strconv.ParseInt(args[1], 10, 64)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
key := &models.AccessKey{Uses: int(keyUses)}
|
||||
if keyName != "" {
|
||||
key.Name = keyName
|
||||
}
|
||||
functions.PrettyPrint(functions.CreateKey(args[0], key))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
keysCreateCmd.Flags().StringVar(&keyName, FlagKeyName, "", "Name of the key")
|
||||
rootCmd.AddCommand(keysCreateCmd)
|
||||
}
|
24
cli/cmd/keys/delete.go
Normal file
24
cli/cmd/keys/delete.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package keys
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gravitl/netmaker/cli/functions"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var keysDeleteCmd = &cobra.Command{
|
||||
Use: "delete [NETWORK NAME] [KEY NAME]",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Short: "Delete a key",
|
||||
Long: `Delete a key`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if functions.DeleteKey(args[0], args[1]) == nil {
|
||||
fmt.Println("Success")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(keysDeleteCmd)
|
||||
}
|
20
cli/cmd/keys/list.go
Normal file
20
cli/cmd/keys/list.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package keys
|
||||
|
||||
import (
|
||||
"github.com/gravitl/netmaker/cli/functions"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var keysListCmd = &cobra.Command{
|
||||
Use: "list [NETWORK NAME]",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "List all keys associated with a network",
|
||||
Long: `List all keys associated with a network`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
functions.PrettyPrint(functions.GetKeys(args[0]))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(keysListCmd)
|
||||
}
|
37
cli/cmd/keys/root.go
Normal file
37
cli/cmd/keys/root.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package keys
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "keys",
|
||||
Short: "Manage access keys associated with a network",
|
||||
Long: `Manage access keys associated with a network`,
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
func GetRoot() *cobra.Command {
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
20
cli/cmd/network/refresh_keys.go
Normal file
20
cli/cmd/network/refresh_keys.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"github.com/gravitl/netmaker/cli/functions"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var networkRefreshKeysCmd = &cobra.Command{
|
||||
Use: "refresh_keys [NAME]",
|
||||
Short: "Refresh public and private key pairs of a network",
|
||||
Long: `Refresh public and private key pairs of a network`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
functions.PrettyPrint(functions.RefreshKeys(args[0]))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(networkRefreshKeysCmd)
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/gravitl/netmaker/cli/cmd/context"
|
||||
"github.com/gravitl/netmaker/cli/cmd/keys"
|
||||
"github.com/gravitl/netmaker/cli/cmd/network"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
@ -45,4 +46,5 @@ func init() {
|
|||
// IMP: Bind subcommands here
|
||||
rootCmd.AddCommand(network.GetRoot())
|
||||
rootCmd.AddCommand(context.GetRoot())
|
||||
rootCmd.AddCommand(keys.GetRoot())
|
||||
}
|
||||
|
|
|
@ -75,7 +75,8 @@ func request[T any](method, route string, payload any) *T {
|
|||
}
|
||||
body := new(T)
|
||||
if err := json.Unmarshal(resBodyBytes, body); err != nil {
|
||||
log.Fatalf("Error unmarshalling JSON: %s", err)
|
||||
// log.Printf("Error unmarshalling JSON: %s", err)
|
||||
return nil
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
|
23
cli/functions/keys.go
Normal file
23
cli/functions/keys.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package functions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gravitl/netmaker/models"
|
||||
)
|
||||
|
||||
// GetKeys - fetch all access keys of a network
|
||||
func GetKeys(networkName string) *[]models.AccessKey {
|
||||
return request[[]models.AccessKey](http.MethodGet, fmt.Sprintf("/api/networks/%s/keys", networkName), nil)
|
||||
}
|
||||
|
||||
// CreateKey - create an access key
|
||||
func CreateKey(networkName string, key *models.AccessKey) *models.AccessKey {
|
||||
return request[models.AccessKey](http.MethodPost, fmt.Sprintf("/api/networks/%s/keys", networkName), key)
|
||||
}
|
||||
|
||||
// DeleteKey - delete an access key
|
||||
func DeleteKey(networkName, keyName string) *string {
|
||||
return request[string](http.MethodDelete, fmt.Sprintf("/api/networks/%s/keys/%s", networkName, keyName), nil)
|
||||
}
|
|
@ -38,3 +38,8 @@ func GetNetwork(name string) *models.Network {
|
|||
func DeleteNetwork(name string) *string {
|
||||
return request[string](http.MethodDelete, "/api/networks/"+name, nil)
|
||||
}
|
||||
|
||||
// RefreshKeys - refresh public and private key pairs for a network
|
||||
func RefreshKeys(networkName string) *models.Network {
|
||||
return request[models.Network](http.MethodPost, fmt.Sprintf("/api/networks/%s/keyupdate", networkName), nil)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue