Skip to content

Commit 30eba36

Browse files
authored
Merge pull request #364 from replicatedhq/joshd/sc-99234/nodegroups-replicated-cluster-nodegroup-ls
Adding nodegroup ls command
2 parents ee99332 + dc0ba02 commit 30eba36

File tree

6 files changed

+150
-21
lines changed

6 files changed

+150
-21
lines changed

cli/cmd/cluster_nodegroup.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
func (r *runners) InitClusterNodeGroup(parent *cobra.Command) *cobra.Command {
8+
cmd := &cobra.Command{
9+
Use: "nodegroup",
10+
}
11+
parent.AddCommand(cmd)
12+
13+
return cmd
14+
}

cli/cmd/cluster_nodegroup_ls.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cmd
2+
3+
import (
4+
"github.com/pkg/errors"
5+
"github.com/replicatedhq/replicated/cli/print"
6+
"github.com/replicatedhq/replicated/pkg/platformclient"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func (r *runners) InitClusterNodeGroupList(parent *cobra.Command) *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "ls [ID]",
13+
Short: "List node groups for a cluster",
14+
Long: `List node groups for a cluster`,
15+
Args: cobra.ExactArgs(1),
16+
RunE: r.listNodeGroups,
17+
}
18+
parent.AddCommand(cmd)
19+
20+
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table|wide (default: table)")
21+
22+
return cmd
23+
}
24+
25+
func (r *runners) listNodeGroups(cmd *cobra.Command, args []string) error {
26+
if len(args) < 1 {
27+
return errors.New("cluster id is required")
28+
}
29+
clusterID := args[0]
30+
31+
cluster, err := r.kotsAPI.GetCluster(clusterID)
32+
if errors.Cause(err) == platformclient.ErrForbidden {
33+
return ErrCompatibilityMatrixTermsNotAccepted
34+
} else if err != nil {
35+
return errors.Wrap(err, "get cluster")
36+
}
37+
38+
return print.NodeGroups(r.outputFormat, r.w, cluster.NodeGroups)
39+
}

cli/cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
220220
runCmds.InitClusterVersions(clusterCmd)
221221
runCmds.InitClusterShell(clusterCmd)
222222

223+
clusterNodeGroupCmd := runCmds.InitClusterNodeGroup(clusterCmd)
224+
runCmds.InitClusterNodeGroupList(clusterNodeGroupCmd)
225+
223226
clusterAddOnCmd := runCmds.InitClusterAddOn(clusterCmd)
224227
runCmds.InitClusterAddOnRm(clusterAddOnCmd)
225228
runCmds.InitClusterAddOnLs(clusterAddOnCmd)

cli/print/cluster_addons.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var addOnsTmpl = template.Must(template.New("ingresses").Funcs(funcs).Parse(addO
1818
var addOnsTmplNoHeader = template.Must(template.New("ingresses").Funcs(funcs).Parse(addOnsTmplRowSrc))
1919

2020
func AddOns(outputFormat string, w *tabwriter.Writer, addOns []*types.ClusterAddOn, header bool) error {
21-
if outputFormat == "table" {
21+
switch outputFormat {
22+
case "table":
2223
if header {
2324
if err := addOnsTmpl.Execute(w, addOns); err != nil {
2425
return err
@@ -28,25 +29,36 @@ func AddOns(outputFormat string, w *tabwriter.Writer, addOns []*types.ClusterAdd
2829
return err
2930
}
3031
}
31-
} else if outputFormat == "json" {
32-
cAsByte, _ := json.MarshalIndent(addOns, "", " ")
32+
case "json":
33+
cAsByte, err := json.MarshalIndent(addOns, "", " ")
34+
if err != nil {
35+
return err
36+
}
3337
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
3438
return err
3539
}
40+
default:
41+
return fmt.Errorf("unsupported output format: %s", outputFormat)
3642
}
3743
return w.Flush()
3844
}
3945

4046
func AddOn(outputFormat string, w *tabwriter.Writer, addOn *types.ClusterAddOn) error {
41-
if outputFormat == "table" {
47+
switch outputFormat {
48+
case "table":
4249
if err := addOnsTmpl.Execute(w, []*types.ClusterAddOn{addOn}); err != nil {
4350
return err
4451
}
45-
} else if outputFormat == "json" {
46-
cAsByte, _ := json.MarshalIndent(addOn, "", " ")
52+
case "json":
53+
cAsByte, err := json.MarshalIndent(addOn, "", " ")
54+
if err != nil {
55+
return err
56+
}
4757
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
4858
return err
4959
}
60+
default:
61+
return fmt.Errorf("unsupported output format: %s", outputFormat)
5062
}
5163
return w.Flush()
5264
}

cli/print/cluster_nodegroups.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package print
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"text/tabwriter"
7+
"text/template"
8+
9+
"github.com/replicatedhq/replicated/pkg/types"
10+
)
11+
12+
var nodeGroupsTmplSrc = `ID NAME DEFAULT INSTANCE TYPE NODES DISK
13+
{{ range . -}}
14+
{{ .ID }} {{ .Name }} {{ .IsDefault }} {{ .InstanceType }} {{.NodeCount}} {{.DiskGiB}}
15+
{{ end }}`
16+
17+
var nodeGroupsTmpl = template.Must(template.New("nodegroups").Parse(nodeGroupsTmplSrc))
18+
19+
func NodeGroups(outputFormat string, w *tabwriter.Writer, addOns []*types.NodeGroup) error {
20+
switch outputFormat {
21+
case "table", "wide":
22+
if err := nodeGroupsTmpl.Execute(w, addOns); err != nil {
23+
return err
24+
}
25+
case "json":
26+
cAsByte, err := json.MarshalIndent(addOns, "", " ")
27+
if err != nil {
28+
return err
29+
}
30+
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
31+
return err
32+
}
33+
default:
34+
return fmt.Errorf("unsupported output format: %s", outputFormat)
35+
}
36+
return w.Flush()
37+
}

cli/print/clusters.go

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ DISTRIBUTION: {{ $d.Name }}
4242
var clusterVersionsTmpl = template.Must(template.New("clusterVersions").Funcs(funcs).Parse(clusterVersionsTmplSrc))
4343

4444
func Clusters(outputFormat string, w *tabwriter.Writer, clusters []*types.Cluster, header bool) error {
45-
if outputFormat == "table" {
45+
switch outputFormat {
46+
case "table":
4647
if header {
4748
if err := clustersTmplTable.Execute(w, clusters); err != nil {
4849
return err
@@ -52,7 +53,7 @@ func Clusters(outputFormat string, w *tabwriter.Writer, clusters []*types.Cluste
5253
return err
5354
}
5455
}
55-
} else if outputFormat == "wide" {
56+
case "wide":
5657
if header {
5758
if err := clustersTmplWide.Execute(w, clusters); err != nil {
5859
return err
@@ -62,71 +63,94 @@ func Clusters(outputFormat string, w *tabwriter.Writer, clusters []*types.Cluste
6263
return err
6364
}
6465
}
65-
} else if outputFormat == "json" {
66-
cAsByte, _ := json.MarshalIndent(clusters, "", " ")
66+
case "json":
67+
cAsByte, err := json.MarshalIndent(clusters, "", " ")
68+
if err != nil {
69+
return err
70+
}
6771
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
6872
return err
6973
}
74+
default:
75+
return fmt.Errorf("invalid output format: %s", outputFormat)
7076
}
7177
return w.Flush()
7278
}
7379

7480
func NoClusters(outputFormat string, w *tabwriter.Writer) error {
75-
if outputFormat == "table" || outputFormat == "wide" {
81+
switch outputFormat {
82+
case "table", "wide":
7683
_, err := fmt.Fprintln(w, "No clusters found. Use the `replicated cluster create` command to create a new cluster.")
7784
if err != nil {
7885
return err
7986
}
80-
} else if outputFormat == "json" {
87+
case "json":
8188
if _, err := fmt.Fprintln(w, "[]"); err != nil {
8289
return err
8390
}
91+
default:
92+
return fmt.Errorf("invalid output format: %s", outputFormat)
8493
}
8594
return w.Flush()
8695
}
8796

8897
func Cluster(outputFormat string, w *tabwriter.Writer, cluster *types.Cluster) error {
89-
if outputFormat == "table" {
98+
switch outputFormat {
99+
case "table":
90100
if err := clustersTmplTable.Execute(w, []*types.Cluster{cluster}); err != nil {
91101
return err
92102
}
93-
} else if outputFormat == "wide" {
103+
case "wide":
94104
if err := clustersTmplWide.Execute(w, []*types.Cluster{cluster}); err != nil {
95105
return err
96106
}
97-
} else if outputFormat == "json" {
98-
cAsByte, _ := json.MarshalIndent(cluster, "", " ")
107+
case "json":
108+
cAsByte, err := json.MarshalIndent(cluster, "", " ")
109+
if err != nil {
110+
return err
111+
}
99112
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
100113
return err
101114
}
115+
default:
116+
return fmt.Errorf("invalid output format: %s", outputFormat)
102117
}
103118
return w.Flush()
104119
}
105120

106121
func NoClusterVersions(outputFormat string, w *tabwriter.Writer) error {
107-
if outputFormat == "table" {
122+
switch outputFormat {
123+
case "table":
108124
_, err := fmt.Fprintln(w, "No cluster versions found.")
109125
if err != nil {
110126
return err
111127
}
112-
} else if outputFormat == "json" {
128+
case "json":
113129
if _, err := fmt.Fprintln(w, "[]"); err != nil {
114130
return err
115131
}
132+
default:
133+
return fmt.Errorf("invalid output format: %s", outputFormat)
116134
}
117135
return w.Flush()
118136
}
119137

120138
func ClusterVersions(outputFormat string, w *tabwriter.Writer, clusters []*types.ClusterVersion) error {
121-
if outputFormat == "table" {
139+
switch outputFormat {
140+
case "table":
122141
if err := clusterVersionsTmpl.Execute(w, clusters); err != nil {
123142
return err
124143
}
125-
} else if outputFormat == "json" {
126-
cAsByte, _ := json.MarshalIndent(clusters, "", " ")
144+
case "json":
145+
cAsByte, err := json.MarshalIndent(clusters, "", " ")
146+
if err != nil {
147+
return err
148+
}
127149
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
128150
return err
129151
}
152+
default:
153+
return fmt.Errorf("invalid output format: %s", outputFormat)
130154
}
131155
return w.Flush()
132156
}

0 commit comments

Comments
 (0)