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

Commit 95bc4be

Browse files
committed
Labels: Add support for glustercli
This PR contains glustercli and client implementation for labels Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
1 parent 0625ac6 commit 95bc4be

8 files changed

Lines changed: 415 additions & 0 deletions

File tree

glustercli/cmd/label-create.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/gluster/glusterd2/pkg/api"
7+
8+
log "github.com/sirupsen/logrus"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
const (
13+
labelCreateHelpShort = "Create a label"
14+
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."
15+
)
16+
17+
var (
18+
flagSnapMaxHardLimit uint64
19+
flagSnapMaxSoftLimit uint64
20+
flagActivateOnCreate bool
21+
flagAutoDelete bool
22+
flagDescription string
23+
24+
labelCreateCmd = &cobra.Command{
25+
Use: "create <labelname>",
26+
Short: labelCreateHelpShort,
27+
Long: labelCreateHelpLong,
28+
Args: cobra.MinimumNArgs(1),
29+
Run: labelCreateCmdRun,
30+
}
31+
)
32+
33+
func init() {
34+
labelCreateCmd.Flags().Uint64Var(&flagSnapMaxHardLimit, "snap-max-hard-limit", 256, "Snapshot maximum hard limit count")
35+
labelCreateCmd.Flags().Uint64Var(&flagSnapMaxSoftLimit, "snap-max-soft-limit", 230, "Snapshot maximum soft limit count")
36+
labelCreateCmd.Flags().BoolVar(&flagActivateOnCreate, "activate-on-create", false, "If enabled, Further snapshots will be activated after creation")
37+
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")
38+
labelCreateCmd.Flags().StringVar(&flagDescription, "description", "", "Label description")
39+
40+
labelCmd.AddCommand(labelCreateCmd)
41+
}
42+
43+
func labelCreateCmdRun(cmd *cobra.Command, args []string) {
44+
labelname := args[0]
45+
46+
req := api.LabelCreateReq{
47+
Name: labelname,
48+
SnapMaxHardLimit: flagSnapMaxHardLimit,
49+
SnapMaxSoftLimit: flagSnapMaxSoftLimit,
50+
ActivateOnCreate: flagActivateOnCreate,
51+
AutoDelete: flagAutoDelete,
52+
Description: flagDescription,
53+
}
54+
55+
info, err := client.LabelCreate(req)
56+
if err != nil {
57+
if GlobalFlag.Verbose {
58+
log.WithError(err).WithFields(
59+
log.Fields{
60+
"labelname": labelname,
61+
}).Error("label creation failed")
62+
}
63+
failure("Label creation failed", err, 1)
64+
}
65+
fmt.Printf("%s Label created successfully\n", info.Name)
66+
}

glustercli/cmd/label-delete.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
log "github.com/sirupsen/logrus"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
const (
11+
labelDeleteHelpShort = "Delete labels"
12+
)
13+
14+
var (
15+
labelDeleteCmd = &cobra.Command{
16+
Use: "delete <labelname>",
17+
Short: labelDeleteHelpShort,
18+
Args: cobra.ExactArgs(1),
19+
Run: labelDeleteCmdRun,
20+
}
21+
)
22+
23+
func init() {
24+
labelCmd.AddCommand(labelDeleteCmd)
25+
}
26+
27+
func labelDeleteCmdRun(cmd *cobra.Command, args []string) {
28+
labelname := args[0]
29+
30+
if err := client.LabelDelete(labelname); err != nil {
31+
if GlobalFlag.Verbose {
32+
log.WithError(err).WithField(
33+
"label", labelname).Error("label delete failed")
34+
}
35+
failure("Label delete failed", err, 1)
36+
}
37+
fmt.Printf("%s Label deleted successfully\n", labelname)
38+
}

glustercli/cmd/label-info.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/gluster/glusterd2/pkg/api"
7+
log "github.com/sirupsen/logrus"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
const (
12+
labelInfoHelpShort = "Get Gluster Label Info"
13+
)
14+
15+
var (
16+
labelInfoCmd = &cobra.Command{
17+
Use: "info <labelname>",
18+
Short: labelInfoHelpShort,
19+
Args: cobra.ExactArgs(1),
20+
Run: labelInfoCmdRun,
21+
}
22+
)
23+
24+
func init() {
25+
labelCmd.AddCommand(labelInfoCmd)
26+
}
27+
28+
func labelInfoDisplay(info *api.LabelGetResp) {
29+
fmt.Println()
30+
fmt.Println("Label Name:", info.Name)
31+
fmt.Println("Snap Max Hard Limit:", info.SnapMaxHardLimit)
32+
fmt.Println("Snap Max Soft Limit:", info.SnapMaxSoftLimit)
33+
fmt.Println("Auto Delete:", info.AutoDelete)
34+
fmt.Println("Activate On Create:", info.ActivateOnCreate)
35+
fmt.Println("Snapshot List:", info.SnapList)
36+
fmt.Println("Description:", info.Description)
37+
fmt.Println()
38+
39+
return
40+
}
41+
42+
func labelInfoHandler(cmd *cobra.Command) error {
43+
var info api.LabelGetResp
44+
var err error
45+
46+
labelname := cmd.Flags().Args()[0]
47+
info, err = client.LabelInfo(labelname)
48+
if err != nil {
49+
return err
50+
}
51+
labelInfoDisplay(&info)
52+
return err
53+
}
54+
55+
func labelInfoCmdRun(cmd *cobra.Command, args []string) {
56+
if err := labelInfoHandler(cmd); err != nil {
57+
if GlobalFlag.Verbose {
58+
log.WithError(err).Error("error getting label info")
59+
}
60+
failure("Error getting Label info", err, 1)
61+
}
62+
}

glustercli/cmd/label-list.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/gluster/glusterd2/pkg/api"
8+
"github.com/olekukonko/tablewriter"
9+
log "github.com/sirupsen/logrus"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
const (
14+
helpLabelListCmd = "List all Gluster Labels"
15+
)
16+
17+
func init() {
18+
19+
labelCmd.AddCommand(labelListCmd)
20+
21+
}
22+
23+
func labelListHandler(cmd *cobra.Command) error {
24+
var infos api.LabelListResp
25+
var err error
26+
labelname := cmd.Flags().Args()[0]
27+
28+
infos, err = client.LabelList(labelname)
29+
if err != nil {
30+
return err
31+
}
32+
33+
table := tablewriter.NewWriter(os.Stdout)
34+
table.SetAutoMergeCells(true)
35+
table.SetRowLine(true)
36+
if len(infos) == 0 {
37+
fmt.Println("There are no labels in the system")
38+
return nil
39+
}
40+
table.SetHeader([]string{"Name"})
41+
for _, info := range infos {
42+
table.Append([]string{info.Name})
43+
}
44+
table.Render()
45+
return err
46+
}
47+
48+
var labelListCmd = &cobra.Command{
49+
Use: "list",
50+
Short: helpLabelListCmd,
51+
Args: cobra.ExactArgs(1),
52+
Run: labelListCmdRun,
53+
}
54+
55+
func labelListCmdRun(cmd *cobra.Command, args []string) {
56+
if err := labelListHandler(cmd); err != nil {
57+
if GlobalFlag.Verbose {
58+
log.WithError(err).Error("error getting label list")
59+
}
60+
failure("Error getting Label list", err, 1)
61+
}
62+
}

glustercli/cmd/label-reset.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/gluster/glusterd2/pkg/api"
8+
9+
log "github.com/sirupsen/logrus"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
const (
14+
labelResetCmdHelpShort = "Reset value of label configuratio options"
15+
labelResetCmdHelpLong = "Reset options on a specified gluster snapshot label. Needs a label name and at least one option"
16+
)
17+
18+
var labelResetCmd = &cobra.Command{
19+
Use: "reset <labelname> <options>",
20+
Short: labelResetCmdHelpShort,
21+
Long: labelResetCmdHelpLong,
22+
Args: cobra.RangeArgs(1, 2),
23+
Run: func(cmd *cobra.Command, args []string) {
24+
var req api.LabelResetReq
25+
labelname := args[0]
26+
options := args[1:]
27+
if len(args) < 2 {
28+
failure("Specify atleast one label option to reset", errors.New("Specify atleast one label option to reset"), 1)
29+
} else {
30+
req.Configurations = options
31+
}
32+
33+
err := client.LabelReset(req, labelname)
34+
if err != nil {
35+
if GlobalFlag.Verbose {
36+
log.WithError(err).WithField("label", labelname).Error("label reset failed")
37+
}
38+
failure("Snapshot label reset failed", err, 1)
39+
}
40+
fmt.Printf("Snapshot label options reset successfully\n")
41+
},
42+
}
43+
44+
func init() {
45+
labelCmd.AddCommand(labelResetCmd)
46+
}

glustercli/cmd/label-set.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/gluster/glusterd2/pkg/api"
8+
log "github.com/sirupsen/logrus"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
const (
13+
labelSetHelpShort = "Set a label value"
14+
labelSetHelpLong = "Modify one or more label value ."
15+
)
16+
17+
var (
18+
labelSetCmd = &cobra.Command{
19+
Use: "set <labelname> <option> <value> [<option> <value>]...",
20+
Short: labelSetHelpShort,
21+
Long: labelSetHelpLong,
22+
Args: labelSetArgsValidate,
23+
Run: labelSetCmdRun,
24+
}
25+
)
26+
27+
func labelSetArgsValidate(cmd *cobra.Command, args []string) error {
28+
// Ensure we have enough arguments for the command
29+
if len(args) < 3 {
30+
return errors.New("need at least 3 arguments")
31+
}
32+
33+
// Ensure we have a proper option-value pairs
34+
if (len(args)-1)%2 != 0 {
35+
return errors.New("needs '<option> <value>' to be in pairs")
36+
}
37+
38+
return nil
39+
}
40+
41+
func init() {
42+
labelCmd.AddCommand(labelSetCmd)
43+
}
44+
45+
func labelSetCmdRun(cmd *cobra.Command, args []string) {
46+
labelname := args[0]
47+
options := args[1:]
48+
49+
if err := labelSetHandler(cmd, labelname, options); err != nil {
50+
if GlobalFlag.Verbose {
51+
log.WithError(err).WithField(
52+
"labelname", labelname).Error("label set failed")
53+
}
54+
failure("Label set failed", err, 1)
55+
} else {
56+
fmt.Printf("Label Values set successfully for %s label\n", labelname)
57+
}
58+
59+
}
60+
61+
func labelSetHandler(cmd *cobra.Command, labelname string, options []string) error {
62+
confs := make(map[string]string)
63+
for op, val := range options {
64+
if op%2 == 0 {
65+
confs[val] = options[op+1]
66+
}
67+
}
68+
req := api.LabelSetReq{
69+
Configurations: confs,
70+
}
71+
err := client.LabelSet(req, labelname)
72+
return err
73+
}

glustercli/cmd/labels.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
const (
8+
helpLabelCmd = "Snapshot Label Management"
9+
)
10+
11+
var labelCmd = &cobra.Command{
12+
Use: "label",
13+
Short: helpLabelCmd,
14+
}
15+
16+
func init() {
17+
snapshotCmd.AddCommand(labelCmd)
18+
}

0 commit comments

Comments
 (0)