diff --git a/cli/cmd/enrollment_key/create.go b/cli/cmd/enrollment_key/create.go new file mode 100644 index 00000000..c1e7e319 --- /dev/null +++ b/cli/cmd/enrollment_key/create.go @@ -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) +} diff --git a/cli/cmd/enrollment_key/delete.go b/cli/cmd/enrollment_key/delete.go new file mode 100644 index 00000000..62108b5d --- /dev/null +++ b/cli/cmd/enrollment_key/delete.go @@ -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) +} diff --git a/cli/cmd/enrollment_key/list.go b/cli/cmd/enrollment_key/list.go new file mode 100644 index 00000000..ce56c080 --- /dev/null +++ b/cli/cmd/enrollment_key/list.go @@ -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) +} diff --git a/cli/cmd/enrollment_key/root.go b/cli/cmd/enrollment_key/root.go new file mode 100644 index 00000000..887b9df9 --- /dev/null +++ b/cli/cmd/enrollment_key/root.go @@ -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) + } +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index f2f52ab8..58d1c905 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -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()) } diff --git a/cli/functions/enrollment_keys.go b/cli/functions/enrollment_keys.go new file mode 100644 index 00000000..40cf2785 --- /dev/null +++ b/cli/functions/enrollment_keys.go @@ -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) +}