Skip to content

Commit

Permalink
Minor improvements to DBaaS commands
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Thakur <harsh@civo.com>
  • Loading branch information
Harsh Thakur authored and RealHarshThakur committed Jan 16, 2023
1 parent cdd83b9 commit 554083d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
5 changes: 2 additions & 3 deletions cmd/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ func init() {

dbCredentialCmd.Flags().BoolVarP(&connectionString, "connection-string", "c", false, "show the connection string for the database")

dbCreateCmd.Flags().IntVarP(&nodes, "nodes", "", 0, "the number of nodes for the database")
dbCreateCmd.Flags().IntVarP(&nodes, "nodes", "", 1, "the number of nodes for the database")
dbCreateCmd.Flags().StringVarP(&firewallID, "firewall", "", "", "the firewall to use for the database")
dbCreateCmd.Flags().StringVarP(&networkID, "network", "n", "", "the network to use for the database")
dbCreateCmd.Flags().StringVarP(&rulesFirewall, "firewall-rules", "u", "", "the firewall rules to use for the database")
dbCreateCmd.Flags().StringVarP(&size, "size", "s", "", "the size of the database")
dbCreateCmd.MarkFlagRequired("size")
dbCreateCmd.Flags().StringVarP(&size, "size", "s", "g3.db.small", "the size of the database. You can list available DB sizes by `civo size list -s database`")

dbUpdateCmd.Flags().IntVarP(&nodes, "nodes", "", 0, "the number of nodes for the database")
dbUpdateCmd.Flags().StringVarP(&firewallID, "firewall", "", "", "the firewall to use for the database")
Expand Down
10 changes: 1 addition & 9 deletions cmd/database/database_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ var dbCreateCmd = &cobra.Command{
}
}

if nodes == 0 {
nodes = 1
} else if nodes < 0 {
utility.Error("Nodes can't be negative")
os.Exit(1)
}

if firewallID != "" {
_, err = client.FindFirewall(firewallID)
if err != nil {
Expand All @@ -69,8 +62,7 @@ var dbCreateCmd = &cobra.Command{
sizeIsValid := false
if size != "" {
for _, s := range sizes {
if s.Name == size {
size = s.Name
if s.Name == size && utility.SizeType(s.Name) == "Database" {
sizeIsValid = true
break
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/database/database_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var dbListCmd = &cobra.Command{
ow := utility.NewOutputWriter()
for _, db := range databases.Items {
ow.StartLine()
ow.AppendDataWithLabel("id", db.ID, "ID")
ow.AppendDataWithLabel("id", utility.TrimID(db.ID), "ID")
ow.AppendDataWithLabel("name", db.Name, "Name")
ow.AppendDataWithLabel("size", db.Size, "Size")
ow.AppendDataWithLabel("nodes", strconv.Itoa(db.Nodes), "Nodes")
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func init() {
kubernetesConfigCmd.Flags().BoolVarP(&overwriteConfig, "overwrite", "w", false, "overwrite the kubeconfig file")
kubernetesConfigCmd.Flags().StringVarP(&localPathConfig, "local-path", "p", fmt.Sprintf("%s/.kube/config", home), "local path to save the kubeconfig file")

kubernetesCreateCmd.Flags().StringVarP(&targetNodesSize, "size", "s", "g4s.kube.medium", "the size of nodes to create.")
kubernetesCreateCmd.Flags().StringVarP(&targetNodesSize, "size", "s", "g4s.kube.medium", "the size of nodes to create. You can list available kubernetes sizes by `civo size list -s kubernetes`")
kubernetesCreateCmd.Flags().StringVarP(&networkID, "network", "t", "default", "the name of the network to use in the creation")
kubernetesCreateCmd.Flags().IntVarP(&numTargetNodes, "nodes", "n", 3, "the number of nodes to create (the master also acts as a node).")
kubernetesCreateCmd.Flags().StringVarP(&kubernetesVersion, "version", "v", "latest", "the k3s version to use on the cluster. Defaults to the latest. Example - 'civo k3s create --version 1.21.2+k3s1'")
Expand Down
2 changes: 1 addition & 1 deletion cmd/size/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ var SizeCmd = &cobra.Command{

func init() {
SizeCmd.AddCommand(sizeListCmd)
sizeListCmd.Flags().StringVarP(&filterSize, "filter", "s", "", "filter the result by the type (kubernest, database, inatance)")
sizeListCmd.Flags().StringVarP(&filterSize, "filter", "s", "", "filter the result by the type (kubernetes, database, instance)")
}
10 changes: 6 additions & 4 deletions cmd/size/size_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Example: civo size ls -o custom -f "Code: name (type)"`,
}

filter := []civogo.InstanceSize{}
// TODO: Rename ListInstanceSizes to ListSizes
// TODO: Add a filter in civogo to filter by type
sizes, err := client.ListInstanceSizes()
if err != nil {
utility.Error("%s", err)
Expand All @@ -52,11 +54,11 @@ Example: civo size ls -o custom -f "Code: name (type)"`,
search := ""

switch {
case filterSize == "database" || filterSize == "Database":
case strings.ToLower(filterSize) == "database" || strings.ToLower(filterSize) == "db":
search = ".db."
case filterSize == "kubernetes" || filterSize == "Kubernetes":
case strings.ToLower(filterSize) == "kubernetes" || strings.ToLower(filterSize) == "k8s" || strings.ToLower(filterSize) == "k3s" || strings.ToLower(filterSize) == "kube":
search = ".kube."
case filterSize == "instance" || filterSize == "Instance":
case strings.ToLower(filterSize) == "instance" || strings.ToLower(filterSize) == "iaas" || strings.ToLower(filterSize) == "vm":
search = "iaas"
}

Expand Down Expand Up @@ -84,7 +86,7 @@ Example: civo size ls -o custom -f "Code: name (type)"`,
ow.StartLine()
ow.AppendDataWithLabel("name", size.Name, "Name")
ow.AppendDataWithLabel("description", size.Description, "Description")
ow.AppendDataWithLabel("type", utility.CheckSize(size.Name), "Type")
ow.AppendDataWithLabel("type", utility.SizeType(size.Name), "Type")
ow.AppendDataWithLabel("cpu_cores", strconv.Itoa(size.CPUCores), "CPU")
ow.AppendDataWithLabel("ram_mb", strconv.Itoa(size.RAMMegabytes), "RAM")
ow.AppendDataWithLabel("disk_gb", strconv.Itoa(size.DiskGigabytes), "SSD")
Expand Down
13 changes: 11 additions & 2 deletions utility/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,17 @@ func UpdateNodePool(s []civogo.KubernetesClusterPoolConfig, id string, count int
return s
}

// CheckSize is a utility function to check thesize an return the size in the right format
func CheckSize(size string) string {
// TrimID is a utility function to trim the ID to 6 characters
func TrimID(id string) string {
if len(id) > 6 {
return id[:6]
}
return id
}

// SizeType is a utility function to to return the type of the size given it's name.
// Possible types(case-sensitive) : Instance, Database, Kubernetes
func SizeType(size string) string {
switch {
case strings.Contains(size, ".db."):
return "Database"
Expand Down

0 comments on commit 554083d

Please sign in to comment.