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 <[email protected]>
  • Loading branch information
rafikc30 committed Sep 7, 2018
1 parent 7d1776a commit d097d8a
Show file tree
Hide file tree
Showing 7 changed files with 295 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> [<brick> [<brick>]...|--size <size>]",
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)
}
}
14 changes: 14 additions & 0 deletions glustercli/cmd/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmd

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

const (
helpLabelCmd = "Gluster Label Management"
)

var labelCmd = &cobra.Command{
Use: "label",
Short: helpLabelCmd,
}
1 change: 1 addition & 0 deletions glustercli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func addSubCommands(rootCmd *cobra.Command) {
rootCmd.AddCommand(georepCmd)
rootCmd.AddCommand(snapshotCmd)
rootCmd.AddCommand(volumeCmd)
rootCmd.AddCommand(labelCmd)
}

// GlustercliOption will have all global flags set during run time
Expand Down
52 changes: 52 additions & 0 deletions pkg/restclient/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package restclient

import (
"fmt"
"net/http"

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

// LabelCreate creates Gluster Label
func (c *Client) LabelCreate(req api.LabelCreateReq) (api.LabelCreateResp, error) {
var labelinfo api.LabelCreateResp
err := c.post("/v1/labels", req, http.StatusCreated, &labelinfo)
return labelinfo, err
}

//LabelReset will change its values to default.
func (c *Client) LabelReset(req api.LabelSetReq, labelname string) error {
var labelinfo api.LabelConfigResp
url := fmt.Sprintf("/v1/labels/%s/reset", labelname)
return c.post(url, req, http.StatusOK, &labelinfo)
}

//LabelSet will allow to modify its values.
func (c *Client) LabelSet(req api.LabelSetReq, labelname string) error {
var labelinfo api.LabelConfigResp
url := fmt.Sprintf("/v1/labels/%s/Set", labelname)
return c.post(url, req, http.StatusOK, &labelinfo)
}

// LabelList returns list of all labels
func (c *Client) LabelList(labelname string) (api.LabelListResp, error) {
var labelinfos api.LabelListResp
err := c.get("/v1/labels", nil, http.StatusOK, &labelinfos)
return labelinfos, err
}

// LabelInfo returns information about a label
func (c *Client) LabelInfo(labelname string) (api.LabelGetResp, error) {
var labelinfo api.LabelGetResp
var url string
url = fmt.Sprintf("/v1/labels/%s", labelname)
err := c.get(url, nil, http.StatusOK, &labelinfo)
return labelinfo, err
}

// LabelDelete will delete Gluster Label and respective lv
func (c *Client) LabelDelete(labelname string) error {
url := fmt.Sprintf("/v1/labels/%s", labelname)
err := c.del(url, nil, http.StatusNoContent, nil)
return err
}

0 comments on commit d097d8a

Please sign in to comment.