This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_clusters.go
68 lines (56 loc) · 1.79 KB
/
get_clusters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cmd
import (
"fmt"
"github.com/cedi/kkpctl/cmd/completion"
"github.com/cedi/kkpctl/pkg/output"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
projectID string
datacenter string
)
// clustersCmd represents the clusters command
var getClustersCmd = &cobra.Command{
Use: "cluster [clusterid]",
Short: "Lists clusters for a given project or datacenter",
Example: "kkpctl get cluster --project dw2s9jk28z",
Args: cobra.MaximumNArgs(1),
Aliases: []string{"clusters"},
ValidArgsFunction: completion.GetValidClusterArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clusterID := ""
if len(args) == 1 {
clusterID = args[0]
}
kkp, err := Config.GetKKPClient()
if err != nil {
return err
}
var result interface{}
if clusterID == "" && projectID == "" {
result, err = kkp.ListAllClusters(listAll)
} else if clusterID == "" && projectID != "" {
result, err = kkp.ListClusters(projectID)
} else if clusterID != "" && projectID == "" {
result, err = kkp.GetClusterByID(clusterID, listAll)
} else {
result, err = kkp.GetCluster(clusterID, projectID)
}
if err != nil {
return errors.Wrap(err, "unable to get cluster")
}
parsed, err := output.ParseOutput(result, outputType, sortBy)
if err != nil {
return errors.Wrap(err, "failed to parse cluster")
}
fmt.Print(parsed)
return nil
},
}
func init() {
getCmd.AddCommand(getClustersCmd)
AddProjectFlag(getClustersCmd)
getClustersCmd.Flags().SetAnnotation("project", cobra.BashCompOneRequiredFlag, []string{"false"}) // not required anymore ;)
getClustersCmd.Flags().BoolVarP(&listAll, "all", "A", false, "To list all clusters in all projects if the users is allowed to see. Note: Using this flag will make kkpctl slow!")
}