add enrollment key to nmctl

This commit is contained in:
Anish Mukherjee 2023-03-02 17:40:31 +05:30
parent d19f292e6c
commit c5311c0cd5
6 changed files with 142 additions and 0 deletions

View file

@ -0,0 +1,47 @@
package enrollment_key
import (
"strings"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/spf13/cobra"
)
var (
expiration int
usesRemaining int
networks string
unlimited bool
tags string
)
var enrollmentKeyCreateCmd = &cobra.Command{
Use: "create [--args]",
Args: cobra.NoArgs,
Short: "Create an enrollment key",
Long: `Create an enrollment key`,
Run: func(cmd *cobra.Command, args []string) {
enrollKey := &models.APIEnrollmentKey{
Expiration: int64(expiration),
UsesRemaining: usesRemaining,
Unlimited: unlimited,
}
if networks != "" {
enrollKey.Networks = strings.Split(networks, ",")
}
if tags != "" {
enrollKey.Tags = strings.Split(tags, ",")
}
functions.PrettyPrint(functions.CreateEnrollmentKey(enrollKey))
},
}
func init() {
enrollmentKeyCreateCmd.Flags().IntVar(&expiration, "expiration", 0, "Expiration time of the key in UNIX timestamp format")
enrollmentKeyCreateCmd.Flags().IntVar(&usesRemaining, "uses", 0, "Number of times this key can be used")
enrollmentKeyCreateCmd.Flags().StringVar(&networks, "networks", "", "Comma-separated list of networks which the enrollment key can access")
enrollmentKeyCreateCmd.Flags().BoolVar(&unlimited, "unlimited", false, "Should the key have unlimited uses ?")
enrollmentKeyCreateCmd.Flags().StringVar(&tags, "tags", "", "Comma-separated list of any additional tags")
rootCmd.AddCommand(enrollmentKeyCreateCmd)
}

View file

@ -0,0 +1,23 @@
package enrollment_key
import (
"fmt"
"github.com/gravitl/netmaker/cli/functions"
"github.com/spf13/cobra"
)
var enrollmentKeyDeleteCmd = &cobra.Command{
Use: "delete keyID",
Args: cobra.ExactArgs(1),
Short: "Delete an enrollment key",
Long: `Delete an enrollment key`,
Run: func(cmd *cobra.Command, args []string) {
functions.DeleteEnrollmentKey(args[0])
fmt.Println("Enrollment key ", args[0], " deleted")
},
}
func init() {
rootCmd.AddCommand(enrollmentKeyDeleteCmd)
}

View file

@ -0,0 +1,20 @@
package enrollment_key
import (
"github.com/gravitl/netmaker/cli/functions"
"github.com/spf13/cobra"
)
var enrollmentKeyListCmd = &cobra.Command{
Use: "list",
Args: cobra.NoArgs,
Short: "List enrollment keys",
Long: `List enrollment keys`,
Run: func(cmd *cobra.Command, args []string) {
functions.PrettyPrint(functions.GetEnrollmentKeys())
},
}
func init() {
rootCmd.AddCommand(enrollmentKeyListCmd)
}

View file

@ -0,0 +1,28 @@
package enrollment_key
import (
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "enrollment_key",
Short: "Manage Enrollment Keys",
Long: `Manage Enrollment Keys`,
}
// GetRoot returns the root subcommand
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)
}
}

View file

@ -6,6 +6,7 @@ import (
"github.com/gravitl/netmaker/cli/cmd/acl"
"github.com/gravitl/netmaker/cli/cmd/context"
"github.com/gravitl/netmaker/cli/cmd/dns"
"github.com/gravitl/netmaker/cli/cmd/enrollment_key"
"github.com/gravitl/netmaker/cli/cmd/ext_client"
"github.com/gravitl/netmaker/cli/cmd/host"
"github.com/gravitl/netmaker/cli/cmd/keys"
@ -55,4 +56,5 @@ func init() {
rootCmd.AddCommand(metrics.GetRoot())
rootCmd.AddCommand(network_user.GetRoot())
rootCmd.AddCommand(host.GetRoot())
rootCmd.AddCommand(enrollment_key.GetRoot())
}

View file

@ -0,0 +1,22 @@
package functions
import (
"net/http"
"github.com/gravitl/netmaker/models"
)
// CreateEnrollmentKey - create an enrollment key
func CreateEnrollmentKey(key *models.APIEnrollmentKey) *models.EnrollmentKey {
return request[models.EnrollmentKey](http.MethodPost, "/api/v1/enrollment-keys", key)
}
// GetEnrollmentKeys - gets all enrollment keys
func GetEnrollmentKeys() *[]models.EnrollmentKey {
return request[[]models.EnrollmentKey](http.MethodGet, "/api/v1/enrollment-keys", nil)
}
// DeleteEnrollmentKey - delete an enrollment key
func DeleteEnrollmentKey(keyID string) {
request[any](http.MethodDelete, "/api/v1/enrollment-keys/"+keyID, nil)
}