Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Commit

Permalink
Labels: Add support for glustercli
Browse files Browse the repository at this point in the history
This PR contains glustercli and client implementation
for labels

Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
  • Loading branch information
rafikc30 committed Sep 10, 2018
1 parent 0625ac6 commit 95bc4be
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 0 deletions.
66 changes: 66 additions & 0 deletions glustercli/cmd/label-create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"fmt"

"github.com/gluster/glusterd2/pkg/api"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
labelCreateHelpShort = "Create a label"
labelCreateHelpLong = "Create a label that can use to tag different objects. Label values will be created with default values if choose to omit, specific label values should provide using relevant flags."
)

var (
flagSnapMaxHardLimit uint64
flagSnapMaxSoftLimit uint64
flagActivateOnCreate bool
flagAutoDelete bool
flagDescription string

labelCreateCmd = &cobra.Command{
Use: "create <labelname>",
Short: labelCreateHelpShort,
Long: labelCreateHelpLong,
Args: cobra.MinimumNArgs(1),
Run: labelCreateCmdRun,
}
)

func init() {
labelCreateCmd.Flags().Uint64Var(&flagSnapMaxHardLimit, "snap-max-hard-limit", 256, "Snapshot maximum hard limit count")
labelCreateCmd.Flags().Uint64Var(&flagSnapMaxSoftLimit, "snap-max-soft-limit", 230, "Snapshot maximum soft limit count")
labelCreateCmd.Flags().BoolVar(&flagActivateOnCreate, "activate-on-create", false, "If enabled, Further snapshots will be activated after creation")
labelCreateCmd.Flags().BoolVar(&flagAutoDelete, "auto-delete", false, "If enabled, Snapshots will be deleted upon reaching snap-max-soft-limit. If disabled A warning log will be generated")
labelCreateCmd.Flags().StringVar(&flagDescription, "description", "", "Label description")

labelCmd.AddCommand(labelCreateCmd)
}

func labelCreateCmdRun(cmd *cobra.Command, args []string) {
labelname := args[0]

req := api.LabelCreateReq{
Name: labelname,
SnapMaxHardLimit: flagSnapMaxHardLimit,
SnapMaxSoftLimit: flagSnapMaxSoftLimit,
ActivateOnCreate: flagActivateOnCreate,
AutoDelete: flagAutoDelete,
Description: flagDescription,
}

info, err := client.LabelCreate(req)
if err != nil {
if GlobalFlag.Verbose {
log.WithError(err).WithFields(
log.Fields{
"labelname": labelname,
}).Error("label creation failed")
}
failure("Label creation failed", err, 1)
}
fmt.Printf("%s Label created successfully\n", info.Name)
}
38 changes: 38 additions & 0 deletions glustercli/cmd/label-delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"fmt"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
labelDeleteHelpShort = "Delete labels"
)

var (
labelDeleteCmd = &cobra.Command{
Use: "delete <labelname>",
Short: labelDeleteHelpShort,
Args: cobra.ExactArgs(1),
Run: labelDeleteCmdRun,
}
)

func init() {
labelCmd.AddCommand(labelDeleteCmd)
}

func labelDeleteCmdRun(cmd *cobra.Command, args []string) {
labelname := args[0]

if err := client.LabelDelete(labelname); err != nil {
if GlobalFlag.Verbose {
log.WithError(err).WithField(
"label", labelname).Error("label delete failed")
}
failure("Label delete failed", err, 1)
}
fmt.Printf("%s Label deleted successfully\n", labelname)
}
62 changes: 62 additions & 0 deletions glustercli/cmd/label-info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"fmt"

"github.com/gluster/glusterd2/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
labelInfoHelpShort = "Get Gluster Label Info"
)

var (
labelInfoCmd = &cobra.Command{
Use: "info <labelname>",
Short: labelInfoHelpShort,
Args: cobra.ExactArgs(1),
Run: labelInfoCmdRun,
}
)

func init() {
labelCmd.AddCommand(labelInfoCmd)
}

func labelInfoDisplay(info *api.LabelGetResp) {
fmt.Println()
fmt.Println("Label Name:", info.Name)
fmt.Println("Snap Max Hard Limit:", info.SnapMaxHardLimit)
fmt.Println("Snap Max Soft Limit:", info.SnapMaxSoftLimit)
fmt.Println("Auto Delete:", info.AutoDelete)
fmt.Println("Activate On Create:", info.ActivateOnCreate)
fmt.Println("Snapshot List:", info.SnapList)
fmt.Println("Description:", info.Description)
fmt.Println()

return
}

func labelInfoHandler(cmd *cobra.Command) error {
var info api.LabelGetResp
var err error

labelname := cmd.Flags().Args()[0]
info, err = client.LabelInfo(labelname)
if err != nil {
return err
}
labelInfoDisplay(&info)
return err
}

func labelInfoCmdRun(cmd *cobra.Command, args []string) {
if err := labelInfoHandler(cmd); err != nil {
if GlobalFlag.Verbose {
log.WithError(err).Error("error getting label info")
}
failure("Error getting Label info", err, 1)
}
}
62 changes: 62 additions & 0 deletions glustercli/cmd/label-list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"fmt"
"os"

"github.com/gluster/glusterd2/pkg/api"
"github.com/olekukonko/tablewriter"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
helpLabelListCmd = "List all Gluster Labels"
)

func init() {

labelCmd.AddCommand(labelListCmd)

}

func labelListHandler(cmd *cobra.Command) error {
var infos api.LabelListResp
var err error
labelname := cmd.Flags().Args()[0]

infos, err = client.LabelList(labelname)
if err != nil {
return err
}

table := tablewriter.NewWriter(os.Stdout)
table.SetAutoMergeCells(true)
table.SetRowLine(true)
if len(infos) == 0 {
fmt.Println("There are no labels in the system")
return nil
}
table.SetHeader([]string{"Name"})
for _, info := range infos {
table.Append([]string{info.Name})
}
table.Render()
return err
}

var labelListCmd = &cobra.Command{
Use: "list",
Short: helpLabelListCmd,
Args: cobra.ExactArgs(1),
Run: labelListCmdRun,
}

func labelListCmdRun(cmd *cobra.Command, args []string) {
if err := labelListHandler(cmd); err != nil {
if GlobalFlag.Verbose {
log.WithError(err).Error("error getting label list")
}
failure("Error getting Label list", err, 1)
}
}
46 changes: 46 additions & 0 deletions glustercli/cmd/label-reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"errors"
"fmt"

"github.com/gluster/glusterd2/pkg/api"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
labelResetCmdHelpShort = "Reset value of label configuratio options"
labelResetCmdHelpLong = "Reset options on a specified gluster snapshot label. Needs a label name and at least one option"
)

var labelResetCmd = &cobra.Command{
Use: "reset <labelname> <options>",
Short: labelResetCmdHelpShort,
Long: labelResetCmdHelpLong,
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
var req api.LabelResetReq
labelname := args[0]
options := args[1:]
if len(args) < 2 {
failure("Specify atleast one label option to reset", errors.New("Specify atleast one label option to reset"), 1)
} else {
req.Configurations = options
}

err := client.LabelReset(req, labelname)
if err != nil {
if GlobalFlag.Verbose {
log.WithError(err).WithField("label", labelname).Error("label reset failed")
}
failure("Snapshot label reset failed", err, 1)
}
fmt.Printf("Snapshot label options reset successfully\n")
},
}

func init() {
labelCmd.AddCommand(labelResetCmd)
}
73 changes: 73 additions & 0 deletions glustercli/cmd/label-set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"errors"
"fmt"

"github.com/gluster/glusterd2/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
labelSetHelpShort = "Set a label value"
labelSetHelpLong = "Modify one or more label value ."
)

var (
labelSetCmd = &cobra.Command{
Use: "set <labelname> <option> <value> [<option> <value>]...",
Short: labelSetHelpShort,
Long: labelSetHelpLong,
Args: labelSetArgsValidate,
Run: labelSetCmdRun,
}
)

func labelSetArgsValidate(cmd *cobra.Command, args []string) error {
// Ensure we have enough arguments for the command
if len(args) < 3 {
return errors.New("need at least 3 arguments")
}

// Ensure we have a proper option-value pairs
if (len(args)-1)%2 != 0 {
return errors.New("needs '<option> <value>' to be in pairs")
}

return nil
}

func init() {
labelCmd.AddCommand(labelSetCmd)
}

func labelSetCmdRun(cmd *cobra.Command, args []string) {
labelname := args[0]
options := args[1:]

if err := labelSetHandler(cmd, labelname, options); err != nil {
if GlobalFlag.Verbose {
log.WithError(err).WithField(
"labelname", labelname).Error("label set failed")
}
failure("Label set failed", err, 1)
} else {
fmt.Printf("Label Values set successfully for %s label\n", labelname)
}

}

func labelSetHandler(cmd *cobra.Command, labelname string, options []string) error {
confs := make(map[string]string)
for op, val := range options {
if op%2 == 0 {
confs[val] = options[op+1]
}
}
req := api.LabelSetReq{
Configurations: confs,
}
err := client.LabelSet(req, labelname)
return err
}
18 changes: 18 additions & 0 deletions glustercli/cmd/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/spf13/cobra"
)

const (
helpLabelCmd = "Snapshot Label Management"
)

var labelCmd = &cobra.Command{
Use: "label",
Short: helpLabelCmd,
}

func init() {
snapshotCmd.AddCommand(labelCmd)
}
Loading

0 comments on commit 95bc4be

Please sign in to comment.